/*	XANJAX Copyright 2007,2008,2009, and Trademark, of David Chapman, openPC Labs.

	XANJAX IS FREE SOFTWARE released under GNU Affero General Public License Version 3.
	Read licence.txt distributed with XANJAX, or xanjax.org/license.html for details.
	IF YOU USE OR DISTRIBUTE OR CONVEY XANJAX YOU ARE LEGALLY BOUND BY ITS LICENCE.

	YOU MUST Comply with Copyright and Trademark Rights. DO NOT REMOVE THIS NOTICE */

var x$=function(){

	var anch="";
	var anchorList=new Array();
	var ctype="Content-Type=text/xml&";
	var entryFlag=-1;
	var headLength;
	var iWebKit;
	var lnks=document.links;
	var newAnchor;
	var webKit=true;
	var xhrThreads=new Array();

return{

bodgIE:0,
docRoot:"",
hsh:"",
persURL:"",
rdrQue:{jobs:0,cntxts:{}},
url:"",

doInit:function(){
 headLength=x$.elTags('head')[0].childNodes.length-1;
 newAnchor=x$.crEl('a','*',{style:'position:absolute; visibility:hidden;'});
 if(x$.eltId("xanhist").value.split(":")[0]!="Note")
  anchorList=x$.eltId("xanhist").value.split(",");
 for(var i=0;i<anchorList.length;i++)
  x$.mkAnch(anchorList[i],i);
 x$.eltId("contentbody").onscroll=x$.noBubble;
 entryFlag=x$.yTop();
 xaniMate(true,x$.eltId("logo"));
 if(location.hash) // hide home page and contexts if directly addressing another page
  x$.eltId("xanjaxdiv").style.display="none";
 if(x$.bodgIE==7&&(location.hash!="#"+anchorList[entryFlag])&&anchorList[entryFlag])
  location.replace("#"+anchorList[entryFlag]+"?");
 if(typeof window.onhashchange=="object"){
  window.onhashchange=x$.navEvent;
  x$.navEvent();
 }else{
  window.onscroll=x$.navEvent;
  iWebKit=window.setInterval(x$.locoPoll,100);
  window.scrollTo(0,1024);
 }
 window.setTimeout(function(){
	if(x$.bodgIE||webKit) document.onkeydown=x$.divScrl;
	else window.onkeypress=x$.divScrl;
 },10);
 window.setTimeout(function(){xaniMate();},1000);
},
noBubble:function(e){
 if(e && e.stopPropagation());
 else window.event.cancelBubble=true;
},
yTop:function(y){
 if((y=document.documentElement.scrollTop)||(y=window.pageYOffset))return y;
 return document.body.scrollTop;
},
yDiff:function(refElm,tgtElm){
if(!tgtElm) return 0;
if(tgtElm.offsetParent==refElm) return tgtElm.offsetTop;
else return tgtElm.offsetTop+x$.yDiff(refElm,tgtElm.offsetParent); // recursive call
},
ancScr:function(refElm,ancElm){ // delay is only for IE
window.setTimeout(function(){refElm.scrollTop=x$.yDiff(refElm,ancElm);},10);
},
divScrl:function(e,elm){
 if((e=e)&&!e.target.type||(e=window.event)&&document.activeElement.tagName=="BODY"){
	elm=x$.eltId("xanjaxdiv");
	switch(e.keyCode){
	 case 33:elm.scrollTop-=elm.offsetHeight/1.1;return false;
	 case 34:elm.scrollTop+=elm.offsetHeight/1.1;return false;
	 case 38:elm.scrollTop-=elm.offsetHeight/10;return false;
	 case 40:elm.scrollTop+=elm.offsetHeight/10;return false;
}}},
nodeClone:function(id,deep){
 var nodeRef=x$.eltId(id);
 return{newNode:nodeRef.cloneNode(deep),docNode:nodeRef};
},
nodeReplace:function(newNode,oldNode){
 oldNode.parentNode.replaceChild(newNode,oldNode);
},
eltId:function(id){
 return document.getElementById(id);
},
elTags:function(tag){
 return document.getElementsByTagName(tag);
},
objfyJSON:function(JSONtext){
 return new Function("return({'objSet':"+JSONtext+"});")().objSet;
},
crEl:function(tag){
 var args=arguments;
 if(x$.bodgIE&&tag=="script")
 	var el=document.getElementsByTagName(tag)[0].cloneNode(false);
 else var el=document.createElement(tag);
 for(var i=1;i<args.length;i++)
	if(typeof(args[i])=='object')
	 for(var key in args[i])
		if(x$.bodgIE&&key=='class'){
		 el.className=args[i][key];
		 el.setAttribute(key,args[i][key]);}
		else if(key=='style')
		 if(typeof(args[i][key])=='object')
		  for(var subkey in args[i][key])
			 el[key][subkey]=args[i][key][subkey];
		 else	el.style.cssText=args[i][key];
		else el.setAttribute(key,args[i][key]);
	else el.insertBefore(document.createTextNode(args[i]),el.firstChild);
return(el);
},
killXHR:function(){
 while(xhrThreads.length)
	try{xhrThreads.pop();}catch(e){};
},
xanjaxHttpRequest:function(reqHandler){
 var xanjax=null; var aborted=false;
 var inner=this;
 this.abort=function(){
	aborted=true;
	xanjax.abort();
	xanjax=null;
 };
 this.init=initRequest();
 this.handler=reqHandler || function(){};
 this.request=function(mode,url,async,data){
	if(this.init>0){
	 xanjax.onreadystatechange=function(){
		switch(xanjax.readyState){
		 case 1:break; case 2:break; case 3:break;
		 case 4:
			if(!aborted)
			 inner.handler(url,xanjax.status,xanjax.responseText,xanjax.responseXML);
	 }};
	 xanjax.open(mode,url,async);
	 if(mode.toUpperCase()=="POST"){
	  xanjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	  xanjax.setRequestHeader("Content-Length",data.length);
	 }
	 xanjax.send(data);
	}else alert("Error: Cannot create Http Request Object!");
 };
	function initRequest(){
		if(window.XMLHttpRequest&&(xanjax=new XMLHttpRequest()))return 1;
		if(window.ActiveXObject){
		 var objVer=new Array(".6.0",".3.0",".4.0",".5.0",null);
		 for(var i=0;i<=objVer.length;i++)
			try{
			 xanjax=new ActiveXObject("MSXML2.XMLHTTP" + objVer[i]);
			 if(i==objVer.length)xanjax=new ActiveXObject("Microsoft.XMLHTTP");
			 if(xanjax)return(i + 2);
			}catch(e){if(i==objVer.length)return 0;};
}}},
xanXHR:function(url,handler,tgtId){
var mode='GET';
 var xanReq=new x$.xanjaxHttpRequest();
 xhrThreads.push(xanReq.abort);
 xanReq.handler=function(url,status, text, xml){
	if(status==200) handler(url,text,xml,tgtId);
	else if(status) x$.httpReqErr(url,status);
 };
 if(arguments[3]) mode=arguments[3];
 if(arguments[4]=='sync')var async=false;else var async=true;
 if(arguments.length>5)var data=arguments[5];else var data="";
 xanReq.request(mode,url,async,data);
},
httpReqErr:function(url,status){
 alert("XMLHttpRequest error "+status+" getting "+url);
 x$.lnHsh();
},
getPage:function(pageURL){
 x$.killXHR();
 x$.rdrQue.jobs=2;
 x$.changeSelected(pageURL);
 xaniMate(true);
 anch=pageURL.split("!")[1];
 x$.url=pageURL.split("!")[0].replace(/\/$/,"/?").replace(/.php$/,".php?").replace(/~|\?/,"?"+ctype);
 if(x$.persURL!=x$.url) x$.xanXHR(x$.url,x$.pushPage);
 else {x$.ancScr(x$.eltId("xanjaxdiv"),x$.eltId(anch)); xaniMate(false);}
 x$.persURL=x$.url;
 x$.xanXHR('root.ctx',pushCtxt); // pushCtxt not part of library - INCONSISTENT!
},
pushPage: function(url,text,xml){
 x$.rdrQue.page=xml;
 x$.queHdlr();
},
queHdlr: function(){
 if(!--x$.rdrQue.jobs){
	if(x$.rdrQue.page) x$.insPage("","",x$.rdrQue.page);
	if(x$.rdrQue.css)
	 x$.elTags('head')[0].appendChild(x$.crEl("link",{rel:"stylesheet",href:x$.rdrQue.css,type:"text/css"}));
	if(x$.rdrQue.jsc)
	 x$.elTags('head')[0].appendChild(x$.crEl("script",{src:x$.rdrQue.jsc,type:"text/javascript"}));
	if(x$.rdrQue.xml) insXml(x$.rdrQue.xml);
	for(var key in x$.rdrQue["cntxts"]) addContext.apply(null,x$.rdrQue["cntxts"][key]);
	x$.eltId("xanjaxdiv").style.display="block";
	xaniMate(false);
	x$.lnHsh();
	x$.changeSelected(x$.hsh);
}},
insPage:function(url,text,xml){
 x$.insXhtml(xml,"contentbox");
 if(x$.pgLoad) x$.pgLoad();
 if(x$.eltId(anch)) x$.ancScr(x$.eltId("xanjaxdiv"),x$.eltId(anch));
 else x$.eltId("xanjaxdiv").scrollTop=0;
 wpFrm("xanjaxdiv");
},
insXhtml:function(xmlDoc,docElm,xmlHead,headElts,xmlBody,bodyElts){
 var docHead=x$.elTags('head')[0];
 if(headElts=xmlDoc.getElementsByTagName('head')[0].childNodes)
 if(xmlDoc.getElementsByTagName("title")[0].text){
	while(docHead.childNodes.length>headLength-1)
	 docHead.removeChild(docHead.lastChild);
	for(var i=0;i<headElts.length;i++){
	 if(headElts[i].nodeName.match(/title/)) document.title=headElts[i].text;
	 if(headElts[i].nodeName.match(/link|style|script/)&&headElts[i].getAttribute("id")!="xanify")
		docHead.appendChild(x$.xmlToHtml(headElts[i]));
 }}
 if(bodyElts=xmlDoc.getElementsByTagName('body')[0].childNodes){
  var content=x$.nodeClone(docElm);
	for(var i=0;i<bodyElts.length;i++)
	 if(bodyElts[i].nodeType!=8) content.newNode.appendChild(x$.xmlToHtml(bodyElts[i]));
	x$.nodeReplace(content.newNode,content.docNode);
}},
xmlToHtml:function(xmlElement){
 var htmlNode = x$.htmlClone(xmlElement);
 if(xmlElement.hasChildNodes())
	for(var i=0;i<xmlElement.childNodes.length;i++)
	 if(x$.bodgIE&&i==xmlElement.childNodes.length-1&&xmlElement.tagName.match(/script|style/))
		htmlNode.text=xmlElement.text;
	 else htmlNode.appendChild(x$.xmlToHtml(xmlElement.childNodes[i]));
 return(htmlNode);
},
htmlClone:function(xmlNode){
 if(xmlNode.tagName){
	var clonedHtml=document.createElement(xmlNode.tagName);
	var attribs=xmlNode.attributes;
	for(var i=0;i<attribs.length;i++){
	 if(x$.bodgIE&&attribs[i].name=='class'){
		clonedHtml.className=attribs[i].value;
		clonedHtml.setAttribute(attribs[i].name,attribs[i].value);}
	 else if(x$.bodgIE&&attribs[i].name=='style'){
		var styleset=x$.objfyJSON("{'"+attribs[i].value.replace(/\:/,"':'").replace(/\;/,"'}"));
		for(var key in styleset) clonedHtml.style[key] = styleset[key];}
	 else clonedHtml.setAttribute(attribs[i].name,attribs[i].value);
	} return(clonedHtml);}
 else return(document.createTextNode(xmlNode.data));
},
locoPoll:function(){
 if(x$.yTop()==1024)
  window.scrollTo(0,entryFlag);
 if(theLoc=location.href.split("#")[1]);
 else theLoc="home.xml";
 if(theLoc!=x$.hsh) x$.navEvent();
},
navEvent:function(){
 var y=x$.yTop();
 if(y==1024){
  window.clearInterval(iWebKit);
  webKit=false;
  window.scrollTo(0,entryFlag);
 }else if(x$.persURL||location.href.split("#")[1]) { // if(stuff) added
	if(x$.bodgIE==7&&(x$.eltId('contentbody').style.top=y+'px')&&(x$.hsh=anchorList[y]));
	else if((x$.hsh=location.href.split("#")[1])||(x$.hsh="home.xml")&&!webKit)
	 x$.mkAnch(x$.hsh.replace(/\?$/,""),y);
  x$.getPage(x$.hsh);
 }else x$.lnHsh();
},
changeSelected:function(hsh){
 if(selected=x$.eltId('selected')) selected.removeAttribute('id');
 for(var i=0;i<lnks.length;i++)
	 if(theHref=lnks[i].href.split("#")[1]==hsh)
		if(lnks[i].parentNode.getAttribute('id'))
		 lnks[i].setAttribute('id','selected');
 else lnks[i].parentNode.setAttribute('id','selected');
},
lnHsh:function(i,j){
 x$.docRoot=lnks[0].href;
 for(i=0;i<lnks.length;i++){
	if((theHref=lnks[i].href.replace(x$.docRoot,""))&&!theHref.match(/^#|:\//))
	 lnks[i].href="#"+theHref.replace(/\..?htm.?/,".xml").replace("?","~").replace("#","!");
	for(j=0;j<anchorList.length;j++)
	 if(anchorList[j]==lnks[i].hash) break;
	x$.mkAnch(lnks[i].hash.split("#")[1],anchorList.length);
}},
mkAnch:function(hsh,theOffset){
 if(hsh&&!x$.eltId(hsh)){
	newAnchor.setAttribute("id",hsh);
	newAnchor.style.top=theOffset+"px";
	document.body.appendChild(newAnchor.cloneNode(true));
 if(theOffset==anchorList.length){
	anchorList.push(hsh);
	x$.eltId("xanhist").value=anchorList;
}}}
}}();

window.onload=x$.doInit;
window.onbeforeunload=x$.killXHR;


