var stage_rotation = new Array();
var stage_current_rotation = 0;
var stage_rotation_delay = 4000;
var stage_rotation_current_id = "";
var substage_rotation_current_id = "";
var stage_rotation_init = false;
var stage_rotation_timer;

function change_stage(stage, fade) {
	var stages = new Array();
	stages = document.getElementsByClassName('stage');
	
	for ( var i = 0; i < stages.length; i++ ) {
		stageid = stages[i].id;
		
		if(stageid == stage) {
			// Show stage
			if(stage_rotation_current_id != stageid) {
				if(stage_rotation_init && fade) {
					Effect.Appear(stageid);
				}else{
					$(stageid).show();
				}
				
			}
	
			stage_rotation_current_id = stageid;
			rid = stage_rotation.has(stageid);
			if(rid!==false)stage_current_rotation = rid;
		}else{
			// Hide stage
			if(stage_rotation_init && fade) {
				Effect.Fade(stageid);
			}else{
				$(stageid).hide();
			}
		}
	}
	
	change_substage(stage_rotation_current_id, fade);
	
	stage_rotation_init = true;
}

function pause_rotation() {
	clearTimeout(stage_rotation_timer);
}

function start_rotation() {
	if(stage_current_rotation == "") stage_current_rotation = stage_rotation.has(stage_rotation_current_id);
	change_stage(stage_rotation[stage_current_rotation]);
	stage_rotation_timer = setTimeout("next_in_rotation()", stage_rotation_delay);
}

function next_in_rotation() {
	stage_current_rotation++;
	
	if(stage_rotation.length <= stage_current_rotation) stage_current_rotation = 0;
	change_stage(stage_rotation[stage_current_rotation], true);
	stage_rotation_timer = setTimeout("next_in_rotation()", stage_rotation_delay);
}


// Sub-stages
function change_substage(stageid, fade) {
	substageid = "sub" + stageid;

	if(substage_rotation_current_id != substageid && $(substageid)) {
	
		substages = document.getElementsByClassName('substage');
		
		if(stage_rotation_init && fade) {
			Effect.Appear(substageid);
		}else{
			$(substageid).show();
		}

		substage_rotation_current_id = substageid;
		
		for ( var i = 0; i < substages.length; i++ ) {
			substageid = substages[i].id;
			if(substage_rotation_current_id != substageid && $(substageid)) {
				if(stage_rotation_init && fade) {
					Effect.Fade(substageid);
				}else{
					$(substageid).hide();
				}
			}
		}
	}
	
	
}

function initialise_rotation() {
	var stage_panels = new Array();
	stage_panels = document.getElementsByClassName('stage');
	
	for ( var i = 0; i < stage_panels.length; i++ ) {
		if(i==0) stage_rotation_current_id = stage_panels[i].id;
		stage_rotation[stage_rotation.length] = stage_panels[i].id;
	}
	start_rotation();
}

// JavaScript Document
function lock_n_load() {

	// Get product panels
	var product_panels = new Array();
	product_panels = document.getElementsByClassName('product');

	for ( var i = 0; i < product_panels.length; i++ ) {
		if(product_panels[i].id!="") {
			panelid = product_panels[i].id;
			
			if($("goto"+panelid)) {
				$(panelid).onclick = function (){ location.href = $("goto" + this.id).href; };
			}
			
		}
	}
	
	
	// Set up rotation
	initialise_rotation();

}

Array.prototype.has=function(v){
for (i=0; i<this.length; i++){
if (this[i]==v) return i;
}
return false;
}

window.onload = function () {lock_n_load()};
//$fh.onLoad(lock_n_load);