var menu_links= new Array()
var menu_link_ids= new Array()
var root_menu_link_ids = new Array()
var current_id=0
var mouse_on_menu=0
var hided_selects = new Array()

function MenuLink(str_id, str_link, str_label, int_level, str_target, str_css_class, b_html_link) {
	this.level=int_level
	this.id=str_id
	this.link=str_link
	this.label=str_label
	this.target=str_target
	this.css_class=str_css_class
	this.active=0
	this.children = new Array()
  this.html_link = (b_html_link!=null) ? b_html_link : false;   
} 

function addMenuLink(str_parent_id, str_id, str_link, str_label, str_target, str_css_class, b_html_link) {
	var menu_link
	if(str_parent_id=="") {
		menu_link = new MenuLink(str_id, str_link, str_label,1, str_target, str_css_class, b_html_link)
		root_menu_link_ids.push(str_id)
	} else {
		var _parent_id = getMenuLinkOffset(str_parent_id)
    if (b_html_link == null) {
      b_html_link = menu_links[_parent_id].html_link
    }
		menu_link = new MenuLink(str_id, str_link, str_label,menu_links[_parent_id].level+1, str_target, str_css_class, b_html_link)
		menu_links[_parent_id].children.push(str_id)
	}
	menu_links.push(menu_link)
	menu_link_ids.push(str_id)
}

function menu1MouseOver(e, str_prepend) {
	mouse_on_menu=1

	e = e ? e : window.event;
	tg = (window.event) ? e.srcElement : e.target;
	str_prepend_item = str_prepend + "_item_"
  while (tg.nodeName.toLowerCase() != "body") {
    if (tg.id) {
	    it_prep = tg.id.slice(0,str_prepend_item.length)
	    if(it_prep == str_prepend_item) {
	      var tid=tg.id.slice(str_prepend_item.length)
	      if(tid.slice(tid.length-5)=="_link") {
	        tid = tid.slice(0,tid.length-5)
	      }
	      setSubMenus(tid, str_prepend)
	      break
	    } else {
	      tg = tg.parentNode
	    }
    } else {
      tg = tg.parentNode
    }
  }
}
function menu2MouseOver(e, str_prepend) {
	mouse_on_menu=1
}
function menu1MouseOut(e, str_prepend) {
	e = e ? e : window.event;
	tg = (window.event) ? e.srcElement : e.target;
	str_prepend_item = str_prepend + "_item_"
	if(tg.id==str_prepend +"_level_1") {
		mouse_on_menu=0
		current_id=""
		window.setTimeout("hideSubMenus('" + str_prepend + "');",1000);
	} 
}
function menu2MouseOut(e,str_prepend) {
	e = e ? e : window.event;
	tg = (window.event) ? e.srcElement : e.target;
	str_prepend_item = str_prepend + "_item_"
	if(tg.id==str_prepend +"_level_2") {
		mouse_on_menu=0
		current_id=""
		window.setTimeout("hideSubMenus('" + str_prepend + "');",1000);
	} 
}

function setSubMenus(str_id, str_prepend) {
	var rootlink=menu_links[getMenuLinkOffset(str_id)]
	var lev2_div=document.getElementById(str_prepend + '_level_2')
	if(rootlink.children.length < 1) {
		current_id = str_id
		lev2_div.style.visibility='hidden'
	} else if (str_id != current_id) {
		var i
		var idiv
		var ilink
		var itxt
		var lev1_span=document.getElementById(str_prepend + '_item_' + str_id)
		lev2_div.style.visibility='visible'
		lev2_div.style.left=getLeftOffset(lev1_span) + "px"
		while(lev2_div.childNodes.length >0){
			lev2_div.removeChild(lev2_div.firstChild)
		}
		current_id = str_id
		for (i=0; i<rootlink.children.length; i++) {		
			lnk = menu_links[getMenuLinkOffset(rootlink.children[i])]
			idiv = document.createElement("div")
			idiv.id = str_prepend + "_item_" + str_id
			ilink = document.createElement("a")
			ilink.href = lnk.link
			ilink.target = lnk.target
			idiv.className = lnk.css_class
			ilink.id = str_prepend + "_lnk_" + str_id
      if(lnk.html_link) {
        ilink.innerHTML = lnk.label
      } else {
  			itxt = document.createTextNode(lnk.label)
  			ilink.appendChild(itxt)
      }
			idiv.appendChild(ilink)
			lev2_div.appendChild(idiv)
		}
		if(document.all && (hided_selects.length==0)) {
			flds = document.all.tags('select');
			for (i=0;i<flds.length;i++) {
				fld = flds[i]
				if(fld.style.visibility!='hidden'){
					fld.style.visibility='hidden'
					hided_selects.push(fld)
				}
			}
		}
	}
}
function hideSubMenus(str_prepend) {
	if(mouse_on_menu==0) {
		document.getElementById(str_prepend + '_level_2').style.visibility='hidden'
		while(hided_selects.length>0) {
			fld=hided_selects.pop()
			fld.style.visibility='visible'
		}
	}
}

function setSubMenuSelect(str_id, b_sort) {
  var mParent = menu_links[getMenuLinkOffset(str_id)];
  var mChild;
  var sMainCat = document.getElementById("maincat_sel");
  var sSubCat = document.getElementById("subcat_sel");
  if (!sMainCat.options[0].value) {
    sMainCat.options[0] = null;
  }
  while (sSubCat.options.length > 0) {
    sSubCat.options[0] = null;
  }
  sSubCat.options[0] = new Option("","",true,true);
  for (var i=0, j=mParent.children.length; i< j ; i++) {
    mChild = menu_links[getMenuLinkOffset(mParent.children[i])];
    sSubCat.options[sSubCat.options.length] = new Option(mChild.label,mChild.id,false,false);
  }
}
function gotoSubMenuSelectCategory(str_id) {
  document.location = menu_links[getMenuLinkOffset(str_id)].link
}
function getMenuLinkOffset(str_id) {
	var i
	ret = 0
	for(i=0; i<menu_links.length; i++) {
		if (menu_link_ids[i]==str_id) {
			ret=i
			break
		}
	}
	return ret
}
function getLeftOffset(sObject){
	var offx = 0;
	var ndObject = sObject;
	while (ndObject.nodeName != "BODY")	{
		offx += ndObject.offsetLeft;
		ndObject = ndObject.offsetParent;
	}
	return offx
}
function getBottomOffset(sObject){
	var offy = sObject.offsetHeight;
	var ndObject = sObject;
	while (ndObject.nodeName != "BODY")	{
		offy += ndObject.offsetTop;
		ndObject = ndObject.offsetParent;
	}
	return offy
}