var selected_colors   = new Array();
var selected_regions  = new Array();

var available_colours = new Array();
var available_regions = new Array();

jQuery.each(grape_colour, function(grape_name, colour_ob) {
    // set an array of available colours
    jQuery.each(colour_ob, function(num, colour) {
        if( jQuery.inArray(colour, available_colours) < 0 ) available_colours.push(colour);
    });
});

jQuery.each(regions, function(region_name) {
    // set an array of available regions
    if( jQuery.inArray(region_name, available_regions) < 0 ) available_regions.push(region_name);
});

function color_change(from_colour) {

    var colour_choice = available_colours;
    var region_choice = available_regions;

    if( selected_colors.length < 1 && selected_regions.length < 1 ){
        // if no colours or regions have been selected
        // enable all input fields and stop
        $('#search-options #region-options input, #search-options #grape-options input').removeAttr('disabled');
        $('#search-options #region-options label, #search-options #grape-options label').removeClass('disabled-box');
        $('input[name="alcohol-level"]').removeAttr('disabled').parent().removeClass('disabled-box');
        return false;
    }

    if( selected_colors.length > 0 )
        colour_choice = selected_colors;    // set which colours we can look at

    if( selected_regions.length > 0 )
        region_choice = selected_regions;   // set which regions we can look in

    $('#search-options #region-options input[type="checkbox"], #search-options #grape-options input[type="checkbox"]').attr("disabled", true);
    $('#search-options #region-options label, #search-options #grape-options label').addClass('disabled-box');

    $('input.ignore').removeAttr('disabled');

    jQuery.each(regions, function(region, grapes_in_region) {
        // for each region
        var selected = false;

        if( jQuery.inArray(region, region_choice) > -1 ) {
            // selected region
            jQuery.each(grapes_in_region, function(j, grape) { // each grape in the region
                
                jQuery.each(grape_colour[grape], function(num, colour) {
                    if( jQuery.inArray(colour, colour_choice) > -1 ) { // for each colour from the grape
                        $('#search-options #grape-options input[value="' + grape + '"]').removeAttr('disabled');
                        $('#search-options #grape-options input[value="' + grape + '"]').parent().removeClass('disabled-box');
                        selected = true;
                    }
                });
            });

            if( (typeof(from_colour) != 'undefined' && from_colour) && !selected) { // if colour change and has no grapes
                // remove from regions array
                selected_regions.splice(jQuery.inArray( region, selected_regions),1);
            }
        } else { /* a region is not selected */}

        if( selected ) {
            $('#search-options #region-options input[value="' + region + '"]').removeAttr('disabled');
            $('#search-options #region-options input[value="' + region + '"]').parent().removeClass('disabled-box');
        }

    });

    $('#search-options #region-options input[disabled], #search-options #grape-options input[disabled]').removeAttr('checked').parent().removeClass('checked-box');

    if( typeof(from_colour) && from_colour ) {
        // clean up any previously selected regions if we've added/removed a colour
        region_choice = available_regions;

        if( selected_regions.length > 0 )
            region_choice = selected_regions;   // set which regions we can look in

        jQuery.each(regions, function(region, grapes_in_region) {
            // for each region
            var selected = false;

            if( jQuery.inArray(region, region_choice) > -1 ) {
                // selected region
                jQuery.each(grapes_in_region, function(j, grape) { // each grape in the region

                    jQuery.each(grape_colour[grape], function(num, colour) {
                        if( jQuery.inArray(colour, colour_choice) > -1 ) { // for each colour from the grape
                            selected = true;
                        }
                    });
                });
            } else { /* a region is not selected */}

            if( !selected )
                $('#search-options #region-options input[value="' + region + '"]').attr("disabled", true).removeAttr('checked');

        });
    }

    // clean up any selected regions which we can't select because of colour choice
    colour_choice = available_colours;

    if( selected_colors.length > 0 )
        colour_choice = selected_colors;    // set which colours we can look at
    
    jQuery.each(regions, function(region, grapes_in_region) {
        // for each region
        var selected = false;
            // selected region
            jQuery.each(grapes_in_region, function(j, grape) { // each grape in the region
                jQuery.each(grape_colour[grape], function(num, colour) {
                    if( jQuery.inArray(colour, colour_choice) > -1 ) { // for each colour from the grape
                        selected = true;
                    }
                });
            });

        if( selected )
            $('#search-options #region-options input[value="' + region + '"]').removeAttr('disabled');
    });
    
    $('input[name="alcohol-level"]').attr("disabled", true).parent().addClass('disabled-box');

    $('#search-options #grape-options input[name="grape"]').each(function(){
        if( !$(this).attr('disabled') ) {

            if( jQuery.inArray($(this).val(), alcohol.more) > -1 )  // if more alcohol
                $('input[value="more"]').removeAttr('disabled').parent().removeClass('disabled-box');

            if( jQuery.inArray($(this).val(), alcohol.less) > -1 ) // if less alcohol
                $('input[value="less"]').removeAttr('disabled').parent().removeClass('disabled-box');
        }
    });
    
    $('#all-alcohol-radio').removeAttr('disabled').parent().removeClass('disabled-box');


}

$(document).ready(function(){

    var event = 'change';
    
    /* check and see which ones are checked and update the form */
    $('#colour-options input[type="checkbox"]').each(function(){
        if( $(this).attr('checked') ) selected_colors.push($(this).val());
    });

    $('#region-options input[type="checkbox"]').each(function(){
        if( $(this).attr('checked') ) selected_regions.push($(this).val());
    });

    color_change();
    /* check and see which ones are checked and update the form end */

    $('#colour-options input[type="checkbox"]').bind(event, function() {
        // on change the status of a colour
        if( $(this).attr('checked') && jQuery.inArray( $(this).val(), selected_colors) < 0 )
            // add to colours array
            selected_colors.push($(this).val());
        else if( !$(this).attr('checked') && jQuery.inArray( $(this).val(), selected_colors) > -1 )
            // remove from colours array
            selected_colors.splice(jQuery.inArray( $(this).val(), selected_colors),1);

        color_change(true);
    });

    $('#region-options input[type="checkbox"]').bind(event, function() {

        if( $(this).attr('checked') && $(this).attr('name') != 'all_regions' && $(this).attr('name') != 'other_regions' ) {
            $('#region-options input[name="all_regions"]').removeAttr('checked');
        }
    
        if( regions[$(this).val()].length < 1 ) return false; // avoid messing up with empty regions

        // on change the status of a region
        if( $(this).attr('checked') && jQuery.inArray( $(this).val(), selected_regions) < 0 )
            // add to colours array
            selected_regions.push($(this).val());
        else if( !$(this).attr('checked') && jQuery.inArray( $(this).val(), selected_regions) > -1 )
            // remove from regions array
            selected_regions.splice(jQuery.inArray( $(this).val(), selected_regions),1);
           
        color_change();
    });

    $('#grape-options input[type="checkbox"]').bind(event, function() {

        if( $(this).attr('checked') && $(this).attr('name') != 'all_grapes' && $(this).attr('name') != 'other_grapes' ) {
            $('#grape-options input[name="all_grapes"]').removeAttr('checked');
        }
    });

});