/*
 * Copyright (c) Senselogic 2004. All rights reserved.
 */

var popIndex;
if(!popIndex) popIndex=0;
var popups;
if(!popups) popups = new Array();
var hideTimeout=null;
var showTimeout=null;
var HORIZONTAL=0;
var VERTICAL=1;
var SHOW_DELAY=400;
var HIDE_DELAY=1000;
var visiblePopups=new Array();
var intrudingElements=new Array();

if(navigator.userAgent.indexOf('Mozilla/4') != -1 && navigator.userAgent.indexOf('MSIE') == -1) ns4=true; else ns4=false;

function PopupMenu(root, containerId, _mainBg, _popLayout, _mainBorder, _mainSelectBg, _popBg, _popBorder, _popSelectBg, _mainArrow, _popupArrow, _mainClass, _popClass, _mainSeparatorColor, _popupSeparatorColor, _popupMarkImage, _popupPadding, _mainNoSeparatorsAtEdges, _noImagesAtEdges, _noImagesAtFirstLevel, _popupShadow, _fullPopBorder) {
   this.mainSeparatorColor=_mainSeparatorColor;
   this.popupSeparatorColor=_popupSeparatorColor;
   this.root=root;
   this.containerId=containerId;
   this.popLayout=_popLayout;
   this.mainBg=_mainBg;
   this.mainBorder=_mainBorder;
   this.mainSelectBg=_mainSelectBg;
   this.popBg=_popBg;
   this.popBorder=_popBorder;
   this.popSelectBg=_popSelectBg;
   this.popupPadding = _popupPadding;
   this.isTransparent=(_mainBg=="transparent");
   this.mainNoSeparatorsAtEdges = _mainNoSeparatorsAtEdges;
   this.noImagesAtEdges=_noImagesAtEdges;
   this.noImagesAtFirstLevel=_noImagesAtFirstLevel;
   this.mainArrow="/sitevision/portlet/popupmenu/popup_black.gif";
   if(_mainArrow != null && _mainArrow!="") this.mainArrow=_mainArrow;
   this.mainClass=_mainClass;
   this.popClass=_popClass;
   this.popupArrow="/sitevision/portlet/popupmenu/popup_black.gif";
   if(_popupArrow != null && _popupArrow!= "") this.popupArrow=_popupArrow;
   this.popupMarkImage=null;
   if(_popupMarkImage != null && _popupMarkImage!= "") this.popupMarkImage=_popupMarkImage;
   this.menuId = containerId;
   if(_popupShadow) {
      this.shadowOffset=2;
   } else {
      this.shadowOffset=0;
   }
   this.fullPopBorder = _fullPopBorder;

   // Prototypes
   this.renderMenu=pop_renderMenu;
   this.itemOver=pop_itemOver;
   this.itemOut=pop_itemOut;
   this.menuOver=pop_menuOver;
   this.menuOut=pop_menuOut;
   this.showPopup=pop_showPopup;
   this.hidePopups=pop_hidePopups;
   this.renderPopup=pop_renderPopup;
   this.writeMainMenuItem=pop_writeMainMenuItem;
   this.getSubMenuItem=pop_getSubMenuItem;
   menuObject = this;
}

function PopSubMenu(title, url, jsAction) {
   this.visible=false;
   this.title=title;
   this.url=url;
   this.jsAction=jsAction
   this.items=new Array();
   this.parentMenu=null;
   this.level=0;
   // Prototypes
   this.addSubMenu=sub_addSubMenu;
   this.addItem=sub_addItem;
   this.hasChildren=sub_hasChildren;
   this.popId="pop"+(popIndex++);
   this.hasParent=sub_hasParent;
   popups[this.popId]=this;
}

function sub_addSubMenu(title, url, jsAction) {
   var subMenu=new PopSubMenu(title,url, jsAction);
   this.items[this.items.length]=subMenu;
   subMenu.parentMenu=this;
   subMenu.level=this.level+1;
   return subMenu;
}

function sub_addItem(title, url, jsAction) {
   this.addSubMenu(title, url, jsAction);
}

function sub_hasChildren() {
   return this.items.length!=0;
}

function sub_hasParent(aMenu) {
   if(this.parentMenu==null) return false;
   if(this.parentMenu==aMenu) return true;
   return this.parentMenu.hasParent(aMenu);
}

var buffer = "";
function output(s,flush) {
    buffer += s;
    if(buffer.length>10000 || flush) { document.write(buffer); buffer="";}
}

