(function ($) {
	
	$.contactForm = function (options) {
		return $.contactForm.impl.init(options);
	};
	
	$.fn.contactForm = function (options) {
		return $.contactForm.impl.init(this, options);
	};
	
	/*
	 * contactForm default options
	 */
	$.contactForm.defaults = {
	    inquiryType:    'Question'
	};
	
	$.contactForm.impl = {
		
		/*
		 * contactForm options
		 */
		opts: null,
		
		/*
		 * contactForm helper
		 */
		helper: {},
		
		/*
		 * Initialize the contactForm
		 */
		init: function (options) {
            
            var self    = this;
            
            self.opts = $.extend({}, $.contactForm.defaults, options);
            
            // create the helper objects
            self.helper.form            = $('div.contact-form');
            self.helper.confirmation    = $('div.contact-confirmation');
            
            // bind submit
            self.helper.form.find('#btnSubmit').click(function(e){
			    e.preventDefault();
			    
			    self.submit();
			    
			});
            
			return self;
		},
		
		/*
		 * Submit the form
		 */
		submit: function () {
		    
		    var self    = this;
		    
		    // block the form
		    self.blockForm();
		    
		    var senderName = (self.helper.form.find('#senderName').val() != 'Your Name*') ? self.helper.form.find('#senderName').val() : '';
		    var senderEmail = (self.helper.form.find('#senderEmail').val() != 'Your Email Address*') ? self.helper.form.find('#senderEmail').val() : '';
		    var message = (self.helper.form.find('#message').val() != 'Type Your Message Here*') ? self.helper.form.find('#message').val() : '--';
		    var optionalParameters = 'message=' + message;
		    var delimiter = '&&';
		    
		    // submit ajax request
		    CreateThe.Com.WebServices.EmailService.SendInquiryEmail(senderName, senderEmail, self.opts.inquiryType, optionalParameters, delimiter, $.contactForm.impl.submitSuccess, $.contactForm.impl.submitFailure, self);
		    
		    return;
		},
		
		/*
		 * Success
		 */
		submitSuccess: function (e, self) {
		    
		    var errors = eval(e);
		    
		    if (errors.length == 0)
		    {
		        self.showConfirmation();
		    }
		    else
		    {
		        $.each(errors, function(i, error){
		            
		            // highlight field
		            self.helper.form.find('#' + error['FieldName'])
		                .parent().parent()
		                .addClass('validation-failed')
		                ;
		        });
		    }
		    
		    self.unblockForm();
		    
		    // tracking
		    pageTracker._trackPageview('submit_contact');
		    
		    return;
		},
		
		/*
		 * Failure
		 */
		submitFailure: function (e, self) {
		    
		    self.unblockForm();
		    alert('An error has occurred. Please submit the form again.');
		    
		    return;
		},
		
		/*
		 * Show the confirmation
		 */
		showConfirmation: function () {
		    
		    var self    = this;
		    
		    self.helper.form.css('display', 'none');
		    self.helper.confirmation.css('display', 'block');
		    
		    return;
		},
		
		/*
		 * Block the form
		 */
		blockForm: function () {
		    
		    var self    = this;
		    
		    self.helper.form.find('.validation-failed').removeClass('validation-failed');
		    
		    self.helper.form.block({
                message: null,
                overlayCSS: {  
                    backgroundColor:    '#fff',
                    opacity:            '0.60'
                }
            });
		    
		    return;
		},
		
		/*
		 * Unblock the form
		 */
		unblockForm: function () {
		    
		    var self    = this;
		    
		    self.helper.form.unblock();
		    
		    return;
		}
		
	};
})(jQuery);