var hhtTabs = new Class ({

	preOpenTab : function() {

		$$('div.hht_tab_buttons').each(function(item,index){

			var tabId = window.location.hash.substr(1);

			item.getElements('.hht_tab_button').each(function(item, index){

				if( item.getProperty('hht:tab_id') == tabId ) {
					this.click_button(item);
					return (false);
				}

			}.bind(this));

		}.bind(this));

	},

	click_button:function( button ){
			// find tab button
		target = new Element( button );
		while (!target.hasClass('hht_tab_button') && target.getParent() ){
			target = target.getParent();
		}

			// find tab area
		area = target;
		while (area.hasClass( 'hht_tabbed_area' ) == false && area.getParent() ){
			area = area.getParent();
		}
			// do it
		this.click( false, target, area );
		
	},

	click_tab:function ( event ){
			// event
		event = new Event(event);
			// find tab button
		target = new Element(event.target);
		while (!target.hasClass('hht_tab_button') && target.getParent() ){
			target = target.getParent();
		}
			// find tab area
		area = target;
		while (area.hasClass( 'hht_tabbed_area' ) == false && area.getParent() ){
			area = area.getParent();
		}
			// do it
		this.click( event, target, area );
	},

	reload_tab:function ( event ) {

		event = new Event(event);

		target = new Element(event.target);

		var tab_id = target.getProperty('hht:tab_id');
		var tab_element = $(tab_id);
		var paramElementId = target.getProperty('hht:tab_param_add');

		if(paramElementId && $(paramElementId)) {
			paramElement = $(paramElementId);

			paramName = paramElement.getProperty('hht:tab_param_name');
			paramValue = paramElement.options[paramElement.selectedIndex].value;

			if(paramName && paramValue && tab_element) {
				var ajaxUrl = tab_element.getProperty('hht:tab_ajax_url');
				tab_element.setProperty('hht:tab_ajax_url', ajaxUrl+'&'+paramName+'='+paramValue);
				tab_element.setProperty('hht:tab_reload', 1);

					target = tab_element;
					while (!target.hasClass('hht_tab_button') && target.getParent() ){
						target = target.getParent();
					}
						// find tab area
					area = target;
					while (area.hasClass( 'hht_tabbed_area' ) == false && area.getParent() ){
						area = area.getParent();
					}
						// do it
					this.click( false , target, area );
						
			}
		}
	},

	click:function(event, target, area){
		if (target.hasClass('hht_tab_button') && area.hasClass('hht_tabbed_area')  ){

			var tab_id   = target.getProperty('hht:tab_id');
			var ajax_url = target.getProperty('hht:tab_ajax_url');
			var reload_tab = target.getProperty('hht:tab_reload');

			if ( tab_id && !ajax_url ){ //activate preloaded tab
				this.activate_tab(tab_id, area);
				if (event) event.stop();
			} else if ( ajax_url ) { //activate ajax tab
					// genarte tab id if needed
				if ( !tab_id ) {
					tab_id = 'hht_ajax_tab_' + tab_id;
					target.setProperty( 'hht:tab_id',tab_id);
				}

				tab_contents = $E('.hht_tab_contents', area);
				tab_content  = $E( '#' + tab_id , tab_contents);

					// create cobtent area if needed
				if ( !tab_content ){
					var tab_content = new Element('div');
					tab_content.addClass ('hht_tab_content');
					tab_content.setText('übertrage Daten / loading data');
					tab_content.injectInside( tab_contents );
					tab_content.setProperty('id', tab_id);
				}
				
				if ( tab_content && ( tab_content.hht_tabAjaxStarted != ajax_url ) ){
					tab_content.setHTML("");
					tab_content.hht_tabAjaxStarted = ajax_url;
					
					tab_content.addClass("hht_tab_content_loading");
					var ajax = new Ajax(
						ajax_url,
						{
							method: 'get',
							update: tab_content,
							onComplete: function(response) { this.removeClass( 'hht_tab_content_loading') }.bind(tab_content)
						}
					);
					ajax.request();
					
				}
				
					// activate tab
				this.activate_tab(tab_id, area);

				if (event) event.stop();

			} else { // use as normal link
				// let the event roll
			}

		}
	},
	
	activate_tab:function (tab_id, area){

			// track event
		window.fireEvent( 'hht-track' , [ 'tab_select', tab_id, '' ] );

		if (tab_id  ){
				//activate buttons
			var buttons  = $ES('.hht_tab_buttons .hht_tab_button', area);
			for (var i=0 ; i<buttons.length ; i++){
				var button = new Element( buttons[i] );
				if (button.getProperty('hht:tab_id')==tab_id){
					button.addClass('active');
				} else {
					button.removeClass('active');
				}
			}
				// activate contents
			var contents = $ES('.hht_tab_contents .hht_tab_content', area);
			for (var i =0 ; i<contents.length ; i++){
				var content = new Element(contents[i]);
				if (content.getProperty('id')==tab_id){
					content.addClass('active');
				} else {
					content.removeClass('active');
				}
			}	
		}
	}
});

HHT_Tabs = new hhtTabs();

window.addEvent('domready', function(){

	if(HHT_Tabs) {
		HHT_Tabs.preOpenTab();
	}

});