function pop_writeMainMenuItem(anItem, firstItem) {
   if(ns4) {
       output("<img src=\"" + this.mainArrow + "\" />&nbsp;");
       output("<a class='" + this.mainClass + "' " + (anItem.url != null ? "href=\"" + anItem.url + "\"" : "") + ">" + anItem.title + "</a>");
       if(this.popLayout==VERTICAL) output("<br>");
   } else {
       if(this.popLayout==VERTICAL) {
             if(this.mainSeparatorColor!=null && !(firstItem && this.mainNoSeparatorsAtEdges)) output("<table width=100% cellpadding=0 cellspacing=1 border=0><tr><td height=1 width=100% bgcolor='"+this.mainSeparatorColor+"''></td></tr></table>");
             if (anItem.jsAction != null) {
                output("<div " + (anItem.url!=null ? "onclick=\"" +anItem.jsAction +"\"" : "") + " style=\"" + (anItem.url!=null ? "cursor:pointer;cursor:hand" : "cursor:default") + ";padding: 1px; " + (!this.isTransparent ? "border:1px solid " + this.mainBg + "" : "margin-top:1px; margin-bottom:1px") + "\" id='xx" + anItem.popId + "' onmouseover='"+this.menuId+".itemOver(this, \"" + anItem.popId + "\", false)' onmouseout='"+this.menuId+".itemOut(this, \"" + anItem.popId + "\", false)'>");
             }
             else {
                output("<div " + (anItem.url!=null ? "onclick=\"window.location='"+anItem.url+"'\"" : "") + " style=\"" + (anItem.url!=null ? "cursor:pointer;cursor:hand" : "cursor:default") + ";padding: 1px; " + (!this.isTransparent ? "border:1px solid " + this.mainBg + "" : "margin-top:1px; margin-bottom:1px") + "\" id='xx" + anItem.popId + "' onmouseover='"+this.menuId+".itemOver(this, \"" + anItem.popId + "\", false)' onmouseout='"+this.menuId+".itemOut(this, \"" + anItem.popId + "\", false)'>");
             }
             output("<table width=100% cellpadding=0 cellspacing=0 border=0><tr><td>");
       } else if (this.popLayout==HORIZONTAL) {
             output("<span style=\"cursor:pointer;cursor:hand;padding:1px 1px;" + (!this.isTransparent ? "border:1px solid " + this.mainBg + "" : "margin-top:1px; margin-bottom:1px") + "\" id='xx" + anItem.popId + "' onmouseover='"+this.menuId+".itemOver(this, \"" + anItem.popId + "\", false)' onmouseout='"+this.menuId+".itemOut(this, \"" + anItem.popId + "\", false)'>");
             if(!(firstItem && this.noImagesAtEdges) && !this.noImagesAtFirstLevel) output("<img src=\"" + this.mainArrow + "\" />&nbsp;");
       }

       if (anItem.jsAction != null) {
           //output("<a class='" + this.mainClass + "' style='text-decoration:none;' " + (anItem.url != null ? "href=\"javascript:void(" + anItem.jsAction + ";)\"" : "") + ">" + anItem.title + "</a>");
           output("<a class='" + this.mainClass + "' style='text-decoration:none;' " + (anItem.url != null ? "href=\"javascript:void(" + (this.popLayout == VERTICAL ? "" : anItem.jsAction) + ")\"" : "") + ">" + anItem.title + "</a>");
       }
       else {
           output("<a class='" + this.mainClass + "' style='text-decoration:none;' " + (anItem.url != null ? "href='" + anItem.url + "'" : "") + ">" + anItem.title + "</a>");
       }

       if(this.popLayout==VERTICAL) {
             if(anItem.hasChildren()) output("</td><td align=right valign=middle><img src='" + this.mainArrow + "' />");
             else output("</td><td>");
             output("</td></tr></table>");
             output("</div>");
       } else if (this.popLayout==HORIZONTAL) {
             output("</span>");
       }
   }
}

function pop_renderMenu() {
   var i;
   var menu = this.root;
   var n=menu.items.length;
   if(!ns4) {
       output("<div id='xyz' style='padding: 2px; background-color:" + this.mainBg + "' onmouseover='"+this.menuId+".menuOver()' onmouseout='"+this.menuId+".menuOut()'>");
   } else {
       output("<table border=0><tr><td>");
   }

   for(i=0;i<n;i++) {
      var anItem=menu.items[i];
      this.writeMainMenuItem(anItem, i==0);
      if(this.popLayout==HORIZONTAL) output("&nbsp;&nbsp;");
   }
   if(this.popLayout==VERTICAL && this.mainSeparatorColor!=null && !this.mainNoSeparatorsAtEdges) output("<table width=100% cellpadding=0 cellspacing=1 border=0><tr><td height=1 width=100% bgcolor='"+this.mainSeparatorColor+"''></td></tr></table>");

   if(!ns4) {
      output("</div>");
   } else {
      output("</td></tr></table>");
   }
    output("", true);

}

