$(document).ready(function() {
	
	$.landing_images = $('#landing_image img');
	$.image_index = 1;
	
	if ($.landing_images.length > 1) {
   
        setInterval(function(){
   		
            if ($.image_index >= $.landing_images.length) {
                $.image_index = 0;
            }

            $('#landing_image img:visible').hide()

            $($.landing_images[$.image_index]).fadeIn();
            $.image_index++;

        }, $.landing_image_timer);
   }
	
	$('#membership form,#donation form,#kid_membership form, #add_to_cart').submit(function(e) {
		// see if other amount has the required minimum value
        check_minimum_value(e,$(this).find('input.other_amount'));

	});
	
});

var check_minimum_value = function (e,other_amount){
    if(other_amount.val()) {
        
        other_amount_classes = other_amount.attr('class').split(' ');
        
        for (var i=0;i<other_amount_classes.length;i++){
            if (other_amount_classes[i].substring(0,4)=='min_'){
                min_amount = other_amount_classes[i].split('_').pop();
            }
        }
        
        //min_amount = other_amount.attr('class').split(' ').pop().split('_').pop();

        if(Number(other_amount.val(),2) < Number(min_amount,2)) {
            e.preventDefault();
            other_amount.addClass('error');
            notice = other_amount.parents('form').find('div.notice'); 
            notice.fadeIn('fast',function() {

                setTimeout(function() {
                    notice.fadeOut();
                    other_amount.removeClass('error');
                },4000)
            });

        }
    }
}


//When a predefined amount is selected: Clear other amount, show the label and remove the errors.
$('input.predefined-amount').live('click',function(e){
    other_amount = $(this).parents('form').find('input.other_amount');
    other_amount.val('');
    hideLabel(other_amount.attr('ID'),false);
    other_amount.parents('form').find('div.notice').css('display','none');
    other_amount.removeClass('error');
    other_amount.blur();
})
