// JS for my mac panel
//      Handles:
//      - Account links nav states
//      - cart actions within cart panel
//      - cart actions & calls between iframed pages & parent cart window

dojo.require("generic.uri");
dojo.require("dojox.jsonPath");
dojo.require("site.form.DropDownSelect");
dojo.require("site.checkoutPageHandler");
dojo.require("site.checkoutItemHandler");
var pghandler = new site.checkoutPageHandler;
pghandler.context = "cart";


var cartIncludes = {
    // summary:
    //      Callback functions for remove & change qty json calls 
    //      Reloads list of includes specified in var includesToUpdate (defined in cart.tmpl & review.tmpl)
    //      Replaces html in each dom node associated with each include (defined in includesToUpdate)
    //      Destroys & re-parses templated widgets contained in reloaded html (such as DropDownSelect)

    tmplArgs: [
        {
            nodeId: "cart_items",
            path: "/checkout/includes/cart/items.tmpl",
            parse: true // true if include has widgets in markup
        },
        {
            nodeId: "cart_totals",
            path: "/checkout/includes/summary_data.tmpl"
        }
    ],
    
    itemHandlers: {},
    
    progress: new generic.progress({
        containerId: "checkout_shopping_bag",
        progressId: "progress_cart"
    }),
        
    init: function() {
        var self = this;
        
        dojo.subscribe("/checkout/cart/alterCart", this, function(args) {
            self.load(args.actionType, args.callbackArgs);
        });
       
    },
    
    load: function(/* String */actionType,/* Object? */callbackArgs) {
        // summary:
        //      actionType ex: "qty"
        //      callbackArgs (optional): arguments passed from rpc call that triggered this load

        var tmplPaths = [];
        var self = this;
        var request = new generic.jsonrpc();
        var pg = dojo.global.pghandler;
            
        dojo.forEach(self.tmplArgs, function(include, ix) {
            var args = (include.args ? include.args : {});
            tmplPaths[ix] = {
                "path": include.path,
                "args": args
            }
        });
        var requestArgs = [{
            "logic": [
                { "path": "/checkout/cart.logic" }
            ],
            "tmpl": tmplPaths
        }]; 

        // send request
        var d = request.callRemote("include", requestArgs);
        //this.progress.start();

        d.addCallback(function(response){
            self.onLoad(response, callbackArgs, pg);
        });
        
        d.addErrback(function(err) {
            self.progress.clear();
            console.log("error = "+err);
        });    
    },
    
    onLoad: function(response, callbackArgs, pg) {
        // summary
        //      Handler cartIncludes.load response

        console.log("panel onLoad resp: ", response, callbackArgs, pg);
        this.progress.clear();
        var errors = response.error;
        var self = this;
        
        // broadcast that content has loaded
        dojo.publish("/globalnav/event/getcontent/onload", [{ type: "panel", id: "pnav_my_mac_panel" }]);
        
        // if args from a preceding call
        if (callbackArgs) {
            if (callbackArgs.progress) {
                callbackArgs.progress.clear();
            }
            // mixin previous errors
            if (callbackArgs.error) {
                dojo.mixin(errors, callbackArgs.error);
            }
        }
              
        // write out errors
        var errorDetails = pg.showErrors(errors);
        
        // html response contained in response.result.output    
        var output = response.result.output;
        // replace each specified node with new template content
        dojo.forEach(this.tmplArgs, function(content) {
            var containerNode = dojo.byId(content.nodeId);
            // get new html
            /** AAD - This container node sets up the template used for recreating the qty widget, needs to be present to parse. **/
            containerNode.innerHTML = output[content.path];
            if (content.parse) {
                self.reloadWidgets(containerNode);
            }
        });
                    
        // handle page_data response
        var updated_page_data = response.result.page_data;
        if (updated_page_data != null) {
        
            var summary = (updated_page_data.cart ? updated_page_data.cart.summary : false);
            if (summary) {
                var count = parseInt(summary.items);
                // if no items, hide checkout button
                if (count == 0) {
                    self.hideCartSubmit();
                    return;
                }
            }
            
            // reset other js used in includes
            var cartContents = (updated_page_data.cart ? updated_page_data.cart.contents : null);
            addOnReload({
                cartContents: updated_page_data.cart.contents
            });
        
        }
            
    },
    
    reloadWidgets: function(containerNode) {
        dojo.query('.destroy_onreload', containerNode).forEach(function(node) {
            // get regular or useHidden widgets
            var objId = dijit.byId(node.id);
            if (!objId) { objId = dijit.byId(node.id+".display"); }
            if (objId) { 
            
                //MK: isn't this what destroyRecursive is supposed to do?
                //was destroying swatchContainer widgers but wasn't destroying swatch widgets
                var kids = objId.getChildren();
                kids.forEach(function(kid) {
                  //console.log(cartIncludes.reloadWidgets: kid.id + " / " + dijit.byId(kid.id));
                  if (dijit.byId(kid.id)) dijit.byId(kid.id).destroy();
                })
           
                objId.destroyRecursive();
            }
        });
        dojo.parser.parse(containerNode);
    },

    initItems: function(contents) { 
        // summary:
        //      cart item handlers for cart & review pages
        //      qty, remove functions
                
        if (typeof(contents) !== "undefined" && contents.length > 0) {  
        
            var storeHandlers = {};
            var self = this;
            
            dojo.forEach(contents, function(item) { 
                
                // check for giftcard item
                var skuPath, itemType, kitObj;
                itemType = item.type;
                if (item.obj.giftcard_id) {
                    skuPath = item.obj.giftcard_id;
                    itemType = "giftcard";
                } else if ( item.type == 'kit' )  {
                    skuPath = item.obj.path;
                    itemType = 'kit';
                    
        /** 
        //MK: this works for Checkout/cart but not for other pages where the cart is viewed through My Mac, bc no page data 
        var resp = dojox.jsonPath.query(dojo.global.page_data.cart, "$.contents[?(@.obj.path=='"+skuPath+"')]"); 
        kitObj = resp[0] ? resp[0] : ""; 
        **/ 
        //console.log("cartIncludes.initItems: kitObj from Page Data "+dojo.toJson(resp[0] ? resp[0] : ""));
        //console.log("cartIncludes.initItems: kitObj from Cart - "+dojo.toJson(item));
        
        kitObj = item;
        
                } else {
                    skuPath = item.obj.path;
                }

                storeHandlers[skuPath] = new site.checkoutItemHandler({
                    buttonRemove:"btn_remove-"+skuPath,
                    selectQty: "form.f.field."+item.field_name.qty,
                    skuPath: skuPath,
                    itemContainer: "co_prod-"+skuPath,
                    progressIndicator: "co_prod_progress-"+skuPath,
                    progressOffset: {w: 0, h: -2},
                    itemType: itemType,
                    kitObj: kitObj
                }); 
                
            });
            
            this.itemHandlers = storeHandlers;
            
        } else {
            
            // if cart is empty: hide checkout button in cart panel
            this.hideCartSubmit();
            
        }
        
    },
    
    hideCartSubmit: function() {
        // hide checkout now button in cart panel
        var cartbtn = dojo.byId("btn_checkout_now");
        if (cartbtn) {
            cartbtn.style.display = "none";
        }   
    },
    
    refreshSummary: function(callbackArgs) {
        // Refreshes only the summary include
        dojo.global.pghandler.refreshInclude({
            nodeId: "cart_totals",
            logicPath: "/checkout/cart.logic",
            tmplPath: "/checkout/includes/summary_data.tmpl",
            callbackArgs: callbackArgs // ex: "callbackArgs": { "progress": undefined, "error": { "messages": [ ] } }
        });
    }
    
}

