﻿var fadePreInitTimer;
var fadeCurrent;
var fadeTargets;
var fadeTargetsCount=0;
var fadeTargetPos=0;
var	fadeTarget;
var fadeBack=false;
var fadeInterval=5000;
var OnFadeEnd=null;
var OnFadeStart=null;

function fadeLoad(array){fadeTargets=array;fadeTargetsCount=fadeTargets.length;}

function fadePreInit() {
    /* an inspired kludge that - in most cases - manages to initially hide the image
       before even onload is triggered (at which point it's normally too late, and a nasty flash
       occurs with non-cached images) */
    if (document.getElementById) { //&&(fadeTarget=document.getElementById(fadeTargets[0])))
        fadeTarget = fadeTargets[0]; // new
        var aux=0;
        for(aux=0;aux<fadeTargets.length;aux++)
            fadeTargets[aux].style.visibility = "hidden";
        if (typeof preInitTimer != 'undefined') clearTimeout(preInitTimer); // thanks to Steve Clay http://mrclay.org/ for this small Opera fix
    } else {
        fadePreInitTimer = setTimeout("fadePreInit()",2);
    }
}

function fadeInit() {
    if (document.getElementById) {
        /* get a handle on the fadeable object, to make code later more manageable */
        fadePreInit(); /* shouldn't be necessary, but IE can sometimes get ahead of itself and trigger fadeInit first */
        /* set the initial opacity in a (hopefully) cross browser way
           notice that because of the way the image is in front, and not obfuscated
           by another object we need to "fade out", i don't need a fallback mechanism
           to show/hide the covering object...the image is just there, full stop */
        
        if (fadeTarget.style.MozOpacity!=null) {  
	        // Mozilla's pre-CSS3 proprietary rule
	        fadeTarget.style.MozOpacity = 0;
        } else if (fadeTarget.style.opacity!=null) {
	        // CSS3 compatible
	        fadeTarget.style.opacity = 0;
        }
        // make the object visible again
        fadeTarget.style.visibility = 'visible';
        window.setTimeout("fadeIn(0)", 500);
    }
}

function fadeIn(opacity) {
    if(OnFadeStart && opacity==0) OnFadeStart();
    if (fadeTarget) {
        fadeTarget.style.visibility='visible';
        if (opacity <= 100) {
	        if (fadeTarget.style.MozOpacity!=null) {
		        // Mozilla's pre-CSS3 proprietary rule
		        fadeTarget.style.MozOpacity = (opacity/100);
	        } else if (fadeTarget.style.opacity!=null) {
		        // CSS3 compatible
		        fadeTarget.style.opacity = (opacity/100);
	        }
	        opacity += 10;
	        window.setTimeout("fadeIn("+opacity+")", 30);
        }
        else {
            if(OnFadeIn) OnFadeIn();
        }
    }
}

function fadeOut(opacity) {
    if (fadeTarget) {
        if (opacity >= 0) {
	        if (fadeTarget.style.MozOpacity!=null) {
		        // Mozilla's pre-CSS3 proprietary rule
		        fadeTarget.style.MozOpacity = (opacity/100);
	        } else if (fadeTarget.style.opacity!=null) {
		        // CSS3 compatible
		        fadeTarget.style.opacity = (opacity/100);
	        }
	        opacity -= 10;
	        window.setTimeout("fadeOut("+opacity+")",30);
        }
        else {
            fadeTarget.style.visibility='hidden';
            if(OnFadeEnd) OnFadeEnd();
            if(OnFadeOut) OnFadeOut();
        }
    }
}    

var OnFadeOut = function() {
    if(fadeBack)fadeTargetPos--;else fadeTargetPos++;
    while(fadeTargetPos<0)fadeTargetPos+=fadeTargetsCount;
    fadeTargetPos=fadeTargetPos%fadeTargetsCount;
    fadeTarget = fadeTargets[fadeTargetPos];
    fadeCurrent = window.setTimeout("fadeIn(0)", 2);
}

var OnFadeIn = function() {
    fadeCurrent = window.setTimeout("fadeOut(100)",fadeInterval);
}

function FadePrev() {
    clearTimeout(fadeCurrent);
    fadeBack=true;
    window.setTimeout("fadeOut(100)",500);
    OnFadeIn=null;
}

function FadeNext() {
    clearTimeout(fadeCurrent);
    fadeBack=false;
    window.setTimeout("fadeOut(100)",500);
    OnFadeIn=null;
}

