// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
jQuery.fn.checkOrderItems = function(settings){
    var config = {
        'order_items' : 'order_items',
        'error_class' : 'error_class',
        'total_field' : 'total',
        'error_message' : 'error_message'
    };
    if (settings) $.extend(config, settings);

    var total = 0
    $('.' + config['order_items']).each(function(){
        total += parseInt($(this).val());

    });
    if (total > parseInt($('#'+ config['total_field']).val())){
        $('.' + config['error_class']).remove();
        $('#js_messages').append("<div id='error' class="+config['error_class']+">"+config['error_message'] +"</div>");
        $('.' + config['order_items']).val(0);
    }
    else{
        $('.' + config['error_class']).remove();
    }

};



$(document).ready(function() {
    //    $(function(){
    //
    //
    //        $('.fadein img:gt(0)').hide();
    //        setInterval(function(){
    //            $('.fadein :first-child').fadeOut(1000)
    //            .next('img').fadeIn(1000)
    //            .end().appendTo('.fadein');
    //        },
    //        3000);
    //    });
    $.fn.slideShow = function(timeOut) {
        var $elem = this;
        this.children(':gt(0)').hide();
        setInterval(function() {
            $elem.children().eq(0).fadeOut(1000).next().fadeIn(1000).end().appendTo($elem);
        }, timeOut || 6000);
    };

    $(function() {
        $('.slide-show').slideShow();
    });

    // Toggle paticipants info in the volunteers registration form
    $(function(){
        $("#volunteer_group,#user_volunteer_attributes_group").change(function(){
            if ($(this).val() == 'false'){
            $('#participants_info').hide('slow');
            $("#user_volunteer_attributes_adults").val(1)
            $("#user_volunteer_attributes_participants_count").val(1)
            $(".adults_items").val(0)
            $("#individual_order").show('slow');
            }
            else{
            $('#participants_info').show('slow');
            $(".individual_items").attr('checked', false);
            $("#individual_order").hide();
            
            }
        });
    });
    // update participants_count
    $(function() {
        $('#user_volunteer_attributes_children').keyup(function() {
            var children = $(this).val();
            var adults = $('#user_volunteer_attributes_adults').val();
            total = parseInt(children) + parseInt(adults);
            $('#volunteer_participants_count, #user_volunteer_attributes_participants_count').val(total);
            $(this).checkOrderItems({
                'order_items' : 'children_items',
                'error_class' : 'child_errors',
                'total_field' : 'user_volunteer_attributes_children',
                'error_message' : 'You cannot order more children items than you have children.'
            });
        });
    });
    
    $(function() {
        $('#user_volunteer_attributes_adults').keyup(function() {
            var adults = $(this).val();
            var children = $('#user_volunteer_attributes_children').val();
            total = parseInt(children) + parseInt(adults);
            $('#volunteer_participants_count, #user_volunteer_attributes_participants_count').val(total);
            $(this).checkOrderItems({
                'order_items' : 'adults_items',
                'error_class' : 'adult_errors',
                'total_field' : 'user_volunteer_attributes_adults',
                'error_message' : 'You cannot request more adult items than you have adults.'
            });
    
        });

    });
    // datepicker
    $(function() {
        $("#start_date").datepicker({
            showOn: 'button',
            buttonImage: '/images/calendar.gif',
            buttonImageOnly: true
        });
    });
    $(function() {
        $("#stop_date").datepicker({
            showOn: 'button',
            buttonImage: '/images/calendar.gif',
            buttonImageOnly: true
        });
    });
    $(function(){
        $("#report_options").click(function(){
            $('#custom_report').toggle('slow');
        });
    });
    $(function() {
        $('#user_first_name, #user_first_name, #user_email').keyup(function() {
            var first_name = $('#user_first_name').val();
            var last_name = $('#user_last_name').val();
            var email = $('#user_email').val();
            $('#user_volunteer_attributes_first_name').val(first_name);
            $('#user_volunteer_attributes_last_name').val(last_name);
            $('#user_volunteer_attributes_email').val(email);
        });
    });
    $(function(){
        $('.children_items').change(function(){
            $(this).checkOrderItems({
                'order_items' : 'children_items',
                'error_class' : 'child_errors',
                'total_field' : 'user_volunteer_attributes_children',
                'error_message' : 'You cannot order more children items than you have children.'
            });
        });

    });
    $(function(){
        $('.adults_items').change(function(){
            $(this).checkOrderItems({
                'order_items' : 'adults_items',
                'error_class' : 'adult_errors',
                'total_field' : 'user_volunteer_attributes_adults',
                'error_message' : 'You cannot request more adult items than you have adults.'
            });
        });

    });


});