function pop_itemOut(el, id, isPopup) {
     if(isPopup||this.popLayout==VERTICAL) {
         if(isPopup) bg=this.popBg;
           else bg = this.mainBg;
         var els = getPopupParent(id).style;
         els.backgroundColor=bg;
       if(isPopup || !this.isTransparent) els.border="1px solid " + bg;
      }
}

function getPopupParent(popupId) {
   return document.getElementById("xx"+popupId);
}

function pop_itemOver(el, id, isPopup) {
   if(hideTimeout != null) {
      clearTimeout(hideTimeout);
      hideTimeout=null;
   }
   if(isPopup||this.popLayout==VERTICAL) {
      var els = getPopupParent(id).style;
      var bg = "";
      var border = "";
      if(isPopup) bg=this.popSelectBg;
        else bg = this.mainSelectBg;
      if(isPopup) border = this.popBorder;
        else border = this.mainBorder;
      if(isPopup || !this.isTransparent) {
        els.backgroundColor=bg;
        els.border="1px solid " + border;
      }
   }
   if(showTimeout != null) {
     clearTimeout(showTimeout);
     showTimeout=null;
   }
   showTimeout = setTimeout(this.menuId + ".showPopup('" + id + "')", SHOW_DELAY);
}

function pop_hideIntrudingElements() {
   intrudingElements = new Array();
   pop_hideElements("select");
   pop_hideElements("object");
   pop_hideElements("applet");
   pop_hideElements("embed");
   if(navigator.appName.indexOf("MSIE 5.0") != -1) pop_hideElements("iframe");
}

function pop_hideElements(tagName) {
   var selects = document.getElementsByTagName(tagName);
   for(i=0;i<selects.length; i++)
   {
     var o = selects[i];
     if(o.style.visibility != "hidden") {
        intrudingElements[intrudingElements.length] = o;
        o.style.visibility="hidden";
     }
   }
}

function pop_showIntrudingElements() {
   for(i=0; i<intrudingElements.length; i++) {
      intrudingElements[i].style.visibility="";
   }
}

function pop_showPopup(id) {

   var popup=popups[id];
   if(popup.visible==true) { return;}
   pop_hidePopups(id);
   if(visiblePopups.length == 0)
      pop_hideIntrudingElements();
   if(popup.items.length==0) return;
   var popEl = document.getElementById(id);
   if(popEl == null) popEl = this.renderPopup(id);
   var mouseEl = document.getElementById("xx"+id);
   var x = null;
   var y = null;
   if(this.popLayout==VERTICAL||popup.level>1) {
      x = mouseEl.offsetLeft+mouseEl.offsetWidth;;
      y = mouseEl.offsetTop;
   }
   else if(this.popLayout==HORIZONTAL) {
      x=mouseEl.offsetLeft;
      y=mouseEl.offsetHeight+mouseEl.offsetTop+3;
   }
   var offsetParent = mouseEl.offsetParent;
   while(offsetParent != null) {
      x += offsetParent.offsetLeft;
      y += offsetParent.offsetTop;
      offsetParent = offsetParent.offsetParent;
   }

   popEl.shadowEl.style.left=(x+this.shadowOffset)+"px";
   popEl.shadowEl.style.top=(y+this.shadowOffset)+"px";
   popEl.shadowEl.style.width=popEl.offsetWidth+"px";
   popEl.shadowEl.style.height=popEl.offsetHeight+"px";
   popEl.shadowEl.style.zIndex=4999;
   popEl.shadowEl.style.visibility="visible";

   popEl.style.left=x+"px";
   popEl.style.top=y+"px";
   popEl.style.visibility="visible";
   popEl.style.zIndex=5000;
   visiblePopups[visiblePopups.length]=popup;
   popup.visible=true;
   return false;
}

