var oPopup = window.createPopup();

//_____________________________________________________________________
//
//       APMenu
//_____________________________________________________________________
function APMenu(name, menu_length) {
  // Members
  this.mName      = name;
  this.mMenuIndex = menu_length;
  this.mMenuID    = "mainMenu_" + this.mMenuIndex;
  this.mMenuHTML  = "";
  this.mItemHTML  = "";
  this.mActivated = false;
  this.mMenuItemWidth    = 170;
  this.mMenuHeight       = 125;
  this.mSelected         = false;
  this.mSelectedItemText = "";
  this.mItems = new Array();    //put item of menu

  // Methods
  this.activate        = APMenu_activate;
  this.addItem         = APMenu_addItem;
  this.addItemByValues = APMenu_addItemByValues;
  this.onClick         = APMenu_onClick;
  this.pullDown        = APMenu_pullDown;
  this.setSelected     = APMenu_setSelected;

 
  //_____________________________________________________________________
  // addItemByValues: Creates an APMenuItem from the arguments
  //                  and adds it to this menu.
  //                  Returns the menu item.
  //
  function APMenu_addItemByValues(text, link, selected) {
    // addItem()= call APMenu_addItem()
    if (selected) {
      this.mSelected         = selected;
      this.mSelectedItemText = text;
    }
    return this.addItem(new APMenuItem(text, link, selected));
  }
  
  
  //_____________________________________________________________________
  // addItem: Adds an APMenuItem to this menu.
  //          Returns the menu item.
  //
  function APMenu_addItem(item) {
    this.mItems[this.mItems.length]=item;
    return item;
  }
  


  //_____________________________________________________________________
  // activate: Generate the DHTML elements and event handlers
  //           corresponding to the definition of this menu.
  //
  function APMenu_activate(iWidth) {
    if (iWidth) this.mMenuItemWidth=iWidth;
    if (!this.mActivated) { 
      this.mActivated=true;
      APMenu_create(this);
    }
  }
  

  //________________________________________________________________
  // 建立主選單
  function APMenu_create(apMenu) {
    /*	
    var sMenuContext="<DIV id='" + apMenu.mMenuID + "' class='mainMenu_outer'>" + 
                     "<DIV class='mainMenu_inner'" + 
                     "  onmouseover='window.oAPMenus[" + apMenu.mMenuIndex + "].pullDown(" + apMenu.mMenuIndex + ")'>" + 
                     apMenu.mName + "&nbsp;" + 
                     "<IMG src='/PortalTemplates/Images/UI_droparrow.gif' align='absMiddle'>" +
                     "</DIV>" + 
                     "</DIV>";
    */                 

    var sMenuContext="<DIV id='" + apMenu.mMenuID + "' class='mainMenu_outer'" + 
                     "  onclick='event.cancelBubble=true;'" + 
                     "  onmouseout='null'" + 
                     "  onmouseover='window.oAPMenus[" + apMenu.mMenuIndex + "].pullDown(" + apMenu.mMenuIndex + ")' nowrap>" + 
                     apMenu.mName + "&nbsp;<IMG src='/PortalTemplates/Images/UI_droparrow.gif' align='absMiddle'>" +
                     "</DIV>";
    apMenu.mMenuHTML=sMenuContext;
  } // End of APMenu_create
  

  //_____________________________________________________________________
  // onClick 
  function APMenu_onClick(link) {
    if (link!="")
      window.top.location.href=link;
  }

  //________________________________________________________________
  // 呼叫Menu選單
  function APMenu_pullDown(men_index) {
     var apMenu = eval("window.oAPMenus[" + men_index + "]");
     if (apMenu.mItems && apMenu.mItems.length==0) 
       return false;
     APMenu_popupMenu(apMenu);
  }

  //________________________________________________________________
  // 更改Menu被選取狀態
  function APMenu_setSelected(apMenu) {
    var mainMenu = document.getElementById(apMenu.mMenuID);
    mainMenu.setAttribute("className", (apMenu.mSelected ? "mainMenu_outer_selected" : "mainMenu_outer"));
    window.status=apMenu.mSelected ? (apMenu.mName + " > " + apMenu.mSelectedItemText) : window.status;
  }

  //________________________________________________________________
  function APMenu_cancelBubble() {
    window.event.cancelBubble = true;
  }


  //________________________________________________________________
  function APMenu_popupMenu(apMenu) {
    var popMenu_id="oPopMenu_" + apMenu.mMenuIndex;
    var sItemContext="";
    
    if (apMenu.mItems && apMenu.mItems.length>0) {
      //建立 popup window 內容	
      sItemContext="<DIV id='" + popMenu_id + "'>\n" + 
                   "<DIV class='popMenu_outer'>\n";
      for (i=0; i<apMenu.mItems.length; i++) {
      	var oItem=apMenu.mItems[i];
        var sItem="<DIV nowrap" + 
                  "  class='popMenu_inner'" + 
                  "  onmouseover=\"this.runtimeStyle.backgroundColor='#0099CC'\"" +
                  "  onmouseout =\"this.runtimeStyle.backgroundColor='#3C5694'\"" +
                  "  onclick=\"(window.parent).oAPMenus[" + apMenu.mMenuIndex + "].onClick('" + oItem.mLink + "')\">\n" +
                  oItem.mText +
                  "</DIV>\n";

      	sItemContext+=sItem;
      }
      sItemContext+="</DIV></DIV>";
      apMenu.mMenuHeight=eval(24 * apMenu.mItems.length);
    }
    var mainMenu=document.getElementById(apMenu.mMenuID);
    
    //var oPopup=window.createPopup();
    with (oPopup.document) {
      createStyleSheet("/PortalTemplates/CSS/menuStyle.css",0);
      body.style.borderRight = "solid #003366 1px";
      body.style.borderBottom = "solid #003366 1px";
      body.innerHTML=sItemContext; 
    }
    oPopup.show(0, 28, apMenu.mMenuItemWidth, apMenu.mMenuHeight, mainMenu);
    //oPopup.show(0, 28, apMenu.mMenuItemWidth, apMenu.mMenuHeight, document.body);
    //document.body.onmouseup = closePopup;
  }
} //Enf of APMenu




//________________________________________________________________
function closePopup(oPopup) {
  oPopup.hide();
}   

//___________________________________________________________
//
// APMenuItem : add menu item
//___________________________________________________________
function APMenuItem(text, link, selected) {
  this.mText = text;
  this.mLink = link;
  this.mSelected = (selected ? true : false);
} // End of APMenuItem

 

