/*     
     File:        icMenuObject.js
     Created:     2009-06-16
     Modified:    2009-09-24
     Copyright:   Copyright (c) InfoCaption AB 2009
     Author:      Peter Jäderlund
     Description: Menu objects
*/

var g_nMenuHeight = 25;
var g_idLayer = -1;
var g_MenuObjects = new Array(); // For each page store the "Done/NotDone"  pj-add
var g_nNumberOfMenuObjects = 0;
var g_nMenuTimeout = 1000;

// --------------------------------------------------------------
function getMenuObject(id) { 
    for (var i = 1; i <= g_nNumberOfMenuObjects; i++) {
        var mnuObj = g_MenuObjects[i];
        if (mnuObj.id == id) {
            return mnuObj;
        }
    }
    return null;
}


// --------------------------------------------------------------
function getTopMenu(mnu) {
    while (mnu.className != "MenuObject") {
        mnu = mnu.mnuObject;
    }
    return mnu;
}


// --------------------------------------------------------------
function getFirstMenuItem(mnu) {
    var res = mnu;
    if (mnu.className == "MenuObject") {
        res = mnu.menuItem;
    } else {
        var mnuParent = mnu.mnuObject;
        if (mnuParent.className == "MenuObject") {
            res = mnuParent.menuItem;
        } else {
            res = mnuParent.childMenu;
        }
    }
    return res;
}


// --------------------------------------------------------------
function hideMenusAbove(mnu) {     
    if (mnu == null) {
        return;
    }
    
    mnuCur = getFirstMenuItem(mnu);
    while (mnuCur != null) {
       if (elementIsActive(mnuCur.doc, mnuCur.idImg)) {
           return;
       }
       mnuCur = mnuCur.nextMenuItem;
    }
    
    var mo = getTopMenu(mnu);
    if (mo == mnu) {
        mo.hideMenu();    
    } else {
        mo.hideMenu(getFirstMenuItem(mnu));    
        hideMenusAbove(mnu.mnuObject);
    }
}


// --------------------------------------------------------------
function childIsActive() {      
    var mnu = this.menuItem; 
    while (mnu != null) {
        if (mnu.childMenu != null) {
            if (elementIsVisible(mnu.childMenu.doc, mnu.childMenu.id)) {
                return true;
            }
        }
        mnu = mnu.nextMenuItem;    
    }
    return false;
}


// --------------------------------------------------------------
function timeOut(id) {         
    var mnuObject = getMenuObject(id);
    if (mnuObject == null) {
        return;
    }
    mnuObject.stopTimer();
    if (mnuObject.useTimer == false) {
       return;
    }
    
    if (mnuObject.childIsActive()) {
        mnuObject.startTimer();
    } else {
        if (mnuObject.className == "MenuObject") {
            mnuObject.hideMenu();
        } else {
            var mo = getTopMenu(mnuObject);
            if (mo.closeTimer == 90) {
                mo.hideMenu();
            } else {
                mo.hideMenu(mnuObject.childMenu);
                hideMenusAbove(mnuObject);
            }
        }
    }
}


// --------------------------------------------------------------
function nada() {

}


// --------------------------------------------------------------
function startTimer() {
    if (this.closeTimer != 0) {
        clearTimeout(this.closeTimer);
    }
    this.closeTimer = setTimeout("timeOut('" + this.id + "');", g_nMenuTimeout);
}


// --------------------------------------------------------------
function stopTimer() {
    clearTimeout(this.closeTimer);
    this.closeTimer = 0;
}

// --------------------------------------------------------------
function reStartTimer() {
    this.stopTimer();
    this.startTimer();
}


// -------------------------------------------------------------- //zz new
function OnMouseOverMenuObject(id) { 
  var mnuObject = getMenuObject(id);
  
  var btn = document.getElementById("bx" + mnuObject.idLayer);
  btn.style.left = "-" + mnuObject.wdt + "px";

  var txt = document.getElementById("txt" + mnuObject.id);
  txt.className = "menuobjectfontactive";
  hideOtherMenus(mnuObject);
}

    
// --------------------------------------------------------------
function hideOtherMenus(mnuObj) { 
    for (var i = 1; i <= g_nNumberOfMenuObjects; i++) {
        if (mnuObj != g_MenuObjects[i] && g_MenuObjects[i].className == "MenuObject") {
            g_MenuObjects[i].hideMenu();
        }
    }
}


