
window.onload = function() {
	setupPage();
	setupCampTicker();
	doFixBottomLink();
}

function setupPage() {
	// set variables
	var bgOffset = 192;
	
	var oPage = document.getElementById("if-page");
	var oBox = document.getElementById("if-filler-box");
	
	// get height of content without filler box
	if (oBox) oBox.style.display = "none";
	var pageHeight = getPageHeight();
	
	// setup orange IF filler box
	if (oBox) {
		// hide box until setup is done
		oBox.style.visibility = "hidden";
		oBox.style.display = "inline";
		var boxY = getAbsTop(oBox);
		
		// set background position
		oBox.style.backgroundPosition = "0px -" + (boxY - bgOffset) + "px";
		
		// remove list elements one by one if filler is too tall
		if (pageHeight < boxY+parseInt(oBox.offsetHeight)) {
			var oLi = oBox.getElementsByTagName("li");
			for (var i=oLi.length-1; i>=0; i--) {
				oLi[i].style.display = "none";
				if (pageHeight > boxY+parseInt(oBox.offsetHeight)) break;
			}
			if (i<0 && oLi.length>0) oLi[0].parentNode.style.display = "none";
		}
		
		// remove link if filler is too tall
		if (pageHeight < boxY+parseInt(oBox.offsetHeight)) {
			var oLinks = oBox.getElementsByTagName("a");
			for (var i=0; i<oLinks.length; i++) {
				if (oLinks[i].className.indexOf("if-bottom-link")!=-1) oLinks[i].style.display = "none"
			}
		}
		
		// if still too tall remove logo, else expand to fill remaining gap
		if (pageHeight < boxY+parseInt(oBox.offsetHeight)) {
			oBox.style.display = "none";
		} else {
			document.getElementById("if-filler").style.height = pageHeight - (boxY + parseInt(oBox.offsetHeight)) + "px";
			document.getElementById("if-filler").style.display = "block";
		}
		
		// setup complete, show box again
		oBox.style.visibility = "visible";
	}
	
	// resize any other fillers to maintain coloumn height
	var oDiv = document.getElementsByTagName("div");
	for (var i=0; i<oDiv.length; i++) {
		if (oDiv[i].className.indexOf("if-filler")!=-1) {
			//alert(i + ", " + oDiv[i].offsetTop)
			oDiv[i].style.display = "block";
			if (pageHeight == getPageHeight()) {
				oDiv[i].style.height = pageHeight - getAbsTop(oDiv[i]) - parseInt(oDiv[i].offsetHeight) + "px";
				var dif = getPageHeight() - pageHeight;
				if (dif>0) oDiv[i].style.height = parseInt(oDiv[i].offsetHeight) - dif + "px";
				
				// set as gray if child of empty if-camp-text
				if (oDiv[i].parentNode.className.indexOf("if-camp-autoheight")!=-1) {
					if (getAbsTop(oDiv[i])-getAbsTop(oDiv[i].parentNode)<=5) addClass(oDiv[i],"if-filler-gray");
				}
			} else {
				oDiv[i].style.display = "none";
			}
			
		}
	}
}

function getPageHeight() {
	var oPage = document.getElementById("if-page");
	var bottomPadding = 40;
	return getAbsTop(oPage) + parseInt(oPage.offsetHeight) - bottomPadding;
}





var oCampTickers = new Array();
function setupCampTicker() {
	var oDiv = document.getElementsByTagName("div");
	for (var i=0; i<oDiv.length; i++) {
		var n = oCampTickers.length;
		if (oDiv[i].className.indexOf("if-camp-ticker")!=-1) oCampTickers[n] = new campTickerObj(n, oDiv[i]);
	}
}
function campTickerObj(n,o) {
	this.n = n;
	
	this.htmlNode = o;
	this.nodes = o.getElementsByTagName("li");
	this.current = 0;
	
	// hide elements
	for (var i=0; i<this.nodes.length; i++) this.nodes[i].style.display = (i==this.current)? "block" : "none";
	
	// setup navigation
	this.step = campTickerStep;
		
	var oLinks = o.getElementsByTagName("a");
	for (var i=0; i<oLinks.length; i++) {
		if (oLinks[i].className.indexOf("if-prev")!=-1) {
			oLinks[i].style.display = "block";
			oLinks[i].onclick = function() {
				oCampTickers[n].step(-1);
			}
		}
		if (oLinks[i].className.indexOf("if-next")!=-1) {
			oLinks[i].style.display = "block";
			oLinks[i].onclick = function() {
				oCampTickers[n].step(1);
			}
		}
	}
}
function campTickerStep(n) {
	this.nodes[this.current].style.display = "none";
	this.current = (this.current==0 && n==-1)? this.nodes.length-1 : (this.current + n) % this.nodes.length;
	this.nodes[this.current].style.display = "block";
}




function addClass(o,str) {
	remClass(o,str);
	o.className += " " + str;
}
function remClass(o,str) {
	o.className = o.className.replace(str,"");
	o.className = o.className.replace(/' '{2,}/gi," ");
}

 

function getAbsLeft(o) {  
	var iY = 0; 
	while(o.offsetParent){ 
		iY += parseInt(o.offsetLeft);
		o = o.offsetParent;  
	} 
	return iY; 
} 

function getAbsTop(o) {  
	var iX = 0; 
	while(o.offsetParent){ 
		//window.status += o.offsetParent.nodeName + "=" + o.offsetTop + " | "; 
		iX += parseInt(o.offsetTop);  
		o = o.offsetParent;  
	} 
	return iX
}


/*This fixes the "if-bottom-link" that don't display when it's empty in firefox*/
function doFixBottomLink()
{
	var allTags = new Array();
	allTags = document.getElementsByTagName("a");
	for (var i=0;i<allTags.length;i++)
	{
		if (allTags[i].className == 'if-bottom-link' && allTags[i].innerHTML.replace(/^\s+|\s+$/g, '') == "")
		{
			allTags[i].innerHTML = '&nbsp;';
		}				
	}
}
