
var MaxBkg = 38, bkgNum;

function BkgChanger() {
	var imgNum = MaxBkg;
	bkgNum=Math.round((imgNum)*Math.random());
	var bkgClass = "bkg" + (bkgNum);
	document.getElementsByTagName("body")[0].className = bkgClass;
}

function BkgChooser(theBkg) {
	bkgNum = parseInt(theBkg);
	var bkgClass = "bkg" + (bkgNum), Prev = '', Next = '';
	document.getElementsByTagName("body")[0].className = bkgClass;
	if (bkgNum > 1 ) {
		Prev = ('<a href="javascript:void(0)" onclick="BkgChooser(' + (bkgNum-1) + ')">&lt; Prev. Bkg.</a>&nbsp;&nbsp;&nbsp;');
	} 
	if (bkgNum < MaxBkg) {
		Next = ('<a href="javascript:void(0)" onclick="BkgChooser(' + (bkgNum+1) + ')">Next Bkg. &gt;</a>');
	}
	document.getElementById('backgrounds').selectedIndex = bkgNum - 1;
	document.getElementById('prevnext').innerHTML = Prev + Next;
	fixCredit();
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function BkgPreloader() {
	var imgNum = MaxBkg;
	var dment=document; dment.imgArr = new Array();
	for (var i=1; i<=imgNum; i++) {
		dment.imgArr[i]=new Image;
		dment.imgArr[i].src = '/	images/' + i + '.jpg';
		//alert(dment.imgArr[i].src);
	}

}

var globPos = 0;


function getWindowHeight () {

	var Height,oBody = document.getElementsByTagName('html')[0];

	if (typeof document.documentElement != 'undefined'

	&& typeof document.documentElement.clientHeight !=

	'undefined' && document.documentElement.clientHeight != 0)	{

		Height = document.documentElement.clientHeight;

	}

	else {

		Height = document.getElementsByTagName('html')[0].clientHeight;

	}

//	var bPad = parseInt(getStyle(oBody, 'padding-top')) + 
//
//	           parseInt(getStyle(oBody, 'padding-bottom')) + 
//
//	           parseInt(getStyle(oBody, 'margin-top')) + 
//
//	           parseInt(getStyle(oBody, 'margin-bottom'));

//	Height = Height - bPad;

	return Height;

}


window.onload = windowLoader;

function windowLoader(){ 
	BkgPreloader();
	fixLayout();
	fixCredit();
	AddListen(window,'resize',fixLayout);
}

function fixLayout() {

	var MainDiv = document.getElementById('main'), MainTop = getTop(MainDiv) ;
	MainDiv.style.height = "auto";
	
	if((getTop(MainDiv) + MainDiv.offsetHeight) < getWindowHeight())  {

	    MainDiv.style.height = (getWindowHeight() - MainTop - 10) + "px";

	} 
	
	//alert((getTop(FooterDiv) + 26) + " " + getTop(FooterDiv) + " " + getWindowHeight());
	
}


function fixCredit() {
	var theCredit = document.getElementById('photocredit');
	if ((bkgNum > 15) && (bkgNum < 30)){
		theCredit.href = 'mailto:kevinrj@gmail.com';
		theCredit.innerHTML = 'Photo:Kevin Jamieson';
	} else if (bkgNum > 29){
		theCredit.href = 'http://www.danbarham.com';
		theCredit.innerHTML = 'Photo:Dan Barham';
	} else if ((bkgNum == 4) || (bkgNum == 15)) {
		theCredit.href = '';
		theCredit.innerHTML = '';
	} else {
		
		theCredit.href = 'http://www.sterlinglorence.com/';
		theCredit.innerHTML = 'Photo:Sterling Lorence';
	}
}

function AddListen(elE,elT,elF) {			

	if (elE.addEventListener) elE.addEventListener(elT,elF,false);/*if FF*/

	else if (elE.attachEvent) elE.attachEvent('on'+elT,elF);/*if IE*/

	else eval('elE.on'+elT+'=elF');

}

function getStyle(elObj, cssProperty){

    var cssValue = "";

    if(document.defaultView && document.defaultView.getComputedStyle){

        cssValue = document.defaultView.getComputedStyle(elObj, "").getPropertyValue(cssProperty);

    }

    else if(elObj.currentStyle){

        cssProperty = cssProperty.replace(/\-(\w)/g, function (strMatch, prop){ return prop.toUpperCase(); });

        cssValue = elObj.currentStyle[cssProperty];

    }

    return cssValue;

}


function getTop(obj)

{

	var postop = 0;

	if (obj.offsetParent)

	{

		while (obj.offsetParent)

		{

			postop += obj.offsetTop;

			obj = obj.offsetParent;

		}

	}

	return postop;

}

