/*     
     File:        icMenuItem.js
     Created:     2009-07-04
     Modified:    2009-09-24
     Copyright:   Copyright (c) InfoCaption AB 2009
     Author:      Peter Jäderlund
     Description: Menu items
*/

var g_nMenuItemHeight = 23;
var g_nMenuItemSplitterHeight = 15; // IE min size (by some reason)
var g_idItemLayer = 499; // Note: id 51..55 conficts with some other unknown objects !
var g_MenuItemObjects = new Array(); 
var g_nNumberOfMenuItemObjects = 0;


function info(mnuObj){
    var iMax = 25;
    if (mnuObj.menuItem == null) {
        // First menu item
        alert('No menu items');   
    } else {                              
        // Menu items exists
        var lastMenuItem = mnuObj.menuItem;
        alert('MenuItem: ' + lastMenuItem + '\n' +
              'Id: ' + lastMenuItem.id + '\n' +
              'Btn: ' + lastMenuItem.curButton + '\n' +
              'hgt: ' + lastMenuItem.iHeight);

        while (lastMenuItem.nextMenuItem != null) {
            lastMenuItem = lastMenuItem.nextMenuItem;
            alert('MenuItem: ' + lastMenuItem + '\n' +
                  'Id: ' + lastMenuItem.id + '\n' +
                  'Btn: ' + lastMenuItem.curButton + '\n' +
                  'hgt: ' + lastMenuItem.iHeight);
            iMax = iMax - 1;
            if ((iMax <= 0)) {
                return;
            }
        }
    }
}

// --------------------------------------------------------------
function getMenuItemObject(id) { 
    for (var i = 1; i <= g_nNumberOfMenuItemObjects; i++) {
        var mnuItemObj = g_MenuItemObjects[i];
        if (mnuItemObj.id == id) {
            return mnuItemObj;
        }
    }
    return null;
}


// -------------------------------------------------------------- //zz new
function OnMouseOverMenuItemObject(id) { 
  var mnuItemObject = getMenuItemObject(id);
  mnuItemObject.mnuObject.stopTimer();
  
  if (!mnuItemObject.isSelection) { 
      return;
  }
  var btn = document.getElementById("bx" + mnuItemObject.id);
  btn.style.left = -1 * mnuItemObject.mnuObject.iWidth + "px";

  /*var img = document.getElementById(mnuItemObject.idImg);
  var imgFileName = "images/" + mnuItemObject.curButton + mnuItemObject.mnuObject.iWidth + "a.png";
  img.src = imgFileName;                                          */

  var txt = document.getElementById(mnuItemObject.idTxt);
  txt.className = "menuitemfontactive";
}


// -------------------------------------------------------------- //zz new
function OnMouseOutFromMenuItemObject(id) { 
    var mnuItemObject = getMenuItemObject(id);
    mnuItemObject.mnuObject.startTimer();
    
    if (!mnuItemObject.isSelection) {
        return;
    }
    
    var btn = document.getElementById("bx" + mnuItemObject.id);
    btn.style.left = "0px";

    /*var img = document.getElementById(mnuItemObject.idImg);
    var imgFileName = "images/" + mnuItemObject.curButton + mnuItemObject.mnuObject.iWidth + ".png";
    img.src = imgFileName;*/

    var txt = document.getElementById(mnuItemObject.idTxt);
    txt.className = "menuitemfontnormal";
}


// --------------------------------------------------------------
function hideSubMenu(mnu) {
    while (mnu != null) {
        mnu.hideMenuItem();         //zzz
        mnu = mnu.nextMenuItem;    
    }
}


// --------------------------------------------------------------
function hideOtherSubMenus(mnu) {
    var topMenu = getTopMenu(mnu.mnuObject);
    
    var mnu = getFirstMenuItem(mnu);
    while (mnu != null) {
        if (mnu.childMenu != null) {
            topMenu.hideMenu(mnu.childMenu);
        }
        mnu = mnu.nextMenuItem;
    }
}


// --------------------------------------------------------------
function showSubMenu(mnu) {
    if (mnu.mnuObject.useTimer) {
        mnu.mnuObject.startTimer();
    }
    while (mnu != null) {
        mnu.showMenuItem();         //zzz
        mnu = mnu.nextMenuItem;    
    }
}


