var timer;
var activeLink = null;
var baseRef = "/";
var lasttarget = null;
var lastid="";

var isMozilla = navigator.userAgent.indexOf('Mozilla') != -1 && parseInt(navigator.appVersion.substring(0,1)) >= 5;
var isIE = navigator.userAgent.indexOf('MSIE') != -1;

function MouseOutListener(mozEvent)
{
	if (lastid=="") Hide(); else DelayedHide();
};


function IpCheckMenu(lnkElement)
{
    var html ="";
    var ipc_id   = lnkElement.getAttribute("ipc_id"); 
    var ipc_type = lnkElement.getAttribute("ipc_type"); 
    var ipc_class = lnkElement.getAttribute("ipc_class");
    
    Hide();
    lasttarget=lnkElement;

    if (ipc_class != undefined) 
    {
	    var mytr=lnkElement.parentNode.parentNode;
	    if (mytr.tagName!="TR") mytr=lnkElement.parentNode.parentNode.parentNode;
	    var mytds=mytr.childNodes;
	
	    for (var i=0;i<mytds.length;i++)
	    {
		   if (mytds[i].tagName=="TD") 
		   {
		   	if (mytds[i].getAttribute("rowspan")!="2")
			   {
			   	if (ipc_type=="user") {mytds[i].className="container1_header_highlight"};
			   	if (ipc_type=="hosteduser") {mytds[i].className="container1_header_highlight"};
			   	if (ipc_type=="group") {mytds[i].className="container2_header_text_highlight"};
			   	if (ipc_type=="server") {mytds[i].className="container3_header_text_highlight"};
			   	if (ipc_type=="sensor") {mytds[i].className="content1_sensor_highlight"};
			   	if (ipc_type=="notification") {mytds[i].className="content1_sensor_highlight"};
			   	if (ipc_type=="schedule") {mytds[i].className="content1_sensor_highlight"};
			   	if (ipc_type=="userlist") {mytds[i].className="content1_sensor_highlight"};
			   };
		   };
	    };
    };

    if (ipc_id == undefined) 
    {
    	lastid="";
    	return;
    };
  	lastid=ipc_type;
    
    switch (ipc_type)
    {
  		case "group":
			    html += "<div class=menuhead>Group Menu</div>";
			    html += CreateLink(ipc_id,"editgroup","Edit",false);
			    html += CreateLink2(ipc_id,"editserver","Add Server",true);
					html += CreateLink(ipc_id,"graphview","View Network Map",true);
			    html += CreateLink(ipc_id,"maintgroup","Pause/Resume",false);
			    html += CreateLink(ipc_id,"minigroup","Fold/Unfold",false);
			    html += CreateLink(ipc_id,"sortserver","Sort Servers",false);
			    html += CreateLink(ipc_id,"export.ini","Export (*.ini)",true);
			    html += CreateLink(ipc_id,"deletegroup","Delete Group",false);
		  break;
		  case "server":
			    html += "<div class=menuhead>Server Menu</div>";
			    html += CreateLink(ipc_id,"editserver","Edit",false);
			    html += CreateLink2(ipc_id,"editsensor","Add Sensor",true);
			    html += CreateLinkNewWin(ipc_id,"dynamic","Traceroute",false);
			    html += CreateLink(ipc_id,"maintserver","Pause/Resume",false);
			    html += CreateLink(ipc_id,"miniserver","Fold/Unfold",false);
			    html += CreateLink(ipc_id,"sortsensor","Sort Sensors",true);
			    html += CreateLink(ipc_id,"dupserver","Duplicate Server",false);
			    html += CreateLink(ipc_id,"deleteserver","Delete Server",false);
		  break;
		  case "sensor":
			    html += "<div class=menuhead>Sensor Menu</div>";
			    html += CreateLink(ipc_id,"editsensor","Edit",true);
			    html += CreateLink(ipc_id,"scansensor","Scan Sensor Now",true);
			    html += CreateLinkRep(ipc_id, 2, 0, "report","Graph (Last 48 Hours)",false);
			    html += CreateLinkRep(ipc_id, 10, 1, "report","Graph (Last 10 Days)",false);
			    html += CreateLinkRep(ipc_id, 30, 2, "report","Graph (Last 30 Days)",false);
			    html += CreateLink(ipc_id, "repselect","Info & Custom Graph",true);
			    html += CreateLink(ipc_id,"maintsensor","Pause/Resume",false);
			    html += CreateLink(ipc_id,"deletesensor","Delete Sensor",false);
		  break;
		  case "user":
			    html += "<div class=menuhead>User Menu</div>";
			    html += CreateLink(ipc_id,"account","Edit My Account",true);
			    html += CreateLink(ipc_id,"scanall","Scan All Sensors Now",true);
			    html += CreateLinkemailrep(ipc_id,1,"sendrep","Email Report (24 Hours)",false);
			    html += CreateLinkemailrep(ipc_id,7,"sendrep","Email Report (7 Days)",false);
			    html += CreateLinkemailrep(ipc_id,30,"sendrep","Email Report (30 Days)",true);
			    html += CreateLinkNewWin(ipc_id,"quickinfo","Quick View",false);
			    html += CreateLinkNewWin(ipc_id+"&showicons=1","quickinfo","Quick View (With Icons)",true);
			    html += CreateLink(ipc_id,"crossref","Sensor Cross Reference",false);
			    html += CreateLink(ipc_id,"autodetect","Network Auto Discovery",true);
			    html += CreateLink2(ipc_id,"editgroup","Add Group",false);
			    html += CreateLink(ipc_id,"import","Import Group (*.ini)",false);
			    html += CreateLink(ipc_id,"sortgroup","Sort Groups",true);
			    html += CreateLink(ipc_id,"logout","Log Out",false);
		  break;
		  case "hosteduser":
			    html += "<div class=menuhead>User Menu</div>";
			    html += CreateLink(ipc_id,"account","Edit My Account",true);
			    html += CreateLink(ipc_id,"scanall","Scan All Sensors Now",true);
			    html += CreateLinkemailrep(ipc_id,1,"sendrep","Email Report (24 Hours)",false);
			    html += CreateLinkemailrep(ipc_id,7,"sendrep","Email Report (7 Days)",false);
			    html += CreateLinkemailrep(ipc_id,30,"sendrep","Email Report (30 Days)",true);
			    html += CreateLink(ipc_id,"crossref","Sensor Cross Reference",false);
			    html += CreateLinkNewWin(ipc_id,"quickinfo","Quick View",false);
			    html += CreateLinkNewWin(ipc_id+"&showicons=1","quickinfo","Quick View (With Icons)",true);
			    html += CreateLink2(ipc_id,"editgroup","Add Group",false);
			    html += CreateLink(ipc_id,"import","Import Group (*.ini)",false);
			    html += CreateLink(ipc_id,"sortgroup","Sort Groups",true);
			    html += CreateLink(ipc_id,"logout","Log Out",false);
		  break;
		  default:
   /* ansonsten */
    };   
    ShowAt(GetRight(lnkElement),GetTop(lnkElement),html);
};


