/*	XANJAX Copyright 2007,2008, 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 */


// START - Xanjax font scaling functions
var fontIndex = 14;
window.onresize = setFontIndex;

	// function to scale font size in proportion to browser width
	// dependencies setLineHeight()
	// this function is reliable but should be improved
function setFontIndex(){
	var width = 0;
	var preIndex = fontIndex;
	if (width = window.innerWidth);			// W3C compliant browser width
	else if (width = document.documentElement.clientWidth);  // IE Top Document Width
	else if (width = document.body.clientWidth);	// IE Object Encapsulated Document Width
	fontIndex=Math.round(width/70);			// 70 Chosen to Scale Fonts for Full Window
	document.getElementsByTagName('body')[0].style.fontSize = fontIndex + "pt";
	setLineHeight(fontIndex * 8 + "%");	// 8 Chosen to Scale Line Height for Full Window
	return(preIndex!=fontIndex);				// return true if font scaled
	}
	// function to correct Firefox line height during font scaling
	// called by setFontIndex()
function setLineHeight(height){			// Because FireFox doesn't inherit lineheight
	divStyles = document.getElementsByTagName('div');
	for (var i = 0;i < divStyles.length;i++)
		divStyles.item(i).style.lineHeight = height;
	}
// END - Xanjax font scaling functions

// START - XHR loading animation functions
	// function to create/show, and remove, an XHR loading animation
	// dependencies eltId(), crEl() from xanjax.js (Xanjax Core)
	// and .indxr, .indxhrwrap from xanjax.css
function xaniMate(show,node,indNode){
 if(indNode=x$.eltId('xhrIndctr'))
	indNode.parentNode.removeChild(indNode);
 if(show&&(node||(node=x$.eltId('selected')))){
	var wrapDiv=x$.crEl('div',{'id':'xhrIndctr','class':'indxhrwrap'});
	wrapDiv.appendChild(x$.crEl('div',{'class':'indxhr'}));
	node.parentNode.insertBefore(wrapDiv,node);
}}
// END - loading animation functions

/* START - Twisty/Expander functions
	 setExpdr() attaches twisties and expander behaviour to suitable elements
	 dependencies crEl() from xanjax.js (Xanjax Core), and expContent()
	 and .twisty, .expander in content.css

	 script example for page needing expanders; 
	 you must delay setExpdr() call, to fix IE race condition, as below:
			window.setTimeout(function(){setExpdr(elTags("cite"));},10); */

function setExpdr(expElements){
var newDiv=x$.crEl('div',{'class':'twisty'});
	for (var i=0;i<expElements.length;i++){				// walk expander elements
		if (expElements[i].className=="expander"){	// if expander attach twisty
			expElements[i].parentNode.insertBefore(newDiv.cloneNode(true),expElements[i]);
			expElements[i].onclick=function(){expContent(this);};	// now attach behaviour
			expElements[i].previousSibling.onclick=function(){expContent(this.nextSibling);};
}}}
	// function to twist twisty and show/hide subordinate content
	// called by click events assigned in setExpdr()
function expContent(theObj){ // a fun little use of CSS "sprites!"
 if(theObj.nextSibling.style.display!="block"){
	theObj.style.color="rgb(0,0,100)";
	theObj.previousSibling.style.backgroundPosition="right top";
	theObj.nextSibling.style.display="block";}
 else{
	theObj.style.color="rgb(100,0,100)";
	theObj.previousSibling.style.backgroundPosition="left top";
	theObj.nextSibling.style.display="none";}
}
// END - Twisty/Expander functions

// START - xanjax form posting functions
	// function to collect form values for AJAX submission
	// this needs updating - in particular name formPost() and variable post
	// imply method must be POST, when GET is equally valid
function formPost(formId){
 var elms=x$.eltId(formId).elements;
 var post=""; var slctIdx="";
 for(var i=0; i<elms.length;i++)
  if(slctIdx=elms[i].selectedIndex+1)
   post+=elms[i].id+"="+elms[i].options[slctIdx-1].text+"&";
  else post+=elms[i].id+"="+elms[i].value+"&";
 return post.substr(0,post.length-1);
}

	// function provides XHR form submit navigation support for xanjax
	// dependencies mkAnch() from xanjax.js (Xanjax Core), and formPost()
	// originally built to provide native Xanjax support for WordPress
	// but should be generally useful for forms which are not AJAX aware
function getSubmit(id,url){
 url=url.replace("#","!").replace(x$.docRoot,x$.docRoot+"#")+"~"+formPost(id)
 x$.mkAnch(url.split("#")[1],anchorList.length)
 location.assign(url);
 return false;
}
	// function provides XHR form submit "GET" support for xanjax
	// dependencies eltId() from xanjax.js (Xanjax Core), and getSubmit()
	// originally built to provide native Xanjax support for WordPress
	// but should be generally useful for forms which are not AJAX aware
function wpFrm(elm,forms){
	if(forms=x$.eltId(elm).getElementsByTagName("form"))
	 for(i=0;i<forms.length;i++)
		if(forms[i].action.match(x$.docRoot)&&forms[i].method!="post")
	 	  forms[i].setAttribute('onsubmit','return getSubmit(this.id,this.action);');
}
	// function to "ajaxify form submit"
function jxfyFrm(elm,form){
 window.setTimeout(function(){
	if((form=x$.eltId(elm)) && !form.action.match("javascript")){
	 form.action='javascript:x$.xanXHR("'+form.action+'Content-Type=text/xml",x$.insPage,"","POST","async",formPost("webmail"))';
 }},10); // delay prevents IE 'race condition' bug
}
// END - xanjax form posting functions

// START function to correct apache directory listing path
function unHash(divId){
 var listAnchs=x$.eltId(divId).getElementsByTagName("a");
 for(i=1;i<listAnchs.length;i++)
 	if(!listAnchs[i].href.match(/\/$/))
 	 listAnchs[i].href=listAnchs[i].href.replace("#","");
}
// END function to correct apache directory listing path