// --------------------------------------------------------------
function OnClickOnMenuItemObject(id) {
    var mnuItemObject = getMenuItemObject(id);
    if (!mnuItemObject.isSelection) { 
        return;
    }
    if (mnuItemObject.url != "" && mnuItemObject.url != null) {
        
        var topMenu = getTopMenu(mnuItemObject.mnuObject);
        topMenu.hideMenu(); 
        eval(mnuItemObject.url);
    } else { 
       if (mnuItemObject.className == "MenuItem" && mnuItemObject.childMenu != null) {
           hideOtherSubMenus(mnuItemObject);
           showSubMenu(mnuItemObject.childMenu);
       }
    }
}


// --------------------------------------------------------------
function initMenus(mnuObj, hgt) {
    this.closeTimer = 0;
    this.useTimer = false;
    this.mnuObject = mnuObj;
    this.nextMenuItem = null;
    this.iHeight = hgt;
    this.iWidth = mnuObj.iWidth;
    this.className = "MenuItem";
    this.childMenu = null;
    if (mnuObj.defaultVisibility == "visible") {
        this.stopTimer = nada;
        this.startTimer = nada;
    } else {
        this.startTimer = startTimer;
        this.stopTimer = stopTimer;
    }
    this.reStartTimer = reStartTimer;
    this.getTopMenu = getTopMenu;
    this.childIsActive = childIsActive;
    this.defaultVisibility = mnuObj.defaultVisibility;
    if (mnuObj.className == this.className) {
        if (mnuObj.childMenu == null) {
            mnuObj.childMenu = this;
        }
    }
    this.mnuObject.useTimer = true;

    var iTopResult = g_nMenuHeight;
    if (this.mnuObject.menuItem == null) {
        // First menu item
        this.mnuObject.menuItem = this; 
        // alert("null\n" + this.id + '\n' + this.nextMenuItem);
    } else {                              
        // Menu items exists
        var lastMenuItem = this.mnuObject.menuItem; 
        iTopResult = iTopResult + lastMenuItem.iHeight;
        while (lastMenuItem.nextMenuItem != null) {
            lastMenuItem = lastMenuItem.nextMenuItem;
            iTopResult = iTopResult + lastMenuItem.iHeight;
        }      
        lastMenuItem.nextMenuItem = this;
    }

    return iTopResult;
}


// --------------------------------------------------------------
function hideMenuItem() { //zzz
    if (this.useTimer) { 
        this.stopTimer();
    }
    hideElement(this.doc, this.id); // Button
    hideElement(this.doc, this.idTxt); // Caption
    
    if (this.nextMenuItem == null) {
        // Shade
        hideElement(this.doc, this.id + '1');
        hideElement(this.doc, this.id + '2');
        hideElement(this.doc, this.id + '3');
        hideElement(this.doc, this.id + '4');
        hideElement(this.doc, this.id + '5');
    }
}

// --------------------------------------------------------------
function showMenuItem() {  //zzz
    showElement(this.doc, this.id); // Button
    showElement(this.doc, this.idTxt); // Caption
    
    if (this.nextMenuItem == null) {
        // Shade
        showElement(this.doc, this.id + '1');
        showElement(this.doc, this.id + '2');
        showElement(this.doc, this.id + '3');
        showElement(this.doc, this.id + '4');
        showElement(this.doc, this.id + '5');
    }
    if (this.useTimer) {
        var mnuObject = getMenuObject(this.id);
        if (mnuObject == null) {
            g_nNumberOfMenuObjects += 1;
            g_MenuObjects[g_nNumberOfMenuObjects] = this;                                     
        }
    }
}


// --------------------------------------------------------------
function setMenuItemLeft(x) {
    // Move the button
    var obj = this.doc.getElementById(this.id);
    obj.style.left = x + "px";                 

    // Move the text
    obj = this.doc.getElementById(this.idTxt);
    if (obj != null) {
        obj.style.left = x + "px";                 
    }

    // Move the shade
    obj = this.doc.getElementById(this.id + "1");   
    if (obj != null) {
        obj.style.left = (x + this.wdt - 3) + "px";

        obj = this.doc.getElementById(this.id + "2");
        obj.style.left = (x + this.wdt - 3) + "px";

        obj = this.doc.getElementById(this.id + "3");
        obj.style.left = x + "px";

        obj = this.doc.getElementById(this.id + "4");
        obj.style.left = (x + this.wdt - 7) + "px";

        obj = this.doc.getElementById(this.id + "5");
        obj.style.left = (x + 11) + "px";
    }

    if (this.childMenu != null) {
        var x2 = x + (this.iWidth/2);
        if (x2 > 700) {
            x2 = x - (this.iWidth/2);
        }
        this.childMenu.setLeft(x2);
        var mnu = this.childMenu.nextMenuItem;
        while (mnu != null) {
            mnu.setLeft(x2);
            mnu = mnu.nextMenuItem;
        }
    }
}                               


