site.product.init = function() { 
       if (!page_data || site.product.abort) return;
       //console.log("site.product.init ");
        
       //inventory status codes which suppress Add to Bag 
       brand.product.inventoryStatus = {suppressAddToBag : [2, 3, 7] }; 
       
       if (page_data.catalog) {
        if (page_data.catalog.spp) { 
                site.product.spp.init();
        }
        if (page_data.catalog.mpp) { //e.g. what's new, looks
                site.product.mpp.init(); 
        } 
       }
       if (page_data.video_products) { 
        site.product.videoPlayer.init();
       }
        
       //outlier pages      
       if (page_data.cms_generated && page_data.catalog) {
        if (page_data.catalog.cross_sell) {  //e.g. fergie  
            site.product.productDisplay.init({
            data: page_data.catalog.cross_sell,
            altNodeId: true
        }); 
        }
      }   
}



site.product.initShoppables = function() {  
    console.log("site.product.initShoppables");
    brand.product.productPopovers.init({ 
            products: (page_data ? page_data.catalog.mpp.products : null),
            
            // position popup near mouse
            positionPopup: function(evt, cartAddMsg, cartConfirmMsg) {
                var lmin = 6;
                var lmax = 530;
                var offset = 310; // left nav (190) & half of length of popup (120)            

                var t = (evt.pageY);
                var l = (evt.pageX - offset); 
                 // adjust for left & right edges
                if (l < lmin) {
                    l = lmin;
                } else if (l > lmax) {
                    l = lmax;
                }
                cartAddMsg.domNode.style.top = t + "px";
                cartAddMsg.domNode.style.left = l + "px";
                cartConfirmMsg.domNode.style.top = t + "px";
                cartConfirmMsg.domNode.style.left = l + "px";            
            }
         });  
}

