var mljump='';  //my location jump (current location hash)
var currenthash = '';
var l_prefix = "http://sedco.lk/";
var content_div = 'main';//id
var loading_div = 'margin';//id
var navigation_div = 'navigation';//id

function resetNavigation(){
	this.className='';
}
function selectNavigation(){
	this.className='current';
}
function getPageFromUrl(who){
	var hash = who.href;
	var pcs = hash.split('/');
	var hs = pcs[pcs.length-1].split('.');
	hash = hs[0];
	return hash;
}
// PageLoad function
// This function is called when:
// 1. after calling $.historyInit();
// 2. after calling $.historyLoad();
// 3. after pushing "Go Back" button of a browser
function pageload(hash) {
	// hash doesn't contain the first # character.

	
	if(hash){
		//if we have a hash, and the hash is not a JUMP down page hash (like on FAQ), skip load 
		if( $('#margin > ul > li a[href=#'+hash+']').length > 0 ) return;//searching from margin is most compatible, could search closer as well, but how many a tags can there be (lol)
		//the faq page (for example) is not loaded yet!  so load it up, then check again later
		if( hash.indexOf('.') > 0 ) hash = getPageFromUrl( {'href':hash} );
		//no change (must be jump nav such as #faq.q1
		//if( previousPageLUrl == hash ) return; 
		// restore ajax loaded state
		//showLoadingScreen();
		//$("#"+content_div).load(hash + ".php?isAjaxRequest=true",'',onLoadedCallback);
	} else {
		if( origionalPageUrl=='' ){
			//LOAD NOTHING - initial page, already loaded, grab url for later
			hash = getPageFromUrl( {'href':window.location.href} );
			origionalPageUrl=hash;previousPageLUrl=hash;
			return;
		}else{
			hash=origionalPageUrl;
			// back at start page due to history usage, load initial url
			//alert(jQuery.historyCurrentHash);//$("#"+content_div).empty();
		}
	}
	if( previousPageLUrl == hash ) return; //no change (must be jump nav such as #faq.q1
	showLoadingScreen();
	$("#"+content_div).load(hash + ".php?isAjaxRequest=true",'',onLoadedCallback);
	previousPageLUrl=hash;
}
//thse are both URL in HASH format (no .php?)...
var origionalPageUrl=''; //store the start page (blank initially to detect first visit)
var previousPageLUrl='';//another good one... helps us track (if on faq for example) if its unnecessary to reload since the major page has not changed (jump hash, not load hash)... previous page loaded url

function onLoadedCallback(responseText, textStatus, XMLHttpRequest) {
	//AJAX request has loaded data into a div.... proccess stuff
	
	if( $('#margin > ul > li a[href='+jQuery.historyCurrentHash+']').length > 0 ){
  	window.location.replace(window.location.href);//should jump us down the page to teh correct item now that its loaded in :^)
  }
  window.setTimeout(function(){
  rebuildSideNavigation();
  setNewAjaxLinkClickEvents(); //we may have some new links with new ajax calls to set up
  initSlimBoxControls();
	},50);
}

function showLoadingScreen(){
	document.getElementById(loading_div).style.display="block";
	document.getElementById(loading_div).innerHTML='<div id="page_loading"><img alt="Loading..." src="images/ajax-loader.gif" /></div>';
}

$(document).ready(function(){
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload);
	setNewAjaxLinkClickEvents();
});

function setNewAjaxLinkClickEvents(){
	// set onlick event for buttons
	//jQuery(function($) {
	$("a[rel='history']").click(function(e){
		var hash = getPageFromUrl(this)
		
		//clear main nav then select the correct main nav by matching href
		$("#minitabs > li > a").each(resetNavigation);
		$("#minitabs > li > a[href='"+hash+".php']").each(selectNavigation);
		//this.className="current";//does not work unles they click the element specifically, the image above not handled by this one
		// moves to a new page.  
		// pageload is called at once. 
		// hash don't contain "#", "?"
		$.historyLoad(hash);
		
		//if(evt.preventDefault) evt.preventDefault();evt.returnValue = false;
		e.preventDefault();
		return false;
	});
	
	$("a[rel='history']").each(function(){
		this.rel='ajax_history';
	});
}