/*
 * Select active tab
 */
var selectedTab = -1;
var timer;
//the speed in seconds when the tabs should change
var changespeed = 8;
var totaltabs = 4;
var currenttab = 0;
// time counter
var counter = 0;
function sel(n, automaticChange){
	if (!automaticChange) {
		selectedTab = n;
	}
	var count=5;
	for(var i=0; i<count; ++i) {
		var tab=document.getElementById("tab"+i);
		tab.className="";
	}
	var it=document.getElementById("tab" + n);
	if(it != null){
		it.className = "current";
	}
	// Hide unnecessary pages
	for(var i=0; i<count; ++i){
		it=document.getElementById("page" + i);
		if(it != null){
			it.className = "hidden";
		}
	}
	it=document.getElementById("page" + n);
	if(it != null){
		it.className = "visible";
	}
}

function start_autochange() {
	if (selectedTab != -1) {
		stop_autochange();
	}
	counter = counter + 1;
	timer = setTimeout("start_autochange()", 1000);
	if (counter == changespeed + 1) {
		currenttab++;
		if (currenttab > totaltabs) {
			currenttab = 0;
		}				
		sel(currenttab, true);
		restart_autochange();
	}
}

function restart_autochange() {
	clearTimeout(timer);
	counter = 0;
	start_autochange();
}

function stop_autochange() {
	clearTimeout(timer);
	counter = 0;
}

/*
 * Enumerate elements of known type
 * which are children for element with known id
 */
function getChildrenElements(id, type) {
	var nodeArr = new Array();
	var mn=document.getElementById(id);
	if(mn != null) {
		var tmp = mn.childNodes;
		for(var i=0, k=0; i<tmp.length; ++i) {
			if(tmp[i].nodeType == 1 && tmp[i].nodeName==type.toUpperCase()) {
				nodeArr[k] = tmp[i];
				k++;
			}
		}
	}
	return nodeArr;
}
/*
 * Get array of menu items
 * by UL's id
 */
function getMenuItems() {
	return getChildrenElements("nav", "li");
}
/*
 * Activate Main menu item
 */
function active(n) {
	var nodeArr=getMenuItems();
	for(var i=0; i<nodeArr.length; ++i) {
		if(nodeArr[i] != null) {
			nodeArr[i].className=null;
		}
	}
	if(nodeArr[n] != null) {
		nodeArr[n].className="current";
	}
}
/*
 * Clear menu items
 */
function clearCurrent() {
	var nodeArr=getMenuItems();
	for(var i=0; i<nodeArr.length; ++i) {
		if(nodeArr[i] != null) {
			nodeArr[i].className=null;
		}
	}
}
/*
 * Show sub-items of known element
 */
function showSub(el) {
	clearCurrent();	
	el.parentNode.className="current highlight";
}

