// Slideshow
var zInterval = null, current=0, pause=false;

function so_init() {
	var c = document.getElementById("image_container"); 
	if(c){	
		imgs = document.getElementById("image_container").getElementsByTagName("img");
		for(i=0;i<imgs.length;i++){
			imgs[i].style.display = 'none';
			imgs[i].xOpacity = 0;
		}		
		imgs[0].style.display = "block";
		imgs[0].xOpacity = .99;
		
		// Check to see if second image is completely available before beginning
		it = 0;
		function isComplete(){
			if(imgs[1].complete || imgs[1].complete == undefined || it == 40){				
				setTimeout(so_xfade,5000);
				clearTimeout(com);					
			} else {
				it += 1;
			}
		};
		com = setInterval(isComplete,10);
	}
}

function so_xfade() {
	cOpacity = imgs[current].xOpacity;
	nIndex = imgs[current+1]?current+1:0;

	nOpacity = imgs[nIndex].xOpacity;
	
	cOpacity-=.05; 
	nOpacity+=.05;
	
	imgs[nIndex].style.display = "block";
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;
	
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
	
	if(cOpacity<=0) {
		imgs[current].style.display = "none";
		current = nIndex;
		setTimeout(so_xfade,3000);
	} else {
		setTimeout(so_xfade,50);
	}
	
	function setOpacity(obj) {
		if(obj.xOpacity>.99) {
			obj.xOpacity = .99;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}
}

// Cross browser get window dimensions
function getSize(which) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return (which=='height' ? myHeight : myWidth);
};

// Stop page going off the top of the screen when resized
function topBlocker(){
	var h = getSize('height');
	var wr = document.getElementById('wrapper');
	if(h <= 450){
		wr.className = 'halt';
	} else {
		wr.className = '';
	}
};

// Window & Loading Events
window.onresize = function(){
	topBlocker();
};

function init() {
	if (arguments.callee.done) return;
	arguments.callee.done = true;
	if (_timer) clearInterval(_timer);
	
	// List functions to run on DOM Load
	so_init();
	topBlocker();
	// end
};

/* for Mozilla/Opera9 and Safari */
if (document.addEventListener) {
	/* for Safari */
	if (/WebKit/i.test(navigator.userAgent)){
		var _timer = setInterval(function(){if(/loaded|complete/.test(document.readyState)){ init(); }}, 0);
		window.onload(init);
	} else {
		document.addEventListener("DOMContentLoaded", init, false);
	}
} else {
	document.write("<script id=__ie_onload defer src=//:><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			this.onreadystatechange = null;
			init(); // call the onload handler
		}
	};
}
