
var ImageRotation = {};
	var images = new Array();
	images[0] = "/images/theme/header-snowdon.jpg";
	images[1] = "/images/theme/header-llynharlech.jpg";
	images[2] = "/images/theme/header-orig.jpg";
	images[3] = "/images/theme/header-rhinogs-camp.jpg";

	var preLoad = new Array();
	
	var t;
	
	var nextId = 0;
	
	var element = "hdimg";
	
	var speed = 10000;
	
	var opacity = 1.0;
	var firstOpacity = 1.0;
	var secondOpacity = 0.0;

ImageRotation = {
	
	init: function () {
		//var el = document.getElementById("user-menu");
		//el.style.zIndex = 1;
		//el.style.display = "block";
		// start with random picture
		nextId = Math.round(Math.random()*images.length);
		
		// try once, then time it after that
		ImageRotation.rotate();
   		//t = setTimeout('ImageRotation.rotate()', speed);

		// we don't use this, but it puts them in the browser's memory
		for (i = 0; i < images.length; i++){
   			preLoad[i] = new Image();
   			preLoad[i].src = images[i];
		}
	},
	
	rotate: function() {
   		if (document.body) {
   			//document.body.background = Pic[j];
			var el = document.getElementById(element);
			//el.style.background = "url(" + images[nextId++] + ") no-repeat left top" ;
			el.style.backgroundImage = "url(" + images[nextId++] + ")";
			//el.background = preLoad[nextId++];
   			if (nextId > (images.length-1)) {
				nextId=0;
			}
   		}
   		//t = setTimeout('ImageRotation.fadeOut()', speed);
		t = setTimeout('ImageRotation.setSecondPic()', speed);
	},/*
	fadeOut: function() {
		
        //-moz-opacity: 0.6;
        //opacity:0.6;
        //filter: alpha(opacity=60);
		opacity -= 0.1;
		
		var el = document.getElementById(element);
		el.style.mozOpacity = opacity;
		el.style.opacity = opacity;
		el.style.filter = "alpha(opacity=" + Math.floor(opacity*10) + ")";
		
		if (opacity > 0.0) {
   			t = setTimeout('ImageRotation.fadeOut()', 100);
		} else {
   			t = setTimeout('ImageRotation.setImage()', 100);
		}
	},
	setImage: function() {
		var el = document.getElementById(element);
		el.style.backgroundImage = "url(" + images[nextId++] + ")";
   			if (nextId > (images.length-1)) {
				nextId=0;
			}
   		t = setTimeout('ImageRotation.fadeIn()', 100);
	},
	fadeIn: function() {
		opacity += 0.1;
		
		var el = document.getElementById(element);
		el.style.mozOpacity = opacity;
		el.style.opacity = opacity;
		el.style.filter = "alpha(opacity=" + Math.floor(opacity*10) + ")";
		
		if (opacity < 1.0) {
   			t = setTimeout('ImageRotation.fadeIn()', 100);
		} else {
   			t = setTimeout('ImageRotation.fadeOut()', speed);
		}
	},*/
	fadeToFirst: function() {
		firstOpacity += 0.1;
		secondOpacity -= 0.1;
		
		if (firstOpacity > 0.98) {
			secondOpacity = 0.0;
			firstOpacity = 1.0;
		}
		
		var el = document.getElementById('hdimg');
		el.style.mozOpacity = firstOpacity;
		el.style.opacity = firstOpacity;
		el.style.filter = "alpha(opacity=" + Math.floor(firstOpacity*10) + ")";
		
		el = document.getElementById('hdimg2');
		el.style.mozOpacity = secondOpacity;
		el.style.opacity = secondOpacity;
		el.style.filter = "alpha(opacity=" + Math.floor(secondOpacity*10) + ")";
		
		if (firstOpacity < 0.98) {
   			t = setTimeout('ImageRotation.fadeToFirst()', 100);
		} else {
   			t = setTimeout('ImageRotation.setSecondPic()', speed);
		}
	},
	fadeToSecond: function() {
		firstOpacity -= 0.1;
		secondOpacity += 0.1;
		
		if (secondOpacity > 0.98) {
			secondOpacity = 1.0;
			firstOpacity = 0.0;
		}
		var el = document.getElementById('hdimg');
		el.style.mozOpacity = firstOpacity;
		el.style.opacity = firstOpacity;
		el.style.filter = "alpha(opacity=" + Math.floor(firstOpacity*10) + ")";
		
		el = document.getElementById('hdimg2');
		el.style.mozOpacity = secondOpacity;
		el.style.opacity = secondOpacity;
		el.style.filter = "alpha(opacity=" + Math.floor(secondOpacity*10) + ")";
		
		if (secondOpacity < 0.98) {
   			t = setTimeout('ImageRotation.fadeToSecond()', 100);
		} else {
			//alert("first op: " + firstOpacity + ", second op: " + secondOpacity);
   			t = setTimeout('ImageRotation.setFirstPic()', speed);
		}
	},
	setFirstPic: function() {
		var el = document.getElementById('hdimg');
		el.style.backgroundImage = "url(" + images[nextId++] + ")";
   			if (nextId > (images.length-1)) {
				nextId=0;
			}
   		t = setTimeout('ImageRotation.fadeToFirst()', 100);
	},
	setSecondPic: function() {
		var el = document.getElementById('hdimg2');
		el.style.backgroundImage = "url(" + images[nextId++] + ")";
   			if (nextId > (images.length-1)) {
				nextId=0;
			}
   		t = setTimeout('ImageRotation.fadeToSecond()', 100);
	}
};

//var prevLoadVH = window.onload;
//window.onload = function() {
//	if (null != prevLoadVH) {
//		prevLoadVH();
//	}
//	ImageRotation.init();
//};
RegInit.regInit(function() {ImageRotation.init();});