var iframeHandler = {
    // summary:
    //      Handles calls between checkout parent window including cart panel and
    //      iframed forms (shipping, billing, review & confirmation pages)
    
    id: "checkout_iframe",
    frame: null,
    el: null,
    currentPage: null,
    cartIsMerging: false,
    
    init: function(pagename) {  
        this.frame = frames[this.id];
        if (!this.frame) { return; }
        this.currentPage = pagename;
        
        cartIncludes.hideCartSubmit();                
    },
    
    onCartLoad: function() { 
        this.el = dojo.byId(this.id);
        this.setFrameSrc();
        
        // get signin state
        var customer = dojo.global.page_data.customer;
        if (customer && customer.signed_in == 1) {
            this.cartIsSignedIn = true;
        } else {
            this.cartIsSignedIn = false;
        }
        
        // get cart errors in page_data
        var checkout = dojo.global.page_data.checkout;
        if (checkout && checkout.error) {
            var errorDetails = dojo.global.pghandler.showErrors(checkout.error);
        }   
    }, 
    
    onIFrameLoad: function(args) {
        
        // check signin state
        var customer = args.signinState;
        if (customer && customer.signed_in == 1) {
            this.isSignedIn = true;
            // if customer has signed in, but panel signin state is false, update panel display         
            if (!this.cartIsSignedIn) {
                this.cartIsSignedIn = true;
                this.setSigninState(customer, args.contentErrors);
            }           
        } else {
            this.isSignedIn = false;
            // if customer has signed out, but panel signin state is true, update panel display
            if (this.cartIsSignedIn) {
                this.cartIsSignedIn = false;
                this.setSigninState(customer, args.contentErrors);
            }
        }

        // refresh cart content depending on page & signin state
        if (this.currentPage === "complete") {
            this.refreshCart("complete", null, true); // shouldn't be any errors to send at this point
        } else if (!this.cartIsMerging) {
            // refresh totals
            // pass on page_data errors from iframe
            var contentArgs = {error: args.contentErrors};
            this.refreshSummary(contentArgs);
        }
        
        // resize
        //this.resize(args.height);
        
        this.cartIsMerging = false;
        
    },
    
    setFrameSrc: function() {
        // get src of iframe, if specified in url string or page_data
        var framesrc;
        var pdc = dojo.global.page_data.checkout;
        var page = (pdc ? dojo.global.page_data.checkout.page : null);
        if (page && page.length > 1) {
            framesrc = page;
        } else {
            var url = new generic.uri(window.location.href);
            if (url.queryKey && url.queryKey.page) {
                framesrc = url.queryKey.page;
            }
        }
        if (framesrc) {
            this.el.src = framesrc;
        }
    },
    
    resize: function(h) {
        // add slop
        if (h > 600) {
            h += 20;
            this.el.style.height = h+"px";
        }
    },
    
    setSigninState: function(customer, contentErrors) {
        if (this.isSignedIn) {
            this.cartIsMerging = true;
            
            // show name in cart panel
            var name = dojo.byId("signin_name");
            if (name) {
                name.innerHTML = customer.full_name;
            }
        
            // refresh cart in case of merge
            // current error messages for merge are going to come from iframe
            var contentArgs = {error: contentErrors};
            this.refreshCart("signin", contentArgs, true);
        }
        // show correct signin html in panel
        this.toggleSigninDisplay();
    },
    
    toggleSigninDisplay: function() {
        var signedinNode = dojo.byId("co_accountnav_signedin");
        var signedoutNode = dojo.byId("co_accountnav_signedout");
        
        if (signedinNode && signedoutNode) {
            var inStyle = "block";
            var outStyle = "none";
            if (!this.isSignedIn) {
                inStyle = "none"; 
                outStyle = "block";    
            }
            signedinNode.style.display = inStyle;
            signedoutNode.style.display = outStyle;
        }
    },

    refreshCart: function(actionType, callbackArgs, updateCount) {
        cartIncludes.load(actionType, callbackArgs);
        // update count in gnav
        if (updateCount) {
            dojo.publish("/page/cart/alterCart", [{}]);        
        }
    },
    
    refreshSummary: function(callbackArgs) {
        cartIncludes.refreshSummary(callbackArgs);
    },
    
    refreshFrameInclude: function(args) {
        this.frame.pghandler.refreshInclude(args);
    }
    
}


