/*	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){	// rather than explain this, it's a good exercise to help you
 var node="";							// understand crEl() use, prior to customising JSON functions
 if (show&&(node=x$.eltId('selected'))){ // and to understand how the DOM and CSS are used
	 var wrapDiv=x$.crEl('div',{'class':'indxhrwrap'}); // to bring it all together. Enjoy!
	 wrapDiv.appendChild(x$.crEl('div',{'id':'xhrIndctr','class':'indxhr'}));
	 node.parentNode.insertBefore(wrapDiv,node);
 }
 else if(node=x$.eltId('xhrIndctr'))
	node.parentNode.parentNode.removeChild(node.parentNode);
}
// END - loading animation functions

// START - Twisty/Expander functions
	// script for page needing expanders; delays regElement() call, to fix IE race condition
	// window.setTimeout(function(){setExpdr(elTags("cite"));},10);

	// function to attach twisties and expander behaviour to suitable elements
	// dependencies crEl() from xanjax.js (Xanjax Core), and expContent()
	// and .twisty, .expander in content.css
	// called by script in page needing expanders, must delay 10mS to fix IE race condition
function setExpdr(expElements){
var newDiv=x$.crEl('div',{'class':'twisty'});
	for (var i=0;i<expElements.length;i++){										// find expander elements
		if (expElements[i].getAttribute('class')=="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);');
}
// END - xanjax form posting functions

