var filters ={}

$(document).ready(function() {
    filters['filter[all]'] = "true";
    filters['filter[date]'] = $("#calendar-wrapper").attr("rel");
    filters['filter[event_type_id]'] = $("#event_type option:selected")[0].value;
    filters['filter[country_id]'] = $("#country option:selected")[0].value;
    filters['filter[state_id]'] = $("#state option:selected")[0].value;
    filters['filter[province_id]'] = $("#provinces option:selected")[0].value;

    $(".calendar td a").live("click",function(){
        $(".calendar td").removeClass("selected_day");
        $(this).parent().addClass("selected_day");
        filters['filter[all]'] = "false"
        filters['filter[date]'] = $(this).next().attr("value")
        update_results();
    });

    $(".monthName").live("click",function(){
        $(".calendar td").removeClass("selected_day");
        filters['filter[all]'] = "true"
        filters['filter[date]'] = $("#calendar-wrapper").attr("rel");
        update_results();
    });


    $(".prev_next").live("click",function(){
        var date = $(this).attr("rel");
        $.ajax({
            type: "POST",
            url: "/calendar_events/update_calendar",
            data: "date=" + date + "&authenticity_token=" + AUTH_TOKEN,
            success: function (html) {
                $("#calendar-wrapper").replaceWith(html);
                $(".calendar td").removeClass("selected_day");
                filters['filter[all]'] = "true"
                filters['filter[date]'] = $("#calendar-wrapper").attr("rel");
                update_results();
            }
        });
    });

    $("#country").change(function(){
        filters['filter[country_id]'] = this.value;
        if(this.value == 38){
            $("#state").hide()
            $("#provinces").show()
            filters['filter[state_id]'] = "";
        }
        else if(this.value == 232){
            $("#provinces").hide()
            $("#state").show()
            filters['filter[province_id]'] = "";
        }
        else{
            $("#state").hide()
            $("#provinces").hide()
            filters['filter[province_id]'] = "";
            filters['filter[state_id]'] = "";
        }
        update_results();
    });

    $("#event_type").change(function(){
        filters['filter[event_type_id]'] = this.value;
        update_results();
    });

    $("#state").change(function(){
        filters['filter[state_id]'] = this.value;
        update_results();
    });

    $("#provinces").change(function(){
        filters['filter[province_id]'] = this.value;
        update_results();
    });

});


function update_results(){
    $('#filter_results').fadeOut(200,function(){
        $('#filter_results').animate({
            height: 100
        }, 200 );
        $('#filter_results').html("<div id='filter_results_spinner'></div>");
        $('#filter_results').fadeIn("slow",function(){
            $.ajax({
                type: "POST",
                url: "/calendar",
                data: jQuery.param(filters) + "&authenticity_token=" + AUTH_TOKEN,
                success: function (html) {
                    $('#filter_results').fadeOut(200, function(){
                        $('#filter_results').html(html);
                        
                        //code to get incoming height
                        $('#filter_results').css({
                            "position" : "absolute",
                            "left":"-10000px"
                        })
                        $('#filter_results').show()
                        var h = Math.floor( $('#results_content').height() * 1.05);
                        $('#filter_results').hide()
                        $('#filter_results').css({
                            "position" : "static",
                            "left":"0"
                        })

                        $('#filter_results').animate({
                            height: h
                        }, 200 );
                        $('#filter_results').fadeIn("slow");
                    });
                },
                error: function (html) {
                    
                }
            });
        });
    });
    
}