
var MENU_CLOSE_DALAY = 1000;
var MENU_LIST_DELAY = 20;
var menus = new Array();

function openMenu(obj){
  if(obj==null) return;
  var menu = obj.getElementsByTagName('ul');
  if(menu!=null){
    var menu = menu[0];
    //Close all
    for(var i=0;i<menus.length;i++)
      closeMenu(menus[i]);
    if(menu.initalized==null){
      menu.initalized = true;
      menus.push(obj);
      var lis = menu.getElementsByTagName('li');
      for(var i=0;i<lis.length;i++){
        lis[i].style.display = 'none';
        lis[i].menucontainer = obj;
        lis[i].onmouseover = function(){openMenu(this.menucontainer);};
      }
    }
    menu.style.display = 'block';
    showMenuList(menu,true);
    clearTimeout(obj.toid);
    obj.toid = setTimeout(AJS.bind(closeMenu,window,obj,true),MENU_CLOSE_DALAY);
  }
}

function closeMenu(obj){
  if(obj==null)return;
  clearTimeout(obj.toid);
  var menu = obj.getElementsByTagName('ul');
  if(menu!=null){
    var menu = menu[0];
    showMenuList(menu,false);
  }
}

function showMenuList(menu,show){
  if(menu==null) return;
  var lis = menu.getElementsByTagName('li');
  if(lis.length>0){
    for(var i=0;i<lis.length;i++)
      clearTimeout(lis[i].toid);
    var li = lis[0];
    if(!show)
      li = lis[lis.length-1];
    li.toid = setTimeout(AJS.bind(showMenuListItem,window,new Array(menu,li,show),true),MENU_LIST_DELAY);
  }
}
function showMenuListItem(menu,item,show){
  if(menu==null) return;
  if(item==null) return; 
  item.style.display = show?'block':'none';
  var nextItem = AJS.getNextSiblingBytc(item,'li');
  if(!show)
    var nextItem = AJS.getPreviousSiblingBytc(item,'li');
  if(nextItem!=null)
    nextItem.toid = setTimeout(AJS.bind(showMenuListItem,window,new Array(menu,nextItem,show),true),MENU_LIST_DELAY);
  else if(!show)
    menu.style.display = 'none';
}

function fixScrollTo(){
	var agent = navigator.userAgent.toLowerCase ();
	//Mozilla fix
	if(agent.indexOf('mozilla')!=-1 && agent.indexOf('msie')==-1){
		var i = location.href.indexOf('#');
		if(i!=-1){
			var a = location.href.substring(i+1,location.href.length);
			if(a!=null && a.length>0){
				var top = 0;
				var element = document.getElementById(a);
				while (element!=null){
					top += element.offsetTop || 0;
					element = element.offsetParent;
				} 
				if(top>0)
					window.scrollTo(0,top);
			}
		}
	}
}