var mymacNav = {
    // summary: 
    //      Handles display of "my account" accordion
    //      Sets default states for accorion & its sub-sections

    hasRefreshed: false,
    
    subsections: [ "messages", "reorder", "favorites" ],
    
    myaccountSections: [ "address_book", "register", "billing_info", "order_history" ], 
        
    // hack: hide account nav accordion until accordion widget is set up
    init: function() { 
        
        if (!dojo.byId("accountnav")) { return; }
        var self = this;
        var getNav = function() {
            var nav = dijit.byId("psubitem_myaccount");
            if (nav) {
                clearInterval(timer);
                dojo.byId("accountnav_accordion").style.visibility = "visible";
                self.setState(nav);
            }
        }   
        var timer = setInterval(getNav, 600);
        
        // subscribe to "my mac" left nav onclick for non-account/checkout pages
        dojo.subscribe("/globalnav/event/getcontent/my_mac", this, function() {
            self.handlePanelRefresh({event: "show_panel"});
        });
        
    },
    
    handlePanelRefresh: function(args) {
        if (dojo.global.cartStatus.doCartRefresh || !self.hasRefreshed) {

            if (args.event === "alter_cart") {
                // ignore if panel is closed & page is not w/in checkout or account
                var pnav = dijit.byId("pnav_my_mac");
                if (!pnav) { return; }
                var section = null;
                try {
                    section = dojo.global.page_data.panel_nav["default"].id;
                }
                catch(err) {
                    console.log("handlePanelRefresh Err: ", err);
                }
                if (section !== "account" && section !== "checkout" && !pnav.isActive) {
                    return;
                }
            }

            cartIncludes.load("show_panel", null);
            dojo.global.cartStatus.doCartRefresh = false;
            self.hasRefreshed = true;
        }    
    },
    
    setState: function(nav) {
        var pn = dojo.global.page_data.panel_nav["default"];
        var section, sub;

        if (pn && pn.item) {
            section = pn.id;
            sub = pn.item.id;           
        }
        
        if (section === "account") {

            // load cart includes
            cartIncludes.load("show_panel_account", null);
            
            // find "active" subsection
            var ss = this.subsections;
            var mas = this.myaccountSections;
            var activeSection;
            var i = 0;
            
            for (i=0; i<ss.length; i++) {
                if (ss[i] === sub) {
                    activeSection = ss[i];  
                    continue;
                }
            }
            
            // if no section is active, check "my account" subsections
            if (!activeSection) {
                for (i=0; i<mas.length; i++) {
                    if (mas[i] === sub) {
                        activeSection = mas[i];                    
                        nav.open(); // if page is w/in account subsetion open nav
                        continue;
                    }
                }                             
            }
            
        }
        
        // set section/subsection image states
        this.setImages(activeSection);
            
    },
    
    setImages: function(activeSection) {
        // apply rollovers to each non-active section image
        // show sel state for active section image        
        var activeImg = "hd_"+activeSection;
        dojo.query("img.hd_accountnav", "accountnav").forEach( function(node) {
            if (activeImg !== node.id) {
                var r = new generic.rollover(node, null);
            } else {
                var aImg = new generic.img(node, ["sel"]);
                aImg.changeSrc("sel");
            }
        });
    
    }
}

