/* ----------------------------------------------------------------------
	STANDARD VRB FUNCTIONS
---------------------------------------------------------------------- */
function addEvent(obj, type, fn){
  if (obj.addEventListener)
    obj.addEventListener(type, fn, false);
  else if (obj.attachEvent){
    obj["e"+type+fn] = fn;
    obj[type+fn] = function(){obj["e"+type+fn](window.event);}
    obj.attachEvent("on"+type, obj[type+fn]);
  }
}

function addClass(el,c) { if(!containsClass(el, c)) el.className += " " + c; }
function removeClass(el, c) { el.className = el.className.replace(new RegExp(c,"g"),""); }
function containsClass(el, c) { return el.className.indexOf(c) != -1 }


/* ----------------------------------------------------------------------
	TOP MENU
---------------------------------------------------------------------- */
var flagsStatus = [];
var subMenuQuantity = 7;


function init_top_menu(){
	objSubMenus = document.getElementById("sub_menus");
	if (!objSubMenus)
		return false;
	
	objSubMenus.style.visibility = "visible";
	
	//Add event to all items in top menu
	for (q=1; q<=subMenuQuantity; q++){
		objTemp = document.getElementById("tma" + q);
		if (!objTemp)
			continue;
		
		addEvent(objTemp, "mouseover", topMenuMouseOver);
		addEvent(objTemp, "mouseout", topMenuMouseOut);
		flagsStatus[q] = null;
	}
	
	//Add event to all items in sub menu
	allATag = document.getElementsByTagName("A");
	//alert(allATag.length);
	for (q=0; q < allATag.length; q++){
		if (allATag[q].className == "sub_menu_item"){
			objA = allATag[q];
			
			addEvent(objA, "mouseover", stopFlag);
			addEvent(objA, "mouseout", startFlag);
		}
	}
	
}


function topMenuMouseOver(){
	hiddenAllSubMenu();
	
	no = giveMeIdSubMenu(this.id);
	obj = document.getElementById("sub_menu" + no);
	if (!obj)
		return false;
	
	clearInterval(flagsStatus[no]);
	obj.style.visibility = "visible";
	
	//Set activ to A
	addClass(this, "active_sub");
}

function topMenuMouseOut(){
	no = giveMeIdSubMenu(this.id);
	obj = document.getElementById("sub_menu" + no);
	if (!obj)
		return false;
	
	flagsStatus[no] = setInterval("topMenuMouseOutFlag(obj, no);",1000);
}

function startFlag(){
	objSubMenu = this.parentNode.parentNode.parentNode;
	subMenuNo = objSubMenu.id.substr(8,1);
	flagsStatus[subMenuNo] = setInterval("topMenuMouseOutFlag(objSubMenu, subMenuNo);",1000);
}

function stopFlag(){
	objSubMenu = this.parentNode.parentNode.parentNode;
	subMenuNo = objSubMenu.id.substr(8,1);
	clearInterval(flagsStatus[subMenuNo]);
}

function topMenuMouseOutFlag(obj, no){
	obj.style.visibility = "hidden";
	clearInterval(flagsStatus[no]);
	
	removeActivClas(no);
}

function hiddenAllSubMenu(){
	for (q=1; q<=subMenuQuantity; q++){
		objTemp = document.getElementById("sub_menu" + q);
		if (!objTemp)
			continue;
		
		clearInterval(flagsStatus[q]);
		objTemp.style.visibility = "hidden";
		removeActivClas(q);
	}
}

function giveMeIdSubMenu(idATag){
	return idATag.substr(3,1);
}

function removeActivClas(no){
	objA = document.getElementById("tma" + no);
	if (objA)
		removeClass(objA, "active_sub");
}

addEvent(window, "load", init_top_menu);