// --------------------------------------------------------------
function initButton(y, caption, linkUrl) {
    // Create the button image
    var x = this.mnuObject.iLeft;
    y = y + this.mnuObject.iTop;
    this.yOffset = 0;
    if (this.mnuObject.className == this.className) {
        this.yOffset = -3;
    }
    y = y + this.yOffset;
    this.iLeft = x + (this.mnuObject.iWidth/2);
    if (x > 700) {
        this.iLeft = this.iLeft - this.mnuObject.iWidth;
    }
    this.iTop = y;
    this.url = linkUrl;
    this.caption = caption;

    // The button
    var sCursor = "pointer";
    if (!this.isSelection) {                                                    
        sCursor = "default";
    }
    
    if (caption.indexOf("samysql")> 1) {
        x = x - 1 + 1;
    }
    this.doc.write("<div id='" + this.id + "' style='position:absolute;left:" + x + "px;top:" + y + "px;width:" + this.mnuObject.iWidth + 
                   "px;height:" + this.iHeight + "px;overflow:hidden;visibility:" + this.mnuObject.defaultVisibility + ";cursor:" + sCursor + ";z-index:9'>");
    
    var wdt2 = this.iWidth * 2;
    this.doc.write("  <div id='bx" + this.id + "' style='width:" + wdt2 + "px;height:" + this.iHeight + "px;position:relative;left:0px;top:0px;overflow:hidden'>"); //zz new

    if (linkUrl == "" || linkUrl == null) {
        this.doc.write("  <img id='" + this.idImg + "' src='images/" + this.curButton + this.mnuObject.iWidth + ".png' border=0 " +
                       " style='cursor:" + sCursor + "' " +
                       "    OnClick='OnClickOnMenuItemObject(\"" + this.id + "\")' " +
                       "    OnMouseOver='OnMouseOverMenuItemObject(\"" + this.id + "\")' " +
                       "    OnMouseOut='OnMouseOutFromMenuItemObject(\"" + this.id + "\")'>");
        this.doc.write("  </img>");          
    } else {
        this.doc.write("  <a href='" + linkUrl + "'>");
        this.doc.write("  <img id='" + this.idImg + "' src='images/" + this.curButton + this.mnuObject.iWidth + ".png' border=0 " +
                       " style='cursor:" + sCursor + "' " +
                       "    OnMouseOver='OnMouseOverMenuItemObject(\"" + this.id + "\")' " +
                       "    OnMouseOut='OnMouseOutFromMenuItemObject(\"" + this.id + "\")'>");
        this.doc.write("  </img></a>");          
    }
    this.doc.write("  </div>");
    this.doc.write("</div>");
                                                         
    if (caption != "") {
        x = x + 5;
        y = y + 3;
        if (linkUrl == "") {
            this.doc.write("<div id='" + this.idTxt + "' style='position:absolute;left:" + x + "px;top:" + y + 
                           "px;width:" + (this.mnuObject.iWidth-10) + ";overflow:hidden;visibility:" + this.mnuObject.defaultVisibility + ";cursor:" + sCursor + ";z-index:9' class='menuitemfontnormal'" +
                           "    OnClick='OnClickOnMenuItemObject(\"" + this.id + "\")' " +
                           "    OnMouseOver='OnMouseOverMenuItemObject(\"" + this.id + "\")' " +
                           "    OnMouseOut='OnMouseOutFromMenuItemObject(\"" + this.id + "\")' >");
            this.doc.write("  <center>" + caption + "</center>");
            this.doc.write("</div>"); 
        } else {
            this.doc.write("<a href='" + linkUrl + "'>");
            this.doc.write("<div id='" + this.idTxt + "' style='position:absolute;left:" + x + "px;top:" + y + 
                           "px;width:" + (this.mnuObject.iWidth-10) + ";overflow:hidden;visibility:" + this.mnuObject.defaultVisibility + ";cursor:" + sCursor + ";z-index:9' class='menuitemfontnormal'" +
                           "    OnMouseOver='OnMouseOverMenuItemObject(\"" + this.id + "\")' " +
                           "    OnMouseOut='OnMouseOutFromMenuItemObject(\"" + this.id + "\")' >");
            this.doc.write("<center>" + caption + "</center>"); 
            this.doc.write("</div></a>"); 
        }
    } else {
        // alert(y);
    }
}            // http://www.gamedev.net/community/forums/topic.asp?topic_id=460882