function CreateLink(id,action,caption,addline)
{
    var url = baseRef + action +'?id='+ id;
    if (addline) return "<a class='btn' href='"+ url  +"'>"+ caption +"</a><div class=menuline></div>";
    return "<a class='btn' href='"+ url  +"'>"+ caption +"</a>";
};

function CreateLink2(id,action,caption,addline)
{
    var url = baseRef + action +'?parent='+ id;
    if (addline) return "<a class='btn' href='"+ url  +"'>"+ caption +"</a><div class=menuline></div>";
    return "<a class='btn' href='"+ url  +"'>"+ caption +"</a>";
};

function CreateLinkNewWin(id,action,caption,addline)
{
    var url = baseRef + action +'?id='+ id;
    if (addline) return "<a target=_blank class='btn' href='"+ url  +"'>"+ caption +"</a><div class=menuline></div>";
    return "<a target=_blank class='btn' href='"+ url  +"'>"+ caption +"</a>";
};

function CreateLinkRep(id,repdays,reptype,action,caption,addline)
{
    var url = baseRef + action +'?id='+ id + '&id2=0' + '&from=' + repdays + '&grp=' + reptype + '&rel=1'
    if (addline) return "<a class='btn' href='"+ url  +"'>"+ caption +"</a><div class=menuline></div>";
    return "<a class='btn' href='"+ url  +"'>"+ caption +"</a>";
};

function CreateLinkemailrep(id,ival,action,caption,addline)
{
    var url = baseRef + action +'?id='+ id + '&ival=' + ival ;
    if (addline) return "<a class='btn' href='"+ url  +"'>"+ caption +"</a><div class=menuline></div>";
    return "<a class='btn' href='"+ url  +"'>"+ caption +"</a>";
};

function MouseMoveListener(mozEvent)
{
    var e = GetEvent(mozEvent);
    StopHide();
    e.cancelBubble = true;
    var target = GetTarget(mozEvent);
    IpCheckMenu(target);
};

