/**
	@desc This file contains functions to manipulate the history object.
	$Log: history.js,v $
	Revision 1.13  2011/09/02 11:20:33  cvsosir
	*** empty log message ***
	
	Revision 1.12  2011/09/02 11:19:36  cvsosir
	no message
	
	Revision 1.11  2011/09/02 11:18:56  cvsosir
	*** empty log message ***
	
	Revision 1.10  2011/09/02 11:18:04  cvsosir
	*** empty log message ***
	
	Revision 1.9  2011/09/02 11:16:16  cvsosir
	*** empty log message ***
	
	Revision 1.8  2010/06/25 13:33:55  cvsosir
	*** empty log message ***
	
	Revision 1.7  2010/06/14 12:39:51  cvsosir
	fixed UNDEFINED bug
	
	Revision 1.6  2009/05/11 09:41:29  cvsosir
	*** empty log message ***
	
	Revision 1.5  2009/01/12 09:59:34  cvsosir
	Added $Log: history.js,v $
	Added Revision 1.13  2011/09/02 11:20:33  cvsosir
	Added *** empty log message ***
	Added
	Added Revision 1.12  2011/09/02 11:19:36  cvsosir
	Added no message
	Added
	Added Revision 1.11  2011/09/02 11:18:56  cvsosir
	Added *** empty log message ***
	Added
	Added Revision 1.10  2011/09/02 11:18:04  cvsosir
	Added *** empty log message ***
	Added
	Added Revision 1.9  2011/09/02 11:16:16  cvsosir
	Added *** empty log message ***
	Added
	Added Revision 1.8  2010/06/25 13:33:55  cvsosir
	Added *** empty log message ***
	Added
	Added Revision 1.7  2010/06/14 12:39:51  cvsosir
	Added fixed UNDEFINED bug
	Added
	Added Revision 1.6  2009/05/11 09:41:29  cvsosir
	Added *** empty log message *** 
	Added 
	
**/
window.dhtmlHistory.create(
	{
    		debugMode: false ,
   		 toJSON: function(o)
 		{
			return JSON.stringify(o); 

    		} ,
		 fromJSON: function(s) 
		{
			return JSON.parse(s); 
		} 
	}
	);

	/* internal variable currently to track a history event to avoid reload by checkOnBookmark */
	var dhtmlHistory_historyEventHandled = false;
	
	/**thats the listener that gets called when back button is used.
	It handles the history **/
	var historyListener = function(newLocation,historyData)
	{
		//alert('newLocation: '+newLocation+'; '+historyData);
		gl.log('=== historyListener(' + newLocation + ',' + historyData+ ')','i');
		newLocation=unescape(newLocation);

		//a new location consists of a function name
		//and a number of parameters to use when calling the correpsonding function
		//first the parameters need to be extracted of the newLocation (hash)
		if(newLocation != null && newLocation !='')
		{
			var parameters=newLocation.split('$');
		
			if(parameters.length>1)
			{	
				// flag that a history event occured!
				dhtmlHistory_historyEventHandled = true;
				
				//reversing the array so pop() is executed in the desired order
				parameters.reverse();
				
				//first parameter always defines the function to call
				var functionName=parameters.pop();
				switch(functionName)
				{
					case 'CallEx':
						var callType=parameters.pop();
						var params=parameters.pop();
						//alert('functionName: '+functionName+'/ callType: '+callType+'/ params: '+params);
						if( !params )
							params = new Array();
						CallEx(callType,params);
						break;
					case 'ShowSnapshot':
						ShowSnapshot(parameters.pop());
						break;
					case 'ShowSnapshotSimple':
						ShowSnapshotSimple(parameters.pop());
						break;
					case 'ShowSnapshot3':
						ShowSnapshot( parameters.pop(), parameters.pop() );
						break;
					case 'ShowSnapshot2':
						var isin=parameters.pop();
						var uname=parameters.pop();
						var category=parameters.pop();
						ShowSnapshot2(isin,uname,category);
						break;	
					case 'HistDummy':
						
						Params.reverse(); 
					
						var newParams='';
						
						//concat the new parameters. The first one (0) is the function everything behind becomes 
						//the parameters
						for (i=1;i<parameters.length;i++)		
						{
							if(i == 1)
							{
								newParams=parameters[i];
							}
							else
							{
								newParams=newParams + "$" + parameters[i];	
							}	
						}
						
						var newLocation=parameters[0]+ "$" + newParams;
						
						//setting the new location to the one before dummy (got klicked)
						historyListener(newLocation);
						
						break;	
					
					case 'InitKursliste':
					 
						ajaxCall(parameters);
						break;
					
					case 'InitUnderlyingSelection':
						gl.log(parameters);
						ajaxCall(parameters);
						break;
					
					case 'Search':
						ajaxCall(parameters);
						break;
				default:
						
						break;
				}
		}
		}
		gl.log('=== THE END historyListener(' + newLocation + ',' + historyData+ ')','i');
	}
	/**
	Used to add a history mark that will result in a GLOBAL function call. This means the
	function to be called should be in global scope (as well as configured within historyListener)
	@param functionName The name of the function to call when the back button was pressed
	@param param A list with params to use when calling the function. Each param should be seperated by "$" sign
	@param historyData This data is not used within the URL (hash) so this data is not available when setting
	bookmarks. 
	*/
	function historyAdd(functionName,params,historyData)
	{
		gl.log('=== historyAdd(' + functionName + ',' + params + ',' + historyData +')===','i');
		var currentHash=window.location.hash.replace(/.*#(.*)/,"$1");

		//concating the new location string
		var newLocation=functionName + '$' + params;

		//thats added for IE Problem. When the user comes over the Disclaimer site 
		//a second location is added to the browser. IE6 doesnt recognize that both locations are the same.
		//This way a location gets only added once!
		if(currentHash != newLocation)
		{
		
			newLocation=escape(newLocation);
			dhtmlHistory.add(newLocation,historyData)
		
		}
		gl.log('=== THE END historyAdd(' + functionName + ',' + params + ',' + historyData +')===','i');
	}
	
	
	/**
	Used to replace the current history mark that will result in a GLOBAL function call. This means the
	function to be called should be in global scope (as well as configured within historyListener)
	@param functionName The name of the function to call when the back button was pressed
	@param param A list with params to use when calling the function. Each param should be seperated by "$" sign
	@param historyData This data is not used within the URL (hash) so this data is not available when setting
	bookmarks. 
	*/
	function historyReplace(functionName,params,historyData)
	{
		gl.log('=== historyReplace(' + functionName + ',' + params + ',' + historyData +')===','i');
		var currentHash=window.location.hash.replace(/.*#(.*)/,"$1");
			
		//concating the new location string
		var newLocation=functionName + '$' + params;
		
		//thats added for IE Problem. When the user comes over the Disclaimer site 
		//a second location is added to the browser. IE6 doesnt recognize that both locations are the same.
		//This way a location gets only added once!
		if(currentHash != newLocation)
		{
		
			newLocation=escape(newLocation);
			dhtmlHistory.replace(newLocation,historyData)
		
		}
		gl.log('=== THE END historyReplace(' + functionName + ',' + params + ',' + historyData +')===','i');
	}
	
	/** This function checks the hash part of an url. It should be called once after the startup of the page.
	If the user gets to the website through a bookmark, than the bookmarked site (identified by the hash) will
	be loaded
	**/
	function checkOnBookmark()
	{
		
		gl.log('==== checkOnBookmark() ==','i');
		var url=window.location.hash;
		var hash= url.replace(/.*#(.*)/,"$1");
		//alert('checkon: '+hash);
		if(hash != '')
		{
			/* manualy calling the historyListener so the right side can be displayed */
			if (!dhtmlHistory_historyEventHandled)
				historyListener(hash);
		}	
		else
		{
			/*open up the startseite */
			CallEx('Homepage','sessionId='+ GLOBALSESSIONID);
		} 	
		gl.log('==== THE END checkOnBookmark() ==','i');
		
}


/** This function is used when simple ajax calls should be executed by history handling
@param parameters An array of parameters. First one is the url, second the parameters to send, third the 
contentId for the ajax response
**/
function ajaxCall(parameters)
{
	var url=parameters.pop();
	var params=parameters.pop();
	var contentId=parameters.pop();
						
	var ajaxHelper=new AjaxHelper();
	ajaxHelper.publicSend(url,params,contentId);
}



/** This function may be used to add a dummy history element so the back button will direct the user back to the 
"main" page. It should be use to indicate a on side navigation when the side changes but to recover the history
will be to hard. 
@deprecated 
**/
function historyAddDummy()
{			
	var oldLocation=dhtmlHistory.getCurrentHash();
		
	if(oldLocation != null && oldLocation !='')
	{		
		var  oldLocationParts=oldLocation.split('$');
		
		//when the current location isnt already dummy...
		if(oldLocationParts[0] != "HistDummy")
		{
			//add the dummy....
			//because the old location should be added as parameter the \ sign gets replaced by a common parameter
			//sign ($)
			//var oldLocation=oldLocation.replace("\\","$");
			
			//concating the old location as parameters to the new location
			var newLocation='HistDummy$'+ oldLocation ;
		
			dhtmlHistory.add(newLocation)
		}
	}
}

