var intID;
var hideinterval=1000;
var atmenu=0;
var shiftY=5;
var shiftX=-12;
lastDropped="";
var bisOpera= ((navigator.userAgent.indexOf("Opera")>=0));
var bisIE = ((navigator.userAgent.indexOf("MSIE")>=0)&&(navigator.appVersion.substring(0,1)>=4) && (!bisOpera));

//void function
function avoid () {
    
    //do nothing
}

//level is the submenu level of the DROPPER
function drop(dropper, dropId) {
	var dX=0, dY=0;
	var menuContainer;
	//get parent position
	var pos=findPos(dropper);
	var dX= pos[0];
	var dY= pos[1];
	var dW= dropper.offsetWidth;
	dropper.className="dropped";
	var dropped = document.getElementById(dropId);
	//if exists, set its position relative to the parent
	if(dropped) {
		dropped.style.left = (dX+dW+shiftX)+"px";
		dropped.style.top = (dY+shiftY)+"px";
		dropped.style.display = "block";
	} 
	lastDropped = dropId;
}

function show(newId) {
	hidelevel(newId);
	var id=newId.split("_");
	var hid=id[0];
	var subid="sub";
	for (i=1;i<=id.length ;i++) 
	{
			lastmenu = document.getElementById(hid);
			hid+="_"+id[i];
			subid+="_"+id[i];
			sub = document.getElementById(subid);
			//reset class name on child objects
			subitems=lastmenu.getElementsByTagName("div"); //get all menu items
			for(j=0;j<subitems.length;j++) {
				subitems[j].className="submenu";
			}
			if (lastmenu && sub) {
				drop(sub,hid); 
			}
	}
	on();
	//set timer to hide the menu if it's lost its focus
	clearInterval(intID);
	intID = window.setInterval("hidemenu()", hideinterval);
}

//cascading hides all menus from the specified level on
function hidelevel(newId) {
	var menu,i,hid,id;
    if(lastDropped=="") return;
    var id = lastDropped.split("_");
	var hid = id[0];
	for(i=1;i<=id.length;i++) {
		hid+="_"+id[i];
		if(newId.indexOf(hid)<0) {
			menu = document.getElementById(hid);
			if(menu) {
				menu.style.display="none";
				//reset class name on child objects
				subitems=menu.getElementsByTagName("div"); //get all menu items
				for(j=0;j<subitems.length;j++) {
					subitems[j].className="submenu";
				}
			}
		}
	}
    //
    menu=document.getElementById("menu");
    subitems=menu.getElementsByTagName("div");
    for(j=0;j<subitems.length;j++) {
    	subitems[j].className="submenu";
    }
}


//for debugging purposes only!!
function trace(s) {
	document.getElementById("out").innerHTML=s;
}


//cascading hides the displayed menus
function hidemenu() {
	if(!atmenu) {
		clearInterval(intID);
		hidelevel("all");
	}
}

//raises the flag that cursor is over a menu
function on() {
	atmenu=true;
}

//lowers the flag that cursor is over a menu
function off() {
	atmenu=false;
	clearInterval(intID);
	intID = window.setInterval("hidemenu()", hideinterval);
}


//returns the absolute position of an element
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

function popPic(url) {
    divpic= document.getElementById('pic');
    divpic.innerHTML = "<img src='images/btclose.gif' id='btclose' align='right' onclick='hidePic()' alt='Close' /><img src='"+url+"' alt='' />";
    pos=findPos(document.getElementById("phototd"));
    divpic.style.left = (pos[0]-532)+"px";
    divpic.style.top = pos[1]+"px";
    divpic.style.display="block";
}

function hidePic() {
    document.getElementById('pic').style.display="none";
}
