/*

Better(?) Image fader (C)2004 Patrick H. Lauke aka redux

Inspired by Richard Rutter / Clagnut http://www.clagnut.com/blog/1299/ 

Original concept and code adapted from Couloir http://www.couloir.org/ 

preInit "Scheduler" idea by Cameron Adams aka The Man in Blue
http://www.themaninblue.com/writing/perspective/2004/09/29/ 

*/

/* general variables */

var fade_TargetId = 'content'; /* change this to the ID of the fadeable object */
var loadingId = 'loading';
var	fade_Target;
var objLoading;
var preInitTimer;
var timeoutTimer;


/* functions */
fade_init();


function fade_init() {
	/* 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)&&(fade_Target=document.getElementById(fade_TargetId))) {
		fade_Target.style.visibility = "hidden";
		set_Opacity(0);
		setPreloadImg();
		if (typeof preInitTimer != 'undefined') clearTimeout(preInitTimer); /* thanks to Steve Clay http://mrclay.org/ for this small Opera fix */
	} else {
		setPreloadImg();
		preInitTimer = setTimeout("fade_init()",2);
	}
}

function fade_In(opacity){
	fade_Target = document.getElementById(fade_TargetId);
	if (fade_Target) {
		if (opacity > 0) {
			set_Opacity(opacity);			
			opacity = opacity - 5;
			timeoutTimer = window.setTimeout("fade_In("+opacity+")", 20);
			if(opacity <=0){
				fade_Target.style.visibility = '';//hidden
			}
		}
	}
}

function fade_Out(opacity){
	if(document.getElementById(loadingId)) document.getElementById(loadingId).style.display = 'none';
	fade_Target = document.getElementById(fade_TargetId);
	if (fade_Target) {
		if (opacity <= 100) {
			set_Opacity(opacity);			
			opacity += 5;
			timeoutTimer = setTimeout("fade_Out("+opacity+")", 20);
		}
	}
}
function getSize() {
  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;
  }
  
 var out = Array();
 out[0] = myWidth;
 out[1] = myHeight;
 return out;
  //window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );
}
	
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  //return [ scrOfX, scrOfY ];
  return scrOfY;
}
	
function setPreloadImg() {
	var screenY = getSize()[1];
	var offY = getScrollXY();
	var screenX = getSize()[0];
	var loadY = offY + ((screenY/2) - 20);
	objLoading = document.getElementById('loading');
	if(objLoading){
	 	objLoading.style.top = loadY + 'px';
	 	objLoading.style.width = screenX + 'px';
		objLoading.style.display = '';
	}
}

function set_Opacity(opacity){
	if (fade_Target.style.MozOpacity!=null) {
		/* Mozilla's pre-CSS3 proprietary rule */
		fade_Target.style.MozOpacity = (opacity/100)-.001;
		/* the .001 fixes a glitch in the opacity calculation which normally results in a flash when reaching 1 */
	} else if (fade_Target.style.opacity!=null) {
		/* CSS3 compatible */
		fade_Target.style.opacity = (opacity/100)-.001;
	} else if (fade_Target.style.filter!=null) {
		/* IE's proprietary filter */
		fade_Target.style.filter = "alpha(opacity="+opacity+")";
		/* worth noting: IE's opacity needs values in a range of 0-100, not 0.0 - 1.0 */ 
	}
}

function fade_In_Start(){
	clearTimeout(timeoutTimer);
	setPreloadImg();
	if(document.getElementById(loadingId)) document.getElementById(loadingId).style.display = '';
	opacity = 100;
	set_Opacity(opacity);
	fade_In(opacity);
}
function fade_Out_Start(){
	clearTimeout(timeoutTimer);
	if(document.getElementById(loadingId)) document.getElementById(loadingId).style.display = '';
	opacity = 0;
	set_Opacity(opacity);
	fade_Target.style.visibility = 'visible';
	fade_Out(opacity);
	tooltipX();
}

function addEvent(elm, evType, fn, useCapture){
	if (elm.addEventListener){
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}else if (elm.attachEvent){
		var r = elm.attachEvent("on"+evType, fn);
		return r;
	}
}

addEvent (window,'load',fade_Out_Start);
