$(document).ready(function() {
    
    check_brochure_order_limit();
    
    $('#id_scroll_order').scrollFollow({
        speed: 800,
        offset: 25
    });
    
    var curr_sel = $('#id_brox_province').val();
    $('#id_brox_province').html('');
    $('#brox_details_form').show();
    function update_provinces() {
        var country_id = $('#id_brox_country').val();
        if (!country_id) {
            country_id = '';
        }
        $.getJSON('/geography/states?country=' + country_id, function(data) {
            var options = '';
            for (var i = 0; i < data.length; i++) {
                options += "<option value='" + data[i]['pk'] + "' ";
                if (data[i]['pk'] == curr_sel)
                    options += " selected "
                options += ">" + data[i]['fields']['name'] + "</option>";
            }
            $('#id_brox_province').html(options);
        });
    }
    update_provinces();

    $('#id_brox_country').change(function(e){
        update_provinces();
        check_brochure_order_limit();
    });
    
    $('.brochure_select_unique').each(function(){
        var id = parseInt(this.id.replace('id_',''));
        if ($(this).is(':checked')) {
            $('#id_unique_status_' + id).show();
        }
        else {
            $('#id_unique_status_' + id).hide();
        }
    });
    
    $('.brochure_select_unique').click(function(event){
        var id = parseInt(this.id.replace('id_',''));
        $('#id_unique_status_' + id).toggle();

        $('.brochure_select').each(function(){
            var tmp_id = parseInt(this.id.replace('id_',''));
            // only disable the checkbox if its the one we didnt click on.
            if (id != tmp_id)
            {
                $(this).attr("disabled", !$(this).attr("disabled"));
            }
        });
    });
    
    $('.brochure_select').click(function(event){
        var id = parseInt(this.id.replace('id_',''));
        if ($(this).is(':checked')) {
            $.add2cart('id_coverimage_' + id,'id_scroll_order', function(){
                process_ordered_brochures();
            });
        }
        else {
            process_ordered_brochures();
        }
    });
    
    $('.brochure_select').each(function(event){
        process_ordered_brochures();
    });
    
    function process_ordered_brochures() {
        $('.brochure_select').each(function(){
            var id = parseInt(this.id.replace('id_',''));
            if ($(this).is(':checked') && $(this).is(':enabled')) {
                if (!$('#id_brochure_added___' + id).length) {
                    $('#id_brochures_list').append("<li id='id_brochure_added___" + id + "'>" + $('#id_brochure_title_'+id).html() + "</li>");
                }
            }
            else{
                if ($('#id_brochure_added___' + id).length) {
                    $('#id_brochure_added___' + id).remove();
                }
            }
        });
    }
    
    function check_brochure_order_limit() {
        if ( $('[name=brochure_number]').val() > 1 && ( $('#id_brox_country').val() == 124 || $('#id_brox_country').val() == 840 ) ) {
            var message = '<p>If you would like to view our tours for more than one region, <br/>  <a href=\'/brochure\'>order our Worldwide brochure</a>.</p>';
            $.facebox(message);
            $('[name=submit]').attr('disabled', 'disabled');
            $('[name=submit]').fadeTo('slow', 0.5);
        } else {
            $('[name=submit]').removeAttr('disabled');
            $('[name=submit]').fadeTo('fast', 1.0);            
        }
    }
    
});
