var bisOpera= ((navigator.userAgent.indexOf("Opera")>=0));
var bisIE = ((navigator.userAgent.indexOf("MSIE")>=0)&&(navigator.appVersion.substring(0,1)>=4) && (!bisOpera));
var intID;
var hideinterval = 1000; //след колко време да се скриват менютата
var atmenu=false;
var shiftY=2; //колко надолу да слезе подменюто
var shiftX; //колко да се припокриват менютата
var lastDropped;
lastDropped="";
shiftX=0;
var menucontainer ;
var oldclass="";
var cpos;

//void function
function avoid () {}

//level is the submenu level of the DROPPER
function drop(dropper, dropId) {
	var dX=0, dY=0;
	var menuContainer;
    if(!cpos) {
         var nav = document.getElementById("menucontainer");
         cpos = findPos(nav);
    }
	//get parent position
	var pos=findPos(dropper);
	var dX= pos[0]-cpos[0];
	var dY= pos[1]-cpos[1];
	var dW= dropper.offsetWidth;
	oldclass=dropper.className;
	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";
        // init Iframe to match the position and size of the dropped menu
        /*if(bisIE) {
 			var ifr = document.getElementById('ifr');
 			ifr.style.width = dropped.offsetWidth;
 			ifr.style.height = dropped.offsetHeight;
 			ifr.style.top = dropped.style.top;
 			ifr.style.left = dropped.style.left;
 			ifr.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";
				//}
			}
		}
	}
	var mainmenu = document.getElementById("menu");
	subitems=mainmenu.getElementsByTagName("div"); //get all menu items
	for(j=0;j<subitems.length;j++) {
		if(subitems[j].className!="current") subitems[j].className="submenu";
	}
    //document.getElementById("ifr").style.display="none";
}


function initmenu(scriptname) {
	var nav = document.getElementById("menucontainer");
	cpos = findPos(nav);
	var alist = nav.getElementsByTagName("a");
    var s="";

    for(i=0;i<alist.length;i++) {
        s=alist[i].href+"";
        
        if(s.indexOf(scriptname)>0) {
            var adiv = alist[i].parentNode;
            var h=adiv.id.split("_");
            hidiv=document.getElementById("sub_"+h[1]);
            hidiv.className="current";
        }
    }
}


//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 ask() {
	document.getElementById("request").style.display = "block";
   //	document.getElementById("popdiv").style.display = "none";
}

function unask() {
	document.getElementById("request").style.display = "none";
	//document.getElementById("popdiv").style.display = "inline";

}

