/*
    parts: [
        {part: "Plinth", options: [10, 12], defaultSwatch: 10},
        {part: "Language", options: [13], defaultSwatch: 0 }
    ]
*/

    ZONTIK.Class.create("ZONTIK.Controls.Customize", ZONTIK.Controls.Control, 
            function(config) {
                var thisContext = this;
                this.target = config.target;
                this.config = config;
                $(document).ready(function(){
                  thisContext.initialize();
                });
            }, 
            {
                target: "",
                idToDefMap: [],
                options: [],
                parts: [],
                swatches: [],
                variantId: 0,
                
                addToCart: function(fwdToCheckout) {
                    var customizations = "";
                    for (var i=0; i<this.swatches.length; i++) {
                        if(this.swatches[i]["part"] != "variant_id") { //last minute protection
                            if (this.idToDefMap[this.swatches[i]["part"]] != undefined) {
                                if (this.idToDefMap[this.swatches[i]["part"]].length > 0) {
                                    customizations += "&" + "custom[" + this.swatches[i]["part"] + "]=" + $("#" + this.config.parts[i]["label"] + "_select option:selected").attr("value");
                                } else {
                                    customizations += "&" + "custom[" + this.swatches[i]["part"] + "]="  + this.swatches[i]["selection"];                            
                                }
                            } else {
                                customizations += "&" + "custom[" + this.swatches[i]["part"] + "]="  + this.swatches[i]["selection"];                            
                            }
                        }
                    }
                    
                    var variantId = this.variantId || $(".variantList option:selected").attr("value");                    
                    var params = variantId + customizations;
                    if(fwdToCheckout) {
                        cart.addAndCheckout(params);
                    } else {
                        cart.addToCart(params);
                    }
                },
                
                convertDefinitionsToArray: function() {
                    var cdefs = this.config.definitions;
                    for (var i=0; i<cdefs.length; i++){
                        //Map the definition id to the actual index of the options array
                        this.idToDefMap[cdefs[i].id] = i;
                        this.options[i] = new Array();
                        if (cdefs[i].items != undefined) {
                            this.idToDefMap[cdefs[i].id] = new Array();
                            this.idToDefMap[cdefs[i].id][0] = i;
                            var cdefItems = cdefs[i].items;
                            if (cdefs[i].label == "Variants"){
                                this.selectedSwatch({part: "variant_id", swatch: cdefs[i].items[0].id});
                                if (cdefs[i].items.length == 1) {
                                    this.variantId = cdefs[i].items[0].id;
                                }
                            } 
                            if (cdefs[i].items.length > 1) {
                                for (var j=0; j<cdefItems.length; j++) {
                                    //Map sub definition to actual index of select option
                                    this.idToDefMap[cdefs[i].id][cdefItems[j].id+1] = j;
                                    this.options[i][j] = new Array();
                                    this.options[i][j]["label"] = cdefItems[j].label;
                                    this.options[i][j]["id"] = cdefItems[j].id;
                                }
                            }
                        } else {
                            this.options[i]["label"] = cdefs[i].label;
                            this.options[i]["id"] = cdefs[i].id;
                            this.options[i]["src"] = cdefs[i].src;
                        }
                    }
                },

                getHtml: function() {
                    var cparts = this.config.parts;
                    var html = "";
                    for (var i=0; i<cparts.length; i++) {                        
                        html += "<div id='SwatchWrapper"+ i + "'></div>";
                    }
                    return html;
                },

                hide: function() {
                    var thisContext = this;
                    $("#ProductCustomizeContents").slideUp(600, function() {
                        $("#ProductCustomize").removeClass("CustomizeExpanded");
                        $("#ProductDetailsEtc").slideDown(300, function() {
                            $("#ProductCustomize").bind('click', thisContext.show);
                        });
                    });
                },
                
                hideAllPalletes: function(ignore) {
                    for (var i=0; i < this.swatches.length; i++) {
                        if (i != ignore) {
                            this.swatches[i]["instance"].closePallete();
                        }
                    }
                },

                initialize: function() {
                    var thisContext = this;
                    this.convertDefinitionsToArray();
                    $("#" + this.target).html(this.getHtml());
                    this.populateSwatches();
                    $("#ProductCustomize").bind('click', this.show);
                },
                
                populateSwatches: function() {
                    var cparts = this.config.parts;
                    var thisContext = this;
                    for (var i=0; i<cparts.length; i++) {
                        this.swatches[i] = new Array();
                        this.swatches[i]["instance"] = new ZONTIK.Controls.Swatches({
                            "target": "SwatchWrapper" + i,
                            "label": cparts[i].label,
                            "part": cparts[i].part,                            
                            "swatches": cparts[i].options,
                            "defaultSwatch": cparts[i].defaultSwatch,
                            "options": thisContext.options,
                            "idToDefMap": thisContext.idToDefMap,
                            "callbackContext": thisContext,
                            "index": i,
                            "hideCallback": ZONTIK.Delegate.create(thisContext, thisContext.hideAllPalletes, i)
                        });
                        
                        if (this.options[this.idToDefMap[cparts[i].defaultSwatch][0]] != undefined) {
                            if (this.options[this.idToDefMap[cparts[i].defaultSwatch][0]].length > 0) {
                                this.swatches[i]["selection"] = this.options[this.idToDefMap[cparts[i].defaultSwatch][0]][0].id;
                            }
                        } else {
                            this.swatches[i]["selection"] = cparts[i].defaultSwatch;
                        }
                        this.swatches[i]["part"] = cparts[i].part;
                    }
                },
                
                selectedSwatch: function(args) {
                    if (args.part == "variant_id") {
                        //this.variantId = args.swatch;
                    } else {
                        this.swatches[args.index]["selection"] = args.swatch;
                        this.swatches[args.index]["part"] = args.part;
                    }
                },
                
                show: function() {
                    $("#ProductDetailsEtc").slideUp(300, function() {
                        $("#ProductCustomize").addClass("CustomizeExpanded");
                        $("#ProductCustomizeContents").slideDown(600);
                    });
                    $("#ProductCustomize").unbind('click', this.show);
                }
            }
        );