/*
 * ----------------------------------------------
 * Image123 rotator v1.2
 * Copyright (c) RulacDesign
 * URL: http://www.rulac.net
 * ----------------------------------------------
 */


// fade steps
fade_steps	= 15;
// image time rotation
change_time	= 7000;


image123run	= false;
function image123(imagearray){
	for (i = 0; i < imagearray.length; i++){
		if (imagearray[i].style.zIndex == 1){
			if (imagearray[i].style.opacity){
				Fade(imagearray[i].id, 0, 100, fade_steps);
			}
			imagearray[i].style.zIndex	= imagearray.length;
		} else {
			imagearray[i].style.zIndex	= parseInt(imagearray[i].style.zIndex) - 1;
			if (!(imagearray[i].style.opacity)){
				imagearray[i].style.opacity	= "0";
				imagearray[i].style.filter	= "alpha(opacity = 0)";
			} else {
				imagearray[i].style.opacity	= "1";
				imagearray[i].style.filter	= "alpha(opacity = 100)";
			}
		}
	}
	if (image123run	== false){
		addLoadEvent(image123timeout);
		image123run			= true;
	} else {
		image123timeout();
	}
}
function image123timeout(){
	for (i = 0; i < imagearray.length; i++){
		if (imagearray[i].style.opacity == "0"){
			imagearray[i].style.opacity	= "1";
			imagearray[i].style.filter	= "alpha(opacity = 100)";
		}
	}
	image123time		= window.setTimeout(function(){
		image123(imagearray);
	}, change_time);
}
function image123init(){
	imagearray		= new Array();
	links			= document.getElementsByTagName("a");
	count			= 0;//alert(document.getElementsByTagName("a").length);
	for (i = 0; i < links.length; i++){
		if (links[i].rel == "image123"){
			links[i].id		= "image123_" + count;
			links[i].style.position	= "absolute";
			links[i].style.zIndex	= count + 1;
			imagearray[count]	= links[i];
			count++;
		}
	}
	if (imagearray.length){
		var image123navig	= document.createElement("div");
		image123navig.id	= "image123navig";
		imagearray[0].parentNode.parentNode.insertBefore(image123navig, imagearray[0].parentNode);
		imagelink		= document.createElement("a");
		imagelink.href		= "javascript:;";
		imagelink.onclick	= function(){
			image123set(this);
			if ("undefined" != typeof(event)){
				event.returnValue = false;
			}
		}
		imagelink.innerHTML	= "next";
		image123navig.appendChild(imagelink);
	}
	image123(imagearray);
}
wait			= false;
function image123set(obj){
	clearTimeout(image123time);
	if (wait == false){
		wait			= true;
		image123(imagearray);
		setTimeout("wait = false", 50 * fade_steps);
	}
}

if(typeof window.addLoadEvent != "function"){
	function addLoadEvent(func){
		var oldonload = window.onload;
		if (typeof window.onload != "function"){
			window.onload = func;
		} else {
			window.onload = function(){
				if (oldonload){
					oldonload();
				}
				func();
			}
		}
	}
}

// http://www.steffest.com/fadeimage.htm
function Fade(objID, CurrentAlpha, TargetAlpha, steps){
	var obj			= document.getElementById(objID);
	CurrentAlpha		= parseInt(CurrentAlpha);
	if (isNaN(CurrentAlpha)){
		CurrentAlpha		= parseInt(obj.style.opacity * 100);
		if (isNaN(CurrentAlpha)) CurrentAlpha = 100;
	}
	var DeltaAlpha		= parseInt((CurrentAlpha - TargetAlpha) / steps);
	var NewAlpha		= CurrentAlpha - DeltaAlpha;
	if (NewAlpha == 100 && (navigator.userAgent.indexOf("Gecko") != -1 && navigator.userAgent.indexOf("Safari") == -1)){
		NewAlpha		= 99.99;
	}
	obj.style.opacity	= (NewAlpha / 100);
	obj.style.MozOpacity	= obj.style.opacity;
	obj.style.KhtmlOpacity	= obj.style.opacity;
	obj.style.filter	= "alpha(opacity = " + NewAlpha + ")";
	if (steps > 1){
		setTimeout("Fade(\"" + objID + "\", " + NewAlpha + ", " + TargetAlpha + ", " + (steps - 1) + ")", 50);
	}
}

// DOMReady by Stuart Colville
// http://muffinresearch.co.uk/archives/2008/02/15/does-settimeout-solve-the-domcontentloaded-problem/
function DOMReady(f){
	if (/(?!.*?compatible|.*?webkit)^mozilla|opera/i.test(navigator.userAgent)){ // Feeling dirty yet?
		document.addEventListener("DOMContentLoaded", f, false);
	} else {
		window.setTimeout(f, 0);
	}
}
DOMReady(image123init);
