/*
To use this script include this in your code between the <head> tags
<script src="layer_swopper.js" type="text/javascript"></script>
*/
// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
// This unit requires functions.js

var IE = document.all?true:false

function getDisplayLayerOnClassName(class_name) {
	return getElementsByClassName(class_name);
}

function getDisplayContentOnClassName(class_name) {
	return getElementsByClassName(class_name);
}

function startLayerSwopping() {
	var next_index = 0;
	for (i=0; i < this.layers.length; i++) {
		next_index = i+1;
		if (next_index < this.layers.length) {
			this.layers[i].innerHTML =  this.layers[next_index].innerHTML;
		} else {
			this.content_index++;
			if (this.content_index >= this.content.length) {
				this.content_index = 0;
			}
			this.layers[i].innerHTML = this.content[this.content_index].innerHTML;
		}
	}
	this.dispay_index = 0;
	this.fadeIn(this.layers[this.dispay_index]);
}


function layerFadeIn(mlayer) {
	var obj = this;
	obj.opacity = obj.opacity+obj.fade_increments;
	if (obj.opacity <= 100) {
		if (IE) {
			mlayer.filters["alpha"].opacity = obj.opacity;
		} else {
			mlayer.style.opacity = (obj.opacity/100);
		}
		obj.t = setTimeout(function() {obj.fadeIn(mlayer)}, obj.fade_in_speed);
	} else {
		clearTimeout(obj.t);
		obj.opacity = 100;
		obj.t = null;
		if (IE) { //Need to make sure that the layers are completely invisible
			mlayer.filters["alpha"].opacity = obj.opacity;
		} else {
			mlayer.style.opacity = (obj.opacity/100);
		}
		obj.display_index++;
		if (obj.display_index >= obj.layers.length) {
			obj.display_index = 0;
			obj.t_swopper = setTimeout(function() {obj.fadeOut(obj.layers[obj.display_index]);} , obj.display_time);
		} else {
			obj.opacity = 0;
			obj.fadeIn(obj.layers[obj.display_index]);
		}
	}
}

function layerFadeOut(mlayer) {
	var obj = this;
	if (obj.t_swopper != null) {
		clearTimeout(obj.t_swopper);
		obj.t_swopper = null;
	}
	obj.opacity = obj.opacity-obj.fade_increments;
	if (obj.opacity >= 0) {
		if (IE) {
			mlayer.filters["alpha"].opacity = obj.opacity;
		} else {
			mlayer.style.opacity = (obj.opacity/100);
		}
		obj.t = setTimeout(function() {obj.fadeOut(mlayer)} , obj.fade_out_speed);
	} else {
		clearTimeout(obj.t);
		obj.t = null;
		obj.opacity = 0;
		if (IE) { //Need to make sure that the layers are completely invisible
			mlayer.filters["alpha"].opacity = obj.opacity;
		} else {
			mlayer.style.opacity = (obj.opacity/100);
		}
		obj.display_index++;
		if (obj.display_index >= obj.layers.length) {
			obj.display_index = 0;
			obj.startLayerSwopping();
		} else {
			obj.opacity = 100;
			obj.fadeOut(obj.layers[obj.display_index]);
		}
	}
}

function init() {
	var diff = (this.layers.length - this.content.length);
	for (k=1; k <= diff; k++) {
		this.layers.pop();
	}
	for (i=0; i < this.layers.length; i++) {
		if (IE) { //Internet Explorer
			this.layers[i].style.filter = "alpha(opacity=100);";
		} else { //Safari & Mozilla
			this.layers[i].style.opacity = 1;
		}
		this.layers[i].innerHTML = this.content[this.content_index].innerHTML;
		this.content_index++;
	}
	this.t = setTimeout(function() {} , this.display_time);
	clearTimeout(this.t);
	this.display_index = 0;
	var obj = this;
	obj.t_swopper = setTimeout(function() {obj.fadeOut(obj.layers[obj.display_index]);} , obj.display_time);
}

LayerSwopper.prototype.init = init;
LayerSwopper.prototype.getDisplayLayers = getDisplayLayerOnClassName;
LayerSwopper.prototype.getDisplayContent = getDisplayContentOnClassName;
LayerSwopper.prototype.startLayerSwopping = startLayerSwopping;
LayerSwopper.prototype.fadeOut = layerFadeOut;
LayerSwopper.prototype.fadeIn = layerFadeIn;

function LayerSwopper(display_layers_class_name, display_content_class_name,
					  display_time, fade_out_speed, fade_in_speed, fade_increments) {
	this.opacity = 100;
	this.t = null;
	this.t_swopper = null;
	this.display_time = display_time;
	this.fade_out_speed = fade_out_speed;
	this.fade_in_speed = fade_in_speed;
	this.fade_increments = fade_increments;
	this.content_index = 0;
	this.display_index = 0;
	this.layers = this.getDisplayLayers(display_layers_class_name);
	this.content = this.getDisplayContent(display_content_class_name);
	if ((this.layers.length == 0) || (this.content.length == 0)) {
		//alert("You need to define content and layers");
	} else {
		this.init();
	}
}
