// suntra.ca jackd - ajax handler - @kayam khosa
// v1.0 August 11/09
// v1.5 August 20/09 - Fixed some minor issues
// v1.6 August 22/09 - added in params check 
// v1.7 September 2/09 - removed eval for javascript processing and processing js via dom
// v1.8 September 25/09 - Must pass a param - fix there are void calls trigging startPageReload when there are spaces in page name
// v1.8.1 June 14/10 - Added window scroll to top after inpage load.

function GetXmlHttpObject(){
var xmlHttp=null;
try {xmlHttp=new XMLHttpRequest();} catch(e) {try {xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");} catch(e)
{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}} return xmlHttp;}
var lastHash=location.hash;
function checkHistory(){
	if(location.hash!=lastHash){
		if(location.hash.indexOf('j')==1){
			startPageReload(location.hash.substring(2),'');
			} else if(
				location.hash=='' || location.hash=='#'){
					startPageReload(location.href.replace('http://'+location.host,'').replace(location.hash,''),'');
					} lastHash=location.hash;
	}
}
$(document).ready(function() {addEventListeners();});
function getPathOnly(str){return str.substr(0,str.lastIndexOf('/')+0);}

var buttonChecker=setInterval("checkHistory()",250);
var globalLocation=location.href.replace('http://'+location.host,'').replace(location.hash,'');
var globalPathOnly=getPathOnly(globalLocation);

function addEventListeners_inpage() {	
		 $(".gallery_nav a").each(function() { 	
			var newHref = $(this).attr("href");
 			var r = newString = newHref.replace("?jacked=y",""); 
			$(this).click(function() {  
				startPageReload(r,'1');return false;  
		 }); 
	})
		 $(".nav a").each(function() { 	
			 var newHref = $(this).attr("href");
			 var r = newString = newHref.replace("?jacked=y",""); 
			$(this).click(function() {  startPageReload(r,'0');return false;  
		 });

})
			 $(".navigation a").each(function() { 
				 var newHref = $(this).attr("href");
				 var r = newString = newHref.replace("?jacked=y",""); 
				$(this).click(function() {  startPageReload(r,'0');return false;  
			 });

	})
	
			 $(".homelinks a").each(function() { 
				 var newHref = $(this).attr("href");
				 var r = newString = newHref.replace("?jacked=y",""); 
				$(this).click(function() {  startPageReload(r,'0');return false;  
			 });

	})
	
	
// removed sidebar, something broken with categories and archive links.
	
	}
function addEventListeners() {	
		 $(".gallery_nav a").each(function() { 	
			 var newHref = $(this).attr("href");
			 var r = newString = newHref.replace("?jacked=y",""); 
			$(this).click(function() {  startPageReload(r,'1');return false;  
		 });	
})		
		 $(".nav a").each(function() { 	
			 var newHref = $(this).attr("href");
			 var r = newString = newHref.replace("?jacked=y",""); 
			$(this).click(function() {  startPageReload(r,'0');return false;  
		 });

})

		 $(".navigation a").each(function() { 	
			 var newHref = $(this).attr("href");
			 var r = newString = newHref.replace("?jacked=y",""); 
			$(this).click(function() {  startPageReload(r,'0');return false;  
		 });

})
		 $(".homelinks a").each(function() { 
			 var newHref = $(this).attr("href");
			 var r = newString = newHref.replace("?jacked=y",""); 
			$(this).click(function() {  startPageReload(r,'0');return false;  
		 });

})

}
function startPageReload(pageURL,params){
// loading info
if (params != "") { 
var reloadArea='xcontent';
// var loading='Loading Content';
// document.getElementById(reloadArea).innerHTML=loading;	
xmlHttp=GetXmlHttpObject(); 
if(xmlHttp==null){ alert("Browser does not support HTTP Request"); return;}
var url=pageURL;
url=url.replace('http://'+location.host,'');
var thisHash='j'+url;
location.hash=thisHash;
lastHash='#'+thisHash; 
if (params == 1) { url=url+'&jacked=y'; } else { url=url+'?jacked=y'; }
xmlHttp.onreadystatechange=function(){ if((xmlHttp.readyState==4||xmlHttp.readyState=="complete")&&xmlHttp.responseText!=""){ 
var temp=xmlHttp.responseText; 
setAndExecute('xcontent',temp,pageURL);
}
}
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
}

function setAndExecute(divId, innerHTML, pageURL) {  
	var div = document.getElementById(divId);  
    div.innerHTML = innerHTML;  
    evalScript(div); // execute javascripts
	addEventListeners_inpage(); // inpage listners reload when loaded new content
	pageTracker._trackPageview(pageURL); // google analytics 
	window.scrollTo(0,0); // added to scroll back to the top of the page if link was at the bottom.
}
function evalScript(div) {
   var x = div.getElementsByTagName("script");
   var scriptBlocks = [];
   for(var i=0; i<x.length; i++)
   {
       var oScript = document.createElement('script');
       oScript.text = x[i].text;
       scriptBlocks[i] = oScript;
   }
   for(var i=0; i <scriptBlocks.length; i++) {
       div.appendChild(scriptBlocks[i]);
   }
}

