dojo.require("site.globalnav.GlobalNav");   


/*** NAV CONFIG ***/   

site.globalnav.config = {
    items : [
        {
            id: "products",
            content: {
                url: "/includes/panel_nav/catalog.tmpl",
                param: "CATEGORY_ID"
            },
            template: {
                detail: {
                    // detail cell image params
                    hdDir: "/images/pnav/product/headers/",
                    thumbDir: "/product/images/56x56/"
                }
            },
            
            // supercategories
            items : [
                /*{
                    name : "Holiday",
                    id : "holiday",
                    cmcat : "CAT794",
                    header : "/images/gnav/gnav_holiday_157x18_off.gif",
                    content: { url: "/whats_new/holiday.tmpl" },
                    template: {
                        detail: { baseClass: "panelnav_cell_category" }
                    }
                },*/
                {
                    name : "What's New",
                    id : "whatsnew",
                    cmcat : "CAT794",
                    header : "/images/gnav/gnav_whatsnew_157x18_off.gif",
                    content: { url: "/whats_new/panel_nav.tmpl" },
                    template: { 
                        detail: { baseClass: "panelnav_cell_category" } // default baseClass. collections may also have alt baseClass types defined such as "panelnav_cell_header_only"
                    }
                },
                {
                    name : "Eyes",
                    id : "CAT148",
                    cmcat : "CAT148",
                    header : "/images/gnav/gnav_eyes_157x18_off.gif"
                },
                {
                    name : "Lips",
                    id : "CAT163",
                    cmcat :  "CAT163",
                    header : "/images/gnav/gnav_lips_157x18_off.gif"
                },
                {
                    name : "Face",
                    id : "CAT155",
                    cmcat : "CAT155",
                    header : "/images/gnav/gnav_face_157x18_off.gif"
                },
                {
                    name : "Prep+Primer",
                    id : "CAT172",
                    cmcat : "CAT172",
                    header : "/images/gnav/gnav_primer_157x18_off.gif"
                },
                {
                    name : "Studio",
                    id : "CAT1921",
                    cmcat :"CAT1921",
                    header : "/images/gnav/gnav_studio_157x18_off.gif",
                    // studio overlay
                    altContentToggle: {
                        url: "/studio/overlay.tmpl"
                    }
                },
                {
                    name : "Mineralize",
                    id : "CAT869",
                    cmcat :"CAT869",
                    header : "/images/gnav/gnav_mineralize_157x18_off.gif"
                },
                {
                    name : "Multi-use",
                    id : "CAT793",
                    cmcat : "CAT793",
                    header : "/images/gnav/gnav_multiuse_157x18_off.gif"
                },
                {
                    name : "Skincare",
                    id : "CAT176",
                    cmcat : "CAT176",
                    header : "/images/gnav/gnav_skincare_157x18_off.gif"
                },
                {
                    name : "Brushes",
                    id : "CAT144",
                    cmcat : "CAT144",
                    header : "/images/gnav/gnav_brushes_157x18_off.gif"
                },
                {
                    name : "Nails",
                    id : "CAT170",
                    cmcat : "CAT170",
                    header : "/images/gnav/gnav_nails_157x18_off.gif"
                },
                {
                    name : "Fragrance",
                    id : "CAT161",
                    cmcat : "CAT161",
                    header : "/images/gnav/gnav_fragrance_157x18_off.gif"
                },
                {
                    name : "Kit Essentials",
                    id : "CAT133",
                    cmcat : "CAT133",
                    header : "/images/gnav/gnav_kit_essentials_157x18_off.gif"
                },
                {
                    name : "Accessories",
                    id : "CAT139",
                    cmcat: "CAT139",
                    header : "/images/gnav/gnav_accessories_157x18_off.gif"
                },
 
                {
                    name : "Custom Palette",
                    id : "CAT791",
                    cmcat : "CAT791",
                    header : "/images/gnav/gnav_custompalette_157x18_off.gif",
                    uri: "/product/custom_palette.tmpl"
                }, 

                {
                    name : "Goodbyes",
                    id : "discontinued",
                    cmcat : "CAT789",
                    header : "/images/gnav/gnav_discontinuedproducts_157x18_off.gif",
                    content: { 
                        url: "/discontinued/panel_nav.tmpl",
                        param: "dquery",
                        // featured goodbyes data: should come from panel nav include
                        featured : {
                            "name" : "Featured Goodbyes", 
                            "id" : "featured_goodbyes",
                            "header" : "/discontinued/images/headers/pnav_featured_goodbyes_250x18_off.gif",
                            "uri" : "/product/featured_goodbyes.tmpl"
                        }
                    },
                    template: {
                        detail: {
                            type: "simpleDetail",
                            baseClass: "panelnav_cell_category"
                        }
                    },
                    search: {
                        formFieldId: "disc_search_input",
                        formSubmitId: "disc_search_submit"
                        //errorPopup: "pop_search_invalid"
                    }
                },
                {
                    name : "Gift Card",
                    id : "CAT792",
                    cmcat : "CAT792",
                    header : "/images/gnav/gnav_giftcard_157x18_off.gif",
                    content: { url: "/giftcards/panel_nav.tmpl" }
                } 
            ]
        },
        {
            id: "makeup_artistry",
            items : [
                {
                    name: "In The News",
                    id: "newsworthy",
                    header : "/images/gnav/gnav_newsworthy_157x18_off.gif",
                    uri: "/makeup_artistry/newsworthy.tmpl"         
                },
                {
                    name: "The Artists",
                    id: "artists",
                    cmcat : "CAT1048",
                    header : "/images/gnav/gnav_theartists_157x18_off.gif",
                    content: { 
                        url: "/makeup_artistry/artists/panel_nav.tmpl",
                        handleAs: "html",
                        cms: true // global nav writes out cms panel navs for default state
                    }         
                },
                {
                    name: "Looks",
                    id: "looks",
                    cmcat: "CAT81",
                    header : "/images/gnav/gnav_looks_157x18_off.gif",
                    content: { 
                        url: "/makeup_artistry/looks/panel_nav.tmpl",
                        widget: "ProductSubNav" // use ProductSubNav class instead of default PanelSubNav
                    }
                },
                {
                    name: "Artistry in Action",
                    id: "artistry_in_action",
                    cmcat : "260",
                    header : "/images/gnav/gnav_artistry_in_action_157x18_off.gif",
                    content: { url: "/makeup_artistry/artistry_action/panel_nav.tmpl" }           
                },
                {
                    name: "Faves",
                    id: "faves",
                    header : "/images/gnav/gnav_faves_157x18_off.gif",
                    content: {
                        url: "/makeup_artistry/faves/panel_nav.tmpl",
                        widget: "SectionDescSubNav"
                    },
                    template: {
                        detail: { baseClass: "panelnav_cell_category" }
                    }
                }, 
                {
                    name: "Email an Artist",
                    id: "email_an_artist",
                    cmcat: "1300",
                    header : "/images/gnav/gnav_email_an_artist_157x18_off.gif",
                    content: { url: "/makeup_artistry/email_artist/panel_nav.tmpl" },  
                    template: { 
                        detail: { type: "simpleDetail" }
                    }
                },
                {
                    name: "MAC Pro",
                    id: "macpro",
                    cmcat: "280",
                    header : "/images/gnav/gnav_macpro_157x18_off.gif",
                    content: { 
                        url: "/makeup_artistry/mac_pro/panel_nav.tmpl",
                        handleAs: "html"
                    }           
                }
            ]
        },
        {
            id: "giving_back",
            cmcat: "290",
            content: { url: "/giving_back/panel_nav.tmpl" },
            template: { 
                detail: {
                    type: "simpleDetail",
                    baseClass: "panelnav_cell_category"
                }
            }
        },
        {
            id: "my_mac",
            cmcat: "1000",
            handleAs: "html",
            hasLoaded: true // panel nav & subnav already placed into page
        },
        {
            id: "customer_service",
            cmcat: "1400",
            content: {
                url: "/customer_service/panel_nav.tmpl",
                handleAs: "html",
                cms: true
            }
        },
        {
            id: "search",
            cmcat: "1700",
            content: { 
                url: "/search/includes/panel_nav.tmpl",
                param: "query"
            },
            // for product search in includes/global_nav.tmpl
            search: {
                formFieldId: "search",
                formSubmitId: "search_button",
                errorPopup: "pop_search_invalid"
            }
        }
    ],
    
    // key for handling non-default content types as passed in data as item.type (via loader config or via included tmpl JSON)
    // EX: collections Detail modules use default baseClass "panelnav_cell_category" as well as "panelnav_cell_header_only"
    altTypes : {
        "header_only" : { 
            detail: { baseClass: "panelnav_cell_header_only" }
        },
        "simple_detail" : {
            detail: { template: "SimpleDetail" }
        }
    }
    
};


