(function($) {

    $.contact = function(options) {
        return $.contact.impl.init(options);
    };

    $.fn.contact = function(options) {
        return $.contact.impl.init(this, options);
    };

    /*
    * default options
    */
    $.contact.defaults = {
        res: {}
    };

    $.contact.impl = {

        /*
        * options
        */
        opts: null,

        /*
        * helper
        */
        helper: {},

        /*
        * Initialize the page
        */
        init: function(modal, options) {

            var self = this;

            this.opts = $.extend({}, $.contact.defaults, options);

            // helpers
            self.helper.modal = modal;
            self.helper.form = self.helper.modal.find('div.form');
            self.helper.confirmation = self.helper.modal.find('div.confirmation');
            self.helper.overlay = $('<div class="overlay"></div>').insertBefore(self.helper.modal);
            self.helper.close = $('<a class="close-contact" href="#">' + self.opts.res.close + '</a>').prependTo(self.helper.modal);

            // position
            self.position();

            // events
            self.events();

            return self;
        },

        /*
        * Position
        */
        position: function() {

            var self = this;
            var $content = $('div#content');

            self.helper.modal
                .css({
                    left: '50%',
                    top: '50%',
                    marginLeft: -self.helper.modal.outerWidth() / 2 + 'px',
                    marginTop: -self.helper.modal.outerHeight() / 2 + 'px'
                })
                ;

            return;
        },

        /*
        * Events
        */
        events: function() {

            var self = this;

            // open
            $('a.open-contact').click(function(e) {
                e.preventDefault();
                self.show();
            });

            // close
            self.helper.close.click(function(e) {
                e.preventDefault();
                self.hide();
            });

            // cancel
            self.helper.modal.find('a.cancel-contact').click(function(e) {
                e.preventDefault();
                self.hide();
            });

            // overlay
            self.helper.overlay.click(function(e) {
                e.preventDefault();
                self.hide();
            });

            // send
            self.helper.form.find('a.post-contact')
		        .click(function(e) {
		            e.preventDefault();

		            self.post();

		        })
		        ;

            return;
        },

        /*
        * Reset
        */
        reset: function() {

            var self = this;

            self.helper.form
		        .css({
		            display: 'block'
		        })
		        ;

            self.helper.confirmation
		        .css({
		            display: 'none'
		        })
		        ;

            self.helper.form.find('.field')
		        .each(function(i, field) {

		            $(field)
		                .removeClass('validation-failed')
		                    .find('input, textarea')
		                    .val('')
		                    ;

		        })
		        ;

            return;
        },

        /*
        * Show
        */
        show: function() {

            var self = this;

            self.reset();

            self.helper.modal
		        .fadeIn(1000)
		        ;

            self.helper.overlay
		        .css({
		            opacity: '0.00',
		            display: 'block'
		        })
		        .fadeTo(1000, '0.60')
		        ;

            return;
        },

        /*
        * Hide
        */
        hide: function() {

            var self = this;

            self.helper.modal
		        .fadeOut(1000)
		        ;

            self.helper.overlay
		        .fadeOut(1000)
		        ;

            return;
        },

        /*
        * Block
        */
        block: function() {

            var self = this;

            self.helper.form.find('.validation-failed')
		        .removeClass('validation-failed')
		        ;

            self.helper.form
		        .block({
		            message: null,
		            overlayCSS: {
		                backgroundColor: '#000',
		                opacity: '0.60'
		            }
		        })
            ;

            return;
        },

        /*
        * Unblock
        */
        unblock: function() {

            var self = this;

            self.helper.form.unblock();

            return;
        },

        /*
        * Confirm
        */
        confirm: function() {

            var self = this;

            self.helper.form
		        .css({
		            display: 'none'
		        })
		        ;

            self.helper.confirmation
		        .css({
		            display: 'block'
		        })
		        ;

            return;
        },

        /*
        * Post
        */
        post: function() {

            var self = this;

            // block the form
            self.block();

            var firstName = self.helper.form.find('#contact-firstName').val();
            var lastName = self.helper.form.find('#contact-lastName').val();
            var senderName = firstName + ' ' + lastName;
            var senderEmail = self.helper.form.find('#contact-email').val();
            var phone = self.helper.form.find('#contact-phone').val();
            var orderNumber = self.helper.form.find('#contact-orderNumber').val();
            var message = self.helper.form.find('#contact-message').val();
            var optionalParameters = 'phone=' + phone + '&&orderNumber=' + orderNumber + '&&message=' + message;
            var delimiter = '&&';
            var optIn = self.helper.form.find('#contact-optIn').attr('checked');
            
            // post
            CreateThe.Com.Web.CodeBase.WebServices.EmailService.SendInquiryEmailWithOptIn(firstName, lastName, senderEmail, self.opts.inquiryType, optionalParameters, delimiter, optIn, json.region, $.contact.impl.post_Success, $.contact.impl.post_Failure, self);

            return;
        },

        /*
        * post_Success
        */
        post_Success: function(e, self) {

            var errors = eval(e);

            if (errors.length == 0) {
                self.confirm();
            }
            else {
                $.each(errors, function(i, error) {

                    // highlight field
                    self.helper.form.find('li.' + error['FieldName'])
		                .addClass('validation-failed')
		                ;
                });
            }

            self.unblock();

            return;
        },

        /*
        * post_Failure
        */
        post_Failure: function(e, self) {

            var self = $.contact.impl;

            alert('An unknown error has ocurred. Please try again.');

            self.unblock();

            return;
        }

    };
})(jQuery);
