/** 
 * Performance enhancing script
 *
 * Scott P. 21/05/09 - Soup Ltd.
 */

var isPrefetch=true;
var startDate=new Date();
var endDate=null;

Prefetch={

	fetchCache		:	[],
	order			:	[],
	links			:	{},
	tmr				:	null,
	currentLinkIX	:	0,
	iFrame			:	null,
	tries			:	0,
	maxTries		:	50,
	debugging		:	true,
	iFrame			:	null,
	debugDiv		:	null,
	xmlReqOb		:	null,

	init	:	function(){
		// prevent recursion
return;
		if(window.top!=window){
			if(parent.isPrefetch)
				return;
		}

		endDate=new Date();

		with(Prefetch){
			var bod=document.getElementsByTagName('body')[0];
			if(debugging){
				debugDiv=document.createElement('div');
				var dS=debugDiv.style;
				dS.position='fixed';
				dS.top=0;
				dS.left=0;
				dS.fontSize='11px';
				dS.color='#444444';
				dS.backgroundColor='#ffffff';
				dS.padding='8px';
				dS.border='1px solid black';
				dS.width='400px';
				dS.zIndex=10000;
				dS.maximumHeight="100px";
				dS.overflow="hidden";
				bod.appendChild(debugDiv);
			}
			iFrame=document.createElement('iframe');
			iFrame.style.position="absolute";
			iFrame.style.top="-20px";
			iFrame.style.left="-20px";
			iFrame.style.width="10px";
			iFrame.style.height="10px";
			iFrame.style.border="none";
			iFrame.style.zIndex=9999;
			iFrame.src='/media/site/test.html';
			bod.appendChild(iFrame);
			var lnks=document.getElementsByTagName('a');
	
			if(!lnks)
				return;
			var tix;
			var cHref=location.href;
	
			tix=cHref.indexOf('?');
			if(tix>-1)
				cHref=cHref.substring(0,tix);
	
			tix=cHref.indexOf('#');
			if(tix>-1)
				cHref=cHref.substring(0,tix);
			
			baseHref=cHref;
			baseDomain=cHref.substring(0,cHref.indexOf('/',8));
	
			var lHref=cHref.length;
			var hasLinks=false;

			for(lnk in lnks){
				var l=lnks[lnk].href+'';
				if(l && (l!='undefined') && (l.toLowerCase().indexOf('mailto:')!=0)){
	
					tix=l.indexOf('#');
					if(tix!=-1)
						l=l.substring(0,tix);
	
					tix=l.indexOf(cHref);
					if(tix==0)
						l=l.substring(lHref,l.length);
	
					if(l!=''){
						if(!links[l]){
							links[l]=1;
						}else{
							++links[l];
						}
						hasLinks=true;
					}
				}
			}
	
			if(!hasLinks)
				return;
	
			order=sbv(Prefetch.links,-1);
			setTimeout(ticker,50);
		}
	},

	ticker	:	function(){
		with(Prefetch){
			tries=0;
			var url=order[currentLinkIX++];
			if(debugging)
				top.status=currentLinkIX+'/'+order.length;
	
			if (window.XMLHttpRequest)
				xmlReqOb=new XMLHttpRequest();
			else if(window.ActiveXObject)
				xmlReqOb=new ActiveXObject("Microsoft.XMLHTTP");
			else
				return false;
			try{
				xmlReqOb.open('get',url,true);
				xmlReqOb.onreadystatechange=Prefetch.responseDispatcher;
				xmlReqOb.send(false);
			}catch(e){
				if(currentLinkIX<order.length)
					setTimeout(ticker,5);
			}
		}
	},

	checkFrameLoaded	:	function(){
		var ib=Prefetch.iFrame.contentWindow.document.getElementsByTagName('body')[0];
		var i=ib.getElementsByTagName('img');
		var allOk=true;
		for(var b in i[0]){
			if(!i[0].complete){
				allOk=false;
				break;
			}
		}
		if((!allOk) && (Prefetch.tries>Prefetch.maxTries)){
			setTimeout(Prefetch.checkFrameLoaded,100);
			return;
		}else if(allOk || (Prefetch.tries>Prefetch.maxTries)){
			Prefetch.frameLoaded();
			return;
		}
	},

	frameLoaded	:	function(){
		with(Prefetch){
			if(Prefetch.currentLinkIX<Prefetch.order.length){
				setTimeout(Prefetch.ticker,50);
			}else if(debugging){
				var tt=endDate.getTime()-startDate.getTime();
				var n,s='<b><a href="#" onclick="Prefetch.openCloseDD()"><span style="float:right">Loading time:'+tt+'ms</span>Cached '+order.length+' URL'+(order.length==1?'':'s')+':</a></b>';
					s+='<ul id="pfDebug" style="display:none;margin:8px 0 0 0;padding:1px;border:1px solid black;height:80px;overflow-y:scroll;overflow-x:hidden">';
				for(n=0;n<order.length;n++)
					s+='<li>'+order[n]+'</li>';
				debugDiv.innerHTML=s+'</ul>';
			}
		}
	},

	openCloseDD	:	function(){
		var dD=document.getElementById('pfDebug');
		dD.style.display=(dD.style.display=='none'?'block':'none');
	},

	sbv	:	function(arr,dir) {
		keys=[];
		for(k in arr)
			keys.push(k);
		return keys.sort(
			(dir==1	?
				function(a,b){
					return arr[a]-arr[b];
				}
				:
				function(a,b){
					return arr[b]-arr[a];
				}
			)
		);
	},

	responseDispatcher	:	function(url){
		if(Prefetch.xmlReqOb.readyState!=4)
			return;
		if(Prefetch.xmlReqOb.status==200 || Prefetch.xmlReqOb.status==404){
			Prefetch.iFrame.contentWindow.document.getElementsByTagName('body')[0].innerHTML=Prefetch.xmlReqOb.responseText;
			setTimeout(Prefetch.checkFrameLoaded,50);
		} else {	// Something's wrong!
		}
	},



	dummy	:	function(){}
}

/**
 * Generic attach event function.
 */
function attachDomEvent(ob,eType,f,cap){
	if(ob.addEventListener)
			ob.addEventListener(eType,f,cap);
	else if(ob.attachEvent)
			ob.attachEvent("on"+eType,f);
	else if(navigator.userAgent.toLowerCase().indexOf('mac')!=-1||navigator.userAgent.toLowerCase().indexOf('apple')!=-1)
			eval('ob.on'+eType+'='+f);
	else
			alert("Handler could not be added");
}

attachDomEvent(this,'load',function(){Prefetch.init();});