﻿//
// create closure
//
(function ($) {
    jQuery.fn.login = function (options) {
        var defaults = {
            overlay: null
        };
        var opts = $.extend(defaults, options);

        return this.each(function () {
            // current object
            var _this = this;
            var $this = $(this);

            var _form = $this;

            var _email = $('.email', _this);
            var _password = $('.password', _this);

            var _errorMsg = $('#errorMsg', _this);
            var _lostPassword = $('#login_forgot A', _this);

            var _button = $('INPUT[type=button]', _this);

            _button.bind("click", function (e) {
                // Validate the form
                _errorMsg.html("");
                var isValid = true;
                _form.find(':input').each(function (i, item) { isValid = (!$(item).valid()) ? false : isValid; });

                if (isValid) {
                    $this.trigger("processing");

                    var a = {};
                    a.Email = _email.val();
                    a.Password = _password.val();

                    // Call the webservice
                    cms.webservice.IMember.Authenticate(a, function (r) {
                        if (r.length > 0) {
                            _errorMsg.html(r);
                            $this.trigger("error");
                        }
                        else {
                            $this.trigger("completed");
                        }
                    });
                }
            });

            _lostPassword.bind("click", function () {
                $this.trigger("lostpassword");
            });

            _password.bind('keypress', function (e) {
                if (e.which == 13) {
                    _button.trigger('click');
                }
            });
        });
    };
    //
    // end of closure
    //
})(jQuery);


//
// create closure
//
(function ($) {
    jQuery.fn.lostPassword = function (options) {
        var defaults = {
            overlay: null
        };
        var opts = $.extend(defaults, options);

        return this.each(function () {
            // current object
            var _this = this;
            var $this = $(this);

            var _form = $this;

            var _email = $('.lost_email', _this);

            var _errorMsg = $('.errorMsg', _this);

            var _button = $('INPUT[type=button]', _this);
        
            $this.overlay({
                load: false,
                fixed: false,
                mask: {
                    color: '#000000',
                    loadSpeed: 200,
                    opacity: 0.3
                },
                onBeforeLoad:  function() {
                        // for IE 6,7
                        if ($.browser.msie  && (parseInt($.browser.version, 10) <= 7)) {
                            this.getOverlay().insertBefore('#wrapper');                        
                        }
                }            
            });

            this.open = function() {
                $this.overlay().load();
                $this.css("top", "");
                $this.css("left", "50%");
            };

            _button.bind("click", function (e) {
                // Validate the form
                _errorMsg.html("");
                var isValid = true;
                _form.find(':input').each(function (i, item) { isValid = (!$(item).valid()) ? false : isValid; });

                if (isValid) {
                    $this.trigger("processing");

                    // Call the webservice
                    cms.webservice.IMember.LostPassword(_email.val(), function (r) {
                        if (r.length > 0) {
                            _errorMsg.html(r);
                            $this.trigger("error");
                        }
                        else {
                            $this.trigger("completed");
                        }
                    });
                }
            });
        
        });
    };
    //
    // end of closure
    //
})(jQuery);