// -------------------------------------------------------------- //zz new
function OnMouseOutFromMenuObject(id) { 
    var mnuObject = getMenuObject(id);
    
    var btn = document.getElementById("bx" + mnuObject.idLayer);
    btn.style.left = "0px";
    
    var txt = document.getElementById("txt" + mnuObject.id);
    txt.className = "menuobjectfontnormal";
}


// --------------------------------------------------------------
function OnClickOnMenuObject(id) {                
    var mnuObject = getMenuObject(id);
    if (mnuObject.url != "" || mnuObject.url != null) {
        eval(mnuObject.url);
    }

    if (mnuObject.menuItem != null) {
        mnuObject.showMenu();
    }
}


// --------------------------------------------------------------
function hideMenu(mnu) {
    if (this.useTimer) { 
        this.stopTimer();
    }
    if (mnu == null) {
        mnu = this.menuItem; 
    }
    while (mnu != null) {
        mnu.hideMenuItem();         //zzz
        if (mnu.childMenu != null) {
            hideMenu(mnu.childMenu);
        }
        mnu = mnu.nextMenuItem;    
    }
}


// --------------------------------------------------------------
function showMenu(mnu) {
    if (mnu == null) {
        mnu = this.menuItem;
    }            
    while (mnu != null) {
        mnu.showMenuItem();         //zzz
        mnu = mnu.nextMenuItem;    
    }
    if (this.useTimer) {
        var mnuObject = getMenuObject(this.id);
        if (mnuObject == null) {
            g_nNumberOfMenuObjects += 1;
            g_MenuObjects[g_nNumberOfMenuObjects] = this;                                     
        }
        this.startTimer();
    }
}


// --------------------------------------------------------------
function setMenuObjectLeft(x) { 
    var mnu = this.menuItem;

    // Move the button
    var obj = this.doc.getElementById(this.idLayer);
    obj.style.left = x + "px";

    // Move the text
    obj = this.doc.getElementById("txt" + this.id);
    obj.style.left = x + "px";


    // Move the shade
    obj  = this.doc.getElementById(this.id + "s1"); 
    obj.style.left = (x + this.wdt - 3) + "px";
    
    obj  = this.doc.getElementById(this.id + "s2"); 
    obj.style.left = (x + this.wdt - 3) + "px";
    
    obj  = this.doc.getElementById(this.id + "s3"); 
    obj.style.left = x + "px";


    obj  = this.doc.getElementById(this.id + "s4"); 
    obj.style.left = (x + this.wdt - 7) + "px";
    
    obj  = this.doc.getElementById(this.id + "s5"); 
    obj.style.left = (x + 11) + "px";

    // Move sub items...
    while (mnu != null) {
        mnu.setLeft(x);
        mnu = mnu.nextMenuItem;    
    }   

}


// --------------------------------------------------------------
function writeShade(doc, id, imgFileName, x, y, wdt, hgt) {
    x = x + 1;
    y = y + 1;
    doc.write("<div id='" + id + "' style='position:absolute;left:" + x + "px;top:" + y + "px;width:" + wdt + "'>");
    doc.write("  <img src='" + imgFileName + "' border=0 width='" + wdt + "' height='" + hgt + "'></img>");
    doc.write("</div>"); 
}