site.product.spp = { 
    init: function() { 
        console.log("site.product.spp.init");
        
        // SPP main product
        var is_shaded = false;
        var smoosh_container = $("swatch_lg");
        var smoosh = $("swatch_lg_img");
        var smoosh_img = $("smooshImg");
        var color_name = $("swatch_lg_name");
        var color_desc = $("swatch_lg_desc");
        var sku_field = $("prod_sku"); 
        var sku_fav_field = $("btn_save_to_favorites");
        var hex_field = $("btn_color_play");
        var swatch_placeholder_instructions = $("swatch_select");
        var product = page_data.catalog.spp.product;
        var is_discontinued = false; 
        try {
            if (page_data.panel_nav["default"].item.id == "discontinued") is_discontinued = true; 
        } catch(e) { 
        }
        
        var shaded_type = product.product_multicolor_type;

        if (product.shaded == 1) {
            is_shaded = true;
            if (!shaded_type) {
                shaded_type = product.skus[0].sku_multicolor_type;
                if (!shaded_type) {
                    shaded_type = "solo";
                }
            }
        }
    
        var cartConfirmMsg = new site.product.CartConfirm({
            id: "cart_confirm_spp",
            is_shaded: is_shaded,
            prodName: product.name,
            nodeToReplace: $("cart_confirm_placeholder"),
            closeCallback: function() {
                  if (generic.env.isIE6 && $("prod_options")) $("prod_options").removeClassName("popupVisible");
            }
        }); 
                
        // show swatch container for any singles, duos, trios, etc...
        if (is_shaded || shaded_type) {
      console.log("site.product.spp: is_shaded");
            var is_single_sku = false;
            
            // for trios & quads only dealing w/ single sku
            if (shaded_type === "trio" || shaded_type === "quad") {
                is_single_sku = true;
            }

            // init swatches by color (grid)
            var swatchArgs = {
                product: product,
                skuField: sku_field,
                shadedType: shaded_type,
                initDefault: true,
                smooshNode: smoosh,
                smooshContainer: smoosh_container,
                smooshImg: smoosh_img,
                swatchTitle: color_name,
                swatchDesc: color_desc,
                instructionsNode: swatch_placeholder_instructions,
                isDiscontinued: is_discontinued,
                selectedSkuPath: site.product.spp.getSelectedSku(),
                domParent: "prod_details"         
            }
            var pageArgs = {
                isSingleSku: is_single_sku,
                hexField: hex_field,
                favField: sku_fav_field,
                cartConfirm: cartConfirmMsg
            };
            
            var swatchContainer = site.product.initSwatchSet({
                node: $("swatch_colors_placeholder"),
                swatchArgs: swatchArgs,
                pageArgs: pageArgs
            });

            //set up swatch filtering/name menu actions
            if (!is_single_sku) {                
                site.product.swatchMenus.init({
                    swatchContainer: swatchContainer,
                    product: product
                });    
            }

            // init color play link (optional)
            site.product.colorPlayButton.init(hex_field);

        } else {
            // non-shaded: 1st sku is default add-to-bag button value
            //console.log("site.product.spp.init: non-shaded");

            site.product.sppInventoryStatus({
                sku: product.skus[0],
                shaded: false 
            });
            
            sku_field.value = product.skus[0].path;
            sku_fav_field.value = sku_field.value;
            cartConfirmMsg.sku = product.skus[0];
        } 

        // main image rollover (optional)
        site.product.photoRollover.init(product.image_medium, product.image_medium_rollover);
        
        // unless discontinued: set up cart/favorites buttons & cross-sell items
        if (is_discontinued ) return;
        
        // add to cart buttons
        var cartConfirmProps = {
                type: "order",
                addedMessage: generic.rbKeys.get("was_added_to_your_shopping_bag"),
                itemCountCopy: generic.rbKeys.get("items_in_bag"),
                itemCountCopySingular: generic.rbKeys.get("item_in_bag")
        } 
            
        var sppButtonTopContainer = $("add_to_bag_top_container");
        if (sppButtonTopContainer) {
                var sppButtonTopPos = $("add_to_bag_top_container").offsetTop;         
                
                var sppButtonTop = new site.product.productButton({
                    node: $("add_to_bag_top"),
                    valueField: sku_field,
                    progressIndicator: "progress_add_to_bag_top",
                    callback: function(resp) {
                        //console.log("site.product.sppInit sppButtonTop callback: "+cartConfirmMsg);
                        if (generic.env.isIE6 && $("prod_options")) $("prod_options").addClassName("popupVisible");
                        var cartbtn_top_pos = ((sku_field.offsetTop - sppButtonTopPos) * -1);
                        cartConfirmMsg.domNode.style.top = cartbtn_top_pos + "px";
                        cartConfirmMsg.setDisplayProperties(cartConfirmProps);
                        cartConfirmMsg.show(resp);
                    }
                }); 
         }
         
         var sppButtonBtm = new site.product.productButton({
                node: sku_field,
                progressIndicator: "progress_add_to_bag",
                callback: function(resp) {
                    console.log("site.product.sppInit sppButtonBtm callback "+cartConfirmMsg);
                    cartConfirmMsg.domNode.style.top = "0px";
                    cartConfirmMsg.setDisplayProperties(cartConfirmProps);
                    cartConfirmMsg.show(resp);                
                }
         });
    
         // add to favorites button
         var favSppButton = new site.product.productButton({
                node: sku_fav_field,
                valueField: sku_field,
                cartType: "favorites",
                method: "alterCollection",
                progressIndicator: "progress_add_to_fav",
                callback: function(resp) {
                    cartConfirmMsg.domNode.style.top = "0px";
                    cartConfirmMsg.setDisplayProperties({
                        type: "favorites",
                        addedMessage: generic.rbKeys.get("was_added_to_your_favourites"),
                        itemCountCopy: generic.rbKeys.get("items_in favourites"),
                        itemCountCopySingular: generic.rbKeys.get("item_in_favourites")
                    });
                    cartConfirmMsg.show(resp);
                }
         });
 
         // MPP cross-sell products
         site.product.productDisplay.init({
                data: page_data.catalog.spp.product.cross_sell
         });
           
    },
    
     // JP doesn't seem to have page_data.selected_sku, so try to find it elsewhere
    getSelectedSku: function() {
        var sku = page_data.selected_sku;
        var def;
        if (!sku) {
            try {
                def = page_data.panel_nav["default"];
            } catch(e) { 
            } 
            if (def && def.item && def.item.id && def.item.id === "search") {
                try {
                    sku = def.item.item.id;
                } catch(e) { 
                }
            }
        }
        return sku;
    }
}
    