// CoreMetrics hash maps for later lookups in open in Accordion.js and Panel.js places here so they are generated once only.
function cmLookup() {
    var cm_map = {}; // combined map of all cmcats on all nav categories
    var cm_corrected_cat ={}; // map of ids to cmcats
    var skipcount = 0;
    var sections = site.globalnav.config.items;
    
    for (x in sections)
    {
    
      for (y in sections[x]){
        if (y === "items"){       
           var items = sections[x].items;
          for (var element in items){
            if (typeof(items[element].id)  != "undefined" && typeof(items[element].cmcat) != "undefined"){
              cm_map[items[element].id] = items[element].name;
            }
            if ( !(items[element].id.match("CAT[0-9]*")) ){
              cm_corrected_cat[items[element].id] = items[element].cmcat;   
            }
          }
        }else{
          if (typeof(sections[x].id)  != "undefined" && typeof(sections[x].cmcat) != "undefined"){
            if ( !(sections[x].id.match("CAT[0-9]*")) ){
                  cm_corrected_cat[sections[x].id] = sections[x].cmcat;
                }else{
                    cm_map[sections[x].id] = sections[x].name;
                }
           }
         } 
       }
         
    }
    return { map: cm_map, corrected_cat: cm_corrected_cat };
}
var cm_map_lookup = cmLookup();
var cm_map = cm_map_lookup.map; // combined map of all cmcats on all nav categories
var cm_corrected_cat = cm_map_lookup.corrected_cat; // map of ids to cmcats



