// JavaScript Document

var HashRegistery = Class.create( {
initialize: function()
{
	this.currentHash = window.location.hash;
	this.registryKeys = Array();
	this.registryValues = Array();
	this.callbacks = Array();
	
	this.loadHash();
	if(Prototype.Browser.IE == false)
		new PeriodicalExecuter(this.checkHistory.bind(this) , .1);
	
	//this.checkHistory();
	
},

loadHash: function() 
{
	this.registryKeys = Array();
	this.registryValues = Array();
	var hash = "";
	var hashes = this.currentHash.sub('#' , '').strip().split("/");
	
	for (var i = 0; i < hashes.length; i++) 
	{
		hash = hashes[i].split(":");
		if (hash.length == 2) 
		{
			this.registryKeys.push(hash[0]);
			this.registryValues.push(hash[1]);
		}
	}
},

registerHistoryCallback: function(callback) 
{
	this.callbacks.push(callback);
},

checkHistory: function()
{
	
	var hashToCheck = window.location.hash;
	
	if(Prototype.Browser.IE == true)
		hashToCheck = String($("sessionFrame").contentWindow.location.search).sub(/\?/ , "#");
	
	if(this.currentHash.strip() != hashToCheck.strip())
	{
		this.currentHash = hashToCheck;
		if(Prototype.Browser.IE == true)
			window.location.hash = hashToCheck;
		this.loadHash();
		for (var i = 0; i < this.callbacks.length; i++)
		{
			this.callbacks[i]();	
		}
		
	}
},
commit: function() 
{
	var hash = "";
	for (var i = 0; i < this.registryKeys.length; i++) 
	{
		hash += (this.registryKeys[i] + ":" + this.registryValues[i] ) + "/";
	}
	if(Prototype.Browser.IE == true)
		$("sessionFrame").src = '/iehist.html?' + hash;
	
	this.currentHash = '#' + hash;
	window.location.hash = hash;

},

readHash: function(key) 
{
	var valueIndex = this.registryKeys.indexOf(key);
	if(valueIndex != -1)
		return this.registryValues[valueIndex];
	else
		return false;
},

writeHash: function(key , value) 
{
	var keyIndex = this.registryKeys.indexOf(key);
	if (keyIndex != -1) 
	{
		this.registryValues[keyIndex] = value;
	}
	else 
	{
		this.registryKeys.push(key);
		this.registryValues.push(value);
	}
},

removeHash: function(key)
{
	var keyIndex = this.registryKeys.indexOf(key);
	if (keyIndex != -1) 
	{
		this.registryKeys.splice(keyIndex , 1);
		this.registryValues.splice(keyIndex , 1);
	}
}
});


var hashRegistry = new HashRegistery();
document.hashRegistry = hashRegistry;


var PageSwitcher = Class.create( {
initialize: function(pages , tabs , firstPage)
{
	this.pages = pages;
	this.tabs = tabs;
	this.afterHooks = Array();
	this.beforeHooks = Array();
	
	hashRegistry.registerHistoryCallback(this.historySwitcher.bind(this));
	
	var hashedPage = hashRegistry.readHash('page');
	if(hashedPage)
		firstPage = hashedPage + "Page";
	
	if(pages.indexOf($(firstPage)) == -1)
		this.currentPage = 0;
	else
		this.currentPage = pages.indexOf($(firstPage))
		
	$(this.pages[this.currentPage]).show();
	

	for (var i = 0; i < pages.length; i++) 
	{
		this.afterHooks[i] = Array();
		this.beforeHooks[i] = Array();
		if(this.currentPage != i)
			this.pages[i].hide();
		//remove all of the classes set for javascript-less people
		this.pages[i].writeAttribute('class', '');
	}
		
	for (var i = 0; i < tabs.length; i++) {
		this.tabs[i].writeAttribute('class', '');
		Event.observe($(tabs[i]), "click", this.switcher.bindAsEventListener(this, this.tabs[i]));
	}
	
	$(this.tabs[this.currentPage]).addClassName("active");
},
switcher: function(evt , tab)
{
	if(evt)
		evt.stop();
		
	var tabIndex = this.tabs.indexOf(tab);
	var nextPage = $(this.pages[tabIndex]);
	var currentPage = $(this.pages[this.currentPage]);
	
	hashRegistry.writeHash("page" , nextPage.id.sub("Page" , ""));
	hashRegistry.commit();
	if(this.currentPage == nextPage)
		return;
	
	for(var i = 0 ; i < this.afterHooks[this.currentPage].length ; i++)
		this.afterHooks[this.currentPage][i]();
	
	$(this.tabs[this.currentPage]).removeClassName("active");
	tab.addClassName("active");
	
	currentPage.hide();
	nextPage.show();
	
	for(var i = 0 ; i < this.beforeHooks[tabIndex].length ; i++)
		this.beforeHooks[tabIndex][i]();
	
	this.currentPage = tabIndex;
},
historySwitcher: function()
{
	var page = hashRegistry.readHash('page');
	if(page)
		this.switcher(null , $(page + "Tab"));
	else
		this.switcher(null , this.tabs[0]);
},
/**
 * Add a function to be called before a specific page is loaded
 */
addBeforeHook: function(page , method)
{
	var index = this.pages.indexOf($(page));
	
	if (index == -1)
		return;

	if (this.currentPage == index)
		method();
		
	this.beforeHooks[index].push(method);
},
/**
 * Add a function to be called after a page is unloaded
 */
addAfterHook: function(page , method)
{
	var index = this.pages.indexOf($(page));
	if (index == -1)
		return;
	this.afterHooks[index].push(method);
}
});