/*** NAV CONFIG ***/

site.globalnav.config = {};

// default settings for items that are different in other contexts (e.g. ipad)
site.globalnav.contextualConfig = {
    artistryinaction: {
        name: "Artistry in Action",
        id: "artistry_in_action",
        cmcat : "260",
        header : "/images/gnav/gnav_artistry_in_action_157x18_off.gif",
        content: {
            widget: "ArtistryInActionSubNav",
            url: "/cms/makeup_artistry/artistry_action/panel_nav.tmpl",
            featuredMax: 5 // number of videos to show in featured (initial display) list
        }
        //was//content: { url: "/cms/makeup_artistry/artistry_action/panel_nav3.tmpl" }           
        //content: { url: "/cms/makeup_artistry/artistry_action/panel_nav2.tmpl" }           
    },
    sceneandspotted: {
        name: "Scene and Spotted",
        id: "sceneandspotted",
        header : "/images/gnav/gnav_scenespotted_157x18_off.gif",
        uri: "/sceneandspotted/index.tmpl#/all"
    }
};

// ipad-specific settings 
site.globalnav.contextualConfigIPad = {
    artistryinaction: null
};



// Returns final set of config items based on site status 
// Runs after page (page_data) has loaded so pro status is known
site.globalnav.getConfig = function() {
    var contextualConfig = site.globalnav.contextualConfig;
    if (global.isipad) {
        contextualConfig = site.globalnav.contextualConfigIPad;
    }
    
    var config = {
        items : [
            {
                name: "Shop Products",
                id: "products",
                parentId: "globalnav_container",
                domParent: "globalnav",
                header: "/images/gnav/gnav_products_157x18_off.gif",
    
                 
                // supercategories: shared attributes
                itemsConfig: {
                    content: {
                        widget: "ProductSubNav",
                        url: "/includes/panel_nav/catalog.tmpl",
                        param: "CATEGORY_ID"
                    }
                },
                // supercategories: data
                items : [
                    contextualConfig.sceneandspotted,
                    {
                        name : "Neuheiten",
                        id : "whatsnew",
                        cmcat : "CAT794",
                        header : "/images/gnav/gnav_whatsnew_157x18_off.gif",
                        content: {
                            widget: "ProductSubNav",
                            url: "/cms/whats_new/panel_nav.tmpl"
                        },
                        template: { 
                            detail: { baseClass: "panelnav_cell_category" }
                        }
                    },
                    {
                        name : "Augen",
                        id : "CAT148",
                        cmcat : "CAT148",
                        header : "/images/gnav/gnav_eyes_157x18_off.gif"
                    },
                    {
                        name : "Lippen",
                        id : "CAT163",
                        cmcat :  "CAT163",
                        header : "/images/gnav/gnav_lips_157x18_off.gif"
                    },
                    {
                        name : "Gesicht",
                        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"
                    },
                     {
                        name : "Mineralize",
                        id : "CAT869",
                        cmcat :"CAT869",
                        header : "/images/gnav/gnav_mineralize_157x18_off.gif"
                    },
                    {
                        name : "Wunderwaffen",
                        id : "CAT793",
                        cmcat : "CAT793",
                        header : "/images/gnav/gnav_multiuse_157x18_off.gif"
                    },
                    {
                        name : "Pflege",
                        id : "CAT176",
                        cmcat : "CAT176",
                        header : "/images/gnav/gnav_skincare_157x18_off.gif"
                    },
                    {
                        name : "Pinsel",
                        id : "CAT144",
                        cmcat : "CAT144",
                        header : "/images/gnav/gnav_brushes_157x18_off.gif"
                    },
                    {
                        name : "Nagel",
                        id : "CAT170",
                        cmcat : "CAT170",
                        header : "/images/gnav/gnav_nails_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 : "Goodbyes",
                        id : "discontinued",
                        cmcat : "CAT20833",
                        header : "/images/gnav/gnav_discontinuedproducts_157x18_off.gif",
                        content: { 
                            url: "/discontinued/panel_nav.tmpl",
                            param: "dquery",
                            // goodbyes data: should come from panel nav include
                            featured : {
                                "name" : "Featured Goodbyes", 
                                "id" : "featured_goodbyes",
                                "header" : "/images/goodbyes/pnav/pnav_featured_goodbyes_250x18_off.gif",
                                "uri" : "/products/featured_goodbyes.tmpl"
                            }
                        },
                        template: {
                            detail: {
                                type: "jsTemplates.globalnav.SimpleDetail",
                                baseClass: "panelnav_cell_category"
                            }
                        }
                        /* No disc searc for now
                        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" }
                    } 
                    */
                ]
            }, // end products
          
            {
                name: "Makeup Artistry",
                id: "makeup_artistry",
                parentId: "globalnav_container",
                domParent: "globalnav",
                header: "/images/gnav/gnav_artistry_157x18_off.gif",
                
                items : [
                    {
                        name: "The Artists",
                        id: "artists",
                        cmcat : "CAT1048",
                        header : "/images/gnav/gnav_theartists_157x18_off.gif",
                        content: { 
                            url: "/cms/makeup_artistry/artists/panel_nav.tmpl",
                            handleAs: "html",
                            cms: true // global nav writes out cms panel navs for default state
                        }         
                    },
                    contextualConfig.artistryinaction,
                    {
                        name: "Email An Artist",
                        id: "email_an_artist",
                        cmcat: "1300",
                        header : "/images/gnav/gnav_email_an_artist_157x18_off.gif",
                        content: { url: "/cms/makeup_artistry/email_artist/panel_nav.tmpl" },
                        template: { 
                            detail: {
                                type: "jsTemplates.globalnav.SimpleDetail"
                            }
                        }
                    },
                    {
                        name: "MAC Pro",
                        id: "macpro",
                        cmcat: "280",
                        header : "/images/gnav/gnav_macpro_157x18_off.gif",
                        content: { 
                            url: "/cms/makeup_artistry/mac_pro/panel_nav.tmpl",
                            handleAs: "html"
                        }
                    }
                ]
            }, // end makeup_artistry
    
            {
                name: "Giving Back",
                id: "giving_back",
                cmcat: "290",
                parentId: "globalnav_container",
                domParent: "globalnav",
                header: "/images/gnav/gnav_givingback_157x18_off.gif",          
                content: { url: "/cms/giving_back/panel_nav.tmpl" },
                template: { 
                    detail: {
                        type: "jsTemplates.globalnav.SimpleDetail",
                        baseClass: "panelnav_cell_category"
                    }
                }
            },
    
            {
                name: "My Mac",
                id: "account",
                cmcat: "1000",
                parentId: "globalnav_container",
                domParent: "globalnav",
                header: "/images/gnav/gnav_mymac_157x18_off.gif",
                // subnav settings
                content: {
                    hasLoaded: true, // subnav already placed into page
                    handleAs: "html",
                    cache: false,
                    reinsertNode: true
                },
                "accountnav": {
                    sections: ["account_index", "registration", "address_book", "order_history", "purchases", "favorites", "messages"]
                }
            },
    
            {
                name: "Customer Service",
                id: "customer_service",
                cmcat: "1400",
                parentId: "globalnav_container",
                domParent: "utilitynav_links",
                header: "/images/gnav/gnav_customer_service.gif",
                content: {
                    url: "/cms/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" }
            }
        }
        
    }; // end config
   
    // notify analtyics this is ready
    document.fire('sitenav:loaded',config);
 
    return config;
};


// initialize globalnav 
site.globalnav.init = function() { 
    if (site.globalnav.abort || !$("globalnav")) return;
    //console.log("site.globalnav.init "+page_data.panel_nav["default"].id);

    var config = site.globalnav.config = site.globalnav.getConfig();    
    var section = page_data.panel_nav["default"].id;
    
    // get default page state 
    // structure: defaultState{ id: "", item: { id: "", item: {...} } }
    var defaultState = {};
    if (page_data && page_data.panel_nav) {
        defaultState = page_data.panel_nav["default"];
    }
    
    if (section === "products") {
        $("panel_open").addClassName("panel_open_products_panel");     
    }
    
    // in global_nav.tmpl
    globalNavSetId = "globalnav_container"; 
      
    // init entire nav
    var globalSet =  new site.globalnav.GlobalSet({id: globalNavSetId});
    globalSet.gnav = new site.globalnav.GlobalNav({
        config: config, // all nav items
        defaultState: defaultState, // default/open state data
        globalNavSetId: globalNavSetId
    });

    // after globalnav renders
	// sets reveal of content that's supposed to be hidden until gnav loads
	// hidden content specified by "hide-before-globalnav-load" css class
    try{
		if (!config.items) return;	
		var nodesToShow = $$(".hide-before-globalnav-load"); 
		nodesToShow.each(function(node) {
			node.removeClassName("hide-before-globalnav-load");
        });
        
        // init rollovers for whats new
        // MAC ME OVER only currently
        site.globalnav.collectionThumbnailRollovers.init({ ids: ["CAT6411", "CAT6412", "CAT6413"] });
    }
    catch(err) { console.log(err) }

};

// custom rollover states for collection panel nav thumbnails
site.globalnav.collectionThumbnailRollovers = {
    isInitialized: false,
    init: function(args) {
        var ids = args.ids;
        var self = this;
        
        var initRollover = function(imgNode, id) {
        //console.log("initRollover");
        var outImg = imgNode.src;
        var linkNode = $$("#psubitem_"+id+" A")[0]; // workaround for IE: get inner A element to attach event to since IE throws error on event handler for outer link element
        if (!outImg || !linkNode) return;
        var imgPath = outImg.split(".jpg")[0];
        if (imgPath) imgPath = imgPath.split("://")[1]; // filter out domain, so US ES file requests don't get funky.
        if (imgPath) imgPath = imgPath.split("/images/")[1]; // not assuming .com in domain so assume /images/ path instead
        var overImg = "/images/" + imgPath + "_alt." + "jpg"; // naming convention for images: xxx.jpg & xxx_alt.jpg. NOTE: "alt." & "jpg" not concatenated to avoid ES MP servers from replacing "_alt.jpg" with domain+"_alt.jpg"
        if (!overImg) return;
        linkNode.outImg = outImg;
        linkNode.imgNode = imgNode;
        linkNode.isOver = false;
        var overImgObj = new Image();
        overImgObj.src = overImg;
        linkNode.overImgObj = overImgObj;
        
        var over = function(e) {
            this.isOver = true;
            if (this.imgNode) this.imgNode.src = this.overImgObj.src; // this = event target node
        };
        
        var out = function(e) {
            var eventObj = this;
            var doOut = function() {
                if (!generic.env.isIE || (generic.env.isIE && !eventObj.isOver)) {
                    if (eventObj.imgNode) eventObj.imgNode.src = eventObj.outImg;
                }
            }
            // IE workaround for rollover flickering. cancel out animation unless mouse is totally out of item
            if (generic.env.isIE) {
                eventObj.isOver = false;
                doOut.delay(0.2);
            } else {
                doOut();
            }
        };
        linkNode.observe("mouseover", over);
        linkNode.observe("mouseout", out);
    };
    // listen for collection panel nav loading event
    generic.events.observe("panelnav:contentloaded", function(event) {
        if (event && (event.itemId === "whatsnew")) {
            if (self.isInitialized) return;
                ids.each(function(id, idx) {
                    var imgNode = $("psubitem_"+id+"_thumb");
                    if (imgNode) initRollover(imgNode, id);
                });
                self.isInitialized = true;
            }
        });
    }
};

