var SLI = {};

// Number of slides
SLI.NUM = 5;
// Fade on/off
SLI.fade = true;
// Fade rate
SLI.rate = 2;
// Fade Speed in milliseconds (higher is slower)
SLI.speed = 10;
// Slide pause rate in milliseconds
SLI.pause = 8500;

SLI.$ = function(x){
	return document.getElementById(x);
};

SLI.onWindowLoad = function(func){
	// Run function 'func' at window.onload after other (previously set) handlers have fired. 
	if(typeof window.addEventListener != 'undefined') {
		// moz, saf1.2, ow5b6.1
		window.addEventListener('load', func, false);
	} else if (typeof document.addEventListener != 'undefined') {
		// MSN/OSX, op7.50, saf1.2, ow5b6.1
		document.addEventListener('load', func, false);
	} else if (typeof window.attachEvent != 'undefined') {
		// ie5.0w, ie5.5w, ie6w
		window.attachEvent('onload', func);
	} else {
		if (typeof window.onload == 'function') {
			// Add 'func' to existing handlers
			var oldonload = window.onload;
			window.onload = function() {
				oldonload();
				func();
			};
		} else {
			window.onload = func;
		}
	}
};

SLI.addEvent = function(el,nm,evt){
	var rtn = true;
	if(typeof el.addEventListener != "undefined"){ // Mozilla		
		el.addEventListener(nm,evt,false);
	}else if(typeof el.attachEvent != "undefined"){
		el.attachEvent("on" + nm,evt);
	}else{
		rtn = false;
	}
	return rtn;
};

SLI.changeOpacity = function(){
	if(document.all){
		SLI.$("homemaincallout" + SLI.index).style.filter = "alpha(opacity="+SLI.fadeOpacity+")"
	}else{
		try{
			SLI.$("homemaincallout" + SLI.index).style.opacity = SLI.fadeOpacity/100;
		}catch(err){}
	}
};

SLI.click = function(i){	
	if(typeof SLI.index == "undefined")
		SLI.index = 1;
	clearTimeout(SLI.timeout);
	if(SLI.fade){
		SLI.fadeOpacity = 0;
		SLI.changeOpacity();
	}
	SLI.$("homemaincallout" + SLI.index).style.display = "none";
	SLI.$("button" + SLI.index).style.backgroundPosition = "0 0";
	SLI.index = i;
	if(SLI.fade){
		SLI.fadeOpacity = 100;
		SLI.changeOpacity();
	}
	SLI.$("homemaincallout" + SLI.index).style.display = "block";
	SLI.$("button" + SLI.index).style.backgroundPosition = "0 -35px";
	
	SLI.timeout = setTimeout(SLI.fadeOut,SLI.pause);
};

SLI.fadeIn = function(){
	SLI.changeOpacity();
	SLI.fadeOpacity += SLI.rate;
	if(SLI.fadeOpacity < 100){
		SLI.timeout = setTimeout(SLI.fadeIn,SLI.speed);
	}else{
		SLI.fadeOpacity = 100;
		SLI.changeOpacity();
		SLI.timeout = setTimeout(SLI.fadeOut,SLI.pause);
	}
}
SLI.fadeOut = function(){
	if(SLI.fade){
		if(typeof SLI.fadeOpacity == "undefined"){
			SLI.fadeOpacity = 100;
		}
		SLI.changeOpacity();
		SLI.fadeOpacity -= SLI.rate;
		if(SLI.fadeOpacity > 0){
			SLI.timeout = setTimeout(SLI.fadeOut,SLI.speed);
		}else{
			SLI.fadeOpacity = 0;
			SLI.changeOpacity();
			SLI.$("homemaincallout" + SLI.index).style.display = "none";
			SLI.$("button" + SLI.index).style.backgroundPosition = "0 0";
			if(SLI.index < SLI.NUM){
				SLI.index++;
			}else{
				SLI.index=1;
			}		
			SLI.$("homemaincallout" + SLI.index).style.display = "block";
			SLI.$("button" + SLI.index).style.backgroundPosition = "0 -35px";
			SLI.fadeIn();
		}
	}else{
		SLI.$("homemaincallout" + SLI.index).style.display = "none";
			SLI.$("button" + SLI.index).style.backgroundPosition = "0 0";
		if(SLI.index < SLI.NUM){
			SLI.index++;
		}else{
			SLI.index=1;
		}
		SLI.$("homemaincallout" + SLI.index).style.display = "block";
		SLI.$("button" + SLI.index).style.backgroundPosition = "0 -35px";
		SLI.timeout = setTimeout(SLI.fadeOut,SLI.pause);
	}
};

SLI.startSlide = function(){	
	if(typeof SLI.index == "undefined"){
		SLI.index = 1;
	}
	SLI.fadeOut();
	
};

SLI.init = function(){
	var bool = true;
	for(var i = 1; i <= SLI.NUM; i++){
		bool = SLI.addEvent(SLI.$("button" + i),"click",new Function("SLI.click("+i+")"));
		SLI.$("button" + i).setAttribute("href","javascript:void(0);");
	}
	if(bool)
		SLI.timeout = setTimeout(SLI.startSlide,SLI.pause);
};
SLI.onWindowLoad(SLI.init);
