var opacity = 0.3;
var locked = false;




window.addEvent('domready',function(){
	
	var anwaelte = $$('#anwaelte .bild');
	var namen = $$('#namen .name');
	var fachgebiete = $$('#fachgebiete .fachgebiet_link');
	
	
	// opacity tween
	var tweens = {};




	function unmaskAll(){
		anwaelte.each(function(anwalt){
			tweens[anwalt.get('id')].start('opacity',1);

		});

		$$('#namen .active').each(function(aktiv){
			aktiv.addClass('invisible');
		});

		$$('#namen .inactive').each(function(inactive){
			inactive.removeClass('invisible');
		});

		$$('#fachgebiete .active').each(function(aktiv){
			aktiv.addClass('invisible');
		});

		$$('#fachgebiete .inactive').each(function(inactive){
			inactive.removeClass('invisible');
		});
	}

	function maskImages(){
		anwaelte.each(function(anwalt){
			
			tweens[anwalt.get('id')].start('opacity',opacity);
			
			// anwalt.setStyle('opacity',opacity);
		});
	}
	

	
		
	
	
	anwaelte.each(function(anwalt){

		tweens[anwalt.get('id')] = new Fx.Tween(anwalt,{duration:200, wait:false});
		tweens[anwalt.get('id')].set('opacity',0);

		anwalt.getElement('a').addEvent('click',function(e){
			if(locked){
				var e = new Event(e);
				e.stop();
				var url = this.get('href')+"/"+locked;
				window.location.href = url;
				
			}
		});
		
		anwalt.addEvent('mouseenter',function(){
			if(locked){
				return false;
			}
			unmaskAll();
			maskImages();
			
			
			tweens[anwalt.get('id')].start('opacity',1);

			var fgs = JSON.decode(anwalt.get('rel'));
			
			// id von anwalt
			var id = anwalt.get('id').replace(/bild_anwalt_/,'');

			
			$('name_anwalt_'+id).addClass('invisible');
			$('name_anwalt_'+id+'_aktiv').removeClass('invisible');
			
			fgs.each(function(fg){
				if(fg!=''){
					$('fachgebiet_'+fg).addClass('invisible');
					$('fachgebiet_'+fg+'_aktiv').removeClass('invisible');
				}
			});
			
		});
		
		anwalt.addEvent('mouseleave',function(){
			if(locked){
				return false;
			}

			unmaskAll();
		});
		
		
	});
	



	namen.each(function(name){
		name.addEvent('click',function(e){
			if(locked){
				var e = new Event(e);
				e.stop();
				var url = this.get('href')+"/"+locked;
				window.location.href = url;
				
			}
		});
		
		name.addEvent('mouseenter',function(){
			if(locked){
				return false;
			}
			unmaskAll();
			maskImages();
			
			
			name.getElement('.active').removeClass('invisible');
			name.getElement('.inactive').addClass('invisible');
			
			var fgs = JSON.decode(name.getElement('.active').get('rel'));

			
			// id von anwalt
			var id = name.get('rel');
			
			
			
			tweens['bild_anwalt_'+id].start('opacity',1);
			// $('bild_anwalt_'+id).setStyle('opacity',1);
			
			
			fgs.each(function(fg){
				if(fg!=''){
					$('fachgebiet_'+fg).addClass('invisible');
					$('fachgebiet_'+fg+'_aktiv').removeClass('invisible');
				}
			});
			
		});
		
		name.addEvent('mouseleave',function(){
			if(locked){
				return false;
			}
			unmaskAll();
		});
	});


	fachgebiete.each(function(fachgebiet){
		fachgebiet.addEvent('mouseenter',function(){
			if(locked){
				return false;
			}
			unmaskAll();
			maskImages();
			
			fachgebiet.getElement('.active').removeClass('invisible');
			fachgebiet.getElement('.inactive').addClass('invisible');
			
			var aws = JSON.decode(fachgebiet.getElement('.active').get('rel'));

			
			aws.each(function(anwalt){
				tweens['bild_anwalt_'+anwalt].start('opacity',1);
				// $('bild_anwalt_'+anwalt).setStyle('opacity',1);
				$('name_anwalt_'+anwalt+'_aktiv').removeClass('invisible');
				$('name_anwalt_'+anwalt).addClass('invisible');
				
			});
			
			
		});
		
		fachgebiet.addEvent('mouseleave',function(){
			if(locked){
				return false;
			}
			unmaskAll();
		});
		
		fachgebiet.addEvent('click',function(){

			if(locked == fachgebiet.get('rel')){			
				locked = false;
				$('notice_text').setStyle('display','none');
				unmaskAll();

			}else{
				if(locked){
					locked = false;
					fachgebiet.fireEvent('mouseenter');
				}
				
				locked = fachgebiet.get('rel');
				$('notice_text').setStyle('display','inline');
			}
		});
	});
	
	
	function getShuffledArray(arr){

		// einen array mit den ids erstellen
		var ids = [];
		for (var i=0; i < arr.length; i++) {
			ids.push(i);
		}
		
		// die ids mischen
		var shuffledIds = [];
		var i = 0;
		while (ids.length != 0){
			i++;
			var rand = ids.getRandom();
			shuffledIds.push(rand);
			ids.erase(rand);
			
		}
		
		// array neu zusammenstellen
		var newArr = [];
		
		shuffledIds.each(function(id){
			newArr.push(arr[id]);
		});
		
		return newArr;
		
	}
	
	
	
	function showAnwalt(anwalt){
		tweens[anwalt.get('id')].start('opacity',1);
	}
	
	
	function showThemAll(){
		var i = 100;
		
		var shuffled = getShuffledArray(anwaelte);

		
		
		shuffled.each(function(anwalt){
			i+=50;
			showAnwalt.delay(i,this,anwalt);
		});
		
	}
	
	if(Browser.Engine.trident){
		showThemAll();
	}
	
	window.addEvent('load',function(){
		// fachgebiete.each(function(fg){
		// 	// console.log('fg',fg);
		// 	
		// 	var child = fg.getElement('img').getCoordinates().width+8;
		// 	
		// 	// console.log('child',child);
		// 	
		// 	fg.setStyle('width',child);
		// });
		
showThemAll();

		
		
	});	
	

	var delayId = false;
	var delay = 500;
	var running = false;
	var qsFx = new Fx.Tween('quickSearch',{duration:500}).set('opacity',0);
	
	
	function showQs(){
		qsFx.start('opacity',1);
	}
	
	function hideQs(){
		qsFx.start('opacity',0);		
	}
	
	
	
	function search(){
		running = true;
		var suchwort = $('suchfeld').get('value');
		var request = new Request({
			url:base_url + "ajaxSearch",
			method:'post',
			data:{
				'search':suchwort
			},
			onSuccess:function(text){
				running = false;
				if(text!=''){
					$('qsContent').set('html',text);
					showQs();
					
				}else{
					hideQs();
				}
			}
		}).send();
	}
	
	
	// quicksearch
	$('suchfeld').addEvent('keyup',function(){
		// delay
		
		if($('suchfeld').get('value')=="" || running || $('suchfeld').get('value').length <= 3){
			hideQs();
			return;
		}

		$clear(delayId);
		delayId = search.delay(500);
		
	});
	
	$('suchfeld').addEvent('blur',function(){
		//hideQs();
	});
});



