(function ($) {
	
	$.inviteForm = function (options) {
		return $.inviteForm.impl.init(options);
	};
	
	$.fn.inviteForm = function (options) {
		return $.inviteForm.impl.init(this, options);
	};
	
	/*
	 * inviteForm default options
	 */
	$.inviteForm.defaults = {
	    path:   '/'
	};
	
	$.inviteForm.impl = {
		
		/*
		 * inviteForm options
		 */
		opts: null,
		
		/*
		 * inviteForm helper
		 */
		helper: {},
		
		/*
		 * Initialize the inviteForm
		 */
		init: function (options) {
            
            var self    = this;
            
            self.opts = $.extend({}, $.inviteForm.defaults, options);
            
            // create the helper objects
            self.helper.container       = $('div.invite-container');
            self.helper.overlay         = $('<div class="modal-overlay"></div>');
            self.helper.form            = $('div.invite-form');
            self.helper.entry           = self.helper.form.find('div.form-entry');
            self.helper.confirmation    = self.helper.form.find('div.form-confirmation');
            
            // bind submit click
            self.helper.form.find('#btnInviteSubmit').click(function(e){
			    e.preventDefault();
			    
			    self.submit();
			    
			});
			
			// bind open click
			$('a.open-invite').click(function(e){
			    e.preventDefault();
			    
			    // reset the form
			    self.reset();
			    
			    self.helper.container
		            .css({
		                display:    'block',
		                left:       $(this).offset().left + 'px',
		                top:        $(this).offset().top - self.helper.container.height() - 5 + 'px'
		            })
		            ;
		        
//		        self.helper.overlay
//		            .insertBefore(self.helper.container)
//		            .css({
//		                display:    'block',
//		                opacity:    '0.00'
//		            })
//		            .fadeTo(300, '0.60');
//		            ;
		        
		        // bind close click
		        $('a.close-invite').unbind().click(function(e){
		            e.preventDefault();
		            self.helper.container
		                .css({
		                    display:    'none'
		                })
		                ;
		        });
			    
			});
            
			return self;
		},
		
		/*
		 * Reset the form
		 */
		reset: function () {
		    
		    var self    = this;
		    
		    self.helper.form.find('.validation-failed')
		        .removeClass('validation-failed')
		        ;
		    self.helper.entry
	            .css('display', 'block')
	            ;
	        self.helper.confirmation
	            .css('display', 'none')
	            ;
	        self.helper.form.find('li.field input, li.field textarea').each(function(){
	            $(this)
                    .val($.data($(this).get(0), 'value'))
                    .css({
                        color:          '#666',
                        textTransform:  'uppercase'
                    })
                    ;
	        });
		    
		    return;
		},
		
		/*
		 * 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 recipientName = (self.helper.form.find('#recipientName').val() != 'Friend\'s Name*') ? self.helper.form.find('#recipientName').val() : '';
		    var recipientEmail = (self.helper.form.find('#recipientEmail').val() != 'Friend\'s Email Address*') ? self.helper.form.find('#recipientEmail').val() : '';
		    var optionalParameters = 'path&&' + self.opts.path;
		    var delimiter = '&&';
		    var emailTemplatePath = '/templates/invite.html';
		    var copyMe = self.helper.form.find('#chkCopyMe').attr('checked');
		    var subject = senderName + ' sent you an email from 10 Cane Rum';
		    var fromName = '10 Cane Rum';
		    var fromEmail = "no-reply@10cane.com";
		    
		    CreateThe.Com.WebServices.EmailService.SendEmail(fromName, fromEmail, recipientName, recipientEmail, subject, emailTemplatePath, optionalParameters, delimiter, $.inviteForm.impl.submitSuccess, $.inviteForm.impl.submitFailure, self);
		    
		    if (copyMe)
		        CreateThe.Com.WebServices.EmailService.SendEmail(fromName, fromEmail, senderName, senderEmail, subject, emailTemplatePath, optionalParameters, delimiter, $.inviteForm.impl.sendMeACopySuccess, $.inviteForm.impl.sendMeACopyFailure, self);
		    
		    return;
		},
		
		/*
		 * Success
		 */
		submitSuccess: function (e, self) {
		    
		    var errors = eval(e);
		    
		    if (errors.length == 0)
		    {
		        self.showConfirmation();
		    }
		    else
		    {
		        // highlight field
		        $.each(errors, function(i, error){
		            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.entry
		        .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);