// JavaScript Document
function addEvent(obj, evType, fn){ 
	if (obj.addEventListener){ 
		obj.addEventListener(evType, function(){fn(obj)}, true); 
   		return true; 
 	}else if(obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, function(){fn(obj)}); 
		return r; 
 	}else{ 
		return false; 
 	} 
}

if (window.addEventListener){ //NO IE
	document.addEventListener( "DOMContentLoaded", initTopGMenu, false );
	// A fallback to window.onload, that will always work
	window.addEventListener('load', initTopGMenu, false); 
}else{ //IE
	window.attachEvent("onload", initTopGMenu);
}

var TOP_GMENU_INIT = false;
var TOP_GMENU_ACTIVES = [];
function initTopGMenu(src)
{
	if( !TOP_GMENU_INIT ){
		TOP_GMENU_INIT = true;
		
		var menu = document.getElementById("topgmenu");
		if( menu!=null ){
			var lis = menu.getElementsByTagName("UL")[0].getElementsByTagName("LI");
			for(var i=0,l=lis.length;i<l;i++){
				addEvent(lis[i],'click',bindClick);
			}
		}
		
		addEvent(document.body,'click',clearGMenusActives);
	}
}

function bindClick(obj)
{
	var e = null;
	if (!e) e = window.event;
	var uls = obj.getElementsByTagName("UL");
	if( uls.length > 0 ){
		clearGMenusActives();
		obj.className += obj.className.indexOf('active')<0 ? ' active' : '';
		TOP_GMENU_ACTIVES.push(obj);
		if(e) e.cancelBubble = true;
		if (e && e.stopPropagation) e.stopPropagation();
		
		var ulis = uls[0].getElementsByTagName("LI");
		for(var i=0,l=ulis.length;i<l;i++){
			addEvent(ulis[i],'click',function(){
											  
				if (!e) e = window.event;
				if (e) e.cancelBubble = true;
				if (e && e.stopPropagation) e.stopPropagation();
				
			});
		}
		
		return false;		
	}
}

function clearGMenusActives(src)
{
	for(var i=0,l=TOP_GMENU_ACTIVES.length;i<l;i++){
		TOP_GMENU_ACTIVES[i].className=TOP_GMENU_ACTIVES[i].className.replace(/\s+active/,'');
	}
	
	TOP_GMENU_ACTIVES = [];
}
