/*
  SOME COMMON GOBAL VARIABLES FOR SCOPING OF THE FLYOUT
  MENU
    FAMILY : jsFlyout
*/

var g_ContextMenuOwner = document.getElementById("cgBtnDummy");
var g_ContextMenuOwnerType = "";
var g_ContextMenuOwnerState = "off";
var g_ContextMenuFlyoutState = "off";
var g_ContextMenuFlyoutTimer = 0;
var g_ContextMenuItem;
var g_ContextMenuPreserveFunc;

/*
  THIS FUNCTION PHYSICALLY DRAWS THE FLYOUT MENU
    FAMILY : jsFlyout
*/
function drawFlyoutMenu(Owner, List, Actions, sType)  
{ 
  // ? event.cancelBubble=true; // might be a bit dangerous here??
  if(g_ContextMenuFlyoutState=="dusk") {
    window.clearTimeout(g_ContextMenuFlyoutTimer);    
  }
  
  var iBodyPadding = 15;
  var iExtraSpaceY = 93;
  if (bt.ie4||bt.ie5||bt.ie6) {
    var bNeedsIEBGFix = true;
  } else {
    var bNeedsIEBGFix = false;
  }

  var oIEBGFix = document.getElementById("FlyoutIEBGFix");
  if (sType=="right") {
    var iExtraSpaceX = 11;
    var iBoxStartX = document.body.clientWidth-Owner.offsetLeft-1-iBodyPadding-iExtraSpaceX;
    var iBoxStartY = Owner.offsetTop+Owner.offsetParent.offsetTop-1+iBodyPadding+iExtraSpaceY;
    var iOwnerX = document.body.clientWidth-Owner.offsetLeft-iBodyPadding;
    var iOwnerY = Owner.offsetTop+Owner.offsetParent.offsetTop+iBodyPadding;
  } else {
    var iExtraSpaceX = 10;
    var iBoxStartX = document.getElementById(Owner.id).offsetWidth+1+iBodyPadding+iExtraSpaceX;
    var iBoxStartY = Owner.offsetTop+Owner.offsetParent.offsetTop-1+iBodyPadding+iExtraSpaceY;
    var iOwnerX = 0;
    var iOwnerY = 0;
  }
  
//  cgMnuOver(g_ContextMenuOwner,g_ContextMenuOwnerType,"hand");

  g_ContextMenuOwner = Owner;
  g_ContextMenuOwnerType = sType;
  List = List.split(","); 
  Actions = Actions.split(",");
  
  if ((List.length <= 1) && (List[0].length == 0)) return;
  
  var actContent = document.getElementById("obsFlyMenuClick");

  if (actContent.style.display != "none") hidePopupMenu();

  var actMenuHTML = "";
  var actMenuHeight = 0, actMenuWidth = 0;
  var actBoxShape = "";
  
  for(var i = 0; i < List.length; i++)
  {
    if (List[i] == "-")
      actMenuHTML += flyMenuSpacer();
    else
      actMenuHTML += flyMenuItem(List[i],Actions[i]);     
  }
  
  actBoxShape = flyMenuBox(1);
  
  if (sType=="right") {
    actBoxShape = actBoxShape.replace("flyArrowA_1","flyArrowOff");
    actBoxShape = actBoxShape.replace("flyArrowA_3","flyArrowOn");
  } else {
    actBoxShape = actBoxShape.replace("flyArrowA_1","flyArrowOn");
    actBoxShape = actBoxShape.replace("flyArrowA_3","flyArrowOff");  
  }
  
  actMenuHTML = actBoxShape.replace("<!--BUTTONPART-->",actMenuHTML);  
  
  actContent.innerHTML = actMenuHTML;
  
  if (sType == "right") {
    document.getElementById("flyArrowOn").className="arrowOnRight";  
  } else {
    document.getElementById("flyArrowOn").className="arrowOnLeft";
  } 
  
  //I CANNOT DERIVE THE NEW WIDTH AND HEIGHT OF THE MENU WITHOUT
  //HAVING THE BROWSER REVEAL IT.  SO, THROUGH A HIT-N-RUN,
  //I REVEAL IT, GRAB THE STUFF I NEED, AND HIDE IT AGAIN.  
  actContent.style.display = "block";

  actMenuWidth = document.getElementById("obsFlyMenuClickContent").offsetWidth;
  actMenuHeight = document.getElementById("obsFlyMenuClickContent").offsetHeight;  
  
  //SET THE SIZE OF THE IEBGFIX IFRAME TO THE SAME AS THE NEW MENU
  if (bNeedsIEBGFix) {
    oIEBGFix.style.width = actMenuWidth;
    oIEBGFix.style.height = actMenuHeight;
  }
  
  if (sType=="right") {
    actContent.style.left = iBoxStartX-actMenuWidth-Owner.offsetWidth;
    if (bNeedsIEBGFix) {
      oIEBGFix.style.left = iBoxStartX-actMenuWidth-Owner.offsetWidth;
    }
  } else {
    actContent.style.left = iBoxStartX;
    if (bNeedsIEBGFix) {
      oIEBGFix.style.left = iBoxStartX;
    }
  }
  actContent.style.top = iBoxStartY;
  if (bNeedsIEBGFix) {
    oIEBGFix.style.top = iBoxStartY;
  }
  
  if (bNeedsIEBGFix) {  
    oIEBGFix.style.display = "block";
  }

  document.getElementById("curOption").innerText=Owner.id;

  g_ContextMenuPreserveFunc = document.onclick;
  g_ContextMenuOwnerState = "on";
  g_ContextMenuFlyoutState = "on"
  document.getElementById("ecContentPart").onclick = timerHidePopupMenu;

}