/*** INIT NAV ***/
dojo.addOnLoad(function() {
    // get default page state 
    // structure: defaultState{ id: "", item: { id: "", item: {...} } }
    var defaultState = {};
    if (dojo.global.page_data && dojo.global.page_data.panel_nav) {
        defaultState = dojo.global.page_data.panel_nav["default"];
    }
    
    // in global_nav.tmpl
    dojo.global.globalNavSetId = "globalnav_container";

    // only build nav if globalnav containing html exists
    if (!dijit.byId(dojo.global.globalNavSetId) || !dojo.byId("globalnav")) {
        return;
    }
    
    // init entire nav    
    var gnav = new site.globalnav.GlobalNav({
        config: site.globalnav.config, // all nav items
        defaultState: defaultState, // default/open state data
        globalNavSetId: dojo.global.globalNavSetId
    });
    
    var cm = site.globalnav.Coremetrics;
    setTimeout(function() { cm.init(); },5000); //make sure cm functions don't load until panel is built for the first time
});


/*** COREMETRICS ***/

// yes horrible but not the time
cm_map['CAT1124'] = 'Brunette Blonde Redhead : Brunette Blonde Redhead Looks';
cm_map['CAT972']='Dame Edna : dameednalooks';
cm_map['CAT848']='Viva Glam : Viva Glam VI Special Edition Looks';

/* Added 20091006  */
cm_map['CAT1493']='Style Warrior : Style Warrior Looks';
cm_map['CAT1439']='A Rose Romance : A Rose Romance Looks';
cm_map['CAT1834']='Richard Phillips : Richard Phillips Looks';
cm_map['CAT1812']='Maira Kalman : Maira Kalman Looks';
cm_map['CAT1821']='Marilyn Minter : Marilyn Minter Looks';
cm_map['CAT1923']='Style Black : Style Black Looks';


