var tabs_selected = {"green": false, "yellow": false, "blue": false, "red": false};
var url;
var tabs = {
	"Home": "green",
	"Nieuws": "yellow",
	"Informatie": "blue",
	"Interactief": "red"
};

var tab = {

	init: function (){
		//get all tabs
		var children = $("menu").getChildren();
		for (i = 0; i < children.length ; i++)
		{
			//set the events
			children[i].addEvent("mouseover", function() { tab.onTabOver(this); });
			children[i].addEvent("mouseout", function() { tab.onTabOut(this); });
		}

		//Get the url for the images by getting the logo source
		var logo_url = $("logo").getAttribute("src");
		var first_sequence = logo_url.substr(0, logo_url.lastIndexOf("/"));
		url = logo_url.substr(0, first_sequence.lastIndexOf("/"))+"/";

		//Load the correct tab
		tab.loadTab();
	},

	onTabOver: function(el) {
		var id = el.getAttribute("id");
		tabs_selected[id] = true;

		//Preload the image, faster switching
		$("upperbar_hidden").setStyle("background", "url("+url+"images/upperbar_"+id+".gif) repeat-x");
		tab.changeTab.delay(200, '', id);
	},

	onTabOut: function(el) {
		tabs_selected[el.getAttribute("id")] = false;
	},

	changeTab: function(id) {
		if (tabs_selected[id] == true) {
			$("upperbar").setStyle("background", "url("+url+"images/upperbar_"+id+".gif) repeat-x");
			$("upperbar").getChildren()[0].setHTML($("upperbar_"+id).innerHTML);
			$("upperbar").className = id;
			$("logo").setAttribute("src", url+"images/logo_"+id+".gif");
		}
	},

	loadTab: function() {
		var current_url = new String(document.location);
		current_url = current_url.replace((url+"index.php?"), "");

		if (current_url == url) {
			tabs_selected["green"] = true;
			tab.changeTab("green");
			return;
		}

		if (current_url.indexOf("/") > 0) {
			current_url = current_url.substr(0, current_url.indexOf("/"));
		}
		if (current_url.length == 0) {
			return 0;
		}

		if (tabs[current_url] != null) {
			tabs_selected[tabs[current_url]] = true;
			tab.changeTab(tabs[current_url]);
		}
	},

	resetLogo: function() {
		$("logo").setAttribute("src", "images/logo.gif");
	}
};
window.addEvent("domready", tab.init); 
