brand.globalnav.Header = Class.create(Widget, 
{
    // summary:
    //      Renders header image for item that doesn't open a Panel or Accordion       
    templatePath : "headerTemplate",
    templateGroup : {
        href :
        '<li id="#{id}" class="link_hd">'+
            '<a href="#{url}" class="sprite" style="background-image:url(#{hdPath})" attachPoint="hdNode">#{displayName}</a>'+
        '</li>'
    },

    templateGroupDIV : {    
        href : 
        '<div id="#{id}" class="link_hd">'+
            '<a href="#{url}" class="sprite sprite-on" style="background-image:url(#{hdPath})" attachPoint="hdNode">#{displayName}</a>'+
        '</div>'
    },

    // 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
        var templateType = this.templateGroup;
        var preNode = $(args.id);
        if (preNode) {
            var parentNode = preNode.parentNode;
        } else {
            var parent = $(args.parentId).widget; 
            if (parent) {
                var parentNode = (parent.containerNode ? parent.containerNode : parent.domNode);
            }
        }  
        
        /**eg Gift Card **/
        if (parentNode && parentNode.nodeName === "DIV") { 
            templateType = this.templateGroupDIV;
        }

        // set template string by href vs. no href
        this.templateString = templateType.href;
        this.removeLink = (args.isdefault || !args.url);  
        
        //console.log("brand.globalnav.Header "+args.id);
        $super();
        this.startup();
    },

    startup: function() {
    //console.log("STARTUP id = "+this.id+" isdefault = "+this.isdefault);
        if (this.removeLink) {
             this.hdNode.removeAttribute("href");
             this.hdNode.addClassName("unclickable");
        }    
        
        if (this.isdefault) {
           this.setDefaultState();
        } 
    },
    
/*   
    startup: function() {
console.log("STARTUP id = "+this.id+" isdefault = "+this.isdefault);
        // if image is *not* already declared in page  
        // SS: seems moot in new context since there are no declarative instances
        //if (!this.hasLoaded || !this.isdefault) {   
            //  set "default" state (on/sel)
            //if (this.isdefault) {
            //    this.hdNode.addClassName("sprite-on");
            //}
       // }


        if (this.removeLink) {
             this.hdNode.removeAttribute("href");
             this.hdNode.addClassName("unclickable");
        }    
        
        if (this.isdefault) {
           this.setDefaultState();
        } 
    },
    */
     
    _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);
        }
    },
    
    setDefaultState: function() {
        // summary:
        //
        //      Handles state of header image in left nav associated with default/open panel

        //  set "default" state (on/sel)
        this.hdNode.addClassName("sprite-on");
        
        var parentId = this.parentId;        
        if (parentId.indexOf("psubnav") != -1) { return; } // ignore header instances inside panelsubnav 
                
        var parent = $(parentId).widget;
        var accordionId = "";

        // 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") { 
            $(this.id).addClassName("clickable");
            //console.log("globalnav.Header.setDefaultState: "+this.id + "/" + accordionId +" / "+ parent.id); 
            var self = this;
            var onclick = function() {
                self._showDefault(accordionId, parent);
            }
             
            $(this.id).observe("click", onclick );
        } else {
            
            
        }
    }
    
});