site.product.mpp = { 
    init: function() { 
        console.log("site.product.mpp.init");
        if ( page_data.panel_nav["default"].item && page_data.panel_nav["default"].item.id == "looks") {
            site.product.mpp.initLooks();
            return;
        }

                if ( page_data.panel_nav["default"].item && page_data.catalog.subcollection_page ) {
                        site.product.mpp.initSubcollection();
                        return;                        
                }

        if ( page_data.panel_nav["default"].item && page_data.panel_nav["default"].item.id == "favorites") { 
            site.product.favoritesMpp.init();
            return; 
        } 
        
        if ($$(".shoppable")[0]) { 
                    //site.product.initShoppables();
                    //return;
            }
       
        site.product.productDisplay.init({
                    data: page_data.catalog.mpp.products,
                    altNodeId: true,
                    altType: true
            }); 
    },
    initLooks: function() {  
        // confirmation popovers
        function initConfirmMsg(id) {
            var pop = new generic.popupMessage({ 
                popup: $("popup_confirm_"+id),
                buttonClose: $("popup_close_confirm_"+id) //,
                //displayDuration: 4000
            });
            return pop;
        }  
 
        // buttons
        var looksSkus = page_data["all_shoppable_looks_skus"]; 
        
        if (looksSkus) {
         var skus = [];
         looksSkus.each(function(sku){
            skus.push(sku+":1");
         }) 
         
         var cartConfirmMsg = initConfirmMsg("all_to_cart");
         var addAllToCart = new site.product.productButton({
            node: $("all_to_cart_img"),
            skus: skus, 
            progressIndicator: "progress_all_to_cart",
            callback: function(resp) {
                cartConfirmMsg.show(resp);
            }
          }); 
        }
        
        var products = page_data.catalog.mpp;
        for( catId in products ) { 
            site.product.productDisplay.init({
                data: page_data.catalog.mpp[catId].products,
                initButtons: true,
                altNodeId: true,
                altType: true
            });
        }   
    }, // end initLooks     

        initSubcollection: function() {
            // confirmation popovers
            function initConfirmMsg(id) {
                var pop = new generic.popupMessage({
                    popup: $("popup_confirm_"+id),
                    buttonClose: $("popup_close_confirm_"+id) //,
                    //displayDuration: 4000
                });
                return pop;
            }
            var products = page_data.catalog.mpp;
            for( catId in products ) {
                site.product.productDisplay.init({
                    data: page_data.catalog.mpp[catId].products,
                    initButtons: true,
                    altNodeId: true,
                    altType: true
                });
            }
        }// end initSubcollection
}

site.product.favoritesMpp = {
     confirm_props : {
        type: "order",
        addedMessage: generic.rbKeys.get("was_added_to_your_shopping_bag"),
        itemCountCopy: generic.rbKeys.get("items_in_bag"),
        itemCountCopySingular: generic.rbKeys.get("item_in_bag")
    },
    init: function() {
              //console.log("site.product.favoritesMpp");
              var data = page_data.catalog.mpp.products;
            var confirm_props = this.confirm_props;
            var self = this;
            this.currentData = data;
            
            site.product.productDisplay.init({
                data: page_data.catalog.mpp.products,
          altNodeId: true,
          altType: true,
                initButtons: true,
                type: "favorites",
                
                // configure confirm messages for cart or favorites remove
                messageProps: {
                    confirm: confirm_props,
                    callbackRemoveButton: self.removeFavorite 
                },
                shadedMessageProps: {
                    callback: function(response) {
                        this.setConfirmProperties(confirm_props);
                    },
                    callbackRemoveButton: self.removeFavorite  
                }

            });  

        },
        
        removeFavorite: function(args) {
           //console.log("site.product.favorites.removeButton "+args.removeNodeId+"/"+args.skuFieldValue);  
           if (!args||!args.removeNodeId) return; 
           var catprodsku = args.removeNodeId.split("-")[1];
           if (!catprodsku) return; 
           
           var productNode = $(catprodsku); 
           var swatches = productNode.select(".swatch_by_color")[0];
           var removeNode;
           
           //if product is unshaded or has only 1 swatch, remove the product
           if (!swatches || swatches.select(".swatch_hex_container").length==1) { 
               removeNode = productNode;
           }
           //else just remove the swatch
           else if (args.skuFieldValue) {
               var skuId = args.skuFieldValue.split("SKU")[1]; 
               removeNode = $("swatch_SKU"+skuId);
           }
           
           //console.log("removeNode: "+removeNode.id);
           if (removeNode) removeNode.remove(); 
        } 
}

 
