// JavaScript Document
/* ################################ GENERIC RANDOM BACKGROUND IMAGE ############################## */
/* This function changes the background image of the header, if no Javascript leaves the defaut in */

var MyElement = "header" //The ID of the element you want to change
//var ImgPath = "http://localhost/wordpress/wordpress2.8.4/wp-content/tmhhir-images/" //The file path to your images
var ImgPath = "http://www.chiptuningexperience.nl/wp-content/tmhhir-images/" //The file path to your images
var fade_next = 'in';

function ChangeCSSBgImg() {
	//alert(js_img_list);
	if (!document.getElementById) return false;	
	if (!document.getElementById(MyElement)) return false;
	
	var random_images = new Array ();
	random_images = js_img_list;
	/*random_images[0] = "1.jpg";
	random_images[1] = "2.jpg";
	random_images[2] = "3.jpg";*/
	
	var $header = document.getElementById(MyElement);
	var $backgroundurl = $header.style.backgroundImage;
	var ImgURL = "url(" + ImgPath + random_images[rand(random_images.length)] + ")";
	
	if ($backgroundurl != ImgURL) {
		$header.style.backgroundImage = ImgURL;	
		if(fade_next=='in') fadeIn($header);
		else if(fade_next=='out') fadeOut($header);
	}
	
	movement = setTimeout("ChangeCSSBgImg()",12000);
}

/* random number generator */
function rand(n) {
  return ( Math.floor ( Math.random ( ) * n ) );
}

/* Custom onload function */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
/* trigger onload */
addLoadEvent(ChangeCSSBgImg);

function fadeIn(fobj){
	var fadespeed   = 100;  //higher = slower
	var baseopacity = 50;
	winObj = fobj;
	browserdetect = fobj.filters? "ie" : typeof fobj.style.MozOpacity=="string"? "mozilla" : "";
	instantSet(baseopacity);
	highlighting = setInterval("gradualFadeIn(winObj)",fadespeed);
}
function gradualFadeIn(fobject){ 
	//alert(fobject.filters[0].opacity)
	if (browserdetect=="mozilla" && fobject.style.MozOpacity<1) {
		fobject.style.MozOpacity=Math.min(parseFloat(fobject.style.MozOpacity)+0.05, 1);
	} 
	else if (browserdetect=="ie" && fobject.filters[0].opacity<100) {
		fobject.filters[0].opacity+=5;
	}
	else if (window.highlighting) {
		fade_next = 'out';
		clearInterval(highlighting);
	}
}
function instantSet(degree){
	clearTimer()
	if (browserdetect=="mozilla") {
		winObj.style.MozOpacity=degree/100;
	} else if (browserdetect=="ie") {
		winObj.style.filter = 'alpha(opacity:'+degree+')';
	}
}
function clearTimer(){
	if (window.highlighting) {
		clearInterval(highlighting);
	}
}
function fadeOut(fobj){
	var fadespeed   = 100;  //higher = slower
	var baseopacity = 100;
	winObj = fobj;
	browserdetect = fobj.filters? "ie" : typeof fobj.style.MozOpacity=="string"? "mozilla" : "";
	instantSet(baseopacity);
	highlighting = setInterval("gradualFadeOut(winObj)",fadespeed);
}
function gradualFadeOut(fobject){
	if (browserdetect=="mozilla" && fobject.style.MozOpacity>0.6) {
		fobject.style.MozOpacity=Math.min(parseFloat(fobject.style.MozOpacity)-0.05, 1);
	} else if (browserdetect=="ie" && fobject.filters[0].opacity>60) {
		fobject.filters[0].opacity-=5;
	} else if (window.highlighting) {
		fade_next = 'in';
		if (browserdetect=="mozilla") fobject.style.MozOpacity = 1;
		else if (browserdetect=="ie") fobject.filters[0].opacity = 100;
		clearInterval(highlighting);
	}
}