// --------------------------------------------------------------
function createMenuObject(doc, x, y, wdt, hgt, caption, linkUrl, prevMenu) {
    this.nextMenu = null;
    this.prevMenu = prevMenu;
    this.menuItem = null;
    this.url = linkUrl;
    this.iWidth = wdt;
    this.wdt = wdt;
    this.iTop = y;
    this.iLeft = x;
    this.showMenu = showMenu;
    this.hideMenu = hideMenu;
    this.className = "MenuObject";
    this.closeTimer = 0;
    this.useTimer = false;
    this.startTimer = startTimer;
    this.stopTimer = stopTimer;
    this.reStartTimer = reStartTimer;
    this.childIsActive = childIsActive;
    this.defaultVisibility = "hidden";
    this.setLeft = setMenuObjectLeft;
    this.doc = doc;
    
    g_idLayer += 1;
    this.id = "mo" + g_idLayer;
    this.idLayer = g_idLayer;
    this.curButton = "SingleButton" + wdt;
    var idImg = "img" + this.id;
    var idTxt = "txt" + this.id;
    g_nNumberOfMenuObjects += 1;
    g_MenuObjects[g_nNumberOfMenuObjects] = this;                                     

    // The shade
    writeShade(doc, this.id + "s1", "images/ShadeRightTop.png", x + wdt -3, y, 10, 11);
    writeShade(doc, this.id + "s2", "images/ShadeRight.png", x + wdt -3, y + 11, 10, hgt - 17);
    writeShade(doc, this.id + "s3", "images/ShadeBottomLeft.png", x, y + hgt - 6, 11, 12);
    writeShade(doc, this.id + "s4", "images/ShadeBottomRight.png", x + wdt - 7, y + hgt - 6, 14, 12);
    writeShade(doc, this.id + "s5", "images/ShadeBottom.png", x + 11, y + hgt - 6, wdt - 18, 12);
                  
    // The button
    doc.write("<div id='" + g_idLayer + "' style='position:absolute;left:" + x + "px;top:" + y + "px;width:" + wdt + "px;height:" + hgt + 
              "px;overflow:hidden;cursor:pointer'>");
    var wdt2 = wdt * 2;
    doc.write("  <div id='bx" + g_idLayer + "' style='width:" + wdt2 + "px;height:" + hgt + "px;position:relative;left:0px;top:0px;overflow:hidden'>"); //zz new
    doc.write("    <img id='" + idImg + "' src='images/" + this.curButton + ".png' border=0 " +
              "      style='cursor:pointer' " + 
              "      OnClick='OnClickOnMenuObject(\"" + this.id + "\")' " +
              "      OnMouseOver='OnMouseOverMenuObject(\"" + this.id + "\")' " +
              "      OnMouseOut='OnMouseOutFromMenuObject(\"" + this.id + "\")' >");
    doc.write("    </img>");          
    doc.write("  </div>");  //zz new
    doc.write("</div>");
                                                                                      
    // The caption
    doc.write("<div id='" + idTxt + "' style='position:absolute;left:" + (x+5) + "px;top:" + (y+3) + "px;width:" + 
              (wdt-10) + ";overflow:hidden;cursor:pointer' class='menuobjectfontnormal'" +
              "    OnClick='OnClickOnMenuObject(\"" + this.id + "\")' " +
              "    OnMouseOver='OnMouseOverMenuObject(\"" + this.id + "\")' " +
              "    OnMouseOut='OnMouseOutFromMenuObject(\"" + this.id + "\")'>");
    doc.write   ("  <center>" + caption + "</center>");
    doc.write("</div>"); 
                                                                                                    
    return this;
}

// --------------------------------------------------------------
function createMenu(doc, x, y, wdt, caption, linkUrl) {
    if (this.firstMenu == null) {
        var mnu = new createMenuObject(doc, x, y, wdt, g_nMenuHeight, caption, linkUrl, null);
        this.firstMenu = mnu;
    } else {
       var lastMenu = this.firstMenu;
       while (lastMenu.nextMenu != null) {
           lastMenu = lastMenu.nextMenu;
       }           
       var mnu = new createMenuObject(doc, x, y, wdt, g_nMenuHeight, caption, linkUrl, lastMenu);
       lastMenu.nextMenu = mnu;
    }
    return mnu;
} 


// --------------------------------------------------------------
function createHiddenMenuContainer(x, y, wdt) {
    this.iWidth = wdt;
    this.defaultVisibility = "visible";
    this.iTop = y - g_nMenuHeight;
    this.iLeft = x;
    this.startTimer = nada;
    this.stopTimer = nada;
    this.hideMenu = nada;
    this.className = "MenuObject";
}

  
// --------------------------------------------------------------
function hideAllMenus() {  
    var obj = this.firstMenu;
    while (obj != null) {
        obj.hideMenu();
        obj = obj.nextMenu;
    }
}


// --------------------------------------------------------------
function createAllMenusObject(wdt) {  
    this.firstMenu = null;
    this.createMenu = createMenu;
    this.hideMenus = hideAllMenus;
}


// --------------------------------------------------------------
function createTopMenuItem(doc, mnuObj, caption, wdt) {
    var mnuItem = new createTopMenuItemObject(doc, mnuObj, caption, wdt);
}


// --------------------------------------------------------------
function createMenuItem(doc, mnuObj, caption, linkUrl) {
    var mnuItem = new createMenuItemObject(doc, mnuObj, false, false, caption, linkUrl);
    return mnuItem;
}


// --------------------------------------------------------------
function createMenuItemSplitter(doc, mnuObj) {
    var mnuItem = new createMenuItemObject(doc, mnuObj, true, false);
    return mnuItem;
}


// --------------------------------------------------------------
function createLastMenuItem(doc, mnuObj, caption, linkUrl) {
    var mnuItem = new createMenuItemObject(doc, mnuObj, false, true, caption, linkUrl);
    return mnuItem;
}


