brand.globalnav.Header = Class.create(Widget, 
{
    // summary:
    //      Renders header image for item that doesn't open a Panel or Accordion  
    templateLi: "jsTemplates.globalnav.headerLi",
    templateDiv: "jsTemplates.globalnav.headerDiv",

    // displayName: String
    displayName: "",
    
    // hdPath: String
    // header img src
    hdPath: "",

    // description: String
    description: "",
    
    // url: String (optional)
    // template href value
    url: "",

    // isdefault: Boolean
    // Item is (section, page, etc...) associated with default page state display
    isdefault: false,
    
    // hasLoaded: Boolean
    // have template nodes loaded in DOM
    // declarative instance = true
    hasLoaded: false,
    
    // parentId: String (optional)
    // parent object id
    // used to check status of other menus in a set (ex: if this is default item & clicking on it should close open menus to bring focus back to default)
    parentId: "",

    initialize: function($super, args) {
        this.setProperties(args);
        // set template group based on parent node element type
        this.templatePath = this.templateLi;
        var preNode = $(args.id);
        if (preNode) {
            var parentNode = preNode.parentNode;
        } else {
            var parentNode = $(args.parentId);
            if (!parentNode) {
                console.log("brand.globalnav.Header: Node UNDEFINED for args.parentId: "+args.parentId);
                return false;
            }
            var parent = parentNode.widget; 
            if (parent) {
                var parentNode = (parent.containerNode ? parent.containerNode : parent.domNode);
            }
        }  
        
        // eg Gift Card
        if (parentNode && parentNode.nodeName === "DIV") { 
            this.templatePath = this.templateDiv;
        }

        this.removeLink = (args.isdefault || !args.url);
        
        //console.log("brand.globalnav.Header "+args.id);
        $super();
        this.startup();
    },

    startup: function() {
        //console.log("STARTUP id = "+this.displayName+" isdefault = "+this.isdefault);
        if (this.removeLink) {
            var linkNode = $$("#"+this.id+" a")[0];
            if (linkNode) {
                linkNode.removeAttribute("href");
                linkNode.addClassName("unclickable"); 
                this.containerNode.removeClassName("clickable");
            }
        }    

        // if image is *not* already declared in page
        //console.log("Header: this.hasLoaded = "+this.hasLoaded);
        if (!this.hasLoaded || !this.isdefault) {
            var hdNode = this.hdNode;
            this.hdImg = new brand.img(hdNode, ["off","on","sel"]);
        }
        
        if (this.isdefault) {
           this.setDefaultState();
        } else {
            // set up mouseover events
            this.domNode.observe("mouseover", this._onMouseOver.bind(this));
            this.domNode.observe("mouseout", this._onMouseOut.bind(this));
        }
    },
     
    _showDefault: function(accordionId, parent) {
        //console.log("globalnav.Header._showDefault "+accordionId + " / " + parent); 
        // tell parent set to bring focus back to default
        if (parent.onChildClick && (parent.activeItemId !== "")) {
            parent.onChildClick(accordionId, true);
        }
    },
    
    _onMouseOver: function(e) {
        if (this.hdImg) {
            this.hdImg.changeSrc("on");
        }
    },
    
    _onMouseOut: function(e) {
        if (this.hdImg) {
            this.hdImg.changeSrc("off");
        }
    },
    
    setDefaultState: function() {
        // summary:
        //
        //      Handles state of header image in left nav associated with default/open panel

        //  set "default" state (on/sel)
        if (this.hdImg) this.hdImg.changeSrc("sel");
        
        var parentId = this.parentId;        
        if (parentId.indexOf("psubnav") != -1) { return; } // ignore header instances inside panelsubnav 
                
        var parent = $(parentId).widget;
        var accordionId = "";
        //console.log("globalnav.Header.setDefaultState: "+this.id + "/ parentId = "+parentId+" parent = "+parent); 
        // if parent is accordion in left nav, globalnav_container will be 1 level-up
        if (parentId !== "globalnav_container") {
            accordionId = parentId;
            parentId = parent.parentId;
            parent = $(parentId).widget;
        }
        if (parentId === "globalnav_container") { 
            //console.log("globalnav.Header.setDefaultState: "+this.id + "/" + accordionId +" / "+ parent.id); 
            var self = this;
            var onclick = function() {
                self._showDefault(accordionId, parent);
            }
             
            this.domNode.observe("click", onclick );   
        }
    }
    
});
