
// ROTATOR CONFIG

// NB: make sure that you have a comfort margin
//   slidefadersecondaryinc x slidefadersecondarysteps should be less than slidefaderprimaryinc
//   pref less than a quarter of slidefaderprimaryinc
//   many browsers wont be able to keep up with 25 frames a second.

slidefaderfadein=         1;
slidefaderautoplay=       1;
slidefaderdirection=      1; //0 =backward 1 =forward
slidefadersecondaryinc=   40;  //ms time between fade steps, do NOT try to go smaller than 40.
slidefadersecondarysteps= 100;  //no of steps to fade
slidefaderprimaryinc=     12000; //ms time between slides


//preload('/templates/imgs/nav-dropdown-bg-gallery.png');



//addtional stuff to call on pageload
function	siteonloads() {
	//start up  dynamic bg resizer
	setBackground('background');
	
	//sticky footer
	setFooter(1);
	
	//resize event handlers
	window.onresize = function() { resizestuff();}
}

function resizestuff (){
	setFooter();
	setBackground();
}



//globals for bg image resizer
var iratio;
var imgobj;
//ditto for sticky
var contentheight;
var bodyheight;


//set bg image size
function setBackground(divid) {

	//on first run 
	// -onload get the img object
	// -and get orig img size
	// -set resize event
	
	if (divid && !imgobj) {
		//find the img
		imgobj= getfirstchild(divid,'IMG');
		if (imgobj) { 
			//get img native dims
			//these seem to be only be available when fresh, after that they change
			iratio= imgobj.width/imgobj.height;	
			
		}
	}

	if (!imgobj || !iratio) return false;
	
	//get window
	var wheight=    getWindowHeight();
	var wwidth=    getWindowWidth();
	if (wheight==0) return;
		
	//declare final img parameters
	var itop= 0;
	var ileft=0;
	var iwidth=0;
	var iheight=0;
	
	var wratio= wwidth/wheight;
	
	//if need to crop top and bottom
	if (wratio > iratio) {
		iwidth=  wwidth;
		iheight= Math.round(wwidth/iratio); 
		ileft=   0;
		itop=   Math.round((wheight-iheight)/2);
	}
	//or crop sides
	else {
		iwidth=  Math.round(wheight*iratio); 	
		iheight= wheight;
		ileft=   Math.round((wwidth-iwidth)/2);
		itop=    0;
	}
	
	//set new img size
	imgobj.style.width=  iwidth+'px';
	imgobj.style.height= iheight+'px';
	imgobj.style.position='relative';
	imgobj.style.top=   itop+'px';
	imgobj.style.left=  ileft+'px';
	
	
}


//ie get first img tag inside divid
function getfirstchild(divid,tagname){
	var dobj =   document.getElementById(divid);	
	var elements= dobj.all ? dobj.all : dobj.getElementsByTagName(tagname);
	if (!elements) return(false);
	
	var imgobj;
	for (i=0; i< elements.length; i++) {
		if (elements[i].tagName.toUpperCase()=='IMG'){
			imgobj=elements[i];
			return(imgobj);
		}
	}
	return(false);
}

//sticky footer
function setFooter(firstrunflag) {

	//set this to the content container, that which carrys the total content height
	var contentid='container';
	//set this to the body elem that gets its height increased to achive sticky foot
	var bodyid='body-box';
	
	//store nat heights before they are messed with
	if (firstrunflag) {
		contentheight=  document.getElementById(contentid).offsetHeight;
		bodyheight=     document.getElementById(bodyid).offsetHeight;
	}
	
	if (document.getElementById) {
		var windowHeight=getWindowHeight();
		if (windowHeight>0) {
			var newContentHeight= document.getElementById(contentid).offsetHeight;
			var bodyElement=      document.getElementById(bodyid);
			//var newBodyHeight=  bodyElement.offsetHeight;
			var newBodyHeight= windowHeight- (contentheight-bodyheight);
			if (newBodyHeight < bodyheight) {newBodyHeight=bodyheight;}
			bodyElement.style.height=newBodyHeight+'px';
			
			//if (windowHeight > contentheight) {
			//bodyElement.style.height=(newBodyHeight + windowHeight - newContentHeight)+'px';
			//}
				
			
		}
	}
}

function getWindowHeight() {
	if      (self.innerHeight)	return self.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight;
	else if (document.body)	return document.body.clientHeight;
}

function getWindowWidth() {
	if      (self.innerWidth)	return self.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientWidth;
	else if (document.body)	return document.body.clientWidth;
}


