dojo.require("site.globalnav.GlobalNav");   


/*** NAV CONFIG ***/   

site.globalnav.config = {
    items : [
        {
            id: "products",
            content: {
                url: "/ajax/1.txt",
                param: "CATEGORY_ID"
            },
            template: {
                detail: {
                    // detail cell image params
                    hdDir: "/images/pnav/product/headers/",
                    thumbDir: "/images/pnav/product/right/"
                }
            },
            
            // 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 : "eye",
                    id : "whatsnew",
                    cmcat : "CAT794",
                    header : "images/gnav/eye_off.jpg",
                    content: { url: "ajax/getCat.php?Cid=15" ,
                        handleAs: "html"}
                },
				 {
                    name : "lip",
                    id : "CAT148",
                    cmcat : "CAT148",
                    header : "images/gnav/lip_off.jpg",
					  content: { url: "ajax/getCat.php?Cid=16",
					  	handleAs: "html"}
                },
                {
                    name : "face",
                    id : "CAT163",
                    cmcat :  "CAT163",
                    header : "images/gnav/face_off.jpg",
					 content: { url: "ajax/getCat.php?Cid=17",
                        handleAs: "html"}
                },
                {
                    name : "nail",
                    id : "CAT155",
                    cmcat : "CAT155",
                    header : "images/gnav/nail_off.jpg",
					 content: { url: "ajax/getCat.php?Cid=18" ,
                        handleAs: "html"}
                },
                {
                    name : "body",
                    id : "CAT172",
                    cmcat : "CAT172",
                    header : "images/gnav/body_off.jpg",
					content: { url: "ajax/getCat.php?Cid=32" ,
                        handleAs: "html"}
                },
                {
                    name : "accessories",
                    id : "CAT1921",
                    cmcat :"CAT1921",
                    header : "images/gnav/acces_off.jpg",
                    // studio overlay
					content: { url: "ajax/getCat.php?Cid=19" ,
                        handleAs: "html"}
                }//,
                /*{
                    name : "must haves",
                    id : "CAT869",
                    cmcat :"CAT869",
                    header : "images/gnav/must_off.jpg"
                }*/
            ]
        },
        {
            id: "makeup_artistry",
            items : [
                {
                    name: "shopbylook",
                    id: "looks",
                    cmcat: "CAT81",
                    header : "images/gnav/shopby_off.jpg",
                    content: { url: "ajax/5.txt" ,
                        handleAs: "html"}
                },

                {
                    name: "creatlook",
                    id: "macpro",
                    cmcat: "280",
                    header : "images/gnav/create_off.jpg",
                    content: { url: "ajax/6.txt" ,
                        handleAs: "html"}    
                }
            ]
        },
        {
            id: "giving_back",
            cmcat: "290",
			content: { url: "ajax/whatsnew.txt" ,
                        handleAs: "html"}
      	 },

		
        {
            id: "my_mac",
            cmcat: "1000",
            handleAs: "html",
            hasLoaded: true // panel nav & subnav already placed into page
        },
         {
             id: "my_where",
             cmcat: "290",
             content: { url: "ajax/where.txt",
                 handleAs: "html"
             }
         },
        {
            id: "customer_service",
            cmcat: "14002",
            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

function invokeClick(element) {
    if(element.click)element.click();
    else if(element.fireEvent)element.fireEvent('onclick');
    else if(document.createEvent){
    var evt = document.createEvent("MouseEvents");
    evt.initEvent("click", true, true);
    element.dispatchEvent(evt);
    }
}

   function op()
			{
			 invokeClick(document.getElementById("gnav_products_hd"));
			 invokeClick(document.getElementById("gnav_makeup_artistry_hd"));			
			}
/*** 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
	op();
});


/*** 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);
    } 
}