// Array.indexOf for IE:
if (!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for (var i = 0; i < this.length; i++){
            if (this[i] == obj) return i;
        }
        return -1;
    }
}

// All possible attribute qualifiers
var attributeQualifiers = new Array("wbVariantAttribute1", "wbVariantAttribute2", "wbVariantAttribute3", "wbVariantAttribute4", "wbVariantAttribute5");

function onVariantChange(select, changedSelect, variantValues){
    // Parent form of the select box
    var form = $(select).parents().map(function () {if ($(this).is("form")) return this;});
    adjustVariants(form, select.value, changedSelect, variantValues);
}

// Method is called after a value of one of the attribute selectors has been changed
function allVariantsHandleChange(select, changedSelect, variantValues)
{
    // Parent element with class "product" of the select box
    var form = $(select).parents().map(function () {if ($(this).hasClass("product")) return this;});
    if (form.length > 0)
        form = $(form[0]);
    adjustVariants(form, select.value, changedSelect, variantValues);
}

function adjustVariants(form, newValue, changedSelect, values){
    // New selected value
    if(changedSelect == attributeQualifiers[0]){
        changeColorView(form, newValue, values);
    }
    // Don't do anything if the first "Bitte hier auswählen" option has been chosen
    if (newValue == 0)
    {
        form.find(".price").html("");
        try{
            form.find(".priceValue").val(values["base"].priceValue);
        }catch(e){
        }
        form.find(".priceValue").val(values["base"].priceValue);
        form.find(".seriesPrice").html(values["base"].price);
        form.find(".pk").val("");
        form.find(".article").html("");
        form.find(".selected").val("false");
        form.find(".wishlist").val(values['base'].article);
        return false;
    }

    // Collect possible attribute values
    var selectsToPopulate = new Array();
    var otherSelects = new Array();
    for(var i = 0; i < values.length; i++)
    {
        valuesRow = values[i];
        changedAttributeValue = valuesRow[changedSelect];

        if(newValue == changedAttributeValue)
        {
            var allValuesMatch = true;
            for(var j = 0; j < attributeQualifiers.length; j++)
            {
                var otherAttribute = attributeQualifiers[j];
                if(changedSelect == otherAttribute) continue;
                if(typeof selectsToPopulate[otherAttribute] == "undefined") selectsToPopulate[otherAttribute] = new Array();
                var otherAttributeValues = selectsToPopulate[otherAttribute];
                var otherAttributeValue = valuesRow[otherAttribute];
                if(otherAttributeValues.indexOf(otherAttributeValue) == -1) otherAttributeValues[otherAttributeValues.length] = otherAttributeValue;

                if (typeof otherSelects[otherAttribute] == "undefined") otherSelects[otherAttribute] = form.find("select." + otherAttribute);
                if(typeof otherSelects[otherAttribute].val() != "undefined" && otherSelects[otherAttribute].val() != otherAttributeValue) allValuesMatch = false;
            }
            if(allValuesMatch)
            {
                if (valuesRow["available"] == "false"){
                    form.find(".price").html("ausverk.");
//                    form.find(".priceValue").val("");
                    form.find(".pk").val("");
                    form.find(".article").html("");
                    form.find(".disablebutton").show();
                    form.find(".enablebutton").hide();
                }else{
                    form.find(".price").html(valuesRow["price"]);
                    try{
                        form.find(".priceValue").val(valuesRow["priceValue"]);
                    }catch(e){
                    }
                    form.find(".pk").val(valuesRow["pk"]);
                    form.find(".article").html(valuesRow["article"]);
                    form.find(".disablebutton").hide();
                    form.find(".enablebutton").show();
                }
                form.find(".seriesPrice").html("");
                form.find(".wishlist").val(valuesRow["article"]);
                form.find(".selected").val("true");
            }
        }
    }

    // Populate other SELECT elements with possible options
    for(var i = 0; i < attributeQualifiers.length; i++)
    {
        var otherAttribute = attributeQualifiers[i];
        if(changedSelect == otherAttribute || typeof selectsToPopulate[otherAttribute] == "undefined") continue;
        var otherSelect = form.find("select." + otherAttribute);
        var selectValue = otherSelect.val();
        otherSelect.html("<" + "option value=\"0\">Bitte hier ausw&auml;hlen<" + "/option>");
        var options = selectsToPopulate[otherAttribute].sort()
        var selectValueGone = true;
        for(j = 0; j < options.length; j++)
        {
            otherSelect.append("<option value=\"" + options[j] + "\">" + options[j] + "</option>");
            if(options[j] == selectValue) selectValueGone = false
        }
        otherSelect.val(selectValueGone ? "0" : selectValue);
    }

    return true;
}

function changeColorView(form, newValue, variantValues) {
    var imgSrc = "";
    var aColorName =  (newValue != '' && newValue != 0) ? newValue : "";
    var aRow = findMatchingValue(aColorName, variantValues);
    var anImage = findImage(form);
    if (aRow != null && anImage != null && aRow.img != ""){
        $(anImage).attr('src', aRow.img);
        var nodeContainingImage = document.getElementById("imageViewContainer");
        if (nodeContainingImage){
            var aForm = document.getElementById("detailedPictureHiddenForm");
            if (aForm != null){
                aForm.colorName.value =  aColorName;
            }
        }
    }
}

function findMatchingValue(colorValue, variantValues) {
    if (colorValue != ""){
        for(i = 0; i < variantValues.length; i++){
            if (colorValue ==  variantValues[i]['wbVariantAttribute1']){
                return variantValues[i];
            }
        }
    }else{
        return variantValues['base'];
    }
    return null;
}

function findImage(form) {
    var container = $(form);
    var anImage = container.find(".productImage");
    if (anImage.length > 0)
        return anImage[0];
    var aNewContainer = $(container).parents().map(function () {if ($(this).hasClass("product")) return this;});
//    if (aNewContainer.length == 0){
//        aNewContainer = $(container).parents().map(function () {if ($(this).hasClass("productTile")) return this;});
//    }

    if (aNewContainer.length > 0){
        anImage = aNewContainer.find(".productImage");
        if (anImage.length > 0)
            return anImage[0];
    }
    anImage = $("#imageViewContainer img")
    if (anImage.length > 0)
            return anImage[0];
    return null;
}

function isNighttime(){
    var NIGHTTIME = 18;  //hour (start of nighttime)
    var MORNINGTIME = 8; //hour (end of nighttime)

    var currentTime = new Date().getHours();
    return currentTime >= NIGHTTIME || currentTime < MORNINGTIME;
}
function changeImage(elem, src, containerClass, imageSelector)
{
    // Parent element with containerClass
    var container = $(elem).parents().map(function () {if ($(this).hasClass(containerClass)) return this;});
    container.find(imageSelector).attr("src", src);
}
