﻿(function($) {

    $.sendToFriend = function(options) {
        return $.sendToFriend.impl.init(options);
    };

    $.fn.sendToFriend = function(options) {
        return $.sendToFriend.impl.init(this, options);
    };

    /*
    * default options
    */
    $.sendToFriend.defaults = {
        productName: null,
        productBrand: null,
        productCollection: null,
        productPrice: null,
        productImagePath: null,
        productUrl: null,
        res: {
            shopNow: null,
            emailSubject: null
        }
    };

    $.sendToFriend.impl = {

        /*
        * options
        */
        opts: null,

        /*
        * helper
        */
        helper: {},

        /*
        * Initialize the page
        */
        init: function(modal, options) {

            var self = this;

            this.opts = $.extend({}, $.sendToFriend.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-sendtofriend" 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-sendtofriend').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-sendtofriend').click(function(e) {
                e.preventDefault();
                self.hide();
            });

            // overlay
            self.helper.overlay.click(function(e) {
                e.preventDefault();
                self.hide();
            });

            // submit
            self.helper.form.find('a.post-sendtofriend')
		        .click(function(e) {
		            e.preventDefault();

		            self.post();
		        })
		        ;

            return;
        },

        /*
        * Reset
        */
        reset: function() {

            var self = this;

            self.helper.form.find('#sendtofriend-copyMe').attr('checked', '')

            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;

            self.block();

            var senderName = self.helper.form.find('#sendtofriend-senderName').val();
            var senderEmail = self.helper.form.find('#sendtofriend-senderEmail').val();
            var recipientName = self.helper.form.find('#sendtofriend-recipientName').val();
            var recipientEmail = self.helper.form.find('#sendtofriend-recipientEmail').val();
            var message = self.helper.form.find('#sendtofriend-message').val();
            
            var optionalParameters =
		                                'productName&&' + self.opts.productName
		                                + '&&' + 'productBrand&&' + self.opts.productBrand
		                                + '&&' + 'productCollection&&' + self.opts.productCollection
		                                + '&&' + 'productPrice&&' + self.opts.productPrice
		                                + '&&' + 'productImagePath&&' + self.opts.productImagePath
		                                + '&&' + 'shopNowText&&' + self.opts.res.shopNow
		                                + '&&' + 'productUrl&&' + self.opts.productUrl
		                                + '&&message&&' + ((message != null && message != '') ? message : ' ')
            ;
            var delimiter = '&&';
            var emailTemplatePath = self.opts.emailTemplatePath;
            var subject = senderName + ' ' + self.opts.res.emailSubject;
            var copyMe = self.helper.form.find('#sendtofriend-copyMe').attr('checked');

            CreateThe.Com.Web.CodeBase.WebServices.EmailService.SendEmail(senderName, senderEmail, recipientName, recipientEmail, subject, emailTemplatePath, optionalParameters, delimiter, $.sendToFriend.impl.post_Success, $.sendToFriend.impl.submitFailure, self);

            if (copyMe)
                CreateThe.Com.Web.CodeBase.WebServices.EmailService.SendEmail(senderName, senderEmail, recipientName, senderEmail, subject, emailTemplatePath, optionalParameters, delimiter, function() { }, function() { }, 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);