// Offer Code
var offerCodeHandler = {
    init: function() {
        // send transaction call
        var ocfield = dojo.byId("input_offercode");
        var self = this;
        dojo.global.pghandler.initRpcHandler( {
            submitEvent: {
                "nodeId": "btn_offercode",
                "eventName" : "onclick"
            },
            fields: [
                {id: "input_offercode",  reqKey: "offer_codes", inputType: "text"}
            ],
            progressArgs: {
                containerId: "offercode_container",
                progressId: "progress_offercode"
            },
            method: "Transaction.setOfferCodes",
            params: [{ }],
            callback: function(callbackArgs) {
                self.onResponse(callbackArgs);
            }
        }); 
    },
    
    onResponse: function(args) {
        // get messages in errors hash
        var messages = args.error.messages;
        var hasError = false;
        var i = 0;
        var key = "";
        for (i=0; i<messages.length; i++) {
            // error hash can contain messages that are not errors.  find errors based on known error keys
            keyprefix = messages[i].key.substring(0,11);            
            if (keyprefix === "OFFER_ERROR" || messages[i].key === "errormessages.enter_valid_offer") {
                hasError = true;
                continue;
            }
        }
        // stop & show errors if any, otherwise refresh summary
        if (hasError) {
            args.progress.clear();
            var errorDetails = dojo.global.pghandler.showErrors(args.error);
            
        } else {
            //var contentArgs = {progress: args.progress, error: messages};
            cartIncludes.refreshSummary(args);
            // refresh cart for offers that mod cart items
            // pass args so that sucess message (in errors hash) gets passed along
            if (args.result.refresh_cart == 1) {
                iframeHandler.refreshCart("offer", args, true);
            }
        }
    }
}

