var expectedHash='';

function ajaxhistory(namer,outputfunction,outputfunction2,outputfunction3)
{
	this.outputfunction = outputfunction;
	this.outputfunction2 = outputfunction2;
	this.outputfunction3 = outputfunction3;
	
	this.historylistids= new Array();
	this.historylistids[0]='';
	this.historylist= new Array();
	this.historylist2= new Array();
	this.historylist3= new Array();
	this.expectedHash='';
	this.cn = namer;
	this.enabled=true;
	this.isIE = false;
	this.currentsrc='';
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		this.isIE= true;
	}
}
ajaxhistory.prototype.handleHistory = function(hash)
{
	if(this.enabled!=false)
	{
		
		if(this.isIE)
		{
			
			oldsrc=this.currentsrc;
			splitter = oldsrc.split("#");
			
			if(splitter.length>1)
			{
				hash = unescape(splitter[1]);
			}
			else
			{
				hash='';
			}
			
			if(this.expectedHash!='')
			{
			splitter2 = this.expectedHash.split("#");
			hash2= splitter2[1];
			
			
			}
			else
			{
				hash2='';
			}
			
			if(hash!=hash2)
			{
				
				historyisdifferent=true;	
				this.expectedHash = '#'+hash;
				
			}
			else
			{
				historyisdifferent=false;	
				
			}
			
			
		}
		else
		{

			if ( window.location.hash != this.expectedHash )
			{
				
				historyisdifferent=true
				this.expectedHash = window.location.hash;
			}
			else
			{
				historyisdifferent=false
			}
		}
		if(historyisdifferent)
		{
			
				this.enabled=false;
				id = this.gethistoryid(this.expectedHash);
				//alert('lookup:'+id);
				historydata = this.gethistory(id);
				historydata2 = this.gethistory2(id);
				historydata3 = this.gethistory3(id);
				if(historydata =='')
				{
					//alert('history empty');
				}
				else
				{
					//alert(historydata);
					eval(this.outputfunction+'(historydata,\'\')');
					eval(this.outputfunction2+'(historydata2,\'\')');
					eval(this.outputfunction3+'(historydata3,\'\')');
					
		
				}
				this.enabled=true;
		}
	}
	else
	{
		
	}

}
ajaxhistory.prototype.registerhistory = function(id,data,data2,data3)
{
	bb = id.split("#");
	if(bb.length<2)
	{
		id=bb[0];
	}
	else
	{
		id=bb[1];
	}
	this.enabled=false;
	if(id!='')
	{

		if(this.isIE)
		{
			this.expectedHash = '#'+id;
			document.getElementById('historyframe').src=basepath+'ajaxhistory?hist='+escape('#'+id);
			
		}
		else
		{
			this.expectedHash = '#'+id;
			window.location.hash = '#'+id;
			
		}
	}
	else 
	{
		this.expectedHash='';
	}
	hist = this.gethistoryid(id);
	
	if(hist!='')
	{
		//alert(hist);
	this.historylistids[hist]='#'+id;
	this.historylist[hist]=data;
	this.historylist2[hist]=data2;
	this.historylist3[hist]=data3;
	

	}
	else
	{
	this.historylistids[this.historylistids.length]='#'+id;
	this.historylist[this.historylistids.length-1]=data;
	this.historylist2[this.historylistids.length-1]=data2;
	this.historylist3[this.historylistids.length-1]=data3;

	}
	//alert(this.historylist.length);
	if(!this.isIE)
	{
		this.enabled=true;
	}
	else
	{
		
	}	
}

ajaxhistory.prototype.gethistoryid = function(id)
{
	ids=id.split("#");
	if(ids.length<2)
	{
		id = '#'+id;
	}
	for(i=0;i<this.historylistids.length;i++)
	{
	
		if(this.historylistids[i]==id)
		{
			

				return i;

		}

		
	}
	return ''; 	
}
ajaxhistory.prototype.gethistory = function(id)
{
	if((id!='')&&(id!=undefined))
	{
	return this.historylist[id];
	}
	else
	{
		
		return '';
	}
}
ajaxhistory.prototype.gethistory2 = function(id,number)
{
	return this.historylist2[id];

}
ajaxhistory.prototype.gethistory3 = function(id,number)
{
	return this.historylist3[id];

}
ajaxhistory.prototype.init = function()
{

	if(this.isIE)
	{
		var body_doc = document.getElementsByTagName('body').item(0);
		body_doc.innerHTML += '<iframe class="historyframe" src="'+basepath+'/ajaxhistory?hist='+escape('#')+'" id="historyframe"></iframe>';
	}	

	data1 = document.getElementById('output1').innerHTML;
	data2 = document.getElementById('output2').innerHTML;
	data3 = document.getElementById('output3').innerHTML;
	//alert(data2);
	this.registerhistory('INDEX',data1,data2,data3);
	this.enabled=true;
	window.setInterval(this.cn+'.handleHistory()', 400);

}
ajaxhist= new ajaxhistory('ajaxhist','tooutput1','tooutput2','tooutput3');

