/**
 * Forms Module - Amedia Creative
 *
 * @category   Forms
 * @package    Amedia_Forms
 * @author     Ever Daniel Barreto <e.barreto@amediacreative.com>
 * @copyright  Copyright (c) 2011 Amedia Creative, Inc. (http://www.amediacreative.com)
 * @version    $Id$
 */

// Define Forms Namespace
amedia.forms = amedia.forms || {};

/**
 * Module Methods
 */
amedia.forms.start = function() {

    // Setup the Contact Form Validation
    if ($('#contactForm').length > 0)
        amedia.forms.validateContact();

    // Setup the Newsletter Form Validation
    if ($('form.newsletterForm').length > 0)
        amedia.forms.validateNewsletter();
}

/**
 * Validate Contact Us page
 */
amedia.forms.validateContact = function() {

    // Validate Contact Form
    $('#contactForm').validate({
        errorElement: 'span',
        errorClass: 'error',
        submitHandler: amedia.forms.processContact,
        highlight: function() {},
        unhighlight: function() {},
        rules: {
            contact_name: {
                required: true,
                minlength: 2
            },
            contact_number: {
                required: true,
                minlength: 2
            },
            contact_email: {
                required: true,
                email: true
            },
            contact_comment: {
                required: true,
                minlength: 2
            }
        },
        messages: {
            contact_name: {
                required: '<span class="icon warning"></span>Please enter your name.',
                minlength: '<span class="icon warning"></span>Your name must consist of at least 2 characters.'
            },
            contact_number: {
                required: '<span class="icon warning"></span>Please enter your phone number.',
                minlength: '<span class="icon warning"></span>Your phone number must consist of at least 2 characters.'
            },
            contact_email: {
                required: '<span class="icon warning"></span>Please enter your email.',
                email: '<span class="icon warning"></span>Please enter a valid email address.'
            },
            contact_comment: {
                required: '<span class="icon warning"></span>Please enter your comment.',
                minlength: '<span class="icon warning"></span>Your comment must consist of at least 2 characters.'
            }
        }
    });
        
}

/**
 * Process Contact Form
 */
amedia.forms.processContact = function(form) {
    var currentForm = $(form);
    $.ajax({
        type: 'POST',
        url: '/contact/save-contact-request',
        data: currentForm.serialize(),
        beforeSend: function() {
            $('#contactDiv').css('display', 'none');
            $('#processDiv').css('display', 'block');
        },
        error: function() {
            alert('Whoops, this is embarrassing, something went wrong, please try again later.')
            $('#contactDiv').css('display', 'block');
            $('#processDiv').css('display', 'none');
        },
        success: function() {
            // Track "page view" on success
            /*if (_gaq!=undefined)
                _gaq.push(['_trackPageview', '/contact/success']);*/
            $('#contactDiv').detach();
            $('#processDiv').detach();
            $('#thankYouDiv').css('display', 'block');
        }
    });
    return false;
}

/**
 * Validate Newsletter box
 */
amedia.forms.validateNewsletter = function() {

    // Validate Contact Form
    $('form.newsletterForm').each(function() {
        $(this).validate({
            errorElement: 'span',
            errorClass: 'error',
            errorPlacement: function(error, element) {
                element.parent().prepend(error);
            },
            submitHandler: amedia.forms.processNewsletter,
            rules: {
                newsletter_email: {
                    required: true,
                    email: true
                }
            },
            messages: {
                newsletter_email: {
                    required: 'Please enter your email.',
                    email: 'Please enter a valid email address.'
                }
            }
        });
    });

}

/**
 * Process Newsletter Form
 */
amedia.forms.processNewsletter = function(form) {
    var currentForm = $(form);
    $.ajax({
        type: 'POST',
        url: '/contact/save-newsletter-contact',
        data: currentForm.serialize(),
        beforeSend: function() {
            currentForm.parent().css('display', 'none');
            currentForm.parent().next().css('display', 'block');
        },
        error: function() {
            currentForm.parent().css('display', 'block');
            currentForm.parent().next().css('display', 'none');
            alert('Whoops, this is embarrassing, something went wrong, please try again later.')
        },
        success: function() {
            // Track event on success
            /*if (_gaq)
                _gaq.push(['_trackEvent', 'newsletter', 'signup', 'signup', 'signup']);*/
            currentForm.parent().next().detach();
            currentForm.parent().css('display', 'block');
            currentForm.next().css('display', 'block');
            currentForm.detach();
        }
    });
    return false;
}
