// ===============================================================
// CBkort version 2.x, copyright Carl Bro GIS&IT, 2006
// ===============================================================
// $Archive: /Products/CBKort2/development/2.3/standard_patch_03/wwwroot/js/standard/cbmenu.js $ 
// $Date: 21-01-08 9:24 $
// $Revision: 5 $ 
// $Author: Kpo $
// =============================================================== 

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);
                hideBlock(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);
                hideBlock(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);
                showBlock(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);
    hideBlock(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+"\");");
        }
    }
}

// Set default images on all buttons
MenuBar.prototype.resetButtons = function()
{
    for (var k in this.buttons)
    {
        var b = this.buttons[k];
        b.button.src = b.normalImage;
    }
}

var menuBar;



// Create new togglebuttons. 
// Buttonid is the name of the tool
// Buttonstatus defines the default state of the togglebutton
function ToggleButton(buttonid, buttonstatus, onHandler, offHandler)
{
    this.buttonid = buttonid;
    this.buttonstatus = buttonstatus;
    this.offHandler = offHandler;
    this.onHandler = onHandler;
    this.set();
}

// Use toggle() when the button is clicked 
ToggleButton.prototype.toggle = function()
{
    this.buttonstatus = !this.buttonstatus;
    setCookie(this.buttonid, this.buttonstatus, forever());
    this.set();
    this.callHandlers();
}

// Call loadCookie() onload to use the cookie status
ToggleButton.prototype.loadCookie = function()
{
    var cookie = getCookie(this.buttonid);
    if (cookie=="true")
        this.buttonstatus = true;
    else if (cookie=="false")
        this.buttonstatus = false;
    this.set();
    this.callHandlers();
}

ToggleButton.prototype.set = function()
{
    try {
        menuBar.setCheckValue('togglebutton_'+this.buttonid, this.buttonstatus);
    } catch (e) {}
}
ToggleButton.prototype.callHandlers = function()
{
    if(this.buttonstatus)
    {
        if(this.onHandler)
            this.onHandler(this.buttonstatus);
    }
    else
    {
        if(this.offHandler)
            this.offHandler(this.buttonstatus);
    }
}