function hidePopupMenu()
{
  var oIEBGFix = document.getElementById("FlyoutIEBGFix");
  document.getElementById("curOption").innerText="";
  var rcMenu = document.getElementById("obsFlyMenuClick");
  if (bt.ie4||bt.ie5||bt.ie6) {  
    oIEBGFix.style.display = "none";
  }
  rcMenu.style.display = "none";  
  rcMenu.innerHTML = "";
  g_ContextMenuOwnerState = "off";
  ecMnuOut(g_ContextMenuOwner.id,g_ContextMenuOwnerType);
}

function timerHidePopupMenu()
{
  if(g_ContextMenuFlyoutState=="on"){
    g_ContextMenuFlyoutTimer = window.setTimeout("hidePopupMenu()", 1000);
    g_ContextMenuFlyoutState="dusk";
  }
}

function cancelTimerHidePopupMenu()
{
  window.clearTimeout(g_ContextMenuFlyoutTimer);
  g_ContextMenuFlyoutState="on";
}

function selectPopupItem(Item)
{
  hidePopupMenu();
  g_ContextMenuItem = Item;  
  eval(g_ContextMenuOwner.onpopupselect);  
}

/*
  THIS FUNCTION DRAWS THE CONTAINER FOR THE TOTAL
  HEIGHT OF THE RIGHT CLICK MENU
    FAMILY : jsFlyout
*/
function flyMenuBox(bAction) {
  var tmpHTML = "";
  
  if (bAction==1) {
    tmpHTML = document.getElementById("flyMenuBoxPart1").innerHTML;
    tmpHTML = tmpHTML.replace("obsFlyMenuClickContentTemplate","obsFlyMenuClickContent");
  }
  else if (bAction==2) {
    tmpHTML = document.getElementById("flyMenuBoxPart2").innerHTML;
    tmpHTML = tmpHTML.replace("obsFlyMenuClickContentTemplate","obsFlyMenuClickContent");    
  }  
  return tmpHTML;
}

/*
  THIS FUNCTION DRAWS A FOR THE CONTEXT MENU SPACER
    FAMILY : jsFlyout
*/
function flyMenuSpacer() {
  var tmpHTML = "";
  
  tmpHTML = document.getElementById("flyMenuSpacerHTML").innerHTML;
  tmpHTML = tmpHTML.replace("flyMenuSpacerHTML","btnSeperator");
  
  return tmpHTML;
}

