try
{
    wedebug.addLogger("menu", "Menu");
}
catch(e)
{
    alert(e.message);
}

/**
Class for controlling an expandable menu.

Requires YUI animation libraries.
*/
var MenuController = Class.create();
MenuController.prototype = {
    m_menuFrame : null,
    m_menuDiv : null,
    m_onMenuExpandHandler : null,
    m_onMenuTruncateHandler : null,
    m_onMenuToggleHandler : null,
    m_expanded : true,
    m_animExpand : null,
    m_animTruncate : null,
    m_animToggleExpand : null,
    m_animToggleTruncate : null,
    m_mapController : null,
    m_navigationMenuWidth : 80, //HACK: Should be retrieved from controller.
    measureButtonText : "Start måling",
    measureEnabled : false,
    coordinateButtonText : "Vis koordinat",
    coordinateEnabled : false,
    
    initialize : function( mapController, menuFrame, expanded, menudiv, menuAnimationExpand, menuAnimationTruncate, toggleAnimateExpand, toggleAnimatTruncate)
    {
        try
        {
        wedebug.log("menu", "-> initialize: " + [mapController, menuFrame, expanded, menudiv, menuAnimationExpand, menuAnimationTruncate, toggleAnimateExpand, toggleAnimatTruncate]);
        
        this.m_mapController = mapController;
        this.m_mapController.addEventListener("onresize", this._cb_fitLeftMenu);
        
        this.m_menuFrame = menuFrame;
        this.m_menuDiv = menudiv;
        //this.m_menuToggleDiv = toggleDiv;
        
        if( this.m_menuDiv == null )
        {
            var msg = "argument menudiv cannot be null.";
            wedebug.log("menu", msg, "error");
            throw {message:msg};
        }
//        if( this.m_menuToggleDiv == null )
//        {
//            var msg = "argument toggleDiv cannot be null.";
//            wedebug.log("menu", msg, "error");
//            throw {message:msg};
//        }
        }catch(e){alert(e.message);}
        //store animations
        this.m_animTruncate = menuAnimationTruncate;        
        this.m_animExpand = menuAnimationExpand;
        this.m_animToggleExpand = toggleAnimateExpand;
        this.m_animToggleTruncate = toggleAnimatTruncate;        
        
        //Subscribe to complete events to fire local event trigger
        this.m_animExpand.onComplete.subscribe(
            (function()
            {
                this.m_onMenuExpandHandler.fire();
                this.m_onMenuToggleHandler.fire(this.m_expanded);
                this.m_menuDiv.style.display = "block";
                
                var t_menuWidth = this.getMenuWidth();
                this.m_mapController.internal_setViewOffset(
                    {
                        x : t_menuWidth + this.m_navigationMenuWidth,
                        y : 0,
                        navigationMenuWidth : this.m_navigationMenuWidth,
                        menuWidth : t_menuWidth
                    }
                );  
            }).bind(this)
        );
        
        this.m_animTruncate.onComplete.subscribe(
            (function()
            {
                this.m_onMenuTruncateHandler.fire();
                this.m_onMenuToggleHandler.fire(this.m_expanded);
                
                //this.m_menuDiv.style.display = "none";
            }).bind(this)
        );
        
//        Event.observe( this.m_menuToggleDiv, "click", 
//            (function(event) {
//                this._toggleMenu();
//                Event.stop(event);
//            }).bind(this)
//        );
                
        //Truncate menu if arg is false
        var doTruncate = (typeof expanded != "undefined")? expanded == false : false;
        if( doTruncate ) 
        {
            this.truncate();
        }
        else
        {
            //Set map controller view offset
            var t_menuWidth = this.getMenuWidth();
            this.m_mapController.internal_setViewOffset(
                {
                    x : t_menuWidth + this.m_navigationMenuWidth,
                    y : 0,
                    navigationMenuWidth : this.m_navigationMenuWidth,
                    menuWidth : t_menuWidth
                });
        }
        
        this.m_onMenuExpandHandler = this.addEventType("onmenuexpand");  
        this.m_onMenuTruncateHandler = this.addEventType("onmenutruncate");  
        this.m_onMenuToggleHandler = this.addEventType("onmenutoggle");
        this._cb_fitLeftMenu();
    },
    
    setMeasureButtonText : function(){
        if(this.measureEnabled){
            this.measureEnabled = false;
            this.m_menuFrame.$('coord').disabled = false;
            this.measureButtonText = "Start m&aring;ling";
        }
        else{
        this.measureEnabled = true;
        this.m_menuFrame.$('coord').disabled = true;
            this.measureButtonText = "Avbryt m&aring;ling";
        }
       this.setDistanceHTML("Start m&aring;ling ved &aring; klikke p&aring; <i>Start m&aring;ling</i>-knappen, deretter klikker du i kartet for &aring; markere m&aring;lepunkter. <img src='../../Resource/Map/images/gfx2/poi/measPoint.gif' />  ");
    },
    
    setCoordinateButtonText : function(){
        if(this.coordinateEnabled){
            this.coordinateEnabled = false;
            this.m_menuFrame.$('measure').disabled = false;
            this.coordinateButtonText = "Vis koordinat";
        }
        else{
        this.coordinateEnabled = true;
            this.m_menuFrame.$('measure').disabled = true;
            this.coordinateButtonText = "Avbryt visning";
        }
       this.setCoordinateHTML("Klikk f&oslash;rst p&aring; <i>vis koordinat</i>-knappen og deretter i kartet for &aring; se koordinatene. ");
    },
    
    updateSearchResult : function(result){
        this.m_menuFrame.$('searchResults').innerHTML = result; 
    },
    
    setDistanceHTML : function(html){
        this.m_menuFrame.$('malinger').innerHTML = html + '<br /><br /><button id="measure" onclick="javascript: window.parent.map.setState();window.parent.menuController.setMeasureButtonText();">'+ this.measureButtonText+'</button>';
    },
    
    setCoordinateHTML : function(html){
        this.m_menuFrame.$('coords').innerHTML = html + '<br /><br /><button id="coord" onclick="javascript: window.parent.map.setState(\'showcoordinate\');window.parent.menuController.setCoordinateButtonText();">'+ this.coordinateButtonText+'</button>';
    },
    
    expand : function()
    {
        wedebug.log("menu", "-> expand()");
        if( !this.m_expanded ) 
        {
            this.toggleMenu();
            
            var t_menuWidth = this.getMenuWidth();
            this.m_mapController.internal_setViewOffset(
                {
                    x : t_menuWidth + this.m_navigationMenuWidth,
                    y : 0,
                    navigationMenuWidth : this.m_navigationMenuWidth,
                    menuWidth : t_menuWidth
                });
        }
    },
    
    truncate : function()
    {
        wedebug.log("menu", "-> truncate()");
        if( this.m_expanded )
        {
            this.toggleMenu();
            
            var t_menuWidth = this.getMenuWidth();
            this.m_mapController.internal_setViewOffset(
                {
                    x : this.m_navigationMenuWidth,
                    y : 0,
                    navigationMenuWidth : this.m_navigationMenuWidth,
                    menuWidth : 0
                });
        }
    },
    
    toggleMenu : function()
    {
        wedebug.log("menu", "-> toggleMenu()");
        if( this.m_expanded ) //truncate
        {
            this.m_mapController.internal_setViewOffset(
                    {
                        x : 0,
                        y : 0,
                        navigationMenuWidth : 0,
                        menuWidth : 0
                    }
                );
        
            this.m_menuDiv.style.display = "none";
        
            this.m_animTruncate.animate();  
            this.m_animToggleTruncate.animate();         
            this.m_expanded = false;
        }
        else //expand
        {
            this.m_animExpand.animate();   
            this.m_animToggleExpand.animate();                     
            this.m_expanded = true; 
        }        
    },
    
    _cb_fitLeftMenu : function(t_fullscreen, t_width, t_newHeight)
    {
        if( t_newHeight == undefined )
        {
            var t_size = Utilities.Window.getSize();
            var t_height = t_size.height; //document.body.clientHeight;
            t_height -= $('mapWrapper').offsetTop - 6;
            
            $('leftmenu').style.height = t_height -90+"px";
            $('menuFrame').style.height = t_height -90+ "px";
        }
        else
        {
            $('leftmenu').style.height = t_newHeight -90+"px";
            $('menuFrame').style.height = t_newHeight -90+ "px";
        }
    },
    
    getMenu : function(){ return ((typeof this.m_menuFrame != "undefined") ? this.m_menuFrame : null); },
    
    isVisible : function()
    {
        return this.m_expanded;
    },
    
    getMenuWidth : function()
    {
        //TODO: Create animations inside this object and make width configurable.
        return 340;
    }
}
Object.extend(MenuController.prototype, new AbstractEventTrigger()); //Extend with methods to make object an eventtrigger