site.globalnav.Coremetrics = {
    // summary:
    //      Centralize Coremetrics event tracking   
    
    init: function() {  
        // nav event listener
        dojo.subscribe("/panelnav/event/show", this, function(args) {
            // time out to give panel animation chance to run
            var self = this;
            var t = function() {
                if (args.type === "panel"){
                       self.panelClickOpen(args);
                } else if (args.type === "accordion") {
                       self.open(args);
                }
            }               
            
             setTimeout(t, 600);                      
        });
        /* if needed for close events
        dojo.subscribe("/panelnav/event/hide", this, function(args) {
            //console.log("/panelnav/event/hide, is passing = "+dojo.toJson(args, true));
            this.panelClickClose(args);
        }); 
        */       
    },
    
    open: function(args){
        if (typeof args.parentId != "undefined") {
            var ParentCatID = args.parentId.match("CAT[0-9]*");
            var SubCat = args.id.match("CAT[0-9]*");
            if (cm_map[ParentCatID]){
                cmCreatePageviewTag('MPP : ' + cm_map[ParentCatID]+ ' : '+args.displayName,'',SubCat,'');
            }else{
                ParentCatID = args.id.match("CAT[0-9]*");
                if (typeof cm_map[ParentCatID] != "undefined"){ // this supresses top level Products and Artistry
                    cmCreatePageviewTag('MPP : ' +cm_map[ParentCatID],'',SubCat,'');
                }
            }
        }
        this.ElementTag(args,"open"); 
    },

    close: function(){
        //console.log("Accordian close on ",this.id,this.displayName," called in connected close");
        //site.globalnav.Coremetrics.ElementTag(this,"close");
    },
    
    //top level panel tags other than accordian calls
    panelClickOpen: function(args){
        var s = args.sectionId;
        var d = args.itemId;
        if (s ==="makeup_artistry"){ //inconsitent data don't know what else to do
             s="Makeup Artistry";
        }
        
        if (typeof s != "undefined" && typeof d != "undefined"){
            if (d.match("CAT[0-9]*")){
                cmCreatePageviewTag(s + ' : ' + args.displayName, '',d,'');
            }else{
                cmCreatePageviewTag(s + ' : '+args.displayName, '',cm_corrected_cat[d],'');
            }
        }
        this.ElementTag(args,"open");    
    },

    panelClickClose: function(args){
        //console.log("Panel.js CLICK close panel ",this.id);
        //this.ElementTag(this,"close");
    },

    SubNavChildClick: function(/* String */sectionId){
        var d = this.activeItemId.match("CAT[0-9]*");
        var s = sectionId.match("CAT[0-9]*");
        var ParentCatID = this.parentId.match("CAT[0-9]*");
        if (typeof cm_map[d] != "undefined"){
            cmCreatePageviewTag(cm_map[d] + ' : ' + this.id, '',s,'');
        }
    },
    ElementTag: function(args, action){
        if (!args.parentId || !args.id) {
            return;
        }
        var ParentCatID = args.parentId.match("CAT[0-9]*");
        var SubCatid = args.id.match("CAT[0-9]*");
        
        var EL_ARGS = 'NAV ';
        if (typeof args.sectionId != "undefined" && args.sectionId !=""){
            EL_ARGS = EL_ARGS +  args.sectionId + ' ';}
        if (typeof cm_map[ParentCatID] != "undefined" && cm_map[ParentCatID] !=""){
            EL_ARGS = EL_ARGS + cm_map[ParentCatID] + ' '; }
        if (typeof  args.displayName != "undefined" && args.displayName  !=""){
            EL_ARGS = EL_ARGS + args.displayName.replace(/\s/g,"_") + ' '; }
        if (typeof cm_map[args.id] != "undefined" && cm_map[args.id] !=""){
            EL_ARGS = EL_ARGS + cm_map[args.id].replace(/\s/g,"_") + ' '; }
        
        EL = EL_ARGS.match(/[\w-'\+]+/g);
        var cmargs="";     
        var inc=2;
        if (EL.length > 1){
        inc = 2;}else{
        inc = 1;}
        
        for (i=0;i<=EL.length - inc;i++){
            cmargs = cmargs + EL[i] + ' : ';
        }
        cmargs = cmargs.slice(0,-2);
        cmCreatePageElementTag(EL[EL.length-1],cmargs);
    } 
}