/*
  THIS FUNCTION DRAWS A SPECIFIC ITEM IN THE CONTEXT MENU
    FAMILY : jsFlyout
*/
function flyMenuItem(strCaption,strAction) {
  var tmpHTML = "";    
  
  tmpHTML = document.getElementById("flyMenuItemHTML").innerHTML;
  tmpHTML = tmpHTML.replace("<!--CAPTION-->",strCaption);
  tmpHTML = tmpHTML.replace("<!--ACTION-->",strAction);
  tmpHTML = tmpHTML.replace("flyMenuItemHTML","btnSpace");
  
  return tmpHTML;
}

/*
  THIS FUNCTION CONTROLS THE ROLLOVER EFFECT ONMOUSEOVER
    FAMILY : jsFlyout
*/
function mnuFlyClickHigh(oTd,bState) {
  oTd.className="mnuFlyClickCellOver";
}

/*
  THIS FUNCTION CONTROLS THE ROLLOVER EFFECT ONMOUSEOUT
    FAMILY : jsFlyout
*/
function mnuFlyClickLow(oTd,bState) {
  oTd.className="mnuFlyClickCell";
}

/*
  THIS FUNCTION CONTROLS THE ROLLOVER EFFECT ONMOUSEOUT
    FAMILY : jsFlyout
*/
function mnuStillClickLow(oTd,bState) {
  oTd.className="mnuStillClickItemLow";
}

/*
  THIS FUNCTION CONTROLS THE ROLLOVER EFFECT ONMOUSEOVER
    FAMILY : jsFlyout
*/
function mnuStillClickHigh(oTd,bState) {
    oTd.className="mnuStillClickItemHigh";
}

/*
  THIS FUNCTION CONTROLS THE ROLLOVER EFFECT ONMENUOUT
    FAMILY : jsMenu
*/
function ecMnuOut(oTd,sType) {
  if(g_ContextMenuOwnerState=="off") {
    oTd.className=sType=="left"?"ecMenuButtonLeft":"ecMenuButtonRight";
  }
  window.status="";
}

/*
  THIS FUNCTION CONTROLS THE ROLLOVER EFFECT ONMENUOVER
    FAMILY : jsMenu
*/
function ecMnuOver(oTd,sType,sCursor) {
  if (sType=="left") {
    oTd.className="ecMenuButtonLeftOver";
    window.status=oTd.id;
  }
  else if (sType=="right") {
    oTd.className="ecMenuButtonRightOver";
    window.status=oTd.id;
  }
  if (sCursor!="default") {
    oTd.className=oTd.className+" cursorHand";
  } else {
    oTd.className=oTd.className+" cursorDefault";
  }
  if (g_ContextMenuOwnerState=="on"&&oTd.id!=g_ContextMenuOwner.id) {
    hidePopupMenu();
    ecMnuOut(g_ContextMenuOwner,g_ContextMenuOwnerType);
    //sType=document.getElementById("obsFlyMenuClick").style.display=="block"?document.getElementById("obsFlyMenuClick").style.display="none":sType;
  }
}

/*
  THIS FUNCTION POPULATES THE REQUIRED FLYOUT OPTIONS
  OFF THE FLYOUT MENU FOR THE CURRENT MENU ITEM
*/
function ecMenu(oCur,sItem,sDir){
  var sCaptions = "";
  var sActions = "";
  var bValid = false;
  var sDef = "?ce=";
  if(sItem=="join"){
    sCaptions = "Benefits of Joining,Join Now...";
    sActions = sDef+"join,"+sDef+"joinnow";
    bValid = true;
  }
  if(sItem=="organogram"){
    sCaptions = "Structure,Board,Exco,Sub Committees,Support Structure";
    sActions = sDef+"ogstruct,"+sDef+"ogboard,"+sDef+"ogexco,"+sDef+"ogsbc,"+sDef+"ogsup,";
    bValid = true;
  }
  if(sItem=="minutes"){
    sCaptions = "Minutes of the Board,Minutes of the Exco";
    sActions = sDef+"minutecat&ci=29,"+sDef+"minutecat&ci=29";
    bValid = true;
  }
  if(bValid){
    drawFlyoutMenu(oCur,sCaptions,sActions,sDir);
  }
}