// --------------------------------------------------------------
function writeMenuItemShade(doc, id, imgFileName, x, y, wdt, hgt, defaultVisibility) {
    doc.write("<div id='" + id + "' style='position:absolute;left:" + x + "px;top:" + y + "px;visibility:" + defaultVisibility + ";z-index:9'>");
    doc.write("  <img src='" + imgFileName + "' border=0 width='" + wdt + "' height='" + hgt + "'></img>");
    doc.write("</div>"); 
}


// --------------------------------------------------------------
function createShade(wdt, hgt) {
    // The shade
    var x = this.mnuObject.iLeft; 
    var y = this.mnuObject.iTop + g_nMenuHeight;
    writeMenuItemShade(this.doc, this.id + "1", "images/ShadeRightTop.png", x + wdt -3, y, 10, 11, this.mnuObject.defaultVisibility);       
    writeMenuItemShade(this.doc, this.id + "2", "images/ShadeRight.png", x + wdt -3, y + 11, 10, hgt - 17, this.mnuObject.defaultVisibility);
    writeMenuItemShade(this.doc, this.id + "3", "images/ShadeBottomLeft.png", x, y + hgt - 6, 11, 12, this.mnuObject.defaultVisibility);
    writeMenuItemShade(this.doc, this.id + "4", "images/ShadeBottomRight.png", x + wdt - 7, y + hgt - 6, 14, 12, this.mnuObject.defaultVisibility);
    writeMenuItemShade(this.doc, this.id + "5", "images/ShadeBottom.png", x + 11, y + hgt - 6, wdt - 18, 12, this.mnuObject.defaultVisibility);
}


// --------------------------------------------------------------
function createMenuItemObject(doc, mnuObj, bSplitItem, bLastItem, caption, linkUrl) {    
    g_idItemLayer += 1;
    g_nNumberOfMenuItemObjects += 1;
    g_MenuItemObjects[g_nNumberOfMenuItemObjects] = this;
    this.id = "mi" + g_idItemLayer;
    this.idImg = "img" + this.id;
    this.idTxt = 'txt' + this.id;
    this.curButton = "MenuItem";
    this.initMenus= initMenus;
    this.initButton = initButton;
    this.createShade = createShade;
    this.hideMenuItem = hideMenuItem; //zzz
    this.showMenuItem = showMenuItem; //zzz
    this.setLeft = setMenuItemLeft;
    this.doc = doc;
    this.wdt = 0;
    this.isSelection = !bSplitItem;
    if (bSplitItem) {
        caption = "";
    }

    var y = 0;
    if (bSplitItem) {
        this.curButton = "ItemSplitter";
        y = this.initMenus(mnuObj, g_nMenuItemSplitterHeight);
    } else {
        if (bLastItem) {        
            this.curButton = "BottomItem";
            y = this.initMenus(mnuObj, g_nMenuHeight);
        } else {
            if (mnuObj.menuItem == null) {
                this.curButton = "TopItem";
                y = this.initMenus(mnuObj, g_nMenuHeight);
            } else {
                y = this.initMenus(mnuObj, g_nMenuItemHeight);
            }
        }
    }
            
    this.initButton(y, caption, linkUrl); 
    if (bLastItem) {
        this.wdt = this.mnuObject.iWidth;
        this.createShade(this.mnuObject.iWidth+1, y + 1 + this.yOffset);
    }

    return this;
}


// --------------------------------------------------------------
function createTopMenuItemObject(doc, mnuObj, caption, linkUrl) {    
    g_idItemLayer += 1;
    g_nNumberOfMenuItemObjects += 1;
    g_MenuItemObjects[g_nNumberOfMenuItemObjects] = this;
    this.id = "mi" + g_idItemLayer;
    this.idImg = "img" + this.id;
    this.idTxt = 'txt' + this.id;
    this.curButton = "TopItemCaption";
    this.initMenus= initMenus;
    this.initButton = initButton;
    this.createShade = createShade;
    this.hideMenuItem = hideMenuItem; //zzz
    this.showMenuItem = showMenuItem; //zzz
    this.setLeft = setMenuItemLeft;
    this.doc = doc;
    this.isSelection = false;
    var y = this.initMenus(mnuObj, g_nMenuHeight);            
    this.initButton(y, caption, linkUrl); 
}
