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

/*instantiate our history object*/
window.dhtmlHistory.create({
	debugMode: true//set this to false, or just don't pass in an options bundle, to see real-world, non-debug conditions 
});

function historyChange(newLocation, historyData) {
	var historyMsg = (typeof historyData == "object" && historyData != null
		? historyStorage.toJSON(historyData)
		: historyData
	);
	//updateMyLocationHash();
	LoadDataIntoContent( (newLocation ) );
	//var msg = "<b>A history change has occured:</b> | newLocation=" + newLocation + " | historyData=" + historyMsg + " |";
	//alert(msg);
};

function getLocationJump(){
		
	
				var mhash=new String(window.location.hash);
				
				if( mhash.length > 2 && mhash.indexOf('.html') < 0 ) return mljump;
				
				if(mhash.indexOf("#")>-1){
					mhash=mhash.replace("#","");
				}
				if(mhash.length < 1 ){
					mhash=window.location.href;
				}
				mhash=mhash.replace(l_prefix,'')
				
				return mhash;
			}

var tmljump = window.location.hash;
var hpos = window.location.href.indexOf('#');
var spage = hpos>0?window.location.href.substr(0,hpos):window.location.href;

if( tmljump.length > 2 && tmljump.indexOf('.html')>-1 ){
	document.write('<style type="text/css">'+"\r\n"+'#'+loading_div+'{display:none;}'+"\r\n"+'</style>');
	/*var styleElement = document.createElement("style");
	styleElement.type = "text/css";
	styleElement.appendChild(document.createTextNode('#'+loading_div+'{display:none;}'));
	document.body.appendChild(styleElement);*/
}

function  updateMyLocationHash(){
	if( currenthash != getLocationJump() ){
		//alert(currenthash + window.location.hash + mljump );
       var cJump = getLocationJump();
       if( cJump != mljump ){
       	//mljump stores last known location (on load)
		   //alert ('location changed '+ (currenthash != getLocationJump())+ (currenthash + getLocationJump() + mljump ));
		   mljump = cJump;
		   currenthash=cJump;
		   LoadDataIntoContent( (mljump ) );
		}
	}
	//window.setTimeout( updateMyLocationHash, 250 );
}

function addCustomEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}
addCustomEvent(window, 'load', function(){
	if( getLocationJump() == window.location.href.replace(l_prefix,'') ){
		currenthash=window.location.href.replace(l_prefix,'');
		mlhash=currenthash;
	}
	
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(historyChange);
	
	//window.setTimeout( updateMyLocationHash, 250 );
	updateMyLocationHash();//call once
});

/*basic Ajax wrapper*/
function getXHR() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function LoadDataIntoContent(in_url) {
	if( in_url.indexOf('.html') != in_url.length - 5 ) return;
	
var d_url = in_url.replace(l_prefix,'');
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>';

var request = getXHR();
	var processRequest = function() {
		if (request.readyState == 4) {
			if (request.status == 200) {
				var req=request;
				var contnt=req.responseText.split('<!--CONTENT-DELIMITER-->');
	    	var navtent=req.responseText.split('<!--NAVIGATION-->');
	    	document.getElementById(content_div).innerHTML=contnt[1]; 
	    	document.getElementById(navigation_div).innerHTML=navtent[1];
	    	document.getElementById(loading_div).style.display="block"; 
	    	if(document.getElementById('treemenu1')){ddtreemenu.createTree("treemenu1", false);}  
	    	if( d_url != window.location.href.replace(l_prefix,'') ){
	    		
	    		dhtmlHistory.add(d_url,d_url);
			 
	    		//document.getElementById('pagetop_anchor').name=d_url;//fix IE jump history
	    		//window.location.href=spage+'#'+d_url;
	    		//window.location.hash='#'+d_url; 
	    		//history.go(spage+'#'+d_url);
	    		//currenthash=d_url;//getLocationJump();
	    		//mljump=currenthash;
	    	}else{
	    		currenthash=window.location.href.replace(l_prefix,'')
	    		dhtmlHistory.add(currenthash,currenthash);
	    	}; 
			}
			else {
				//var message = "There was a problem retrieving the subscription list: " + request.statusText
				//log(message);
			}
		}
	};
	request.onreadystatechange = processRequest;
	request.open("GET", in_url, true);
	request.send("");

/*
AjaxRequest.get(
  {
    'url':in_url
    ,'onSuccess':function(req){ 
    	var contnt=req.responseText.split('<!--CONTENT-DELIMITER-->');
    	var navtent=req.responseText.split('<!--NAVIGATION-->');
    	document.getElementById(content_div).innerHTML=contnt[1]; 
    	document.getElementById(navigation_div).innerHTML=navtent[1];
    	document.getElementById(loading_div).style.display="block"; 
    	if(document.getElementById('treemenu1')){ddtreemenu.createTree("treemenu1", false);}  
    	if( d_url != window.location.href.replace(l_prefix,'') ){
    		
    		dhtmlHistory.add(d_url,d_url);
		 
    		//document.getElementById('pagetop_anchor').name=d_url;//fix IE jump history
    		//window.location.href=spage+'#'+d_url;
    		//window.location.hash='#'+d_url; 
    		//history.go(spage+'#'+d_url);
    		//currenthash=d_url;//getLocationJump();
    		//mljump=currenthash;
    	}else{
    		currenthash=window.location.href.replace(l_prefix,'')
    		dhtmlHistory.add(currenthash,currenthash);
    	};   
    }//end onSuccess
  }
);
*/
return false;
}
