if (typeof QMI === "undefined" || !QMI) {
    var QMI = {};
}

QMI.SkuConfigurator = function (nbAttr) {
    this.enableAutoSelection = true;
    this.selectedSku = null;
    this.state = new Array(nbAttr);  // continuously holds state of every attribute.
    this.nbAttributes = nbAttr;
    this.errorMessageShown = true;
    this.skuSelected = new YAHOO.util.CustomEvent("skuSelected", this);
};

QMI.SkuConfigurator.prototype.wire = function() {
    
    var fnValidateSelection = function(e, src) {
        var selectedSku = src.findSkuMatchingSelection();
        if (selectedSku == null) {
            e = e || event;
            YAHOO.util.Event.preventDefault(e);
            showErrorMessage(this.errorMessageShown);
            this.errorMessageShown = true;
        }
    };

    YAHOO.util.Event.addListener('btnAddToCartSKU', 'click', fnValidateSelection, this);
    YAHOO.util.Event.addListener('addToWishList', 'click', fnValidateSelection, this);

    // assign onChange event to every attribue select box.
    for (var i = 0; i < this.nbAttributes; i++) {
        var cboAttr = comboByIndex(i + 1);
        YAHOO.util.Event.addListener(cboAttr, 'change', onChangeCombos, this);
        setComboVisible(cboAttr, false);
    }

    // initialize selection
    this.fillComboBox(1);
    this.updateStates(1);
    this.fireSkuSelected(null);
};

function onChangeCombos(e, src) {
    var targ = YAHOO.util.Event.getTarget(e);
    src.attributeSelected(targ);
}
;

QMI.SkuConfigurator.prototype.attributeSelected = function(srcCombo) {
    var nbAttr = this.nbAttributes;

    var selSku = null;
    var curIndex = indexOfCombo(srcCombo);
    var nextIndex = curIndex + 1;
    if (curIndex == nbAttr) {
        selSku = this.findSkuMatchingSelection();
    } else {
        var autoSelectedValue = this.fillComboBox(nextIndex);
        if (autoSelectedValue) {
            // recurse into next combo box
            this.attributeSelected(comboByIndex(nextIndex));
            return;
        }
    }
    
    // store current state of attributes
    this.updateStates(curIndex);
    this.fireSkuSelected(selSku);
};

QMI.SkuConfigurator.prototype.updateStates = function(upToIndex) {
    for (var i = 1; i <= this.nbAttributes; i++) {
        this.state[i - 1] = (i <= upToIndex) ? comboByIndex(i).value : "";
    }
};

QMI.SkuConfigurator.prototype.fireSkuSelected = function(skuInfo) {
    this.selectedSku = skuInfo;
    this.skuSelected.fire(skuInfo, this.state);

    if (skuInfo != null) {
        hideErrorMessage();
    }
};

QMI.SkuConfigurator.prototype.findSkuMatchingSelection = function() {
    var nbAttrib = this.nbAttributes;
    // gather current attribute selection (in select boxes).
    var selAttributes = new Array(nbAttrib);
    for (var i = 0; i < nbAttrib; i++) {
        var cboAttr = comboByIndex(i + 1);
        selAttributes[i] = cboAttr.value;
    }

    var selectedSku = null;
    // for every available SKU declinaison, check if current selection matches.
    for (var zz=0 ; zz< skuInfo.length; zz++) {

        var curSkuInfo = skuInfo[zz];
        
        var matches = true;
        for (i = 0; i < nbAttrib; i++) {
            var selAttrValue = selAttributes[i];
            var skuAttrValue = getAttributeByIndex(curSkuInfo, i + 1);
            if ((skuAttrValue != null) && selAttrValue != skuAttrValue.value) {
                matches = false;
                break;
            }
        }

        if (matches) {
            selectedSku = curSkuInfo;
            //alert('sku complet: '+selectedSku.skuId);
            this.hideSkuPriceDisplay(selectedSku);
            break;
        }
    }

    return selectedSku;
};

QMI.SkuConfigurator.prototype.hideSkuPriceDisplay = function(selectedSku) {
    document.getElementById('skuTitle_default').style.display = 'none';
    for (var zz=0 ; zz< skuInfo.length; zz++) {
        if(skuInfo[zz].skuId != selectedSku.skuId) {
           document.getElementById('skuPrice_' + skuInfo[zz].skuId).style.display = 'none';
           document.getElementById('skuTitle_' + skuInfo[zz].skuId).style.display = 'none';
       } else {
           document.getElementById('skuPrice_' + skuInfo[zz].skuId).style.display = 'block';
           document.getElementById('skuTitle_' + skuInfo[zz].skuId).style.display = 'block';
       }
   }
}