// Exit checkout: sends user to last page before entering checkout
function exitCheckout() {
    var ref = document.referrer;
    var loc = "/"; // home page default
    if (ref && ref.indexOf(".maccosmetics")) {
        loc = ref;
    }
    window.location = loc;
}
      
// JS to reload when includes are updated
function addOnReload(args) {

    // init remove & qty handlers for each cart item in page_data
    cartIncludes.initItems(args.cartContents); 

    // rollovers
    dojo.query("img.rollover", "cart_items").forEach(
        function(elem) {
            var rollover = new generic.rollover(elem, null);
        }
    );
    
}

// Initial page load
dojo.addOnLoad( function() { 

    iframeHandler.onCartLoad();
    cartIncludes.init(); // handles behaviors & refreshes of cart items & summary
    mymacNav.init(); // handles display of "my account" nav
    offerCodeHandler.init();
    
    // on initial load get cart contents from page_data on cart.tmpl
    // other pages load items from cartIncludes.load response instead
    var cartContents = (page_data.cart ? page_data.cart.contents : null);
    if (cartContents) {
        addOnReload({
            cartContents: page_data.cart.contents
        });
    }


    // popups
    var popargs = {
        w: 480,
        h: 660,
        resizable: "no",
        scrollbars: "yes"
    }   
    var popup_shipping = new generic.popup({        
        activator: "btn_popup_shipping",
        url: "/checkout/popup/shipping_popup.tmpl", 
        resizable: popargs.resizable,
        scrollbars: popargs.scrollbars,
        width: popargs.w,
        height: popargs.h       
    });    
    var popup_tax = new generic.popup({     
        activator: "btn_popup_tax",
        url: "/checkout/popup/tax_popup.tmpl",  
        resizable: popargs.resizable,
        scrollbars: popargs.scrollbars,
        width: popargs.w,
        height: 200   
    });
    var popup_returns = new generic.popup({ 
        activator: "btn_popup_returns",
        url: "/checkout/popup/return_popup.tmpl",  
        resizable: popargs.resizable,
        scrollbars: popargs.scrollbars,
        width: popargs.w,
        height: 500   
    });     
    var popup_security = new generic.popup({    
        activator: "btn_popup_security",
        url: "/checkout/popup/security_popup.tmpl", 
        resizable: popargs.resizable,
        scrollbars: popargs.scrollbars,
        width: popargs.w,
        height: popargs.h   
    }); 
    
    // exit checkout button
    var btn_exit_checkout = dojo.byId("btn_exit_checkout");
    if (btn_exit_checkout) {
        dojo.connect(btn_exit_checkout, "click", exitCheckout);
    }
    
}); 
