// ===============================================================
// CBkort version 2.x, copyright Carl Bro GIS&IT, 2006
// ===============================================================
// $Archive: /Products/CBKort2/development/2.3/standard/wwwroot/js/standard/cbmenu.js $ 
// $Date: 27-04-07 10:53 $
// $Revision: 1 $ 
// $Author: Nsm $
// =============================================================== 

var mbdirHorizontal = 0;
var mbdirVertical = 1;

function MenuBar(id,hintElementName)
{
    this.barId = id;
    this.nButtons = 0;
    this.buttons = new Object();
    this.direction = mbdirHorizontal;
    if (hintElementName)
        this.hintElement = getElement(hintElementName);
}

MenuBar.prototype.addButton = function(button)
{
    this.buttons[button.buttonId] = button;
}

MenuBar.prototype.setCheckValue = function(id,value)
{
    var checkMark = getElement(id);
    if (!id)
        alert("setMenuValue: id "+id+" ikke fundet");
    if (value)
        if (nn6)
            writeHTML(checkMark,String.fromCharCode(0x221A));
        else
            writeHTML(checkMark,"a");
    else
        writeHTML(checkMark,"&nbsp;&nbsp;");
}

MenuBar.prototype.hint = function(event,id)
{
    if (this.hintElement)
    {
        var butt = this.buttons[id];
        if (butt&&butt.hintText)
        {
            var xy = getCoords(event);
            setY(this.hintElement,xy[1]+20);
            setX(this.hintElement,xy[0]);
            writeHTML(this.hintElement,butt.hintText);
            showElement(this.hintElement);
        }
    }
}

MenuBar.prototype.noHint = function(event)
{
    if (this.hintElement)
    {
        hideElement(this.hintElement);
    }
}

MenuBar.prototype.clearMenus = function(id)
{
    for (var k in this.buttons)
    {
        var b = this.buttons[k];
        if (b.menuId)
        {
            var mnu1 = getElement(b.menuId);
            if (isVisible(mnu1)&&b.menuId!=id)
            {
                hideElement(mnu1);
            }
        }
    }
}

MenuBar.prototype.click = function(event,id)
{
    var butt = this.buttons[id];
    if (butt)
    {
        this.clearMenus(butt.menuId);
        if (butt.pressedImage)
        {
            // depress all buttons
            for (var k in this.buttons)
            {
                var b = this.buttons[k];
                if (b.pressedImage&&b.button.src!=b.normalImage)
                    b.button.src = b.normalImage+'?' + (new Date()).getTime();
                    // Trick to have it reloaded
            }
            // press this button
            butt.button.src = butt.pressedImage;
            setCookie("button",butt.cookieId);
        }
        if (butt.handler)
            butt.handler();
        if (butt.menuId)
        {
            var mnu = getElement(butt.menuId);
            if (isVisible(mnu))
            {
                hideElement(mnu);
            }
            else
            {
                if (this.hintElement)
                {
                    hideElement(this.hintElement);
                }
                if (this.direction==mbdirVertical)
                {
                    setX(mnu,getX(butt.button)-getElement(butt.button).offsetWidth);
                    setY(mnu,getY(butt.button));
                }
                else
                {
                    setX(mnu,getX(butt.button));
                    //setY(mnu,getY(butt.button)+getElement(this.barId).offsetHeight);
                }
                showElement(mnu);
            }
        }
    }
}

MenuBar.prototype.update = function()
{
    var pressed = getCookie("button");
    if (pressed)
    {
        for (var b in this.buttons)
        {
            var butt = this.buttons[b];
            if (butt.cookieId==pressed)
            {
                butt.button.src = butt.pressedImage;
                butt.handler();
                return;
            }
        }
    }
    if (this.defaultButton)
    {
        var butt = this.buttons[this.defaultButton];
        butt.button.src = butt.pressedImage;
        butt.handler();
    }
}

function clickButton(event)
{
    menuBar.click(event,this.id);
}

function buttonHint(event)
{
    menuBar.hint(event,this.id);
}

function noButtonHint(event)
{
    menuBar.noHint();
}

function Button(id)
{
    if (arguments.length>0)
        this.init(id);
}

function hideMenu(id)
{
    var mnu = getElement(id);
    hideElement(mnu);
}

Button.prototype.init = function(id)
{
    this.buttonId = id;
    this.button = getElement(id);
    this.normalImage = this.button.src;
    this.handler = null;
    this.button.onclick = clickButton;
}

Button.prototype.setHandler = function(func)
{
    this.handler = func;
}

Button.prototype.setHint = function(hintText)
{
    this.hintText = hintText;
    this.button.onmouseover = buttonHint;
    this.button.onmouseout = noButtonHint;
}

StateButton.prototype = new Button();
StateButton.prototype.constructor = StateButton;
StateButton.superclass = Button.prototype;


function StateButton(id,pressedImage,cookieId,isDefault)
{
    if (arguments.length>0)
    {
        this.init(id);
        this.pressedImage = pressedImage;
        this.cookieId = cookieId;
        if (isDefault)
            menuBar.defaultButton = id;
    }
}

StateButton.prototype.setDefaultButton = function()
{
    menuBar.defaultButton = this.buttonId;
}



MenuButton.prototype = new Button();
MenuButton.prototype.constructor = MenuButton;
MenuButton.superclass = Button.prototype;


function MenuButton(id,menuId)
{
    if (arguments.length>0)
    {
        this.init(id);
        this.menuId = menuId;
        var mnu = getElement(this.menuId);
        var arr = mnu.getElementsByTagName("a");
        for (var i=0;i<arr.length;i++)
        {
            arr[i].onclick=new Function("hideMenu(\""+menuId+"\");");
        }
    }
}

// Kaldes for nulstille src på buttons
MenuBar.prototype.resetButtons = function()
{
    for (var k in this.buttons)
    {
        var b = this.buttons[k];
        b.button.src = b.normalImage;
    }
}

var menuBar;
