$(function(){
    monty.init();
});

monty = function() {
    return {}
}();



/** 
 * Main
 *
 **/
$.extend(monty, {
    
    /** Storage space **/
    storage: {
        maxFeatureHeight: 0,
        sentEmail: false
    },
    
    /** 
     * On Dom Ready 
     **/
    init: function() {
        
        // Image rotate
        $('.rotateme').cycle({
            speed: 500,
            timeout: 4000
        });
        
        // Set the featurebox sizes
        monty.setFeatureBoxSizes();
        
        // Attach the contact submition
        monty.attachContactSubmit();

    },
    
    
    
    /**
     * Sets the feature box sizes all the same height
     **/
    setFeatureBoxSizes: function() {
        // Find the max feature height
        $('.feature').each(function(){
            if($(this).height() > monty.storage.maxFeatureHeight) {
                monty.storage.maxFeatureHeight = $(this).height();
            }
            
        });
        
        // Set the height to the max
        $('.feature').height(monty.storage.maxFeatureHeight);
    },
    
    
    /** 
     * Attaches the onclick to the submit button
     **/
    attachContactSubmit: function() {
        $('#submit').click(function() {
            if(monty.storage.sentEmail === false) {
                formData = $('#contactForm').serializeArray();
                $.ajax({
                    async: false,
                    type: 'post',
                    url: '/contact/process',
                    data: formData,
                    dataType: 'json',
                    beforeSend: function() {
                        $('#sentEmail').slideUp('fast');
                    },
                    success: function(response) {
                        $('#sentEmail').html(response.message);
                        $('#sentEmail').attr('class', response.displayClass);
                        $('#sentEmail').slideDown('fast');
                        switch(response.displayClass) {
                            case 'success': 
                                monty.storage.sentEmail = true;
                                break;
                        }
                    }
                });
            } else {
                $('#sentEmail').slideUp('fast');
                $('#sentEmail').html('You can only send one email to Monty at a time.');
                $('#sentEmail').attr('class', 'fail');
                $('#sentEmail').slideDown('fast');
            }
        });
    }

});