var team = new Class({
	
	scroller:null,
	imageWidth:250,
	thumbs:null,
	opacity:0.5,
	
	
	init:function(){
		// console.log('this',this);
		
		
		this.scroller = $('portrait_scroller');
		if(this.scroller==null){
			return false;
		}
		this.scrollerFx = new Fx.Tween(this.scroller, {link:'cancel', duration:'none'});
		this.thumbs = $('thumbs_container').getElements('div[class!=clear]');
		this.thumbFxs = [];
		
		this.thumbs.each(function(thumb){
			this.thumbFxs.push(new Fx.Tween(thumb, {duration:'none', link:'cancel'}).set('opacity',this.opacity));
			var person = JSON.decode(thumb.get('rel'));
			
			// tooltip
			
		}.bind(this));
		
		this.setEvents();
		this.initTips();
		return true;
	},
	
	initTips:function(){
		this.tips = new Tips('.tooltip', {className:'tooltipContainer', onShow:function(tip){tip.setStyle('opacity',0.8)}});	
	},
	
	scrollToIndex:function(index, direct){
		
		var newMargin = (index * this.imageWidth)*-1;
		if(direct){
			this.scrollerFx.set('margin-left', newMargin);
		}else{
			this.scrollerFx.start('margin-left', newMargin);			
		}

		
	},
	
	setEvents:function(){
		
		this.thumbs.each(function(thumb, index){
			
			thumb.addEvent('click',function(){
				
				this.selectPerson(index);
				
			}.bind(this));
			
		}.bind(this));
		
		$$('.abteilung').each(function(abteilung){
			abteilung.addEvent('click',function(){
				this.blurAbteilungen();
				abteilung.addClass('aktiv');
				this.showAbteilung(abteilung.get('rel'));
				
			}.bind(this));
		}.bind(this));
		
	},
	
	blurAbteilungen:function(){
		$$('.abteilung').each(function(abteilung){
			abteilung.removeClass('aktiv');
		});
		
	},
	
	blurThumbs:function(thumbIndex, direct){
		
		this.thumbs.each(function(thumb, index){
			if(index == thumbIndex){
				if(direct){
					this.thumbFxs[index].set('opacity', 1);					
				}else{
					this.thumbFxs[index].start('opacity', 1);					
				}

				
			}else{
				if(direct){
					this.thumbFxs[index].set('opacity', this.opacity);
				}else{
					this.thumbFxs[index].start('opacity', this.opacity);					
				}

			}
		}.bind(this));
		
	},
	
	selectPerson:function(index, direct){

		if (index==undefined){
			index=$('show_member').get('html');
		}

		var direct = direct || false;

		this.scrollToIndex(index, direct);
		
		this.blurThumbs(index, direct);

		var person = JSON.decode(this.thumbs[index].get('rel'));


		$('team_name').set('text',person.vorname);
		$('team_position').set('html',person.zusatzfeld);
		$('nachname').set('text',person.nachname);
		$('team_tel').set('text',person.telefon);
		//$('team_tel').set('src',person.telefon.filename);
		$('team_sonstiges').set('html',person.sonstiges)
	
		$('email').getElement('a').set('href', 'mailto:' + person.email);
        $('email').getElement('a').set('text', person.email);
		
		/*
		var jsonRequest = new Request.JSON({url: $('base_url').get('text')+"Ansprechpartner/getMail/"+person.id,
			onSuccess: function(answer){
				$('email').set('text','');
				if (answer!=null && answer.mailgraphic!=null){
					//alert (answer.mailgraphic);

					var img = new Element('img',{'src': answer.mailgraphic});
					var link = new Element('a', {'href': 'mailto:'+answer.mailaddress});
					img.setStyle('max-width','200px');
					link.adopt(img);
					$('email').adopt(link);				
				}
			}
		});
		jsonRequest.send();
		//alert (jsonRequest.mailaddress);
		*/
		
	},
	
	showAbteilung:function(id){
		var id = id || 0;
		
		var counter=0;
		
		var displayPerson=false;
		
		this.thumbs.each(function(thumb){
			
				if(id!=0){
					//MFR todo
					
					var found = false;
					
					var person = JSON.decode(thumb.get('rel'));
					//console.log(person);
					
					var tmp = JSON.decode(person.abteilung);
					
					tmp.each(function(tmp_id){
						if (id == tmp_id){
							found=true;
						}
					});
					
					if (found==true){
						//ersten Mitarbeiter anzeigen
						
						if (displayPerson==false){
							this.selectPerson(counter);
						}
						displayPerson=true;
						
						thumb.setStyle('display','block');
						
					}else{
						thumb.setStyle('display','none');
					}
					
				}else{
					thumb.setStyle('display','block');
				}
			
				counter++;
			
			}.bind(this));
		
	}
	
	

	
	
});

var teamInstance = new team();

// console.log('teamInstance',teamInstance);

window.addEvent('domready',function(){
	if(teamInstance.init()){
		teamInstance.selectPerson();		
	}
});
