$(function(){


    $('.rental_form').each(function() {
        $wrapper = $(this);
        $wrapper.data('height', $wrapper.height());
        $wrapper.parent().hide().css({height:0});

    });


    // Accordion type rental request form show/hide
    $('.rental_link').click(function(){
        var $link = $(this);
        var $visForm = $('.rental_form:visible');

        if($visForm.length != 0) {
            $visForm.data('height', $visForm.height());            
            $visForm.parent().animate({height: '0'}, {duration: 500, complete: function(){
                    $visForm.parent().hide();
                }
            });
        }
        
        if($link.nextAll('.formWrapper').find('.rental_form:first:visible').length == 0){
            var $showLink = $link.nextAll('.formWrapper').find('.rental_form:first');
            //  console.log($showLink);
            $showLink.parent().show().animate({height:$showLink.data('height') + 35}, 500, function() {
                    $(this).css({height:'inherit'});
            });
        }
        return false;            
    });

    // jQuery UI Datepicker for two date selection inputs
    $('input[name="from"], input[name="to"]').each(function(){
        var dp = $(this);
        
        if(dp.attr('name') == 'to'){
            dp.datepicker({
                beforeShow: function(input) {
                    var from = $(dp).parent().parent().prev().find('input').datepicker('getDate');
                    ($(this).datepicker('getDate') == null) ? $(this).datepicker('setDate', from) : null;
                    return {minDate: from};
                }
            });   
        } else {
            dp.datepicker({
                minDate: new Date()
            });   
        }
        
    });
    
    $('form').each(function(){
        $(this).validate({
            errorPlacement: function(error, element) {
                error.appendTo($('.rental_form:visible').parent().find('ul'));
            },
            showErrors: function() {
                var errorList = $('.rental_form:visible').parent().find('ul');
                errorList.empty().hide();
                this.defaultShowErrors();
                $('.rental_form').css({height: 'auto'});
                errorList.fadeIn();
            },
            onfocusout: false,
            onkeyup: false,
            onclick: false,
            submitHandler: function(form) {
                $.post('rental_request.php', $(form).serialize(), function(data) {
                    if(data == 'success') {
                        $(form).parent().html('<img src="/images/checkmark.png" alt="" align="absmiddle"/> <strong style="font-size:14px;">Success!</strong>');
                    } else {
                        $(form).parent().html('<img src="/images/error.png" alt="" align="absmiddle"/> <strong style="font-size:14px;">There was a problem submitting your request. Please call 215-672-4200 Ext. 123 for assistance.</strong>');
                    }
                });
                return false;
            },
            wrapper: 'li',
            rules: {
                name: {
                    required: true,
                    minlength: 2
                },
                company: {
                    required: true,
                    minlength: 2
                },
                phone: {
                    required: true,
                    phone: true
                },
                email: {
                    required: true,
                    email: true
                },
                from: {
                    required: true,
                    date: true
                },
                to: {
                    required: true,
                    date: true
                }            
            },
            messages: {
                name: {
                    required: 'Please enter a name.',
                    minlength: 'Please enter more than 2 characters for your name.'
                },
                company: {
                    required: 'Please enter a company name.',
                    minlength: 'Please enter more than 2 characters for your company name.'
                },
                phone: {
                    required: 'Please enter a phone number.',
                    phone: 'Please enter a valid phone number.'
                },
                email: {
                    required: 'Please enter an email address.',
                    email: 'Please enter a valid email address.'
                },
                from: {
                    required: 'Please choose a "From" date.',
                    date: 'Please enter a valid "From" date.'
                },
                to: {
                    required: 'Please choose a "To" date.',
                    date: 'Please enter a valid "To" date.'
                }            
            }
        });
    });
    
    
    // Fancybox for equipment snapshots
        $('.snapshot').fancybox({
    	'overlayShow'			: true,
		'hideOnContentClick'	: true,
		'centerOnScroll'		: true,
		'zoomOpacity'			: true,
		'imageScale'			: true,
		'zoomSpeedIn'			: 200,
		'zoomSpeedOut'			: 300,
		'overlayOpacity'		: 0.5    
    });
    
    $('.snapLink').click(function(){
        $(this).prevAll('a.snapshot').triggerHandler('click');
        return false;
    });
    
});