function MenuMouseMoveListener(mozEvent)
{
    StopHide();
    GetEvent(mozEvent).cancelBubble = true;
};

function GetEvent(mozEvent)
{
   if (isIE) return event;
   else      return mozEvent;
};

function GetTarget(mozEvent)
{
   if (isIE) return GetEvent(mozEvent).srcElement;
   if (isMozilla) return mozEvent.target;
};

function ShowAt(x,y,html)
{
    var mnu = GetMenu();
    mnu.style.left = x;
    mnu.style.top  = y;
    mnu.style.display = "block";
    mnu.innerHTML = html;
};

function Hide()
{
    var mnu = GetMenu();
    mnu.style.display = "none";

    var target = lasttarget;
    if (target!=null)
    {
	    var ipc_type = target.getAttribute("ipc_type"); 
	    var ipc_class = target.getAttribute("ipc_class")
	    if (ipc_class == undefined) return;
	    var mytr=target.parentNode.parentNode;
	    if (mytr.tagName!="TR") mytr=target.parentNode.parentNode.parentNode;
	    var mytds=mytr.childNodes;
	    for (var i=0;i<mytds.length;i++)
	    {
         if (mytds[i].tagName=="TD")
         {
	          if (mytds[i].getAttribute("rowspan")!="2") mytds[i].className=ipc_class;
         };
	    };
	  };
};

function DelayedHide(mozEvent)
{
  timer = window.setTimeout("Hide()",2000);
};

function StopHide()
{
  if (timer) window.clearTimeout(timer);
};

function GetMenu()
{
  return document.getElementById('mnu');
};

function AddListeners()
{
    var lst = document.getElementsByTagName('A');
    for (var i=0;i<lst.length;i++)
    {
		   AddMouseOverListener(lst[i],MouseMoveListener)
		   AddMouseOutListener(lst[i],MouseOutListener)
    };
    AddMouseOverListener(GetMenu(),MenuMouseMoveListener);
    
    var lst = document.getElementsByTagName('INPUT');
    for (var i=0;i<lst.length;i++)
    {
    	if (lst[i].type == "checkbox")
    	{
		   if (((lst[i].parentNode.getAttribute("showhidemaster"))=="latency") | ((lst[i].parentNode.getAttribute("showhidemaster"))=="noti"))
		   {  
          AddShowHideListener(lst[i],ShowHideListener);
          ShowHideEdits(lst[i]);
        };
    	}
    };
    
};

function ShowHideListener(mozEvent)
{
	  var e = GetEvent(mozEvent);
    StopHide();
    e.cancelBubble = true;
    var target = GetTarget(mozEvent);
    ShowHideEdits(target);
};

function ShowHideEdits(target)
{
  var id   = target.parentNode.getAttribute("showhidemaster"); 
  if ((id=="latency") | (id=="noti"))
  {
    var lst = document.getElementsByTagName('SPAN');
    for (var i=0;i<lst.length;i++)
    {
			if (lst[i].getAttribute("id")==id+"_hide")
			{ 
				var mytr=lst[i].parentNode;
				while (mytr.tagName!="TR") {mytr=mytr.parentNode;};
				
				if (target.checked) 
				{
					mytr.style.display='none';
				}
				else
				{
					mytr.style.display='';
				};
			};
    };
	}
}

	



function AddShowHideListener(element,func)
{
   
   if (isMozilla) element.addEventListener("click",func,true);
   if (isIE)      element.onclick = func;
}


function AddMouseOverListener(element,func)
{
   if (isMozilla) element.addEventListener("mouseover",func,true);
   if (isIE)      element.onmouseover = func;
}

function AddMouseOutListener(element,func)
{
   if (isMozilla) element.addEventListener("mouseout",func,true);
   if (isIE)      element.onmouseout = func;
}

function GetTop(eSrc)
{
		var iTop = 0;
		var eParent;
		eParent = eSrc;
		while (eParent.tagName.toUpperCase() != "BODY")
		{
			iTop += eParent.offsetTop;
			eParent = eParent.offsetParent;
			if (eParent == null) break;
		};
		return iTop-5;
};

function GetBottom(eSrc)
{
    return GetTop(eSrc) + eSrc.offsetHeight;
};

function GetRight(eSrc)
{
    return GetLeft(eSrc) + eSrc.offsetWidth;
};

function GetLeft(eSrc)
{
		var iLeft = 0;
		var eParent;
		eParent = eSrc;
		while (eParent.tagName.toUpperCase() != "BODY")
		{
			iLeft += eParent.offsetLeft;
			eParent = eParent.offsetParent;
			if (eParent == null) break;
		};
		return iLeft;
};

