var bw = new cm_bwcheck();//external code

var isover = new Array();//array used to check when mouse is over element
var submenus = new Array();

var depth = 0;

var positions = new Array();//positions of menus
var sources = new Array();

var lastShown = '';

/**
* Class used to store menu (tree) structure.
* @author Pawel Martenka 
*/
function Menu(parent, key) {
	this.parentID = parent;//reference to parent
	this.id = key;//id of this menu
	
	/**
	* Recursive method to hide menu from leaf to parent.
	*/
	this.hide = function hide() {
		if(this.parentID == null) return;
		hideElement(this.id);
		this.parentID.hide();
	}
}

/*Browsercheck object*/
function cm_bwcheck(){
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent.toLowerCase()
	this.dom=document.getElementById?1:0
	
  //this.op5=(this.agent.indexOf("opera 5")>-1 || this.agent.indexOf("opera/5")>-1) && window.opera 
  //this.op6=(this.agent.indexOf("opera 6")>-1 || this.agent.indexOf("opera/6")>-1) && window.opera   
  this.ie5 = (this.agent.indexOf("msie 5")>-1 && !this.op5 && !this.op6)
  this.ie55 = (this.ie5 && this.agent.indexOf("msie 5.5")>-1)
  this.ie6 = (this.agent.indexOf("msie 6")>-1 && !this.op5 && !this.op6)
	this.ie4=(this.agent.indexOf("msie")>-1 && document.all &&!this.op5 &&!this.op6 &&!this.ie5&&!this.ie6)
  this.ie = (this.ie4 || this.ie5 || this.ie6)
	this.mac=(this.agent.indexOf("mac")>-1)
	//this.ns6=(this.agent.indexOf("gecko")>-1 || window.sidebar)
	this.ns6=(this.agent.indexOf("gecko")>-1 || window.sidebar || window.opera)
	
	this.ns4=(!this.dom && document.layers)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.op5 || this.op6)
  this.usedom= this.ns6//Use dom creation
  this.reuse = this.ie||this.usedom //Reuse layers
  this.px=this.dom&&!this.op5?"px":""
	return this
}

//Extra code to find position:
function findPos(bw, sourceID){
  if(bw.ns4){   //Netscape 4
    x = document.layers.sourceID.pageX
    y = document.layers.sourceID.pageY
  }else{ //other browsers
    x=0; y=0; var el,temp
    el = bw.ie4?document.all[sourceID]:document.getElementById(sourceID);
    if(el.offsetParent){
      temp = el
      while(temp.offsetParent){ //Looping parent elements to get the offset of them as well
        temp=temp.offsetParent; 
        x+=temp.offsetLeft
        y+=temp.offsetTop;
            //wnd.document.write("pageXY: " + temp.offsetLeft + ":" + temp.offsetTop + ":" + temp.tagName + "<br>");
      }
    }
    x+=el.offsetLeft
    y+=el.offsetTop
  }
  //Returning the x and y as an array
  return [x,y]
}

/**
* Sets actual depth in menu.
* @author Pawel Martenka 
*/
function setDepth(d) {
	depth = d;
}

/**
* Shows element with id.
* @author Pawel Martenka
*/
function showElement(id) {
	element = document.getElementById(id);
	if(element != null) element.style.display = "block";
}

/**
* Hides element with id. Element can't be null, mouse can't be over
* source TD, and target DIV (id).
* @author Pawel Martenka
*/
function hideElement(id) {
	element = document.getElementById(id);
	if(element != null && !isover[sources[id]] && !isover[id]) element.style.display = "none";
}

/**
* Hide other submenus - don't hide targetID.
* @author Pawel Martenka
*/
function hideOtherSubmenus(sourceID, targetID) {
	for(x in submenus[depth]) {
		if(x != targetID && x != sourceID) {
			hideElement(x);
		}
	}
}

/**
* Shows menu with id: targetID.
* @author Pawel Martenka
*/
function showMenu(sourceID, targetID) {
	lastShown = targetID;//remember last shown. It also is a menu that will be shown in a moment.
	sources[targetID] = sourceID;//sourceID for targetID
	try {
		submenus[depth][targetID] = true;//remember all submenus
	} catch(e) {
		submenus[depth] = new Array();
		submenus[depth][targetID] = true;
	}
	isover[targetID] = false;//;-) whatever
	
	menu = document.getElementById(sourceID);//parent menu
	pos = findPos(bw, sourceID);//find absolute position of this menu
	
	hideOtherSubmenus(sourceID, targetID);//hide other menus - only this one can be shown
	
	submenu = document.getElementById(targetID);
	if(submenu == null) return;

	src = sourceID;
	res = src.replace(/_[0-9]+$/,"");//more shallow in menu

	positions[sourceID] = new Array();
	positions[sourceID][0] = pos[0];
	positions[sourceID][1] = pos[1];
	
	leftPos = pos[0] + menu.offsetWidth;//only for depth==0
	topPos = pos[1];//only for depth==0
	if(depth > 0) {
		leftPos = menu.offsetWidth;//it's relative to parent menu (which has absolute position)
		topPos = pos[1] - positions[res][1];//like above
	}
	
	submenu.style.left = leftPos + "px";
	submenu.style.top = topPos + "px";
	f = new Function('if(submenu != null)submenu.style.display = "block";');
	setTimeout(f, 400);//show after 400ms
}

/**
* Hide menu when mouse isn't above source (TD) or target (DIV).
* @author Pawel Martenka
*/
function hideSub(sourceID, targetID) {
	if(!isover[targetID] && !isover[sourceID]) {
		sub = allMenus[lastShown];
		if(sub != null) sub.hide();
		hideElement(targetID);//IE need it to look correct
	}
}

/**
* Try to hide menu.
* @author Pawel Martenka
*/
function hideMenu(sourceID, targetID) {
	x = setTimeout("hideSub('" + sourceID + "', '" + targetID + "')",700);
}

/**
* All onmouseover events are handled here - it's improtant
* to know, when mouse is over the div.
* @author Pawel Martenka
*/
function over(id) {
	isover[id] = true;
}

/**
* Like above, but for onmouseout.
* @author Pawel Martenka
*/
function out(id) {
	isover[id] = false;
}