/*This script cares for displaying a tab navigation*/

/* Tab constructor
@param1 The sessionId
@param2 The URL/parser that should be called when a tab gets activated
@param3 The css id where the contetn data should be dispalyed
@param4 Bool that indicates if an accordNavigation is used for content area (true,false)
@param5 The css class name for all tabs 
@param6 Bool that indicates if the tab onClick Event should be active or not (true (default),false)
*/
  
 function TabNavDev(session,responseURL,contentAreaId,accordNav,tabClassName,tabOnClickEvent)
{

	//setting up the logger
	var l=new Logger(false);
		
	var divClass='tabNav'; 
	var activated='activated';
	var deactivated='deactivated';	
	
	//Style definitions for active and deactive tabs.
	//tabWidth should be configured for every kind of tabNavigation (see Glossar, FAQ) in statisch.css
	var sessionId=session;
	var url=responseURL; //The URL/Parser to call when a tab gets activated
	var cssContentAreaId=contentAreaId; //The css id of the content area 
	var accordNavigation=accordNav;
	
	var className=tabClassName;
	var activeTab;
	var onClickEvent;
	
	var tabs;
	//use true as default
	if(tabOnClickEvent==undefined)
	{	
		onClickEvent=true;		
	}
	else
	
	{
		onClickEvent=tabOnClickEvent;	
	}
	
	
	//can be used to check if all necessary elements are upon the side
	//checkDocumentElements();

	init();
	
	/**Initializes the tab navigation*/
	function init()
	{		
		
	
		tabs = document.getElementsByClassName(className);
		//tabs = $(divClass).getElementsByClassName(className);
		
	
		
		//alert('divClass:'+divClass);
		//when the tabs should react on click event...
		if(onClickEvent==true)
		{
			
			//for every tab...
			for (var i = 0; i < tabs.length; i++) 
			{
				//...declare the event on click 
				$(tabs[i].id).onclick = function ()
				{		
					getTabData(this.id);
				}
			}
		}

		//alert('className:' + className);
		//alert('tab0:' + tabs[0].id);
		//the first tab is always the active one
		activeTab=tabs[0].id;
		
		//Display the entries for the first tab
		getTabData(activeTab);
		
		//Initialize the accord navigation
		if(accordNavigation==true)
		{	
			initAccordNavigation();
		}
	}
	/*Used to display a loading indicator*/
	function showLoading ()
	{
		//alert('showLoad');
		$(cssContentAreaId).innerHTML= '&#160;<img src="images/animated/red2.gif" id="progress" />';	

	}
	
	
	/* Switchs the tabs between deactive and active. And sends the AJAX request */
 	function getTabData(id) 
	{
		//deactivate the former activated tab
		deactivateTab(activeTab);
	
		//activate the tab klicked on
		activateTab(id);
		
		var tabValueId=id + '_value';
			
		var tabValue=$(tabValueId).innerHTML;
		
		//searching for &amp to convert to &...
		var found=tabValue.search("&amp;");
		if(found>-1)
		{
			//need this to convert '&' signs within url
			tabValue= tabValue.replace("&amp;","&");
			tabValue=escape(tabValue);
		}
		
		var pars = 'tabValue='+tabValue +'&tab=' +id + '&sessionId=' +sessionId;
		//$(cssContentAreaId).innerHTML = "<div style='height:100px'>Inhal</div>";
		showLoading();
			
		//When evalScripts=true then javascript code within the response gets executed
		var myAjax = new Ajax.Updater( cssContentAreaId,url, {method: 'get',evalScripts: 'true', parameters: pars, onComplete: showResponse} );
	}
	
	/* Switchs the tabs between deactive and active. And sends the AJAX request. But this time 
	with a list of params
	@param1 id The id of the tab that got klicked
	@param2 paraNames An array with names
	@param3 paraValues An array with values
	*/
 	function getTabDataByParams (id,paraNames,paraValues) 
	{
 		var tabValueId=id + '_value';
		var tabValue=$(tabValueId).innerHTML;
		
		//searching for &amp to convert to &...
		var found=tabValue.search("&amp;");
		if(found>-1)
		{
			//need this to convert '&' signs within url
			tabValue= tabValue.replace("&amp;","&");
			tabValue=escape(tabValue);
		}
		var pars = 'tabValue='+tabValue +'&tab=' +id + '&sessionId=' +sessionId;
		
		//check if params were passed and if list of names and values are of equal size 	
 		if(paraNames.length>0 && paraValues.length >0 && paraNames.length==paraValues.length)
 		{	
	 		for (var i=0; i<paraNames.length; i++)
	 		{
	     		pars=pars + '&' + paraNames[i] + '=' + paraValues[i]	
	     	}
	     	
	     	//deactivate the former activated tab
			deactivateTab(activeTab);
			//activate the tab klicked on
			activateTab(id);
			
			showLoading();
			
			//When evalScripts=true then javascript code within the response gets executed
			var myAjax = new Ajax.Updater( cssContentAreaId,url, {method: 'get',evalScripts: 'true', parameters: pars,  onComplete: showResponse} );
 		}
 		else
 		{
 			alert('The parameters passed are not valid. Check paraNames and paraValues.');
 		}
	}
	
	//The public access to the getTabData function
	this.publicGetTabData= function (id)
	{	
		getTabData(id);
	}
	
	/*  Is accessable from outside to retreive data. Is able to add a list of parameters to the AJAX call
	and therefore to pass additional params to the parser.
	@param1 id The id of the tab that got klicked
	@param2 paraNames An array with names
	@param3 paraValues An array with values
	*/
	this.publicGetTabDataByParams= function (id,paraNames,paraValues)
	{		
 		getTabDataByParams (id,paraNames,paraValues)	
 	}
	
	
	
	/*Executed when the AJAX response of the server is received*/
	function showResponse(originalRequest) 
	{
		//alert('response');
		var newData = originalRequest.responseText;
		
		$(cssContentAreaId).innerHTML = newData;	
		
		if(accordNavigation==true)
		{
			//everytime a new tab is loaded the accord navigation needs to be reset
			initAccordNavigation();
		}
	}
	
	

	/* Initializes the accordion navigation*/ 
	function initAccordNavigation () 
	{
		try
		{
			var vAccoordion = new accordion('vertical_container');	
		
			//To open the first accodion on startup
			vAccoordion.activate($$('#vertical_container .accordion_toggle')[0]);
		}
		catch(e)
		{
		
		}
	}
	

	/*
	Deactivates the given tab (id)
	@param id The id attribute of the tab to deactivate
	*/
	function deactivateTab(id)
	{	
		$(id).removeClassName(activated);
		$(id).addClassName(deactivated);
		
		//var el=$(id);
		//var styleValue = "background-image:url(../images/main/bg_reitertabellea1.gif)";
		
		//$(id).setAttribute("style", styleValue);
		
		/*
		var element=$(id);	
		
		Element.extend(element);
		
		
		$(id).setStyle(
		{
		backgroundImage: 'url(../images/main/bg_reitertabellea1.gif)'
		}
		);
		
		
		

		
		
		
		*/
		
		
		
		
		

	}
	//The public version
	this.pdeactivateTab= function (id)
	{
		deactivateTab(id)
	}
	
	/*
	Activates the given tab (id)
	@param id The id attribute of the tab to deactivate
	*/
	function activateTab(id)
	{
		$(id).removeClassName(deactivated);		
		$(id).addClassName(activated);
		activeTab=id;
	}
	//The public version
	this.pactivateTab= function (id)
	{	
		activateTab(id);
	}
	
	
	//Checks if all necessary elements can be located inside the document
	function checkDocumentElements()
	{
		var valid=true;
		
		if(!$(cssContentAreaId))
		{
			valid=false;
			alert('The area ID "' + cssContentAreaId + '" could not be found inside the document!');
		}
	
		//concat the class name for active tab
		var activeClassName='div.' + divClass + ' div.' + activated;
			
		var activeTabs=$$(activeClassName);
		
		/*
		if(activeTabs.length!=1)
		{
			
			valid=false;
			alert('There is no or more than one tab with class "' +activated + '" within this document');
		}*/
		return valid;
	}		
}
	

