// script.js - Application library
// Copyright (c) 2008, Clasma Events Inc.

// Define namespace

var app=function() {

// Public start

var public={

// Public functions

// onload
// Called on page load

onload:function()
{
	// initialize the history framework
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(public.historyChange);

	// scan window location
	var hash=window.location.hash;

	if(hash && hash!="") hash=hash.replace(/#/g,"");
	else hash=null;

	public.historyChange(hash,null);
},

// historyChange
// Called when history changes

historyChange:function(hash,url)
{
	// url exists?
	if(!url)
	{
		// hash exists?
		if(!hash || hash=="") hash="home";

		// is it a menu option?
		var a=private.getMenuAnchor(hash,null);
		url=a?a.href:"content/home.asp";
	}
	// pull server content
	private.pull(url);
},

// pull
// Call to pull page from server

pull:function(a)
{
	// strip and convert spaces
	var hash=a.innerHTML;

	hash=hash.replace(/^\s+|\s+$/g,"");
	hash=hash.replace(/\s/g,"_");

	hash=hash.toLowerCase();

	// pull server content and add history
	dhtmlHistory.add(hash,a.href);
	private.pull(a.href);

	return false;
}

// Public end

};

// Private start

var private={

// Private functions

// pull
// Call to pull content from server

pull:function(url)
{
	// select menu?
	var a=private.getMenuAnchor(null,url);

	// set menu and pull content
	menu.set(a);
	private.getContent(url);
},

// getContent
// Call to get page content

getContent:function(url)
{
	// request content
	var callback=function(r) {window.scrollTo(0,0);};
	ajax.include(url,"content",null,callback);
},

// getMenuAnchor
// Call to find menu anchor from hash or url

getMenuAnchor:function(hash,url)
{
	// hash or url exists?
	if(hash || url)
	{
		// run down menu anchors
		var a=$("menu").getElementsByTagName("a");
	
		for(var n=0;n<a.length;n++)
		{
			// found it?
			if((!hash || hash==a[n].innerHTML) &&
				(!url || url==a[n].href))
				return(a[n]);
		}
	}
	return null;
}

// End Private

};

return public;}();

// create history object
window.dhtmlHistory.create();

// initialise page
window.onload=app.onload;
