﻿$(document).ready(function() {
    var disableEvents = false;

    // hide submit button
    $('.winesubmitbutton').css('display', 'none');
    $('.wine_chooser_submit').css('display', 'inline');

    // for the wine chooser at the top of the colour landing pages
    var colourControl = $(colourControlName);
    var colourRadio = $(colourRadioName);
    var countryControl = $(countryControlName);
    var grapeControl = $(grapeControlName);
    var tasteControl = $(tasteControlName)
    var priceControl = $(priceControlName);

    colourRadio.change(function() {
        var selectedValue = colourRadio.find("input[checked]").val()
        if (selectedValue != null) updateControls();
    });
    countryControl.change(function() {
        if (countryControl.val() != null) updateControls();
    });
    grapeControl.change(function() {
        if (grapeControl.val() != null) updateControls();
    });
    /*tasteControl.click(function() {
    var selectedValue = tasteControl.find("input[checked]").val();
    if (selectedValue != null) updateControls();
    });*/
    if ($("#wine_flavour input:checked").length == 1) {
        var tasteCalled = $("#wine_flavour input:checked").val();
    } else {
        var tasteCalled = Math.floor(Math.random() * 101);
    }
    var tasteCall = 0;
    tasteControl.click(function() {
        $("#taste_deselect").val("");
        if (tasteCall == 0) {
            tasteCall++;
        } else {
            tasteControl.find("input[checked]").each(function() {
                if (tasteCalled == $(this).val()) {
                    $(this).removeAttr("checked");
                    $("label[for='" + $(this).attr("id") + "']").removeClass('checked');
                    $("label[for='" + $(this).attr("id") + "']").removeClass('focus');
                    tasteCalled = Math.floor(Math.random() * 101);
                    $("#taste_deselect").val("true");
                } else {
                    tasteCalled = $(this).val();
                }
            });
            updateControls();
            tasteCall = 0;
            return;
        }
    });
    if ($("#wine_price_narrow input:checked").length == 1) {
        var priceCalled = $("#wine_price_narrow input:checked").val();
    } else {
        var priceCalled = Math.floor(Math.random() * 101);
    }
    var priceCall = 0;
    priceControl.click(function() {
        $("#price_deselect").val("");   
        if (priceCall == 0) {
            priceCall++;
        } else {
            priceControl.find("input[checked]").each(function() {
                if (priceCalled == $(this).val()) {
                    $(this).removeAttr("checked");
                    $("label[for='" + $(this).attr("id") + "']").removeClass('checked');
                    $("label[for='" + $(this).attr("id") + "']").removeClass('focus');
                    priceCalled = Math.floor(Math.random() * 101);
                    $("#price_deselect").val("true");
                } else {
                    priceCalled = $(this).val();
                }
            });
            updateControls();
            priceCall = 0;
            return;
        }
    });
    $.each(radioNames, function(i, value) {
        setRadiosEnabledByVal($(value), radioValues[i]);
    });

    function setSelected(value) {
        var $this = $(this);
        $this.selectOptions(value.selected, true);
    }

    function updateControls() {
        var selectedColour;
        if (isLandingPage) {
            selectedColour = colourRadio.find("input[checked]").val();
        }
        else {
            selectedColour = colourControl.val();
        }
        var selectedCountry = countryControl.val();
        var selectedGrape = grapeControl.val();
        var selectedTaste = tasteControl.find("input[checked]").val();
        var selectedPrice = priceControl.find("input[checked]").val();

        var qs = '?';
        if (selectedColour != null && selectedColour != '') qs = qs + 'Colour=' + selectedColour.replace('é', '%c3%a9') + '&';
        if (selectedTaste != null && selectedTaste != '') qs = qs + 'TasteCode=' + selectedTaste + '&';
        if (selectedCountry != null && selectedCountry != '') qs = qs + 'Country=' + selectedCountry + '&';
        if (selectedGrape != null && selectedGrape != '') qs = qs + 'GrapeType=' + selectedGrape + '&';
        if (selectedPrice != null && selectedPrice != '') qs = qs + 'Price=' + selectedPrice;
        var url = '/Ajax/Wine/WineSearch.aspx' + qs;

        grapeControl.removeOption(/./).ajaxAddOption(url + '&OptionType=GrapeType&IncludeSelect=true', null, false, setSelected, [{ "selected": selectedGrape}]);
        countryControl.removeOption(/./).ajaxAddOption(url + '&OptionType=Country&IncludeSelect=true', null, false, setSelected, [{ "selected": selectedCountry}]);
        //colourControl.removeOption(/./).ajaxAddOption(url + '&OptionType=Colour&IncludeSelect=false', null, false, setSelected, [{"selected": selectedColour}]);

        $.getJSON(url + '&OptionType=TasteCode&IncludeSelect=false', setTasteRadios);
        $.getJSON(url + '&OptionType=Price&IncludeSelect=false', setPriceRadios);
    }

    function disableRadios(radiolist) {
        radiolist.find("input").attr('disabled', 'disabled');
        radiolist.find("input").parent().parent().addClass('greyed');
    }

    function enableRadioWithValue(radiolist, value) {
        radiolist.find("input[value='" + value + "']").removeAttr('disabled');
        radiolist.find("input[value='" + value + "']").parent().parent().removeClass('greyed');
    }

    function setRadiosEnabled(radiolist, data) {
        disableRadios(radiolist);
        for (d in data) {
            enableRadioWithValue(radiolist, d);
        }
        var $this = $(this);
    }

    function setRadiosEnabledByVal(radiolist, data) {
        disableRadios(radiolist);
        $.each(data, function(i, value) {
            enableRadioWithValue(radiolist, value);
        });
    }

    function setPriceRadios(data, textStatus) {
        setRadiosEnabled(priceControl, data);
    }

    function setTasteRadios(data, textStatus) {
        setRadiosEnabled(tasteControl, data);
    }
});