var allAttributes = new Array();
// Array.indexOf for the 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");


var selected;
var submitter = null;

function submitFunction() {
    submitter = 1;
}
function popupWindow(url) {
    window.open(url, 'popupWindow', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')
}

function selectRowEffect(object, buttonSelect) {
    if (!selected) {
        if (document.getElementById) {
            selected = document.getElementById('defaultSelected');
        } else {
            selected = document.all['defaultSelected'];
        }
    }

    if (selected) selected.className = 'moduleRow';
    object.className = 'moduleRowSelected';
    selected = object;

    // one button is not an array
    if (document.getElementById('payment'[0])) {
        document.getElementById('payment'[buttonSelect]).checked = true;
    } else {
        //document.getElementById('payment'[selected]).checked=true;
    }
}

function rowOverEffect(object) {
    if (object.className == 'moduleRow') object.className = 'moduleRowOver';
}

function rowOutEffect(object) {
    if (object.className == 'moduleRowOver') object.className = 'moduleRow';
}

function popupImageWindow(url) {
    window.open(url, 'popupImageWindow', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')
}

function toggle(element){


    if (element.className == 'active'){
        element.className = 'default';
    } else {
        var root = element.parentNode;
        var subLists = root.getElementsByTagName("ul");
        for (i = 0; i < subLists.length; i++){
            subLists[i].parentNode.className = 'default';
        }
        element.className = 'active';        
    }
}


// Method is called after a value of one of the attribute selectors has been changed
function allVariantsHandleChange(select, changedSelect, variantValues)
{

    // New selected value
    var newValue = select.value;
    // Parent table of the select box

    var form = $(select).parents().map(function () {if ($(this).hasClass("product")) return this;});

    // Don't do anything if the first "Bitte hier auswählen" option has been chosen
    if (newValue == 0)
    {
        var price = form.find(".price");
        if (price) price.html("");
        var pk = form.find(".pk");
        if (pk) pk[0].value = "";
        var article = form.find(".article");
        if (article) article.html("");
        form.find(".selected").val("false");
        return false;
    }

    // Collect possible attribute values
    var selectsToPopulate = new Array();
    var otherSelects = new Array();
    for(var i = 0; i < variantValues.length; i++)
    {
        valuesRow = variantValues[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)
            {
                // Show the price and article number of the selected variant
                var price = form.find(".price");
                if (price){
                    if (valuesRow["available"] == "false"){
                        price.html("ausverk.");
                    }
                    else{
                        price.html(valuesRow["price"]);
                    }
                }
                var pk = form.find(".pk");
                if (pk){
                    if (valuesRow["available"] == "false"){
                        pk[0].value = "";
                    }
                    else{
                        pk[0].value = valuesRow["pk"];
                    }
                }
                var article = form.find(".article");
                if (article){
                    if (valuesRow["available"] == "false"){
                        article.html("");
                    }
                    else{
                        article.html(valuesRow["article"]);
                    }
                }
                if (valuesRow["available"] == "false")
                {
                    form.find(".disablebutton").show();
                    form.find(".enablebutton").hide();
                }
                else
                {
                    form.find(".disablebutton").hide();
                    form.find(".enablebutton").show();
                }
                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ä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;
}



// Checks if an option is selected in all selects on the product detail page.
// Uses JQuery.
function checkAllSelectionsBeforeCart(element)
{

   var form = $(element).parents().map(function () {if ($(this).is("form")) return this;});

    var submit = true;
    $(form).find(".show select.variants_select").each(function () {
        submit = submit * (this.value != "0")
    });

    if(!submit) alert("Bitte wählen Sie aus allen Bereichen etwas aus!");


    // Check if cookies are enabled, if no go to specific errorpage
    if (!document.cookie)
    {
        var browser = navigator.appName;

        var page = "pages/page.jsf?pageid=datenschutz_cookies_de_textpage";
        var context = "/avena-de";
        var url = (browser == 'Netscape') ? page : context + "/" + page;
        
        cookieErrorPage = window.open( url , '_self' );
        cookieErrorPage.focus();
        submit = false;
    }
    return submit;
}

function showProduct(productId){
    $(productId).show();
    $(productId).addClass("show");
    $(productId).removeClass("hide");
}

function hideProduct(productId){
   $(productId).find(".pk").val("");
   $(productId).find("select.variants_select").each(
           function () {
                   if (this.options.length > 2) this.selectedIndex = 0;}
           );
    $(productId).hide();
    $(productId).removeClass("show");
}

function switchOptions(elem){
    var option = elem.value;
    $(elem).val(option);
    switch(option){
        case 'option1':
            showProduct('#product_0_0');
            hideProduct('#product_0_1');
            showProduct('#product_1_0');
            hideProduct('#product_1_1');
            break;
        case 'option2':
            showProduct('#product_0_0');
            hideProduct('#product_0_1');
            hideProduct('#product_1_0');
            showProduct('#product_1_1');
            break;
        case 'option3':
            hideProduct('#product_0_0');
            showProduct('#product_0_1');
            hideProduct('#product_1_0');
            showProduct('#product_1_1');
            break;
    }
}

function switchOptions(elem){
    var option = elem.value;
    $(elem).val(option);
    switch(option){
        case 'option1':
            showProduct('#product_0_0');
            hideProduct('#product_0_1');
            showProduct('#product_1_0');
            hideProduct('#product_1_1');
            break;
        case 'option2':
            showProduct('#product_0_0');
            hideProduct('#product_0_1');
            hideProduct('#product_1_0');
            showProduct('#product_1_1');
            break;
        case 'option3':
            hideProduct('#product_0_0');
            showProduct('#product_0_1');
            hideProduct('#product_1_0');
            showProduct('#product_1_1');
            break;
    }
}