function pop_renderPopup(id) {
   var popup=popups[id];
   var n=popup.items.length;
   var popH = "";

   for(var i=0;i<n;i++) {
      popH += this.getSubMenuItem(popup.items[i]);
      if (i != n-1) {
         if(this.popupSeparatorColor!=null) popH += "<table width=100% cellpadding=0 cellspacing=1 border=0><tr><td height=1 width=100% bgcolor='"+this.popupSeparatorColor+"''></td></tr></table>";
      }
   }
   var node = document.createElement("div");
   node.style.visibility="hidden";
   node.style.position="absolute";
   if (this.fullPopBorder!=null)
     node.style.border="1px solid " + this.fullPopBorder;
   else
     node.style.border="1px solid black";

   node.style.backgroundColor=this.popBg;
   node.style.padding=this.popupPadding + "px " + this.popupPadding + "px";
   node.style.width="180px";
   node.setAttribute("id", id);
   node.onmouseover=pop_menuOver;
   node.onmouseout=pop_menuOut;
   node.innerHTML=popH;

   var shadow = document.createElement("div");
   var shadows = shadow.style;
   shadows.visibility="hidden";
   shadows.position="absolute";
   shadows.backgroundColor="#888888";
   shadows.width="180px";
   node.shadowEl=shadow;
   document.body.appendChild(node);
   document.body.appendChild(shadow);
   return node;
}

function pop_getSubMenuItem(anItem) {
      var popH = "";
      if (anItem.jsAction != null) {
         popH += "<table cellpadding=0 cellspacing=0 border=0 width=100% " + (anItem.url!=null ? "onclick=\"" +anItem.jsAction +";return false;\"" : "") + " style='padding: 1px; border:1px solid " + this.popBg + ";" + (anItem.url!=null ? "cursor:pointer;cursor:hand" : "cursor:default") + ";' id='xx" + anItem.popId + "' onmouseover='"+this.menuId+".itemOver(this, \"" + anItem.popId + "\", true)' onmouseout='"+this.menuId+".itemOut(this, \"" + anItem.popId + "\", true)'>";
      }
      else {
         popH += "<table cellpadding=0 cellspacing=0 border=0 width=100% " + (anItem.url!=null ? "onclick=\"window.location='"+anItem.url+"'\"" : "") + " style='padding: 1px; border:1px solid " + this.popBg + ";" + (anItem.url!=null ? "cursor:pointer;cursor:hand" : "cursor:default") + ";' id='xx" + anItem.popId + "' onmouseover='"+this.menuId+".itemOver(this, \"" + anItem.popId + "\", true)' onmouseout='"+this.menuId+".itemOut(this, \"" + anItem.popId + "\", true)'>";
      }

      popH += "<tr>";
      if (this.popupMarkImage!=null) {
         popH += "<td width=15><img src='" + this.popupMarkImage + "' /></td>";
      }

      if (anItem.jsAction != null) {
         //popH += "<td><a class='" + this.popClass + "' style='text-decoration:none;' " + (anItem.url != null ? "href=\"javascript:void(" +anItem.jsAction + ";)\"" : "") + ">" + anItem.title + "</a>";
         popH += "<td><a class='" + this.popClass + "' style='text-decoration:none;' " + (anItem.url != null ? "href=\"javascript:void("  + (this.popLayout == VERTICAL ? "" : anItem.jsAction) + ")\"" : "") + ">" + anItem.title + "</a>";
      }
      else {
         popH += "<td><a class='" + this.popClass + "' style='text-decoration:none;' " + (anItem.url != null ? "href='" + anItem.url + "'" : "") + ">" + anItem.title + "</a>";
      }

      if(anItem.hasChildren()) popH += "</td><td align=right><img src='" + this.popupArrow + "' />";
      //else popH += "</td><td>";
      popH += "</td></tr></table>";
      return popH;
}

function pop_hidePopups(id) {
   var popup = popups[id];
   if(visiblePopups.length != 0) {
      var newVisible=new Array();
      for(i=0;i<visiblePopups.length;i++) {
         var aMenu = visiblePopups[i];
         if(id==null || !popup.hasParent(aMenu)) {
            if(aMenu.visible) {
                var pop = document.getElementById(aMenu.popId);
                var pops = pop.style;
                pops.visibility="hidden";
                pop.shadowEl.style.visibility="hidden";
                aMenu.visible=false;
            }
         } else newVisible[newVisible.length]=aMenu;
      }
      visiblePopups = newVisible;
   }
         if(visiblePopups.length == 0)
            pop_showIntrudingElements();

}

function pop_menuOver() {
   if(hideTimeout != null) {
      clearTimeout(hideTimeout);
      hideTimeout=null;
   }
}

function pop_menuOut() {
   if(showTimeout != null) {
      clearTimeout(showTimeout);
      showTimeout=null;
   }
   hideTimeout = setTimeout("pop_hidePopups(null)", HIDE_DELAY);
}
