// JavaScript Document

var $ = function(html_object_id) {
	if(document.layers)					return document.layers[html_object_id];
	else if(document.all)				return document.all[html_object_id];
	else if(document.getElementById)	return document.getElementById(html_object_id);
	else 								return null;
};

var header_rotation = function() {
	
	this.cur_image = 1;
	this.nex_image = 2;
	
	this.img_Bg = null;
	this.img_Sc = null;
	
	this.delay = 5; //in seconds
	this.opac = null;
	
	this.images = Array();
	this.images[1] = "header-1.jpg";
	this.images[2] = "header-2.jpg";
	this.images[3] = "header-3.jpg";
	
	this.init = function(cur_image, nex_image) {
		cur_image = parseInt(cur_image);
		nex_image = parseInt(nex_image);
		
		if(cur_image && cur_image > 0 && cur_image < this.images.length) {
			this.cur_image = cur_image;
			
			if(!isNaN(nex_image) && nex_image > 0 && nex_image < (this.images.length+1)) this.nex_image = nex_image; 
			else {
				if( (cur_image+1) >= this.images.length)	this.nex_image = 1;
				else										this.nex_image = cur_image+1;
			}
			
		}
		
		this.img_Bg = $("imageRotation");
		this.img_Sc = $("imageRotationImage");
		
		//preload images
		var div = document.createElement("div");
		div.setAttribute("id", "preload_header_rotation");
		div.setAttribute("style", "visibility:hidden;height:1px;width:1px;overflow:hidden;");
		for(var i=1;i<this.images.length;++i) div.innerHTML += "<img src=\"/wp-content/themes/tsv-03/images/"+this.images[i]+"\" alt=\"\" />";
		document.body.appendChild(div);
		
		window.setTimeout("header_rotation.rotation()", 1000*this.delay);
	};
	
	this.rotation = function() {
		this.opac = 0;
		this.img_Bg.style.background = "url(/wp-content/themes/tsv-03/images/"+this.images[this.cur_image]+")";
	
		setOpacity(this.img_Sc, this.opac);
		this.img_Sc.src = "/wp-content/themes/tsv-03/images/"+this.images[this.nex_image];
		
		this.fadeOut();
	};
	
	this.setNextImage = function() {
		this.cur_image = this.nex_image;
		
		if( (this.nex_image+1) < this.images.length) this.nex_image += 1;
		else this.nex_image = 1;
		
		window.setTimeout("header_rotation.rotation()", 1000*this.delay);
	};
	
	this.fadeOut = function() {
		if(this.opac < 100) {
			this.opac += 5;
			setOpacity(this.img_Sc, this.opac);
			window.setTimeout("header_rotation.fadeOut()", 75);
		}
		else this.setNextImage();
	};
	
	var setOpacity = function(obj, opacity) {
		obj.style.opacity = (opacity / 100);
		obj.style.MozOpacity = (opacity / 100);
		obj.style.KhtmlOpacity = (opacity / 100);
		obj.style.filter = "alpha(opacity=" + opacity + ")";
	};
	
};