QMI.SkuConfigurator.prototype.fillComboBox = function(attrIdx) {
    var skuSelectAttrib = comboByIndex(attrIdx);
    var nbOptionAtt = 1;

    // clear all options (except first one, which is the 'empty' one)
    if(skuSelectAttrib){
        for (var j = skuSelectAttrib.options.length; j >= 1; j--) {
            skuSelectAttrib.remove(j);
        }



    for (var x=0; x< skuInfo.length; x++){
    

        var attribValue = getAttributeByIndex(skuInfo[x], attrIdx);
    
        var skuAttribValueExist = false;

        // Check if option already added in the select box
            for (var i = 1; i < skuSelectAttrib.options.length; i++) {
                if (attribValue != undefined || attribValue != null) {
                    if (skuSelectAttrib.options[i].value == attribValue.value) {
                        skuAttribValueExist = true;
                        skuAttribValueExist = true;
                    }
                }
            }


            //  if option not already added in the select box  add it
            if (!skuAttribValueExist) {
                if (indexOfCombo(skuSelectAttrib) - 1 == 0) {
                    if (attribValue != undefined || attribValue != null) {
                        skuSelectAttrib.options[nbOptionAtt] = new Option(attribValue.text, attribValue.value);
                        nbOptionAtt++;
                    }
                } else {
                    var atr = getAttributeByIndex(skuInfo[x], attrIdx - 1);
                    if (atr) {
                        if (atr.value == (comboByIndex(indexOfCombo(skuSelectAttrib) - 1)).options[(comboByIndex(indexOfCombo(skuSelectAttrib) - 1)).selectedIndex].value) {
                            if (indexOfCombo(skuSelectAttrib) - 2 != 0) {
                                var atr2 = getAttributeByIndex(skuInfo[x], attrIdx - 2);
                                if (atr2) {
                                    if (atr2.value == (comboByIndex(indexOfCombo(skuSelectAttrib) - 2)).options[(comboByIndex(indexOfCombo(skuSelectAttrib) - 2)).selectedIndex].value & attribValue != 0) {
                                        if (attribValue != undefined || attribValue != null) {
                                            skuSelectAttrib.options[nbOptionAtt] = new Option(attribValue.text, attribValue.value);
                                            nbOptionAtt++;
                                        }
                                    }
                                }
                            } else {
                                if (attribValue != undefined || attribValue != null) {
                                    skuSelectAttrib.options[nbOptionAtt] = new Option(attribValue.text, attribValue.value);
                                    nbOptionAtt++;
                                }
                            }
                        }
                    }
                }
            }
        }
    
        var isAutoSelection = (this.enableAutoSelection && nbOptionAtt == 2);

        skuSelectAttrib.selectedIndex = (isAutoSelection) ? 1 : 0;
        //fill the next comboBox to enable the user to select an item
        if(isAutoSelection){
            this.fillComboBox(attrIdx + 1);
        } else {
             // hide all other combo boxes coming next
            for (var i = attrIdx + 1; i <= this.nbAttributes; i++) {
                var nextCombo = comboByIndex(i);
                if (nextCombo != null) {
                    nextCombo.selectedIndex = 0;
                    setComboVisible(nextCombo, false);
                }
            }
        }

        setComboVisible(skuSelectAttrib, true);
        skuSelectAttrib.parentNode.style.visibility = "visible";

        if (attrIdx > 1) {
            // check selected value on attribute before, if empty, hide the combo box
            var previousCombo = comboByIndex(attrIdx - 1);
            if (previousCombo != null) {
                var previousValue = previousCombo.value;
                if (previousValue == undefined || previousValue == null || previousValue == "") {
                    setComboVisible(skuSelectAttrib, false);
                }
            }
        }
    }
    return isAutoSelection;
};

function setComboVisible(combo, isVisible) {
    /* combo.parentNode.style.visibility = isVisible ? "visible" : "hidden";  */
    combo.disabled = !isVisible;

}


function getAttributeByIndex(skuInfo, idx) {

    return skuInfo['attribut' + idx];
}

function indexOfCombo(combo) {
    switch (combo.id) {
        case "attribut1": return 1;
        case "attribut2": return 2;
        case "attribut3": return 3;
        default : return -1;
    }
}

function comboByIndex(idx) {
    return document.getElementById("attribut" + idx);
}

function hideErrorMessage() {
    document.getElementById('messagesMultiSku').style.display = 'none';
}

function showErrorMessage(highlight) {
    document.getElementById('messagesMultiSku').style.display = 'block';
    //alert(highlight);
    //if (highlight) {
        YAHOO.util.Dom.addClass('multiSkuZone', 'highlight');
    //}
}

