function toggle(id){
	var doc = document.getElementById(id);
	if (doc != null){
		if (doc.style.display == "block"){
			doc.style.display = "none";
		} else {
			doc.style.display = "block";
		}
	}
}

var feature_array = [];
var button_array = [];

var current_index;
var current_feature;

var timeout;
var interval;
var trans_interval;

function feature_init(){
	if (feature_count == null) return;
	for (var i = 0; i < feature_count; i++){
		var box = document.getElementById("featurebox" + i);
		feature_array.push(box);
		box.opacity = 1;
		
		var button = document.getElementById("select" + i);
		button_array.push(button);
		button.onclick = function(){
			var temp = i; 
			return function(){
				if (interval != null){
					window.clearInterval(interval);
					interval = null;
				}
				if (timeout != null){
					window.clearTimeout(timeout);
					timeout = null;
				}
				feature_goto(temp);
				timeout = window.setTimeout(feature_play, 2000);
			};
		}();
	}
	
	if (timeout != null){
		window.clearTimeout(timeout);
		timeout = null;
	}
	current_index = -1;
	timeout = window.setTimeout(feature_play, 2000);
}

function feature_play(){
	if (timeout != null){
		window.clearTimeout(timeout);
		timeout = null;
	}
	if (feature_count == null) return;
	if (interval != null){
		window.clearInterval(interval);
		interval = null;
	}
	interval = window.setInterval(function(){feature_goto((current_index+1)%feature_count);}, 5000);
}

function feature_goto(index){
	if (index == current_index) return;
	set_opacity(feature_array[index], 1);
	feature_array[index].style.zIndex = 1;
	if (current_feature != null) {
		current_feature.style.zIndex = 2;
		button_array[current_index].style.borderTop = "none";
		button_array[current_index].style.background = "transparent";
	}
	button_array[index].style.borderTop = "1px solid #FFFFFF";
	button_array[index].style.background = "#FFFFFF";
	current_index = index;
	fade_out(current_feature, [function(){
		if (current_feature != null) {
			current_feature.style.zIndex = 0;
			set_opacity(current_feature, 0);
		}
		current_feature = feature_array[index];
		current_feature.style.zIndex = 2;
		set_opacity(current_feature, 1);
		if (trans_interval != null){
			window.clearInterval(trans_interval);
			trans_interval = null;
		}
	}]);
}

function set_opacity(element, opacity){
	element.style.MozOpacity = opacity;
	element.style.filter = "alpha(opacity="+(opacity * 100)+")";
	element.opacity = opacity;
}

function reduce_opacity(element, delegate){
	if (element.opacity <= 0) {
		set_opacity(element, 0);
		delegate[0]();
		if (trans_interval != null){
			window.clearInterval(trans_interval);
			trans_interval = null;
		}		
		return;
	}
	else set_opacity(element, element.opacity - 0.1);
}

function fade_out(element, delegate){
	if (element != null) {
		if (trans_interval != null){
			window.clearInterval(trans_interval);
			trans_interval = null;
			delegate[0]();
		}
		trans_interval = window.setInterval(function(){reduce_opacity(element, delegate);}, 50);
	} else {
		delegate[0]();
	}
}
