var main_navigation;
var navi_first = false;
var navi_second = {};
window.addEvent('domready',function(){
	main_navigation = new PSNavigation();
	//new PScssSwitch();
	if(window.location.href.split('#')[1] && $chk($(window.location.href.split('#')[1]))){
		if($chk($(window.location.href.split('#')[1]).getParent('li').getParent('ol').getPrevious('a'))){
			$(window.location.href.split('#')[1]).getParent('li').getParent('ol').getPrevious('a').fireEvent('click');
			$(window.location.href.split('#')[1]).fireEvent('click');
		}
		else{
			$(window.location.href.split('#')[1]).fireEvent('click');
		}
	}
});

var PSNavigation = new Class({
							 
	options		:	{
		speed	:	500
	},
	
	initialize	:	function(){
		
		// Alle .sc_container-Höhen der Portfolios ermitteln
		var all_sc_heights = {};
		$each($$('#navigation li ol .sc_container'),function(item){
			var my_index = $$('#navigation li ol .sc_container').indexOf(item);
			//all_sc_heights[my_index] = item.getStyle('height').toInt();
			all_sc_heights[my_index] = item.measure(function(){
				return this.getSize().y;
			});
			item.setStyles({'height':0,overflow:'hidden'});
		});
		
		// Alle .ce-Höhen der Contentelemente ermitteln
		var all_ce_heights = {};
		$each($$('#navigation li ol .ce'),function(item){
												   
			var my_index = $$('#navigation li ol .ce').indexOf(item);
			item.getParent('li').getParent('ol').setStyles({'display':'block','height':'auto'});
			all_ce_heights[my_index] = item.getSize().y;
			item.getParent('li').getParent('ol').setStyles({'display':'none','height':0});
			if(item.hasClass('showcase') != true){
				item.setStyles({'height':0,overflow:'hidden'});
			}
			
		});
		
		// Alle <ol>-Höhen der zweiten Navigationsebene ermitteln
		var all_li_heights = {};
		$each($$('#navigation li ol'),function(item){
			var my_index = $$('#navigation li ol').indexOf(item);
			item.setStyles({'display':'block','height':'auto'});
			all_li_heights[my_index] = item.measure(function(){
				return this.getSize().y;
			});
			if(item.getChildren('li')[0].hasClass('content_area')){
				all_li_heights[my_index] = item.getChildren('li')[0].measure(function(){
					return this.getSize().y;
				});
			}
			item.setStyles({'height':0,'display':'block'});
		});		
		
		// onclick-Event für die Navigation
		$each($$('#navigation li a'),function(item){
			if(item.hasClass('doktype_4') != true && item.getParent().hasClass('bodytext') != true){
			item.addEvent('click',function(){
										   
				// 2. Navigationsebene
				var next_ol = this.getNext('ol');	
				if(next_ol){					
					var my_index = $$('#navigation li ol').indexOf(next_ol);
					if(next_ol.getStyle('height').toInt() == 0){
						if($chk(navi_first)){
							navi_first.getNext('ol').setStyle('height',navi_first.getNext('ol').getSize().y);
							navi_first.getNext('ol').set('morph',{duration:main_navigation.options.speed}).morph({'height':0});
							navi_first.removeClass('active');
						}
						next_ol.set('morph',{duration:main_navigation.options.speed, onComplete:function(item){
							item.setStyle('height','auto');
							item.getPrevious().addClass('active');
							var my_index = $$('#navigation li ol').indexOf(next_ol);
							all_li_heights[my_index] = item.getSize().y;
						}}).morph({'height':all_li_heights[my_index]});
						navi_first = item;
					}
					else{
						next_ol.setStyle('height',next_ol.getSize().y);
						all_li_heights[my_index] = next_ol.getSize().y;
						item.removeClass('active');
						next_ol.set('morph',{duration:main_navigation.options.speed}).morph({'height':0});
					}
				}
				
				// Showcase Element
				var next_sc = this.getNext('.sc_container');
				if(next_sc){
					
					// Bilder einsetzen
					$each(next_sc.getChildren('.inner')[0].getChildren('.showcase'),function(item){
							$each(item.getChildren('.left')[0].getChildren('.inner')[0].getChildren('div'),function(item){
								$each(item.getChildren('img'),function(item){
									if(item.hasClass('change') == true){
										var href = item.getAttribute('rel');
										item.setAttribute('src',href);
										item.removeClass('change');
									}
								});		
							});
					});
					
					if(item.hasClass('project') != true){
						var my_index = $$('#navigation li ol').indexOf(item.getParent('ol'));
						if($chk(navi_second[my_index])){
							navi_second[my_index].getNext('div').set('morph',{duration:main_navigation.options.speed}).morph({'height':0});
							navi_second[my_index].removeClass('active');
						}
						navi_second[my_index] = item;						
						
						var my_index = $$('#navigation li ol .sc_container').indexOf(next_sc);
						var ol_index = $$('#navigation li ol').indexOf(this.getParent('ol'));
						if(next_sc.getStyle('height').toInt() == 0){
							var new_height = all_sc_heights[my_index];
							if(item.getNext('a').hasClass('project') != true){
								item.addClass('active');
							}
							else{
								item.getParent().getChildren('.project').removeClass('closed');
							}
						}
						else{
							var new_height = 0;
							item.removeClass('active');
							item.getParent().getChildren('.project').addClass('closed');
						}
						next_sc.getParent('ol').getChildren('.sc_container').set('morph',{duration:main_navigation.options.speed}).morph({height:0});
						next_sc.set('morph',{duration:main_navigation.options.speed,onComplete:function(item){
							var li_index = $$('#navigation li ol').indexOf(item.getParent('ol'));
							all_li_heights[li_index]=item.getParent('ol').getSize().y;
						}}).morph({'height':new_height});
					}
					else{	
						// Showcase-Div öffnen, falls geschlossen
						this.getParent('ol').getChildren('.project').removeClass('active');
						$each(this.getParent('ol').getChildren('li'),function(item){
							item.getChildren('.project').removeClass('active');
						});
						this.addClass('active');
						if(next_sc.getStyle('height').toInt() == 0){
							this.getParent().getChildren('a')[0].fireEvent('click');
						}
						
						// 750 = Breite des Portfolio-Containers
						var new_marginleft = item.getAttribute('rel') * 750 * (-1);
						item.getNext('div').getChildren('.inner').set('morph',{duration:500}).morph({'margin-left':new_marginleft});
					}
				}
				
				// Gerenderte CEs
				var next_ce = this.getNext('.ce');
				if(next_ce){
					if(item.hasClass('project') != true){					
						var my_index = $$('#navigation li ol').indexOf(item.getParent('ol'));
						if($chk(navi_second[my_index])){
							navi_second[my_index].getNext('div').set('morph',{duration:main_navigation.options.speed}).morph({'height':0});
							navi_second[my_index].removeClass('active');
						}
						navi_second[my_index] = item;

						var my_index = $$('#navigation li ol .ce').indexOf(next_ce);
						var ol_index = $$('#navigation li ol').indexOf(this.getParent('ol'));

						if(next_ce.getStyle('height').toInt() == 0){
							var new_height = all_ce_heights[my_index];
							item.addClass('active');
						}
						else{
							var new_height = 0;
							item.removeClass('active');
						}
						next_ce.set('morph',{duration:main_navigation.options.speed,onComplete:function(item){
							var li_index = $$('#navigation li ol').indexOf(item.getParent('ol'));
							all_li_heights[li_index]=item.getParent('ol').getSize().y;
						}}).morph({'height':new_height});
					}
				}
				return false;
			});
			}
		});
		
		// Portfolio
		$$('.thumbs div').setStyle('cursor','pointer');
		$$('.thumbs div').addEvent('click',function(){
			this.getParent('div').getChildren('div').setStyle('opacity',0.3);
			this.setStyle('opacity',1);
			var my_index = this.getParent('div').getChildren('div').indexOf(this);
			var inner_div = this.getParent().getPrevious().getChildren('div');
			// 270 = Höhe der großen Bilder pguentert: auf 323 updated.
			var new_margintop = my_index * 323 * (-1);
			inner_div.set('morph',{duration:500}).morph({'margin-top':new_margintop});
		});
		$each($$('.thumbs'),function(item){
			if(item.getChildren('div')[0]){
				item.getChildren('div')[0].fireEvent('click');
			}
		});
	}
});

var PScssSwitch = new Class({

	initialize	:	function(){
		var css_div = new Element('div',{'id':'css_switch'}).inject($$('body')[0]);
		new Element('a',{'class':'first'}).inject(css_div);
		new Element('a',{'class':'second'}).inject(css_div);
		
		// CSS v2 deaktivieren
		$$('#css_switch a')[0].addEvent('click',function(){
			if($('css_v2')){
				$('css_v2').dispose();	
			}
		});
		// CSS v2 aktivieren
		$$('#css_switch a')[1].addEvent('click',function(){
			new Asset.css('fileadmin/css/web_v2.css',{'id':'css_v2'});
		});
		
	}

});







