hhtModules = new Class ({
	
	init:function(){
		
		$$('div.modul-toggle').addEvent('click',function(e){
			var e=new Event(e);
			var target = new Element(e.target);
			hhtModules.toggleState(target);
			e.stop();
		});
		
		$$('.modul-toggle-area').addEvent('click',function(e){
			var e=new Event(e);
			var target = new Element(e.target);
			hhtModules.toggleState(target);
			e.stop();
		});
			
			// add events for toggeling the tabs
		$$('.modul-tabs a.modul-select').addEvent('click',function(e){
			var e=new Event(e);
			var target = new Element(e.target);
			e.stop();
			hhtModules.toggleTab(target);
		});
		
		$$('.modul-tabs a.modul-select').addEvent('focus',function(e){
			var e=new Event(e);
			var target = new Element(e.target);
			target.blur();
			e.stop();
		});
		
			// init the state of the tab bars
		$$('.modul-content .modul-content-tabs').each(function(el){
			var container = hhtModules.getContainer(el);
			var state = $E('.modul-content',container).hasClass('modul-content-opened');
			hhtModules.updateTabs(state, container);
		}); 
		
		
			// init the state of the module boxes in the right column
		$$('#right_column .modul-content-opened').each(function(el){
			var content = $E('.content', el);
			if (content) {
				var target_height = content.getSize().size.y;
				el.setStyles({
					'height': target_height,
					'padding-top': 5,
					'padding-bottom': 5,
					'overflow': 'visible'
				});
			}
			
		}); 
					// init the state of the tab bars
		$$('#right_column .modul-content-closed').each(function(el){
			el.setStyles({
				'height': 0,
				'padding-top':0,
				'padding-bottom':0,
				'overflow':'hidden'
			});
		}); 
	},
	
	toggleTab:function(target){
			// find content menu and module
		var container = hhtModules.getContainer(target);
		var content   = $E( 'div.modul-content' , container );
		var menu      = $ES('div.modul-toggle', container );
		var is_open   = content.hasClass('modul-content-opened');

		
			// switch content of module
		var target_tab = $E('#'+target.getProperty('hht:module_id'),container);
	    
		if (target_tab) {
			
				// deactivate other tabs 
			$ES('.modul-content-tab',container).each(function(el){
				if (el !=target_tab ){
					el.setStyle('display','none');
				}
			});
				// activate target tab
			target_tab.setStyle('display','block');
			
				// open module if needed otherwise resize 
			if (is_open){
					// resize container
				content.setStyle('overflow','hidden');
				var size_fx = new Fx.Styles(content, {duration:400, wait:false, fps:16 });
				size_fx.start({ 'height': target_tab.getSize().size.y }).chain(function(){
					content.setStyle('overflow','visible');
				});	
			} else {
				hhtModules.openModule(menu, content, container);
			}
				// update button state
			hhtModules.updateTabs(true, container);
		}

	},
	
	toggleState:function (target){
	
			// find containner element
		var container = hhtModules.getContainer(target);
			// find module content
		var content = $E( 'div.modul-content' , container );
			// change class of target
		var is_open = content.hasClass('modul-content-opened');
			// find menu
		var menu = 	$ES('div.modul-toggle', container );
		
		if (content)  {
			var content_container = $E('.content',content);
			if (content_container) {
				if (is_open==true) {
					hhtModules.closeModule(menu, content, container);
						// update text 
					var text_src = target.getText();
					var text = text_src;
					var text = text.replace( "Ausblenden", "Einblenden" );
					var text = text.replace( "Hide" , "Show");
					var text = text.replace( "ausblenden" , "einblenden" );
					var text = text.replace( "hide", "show" );				
					var text = text.replace( "ausblenden", "einblenden" );
					if ( text_src != text ){
						target.setText(text);
					}
				} else {
					hhtModules.openModule(menu, content, container);
						// update text 
					var text_src = target.getText();
					var text = text_src;
					var text = text.replace( "Einblenden", "Ausblenden");
					var text = text.replace( "Show", "Hide");
					var text = text.replace( "einblenden", "ausblenden");
					var text = text.replace( "show", "hide");				
					var text = text.replace( "einblenden", "ausblenden");
					if ( text_src != text ){
						target.setText(text);
					}
				}
			}
		}
	},
	
	getContainer:function(item){
			// find content container
		var container = item;
		while ( container.hasClass('content-frame') === false && container.getParent()  ){
			container = container.getParent();
		}
		if (container.hasClass('content-frame')){
			return container;
		} else {
			return false;
		}
	},
	
	openModule:function(menu, content, container){
	
		var content_container  = $E('.content', content);
		if (content_container){
			content.removeClass('modul-content-closed');
			content.addClass('modul-content-opened');
			content.setStyle('height',0);
			content.setStyle('overflow','hidden');
			
			var target_height = false;
			if (content_container){
				target_height = content_container.getSize().size.y;
			}
			
			if (target_height) {
				menu.removeClass('modul-toggle-closed');
				menu.addClass('modul-toggle-opened');
				
				var size_fx = new Fx.Styles(content, {duration:400, wait:false, fps:16});
				size_fx.start({ 
					'height': target_height,
					'padding-top':5,
					'padding-bottom':5
				 }).chain(function(){
					content.setStyle('overflow','visible');
					hhtModules.updateTabs(true, container);
				});
			}
		}
	},
	
	closeModule:function(menu, content , container){
			// header aktualisieren
		menu.removeClass('modul-toggle-opened');
		menu.addClass('modul-toggle-closed');
		content.setStyle('overflow','hidden');
					
		var size_fx = new Fx.Styles(content, {duration:400, wait:false, fps:16});
		size_fx.start({ 
			'height': 0,
			'padding-top':0,
			'padding-bottom':0
		}).chain(function(){
			content.setStyle('overflow','hidden');
			content.removeClass('modul-content-opened');
			content.addClass('modul-content-closed');
	
			hhtModules.updateTabs(false, container);
			
				// fix size of all other modules because ie is a crappy beast
			/*
			$$('#right_column .modul-content-opened').each(function(mod){
				mod.setStyle('border-color','#ff0000');
				mod.setStyle('top','-23px');
			});
			*/
		});
	},
	
	updateTabs:function(state,container){
		
		var tabs = $E('.modul-tabs',container);

		if (tabs){
			if (state){
					// find all buttons
				$ES('.button', tabs).each(function(button){
						// check weather the linked tab is visible
					var module_id = button.getProperty('hht:module_id');
					if (module_id) {
						var module_tab = $E('#'+module_id, container);
					}
						
					if (module_tab){
						if ( module_tab.getStyle("display") != "none" ) {
							button.addClass('current');
						} else {
							button.removeClass('current');
						}
					 }
				});
			} else {
					// disable all buttons
				$ES('.button', tabs).each(function(button){
					button.removeClass('current');
				});	
			}
		}
	}
	
});


window.addEvent('load', function()	{
		
		// start hht modules
	hhtModules = new hhtModules();
	hhtModules.init();
		
});
