(function ($) {
    $.fn.subscribe = function (options) {
        options = $.extend({}, options)
        var $this = this;
        $this.trigger = $(options.trigger);
        $this.contentArea = $this.find(".content");

        //Initialize		
        _init();

        //Private methods		
        function _init() {
            //Move to the right place
            $this.appendTo("#wrap");

            _hide();
            _generateEventTriggers();
        };

        $this.hideMe = function () {
            _hide();
            return $this;
        };

        $this.showMe = function () {
            _show();
            return $this;
        };

        function _generateEventTriggers() {
            //Maintain position when window is resized.
            $(window).resize(_move);

            //Close
            $this.find(".close a").click(function (e) {
                e.preventDefault();
                _hide();
            });

            $this.find(":button").click(function (e) {
                if (!_validateEmail()) {
                    $this.contentArea.find("label.error").show();
                    return;
                } else {
                    $this.find("label.error").hide();
                }

                _addMask();
                var result = { email: $("#txtSubscribeEmail").val(), subscribe: ($("input[name=subscribe]:radio:checked", ".subscribe").val() == 'yes') };
                var jqXHR = $.ajax({
                    url: options.url
					, method: "POST"
					, data: result
					, cache: false
					, timeout: 30000
					, dataType: "html"
					, success: function (data, status, obj) {
					    _updateContent(data, "content");
					}
					, error: function (obj, status, error) {
					    _updateContent(error, "error");
					}
					, complete: function (obj, status) {
					    _removeMask();
					}
                });
                $this.data("ajax", jqXHR);
            });

            //Trigger
            $this.trigger.click(function (e) {
                e.preventDefault();
                if ($this.isHidden) {
                    _show();
                } else {
                    _hide();
                }
            });
        };

        function _addMask() {
            $this.contentArea.mask("Processing...");
            $this.contentArea.find(".loadmask").css({ "opacity": 0 });
        };

        function _removeMask() {
            $this.contentArea.unmask();
        };

        function _validateEmail() {
            var email = $this.contentArea.find("input[type=text]").val() || "";
            email = email.replace(/^\s+|\s+$/g, "");
            return email.match(/^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i);
        };

        function _updateContent(content, className) {
            $this.contentArea.hide();
            $("<div />").addClass(className).addClass("temp").html(content).insertAfter($this.contentArea);
        };

        function _move(force) {
            //Required jQuery UI Position
            if ((force === true) || !$this.isHidden) {
                var opt = options.oPosition;
                var newMyPos = { "my": options.oPosition.my.replace(/(\w+)\s+(\w+)/ig, "$1 bottom") };
                opt = $.extend(options.oPosition, newMyPos);
                $this.position(opt);
            }
        };

        function _hide() {
            $this.trigger.removeClass("active");
            _removeMask();

            $this.isHidden = true;
            $this.each(function () {
                var $self = $(this);
                var height = options.height + parseInt($(this).css("padding-top")) + parseInt($(this).css("padding-bottom"));
                $self.stop().animate({ "top": $self.position().top + height, "height": 0 }, options.transitionSpeed
					, function () {
					    $(this).css({ "top": -10000 });
					});
            });
        };

        function _show() {
            $this.trigger.addClass("active");
            _move(true);

            //Clear input
            $this.find(".content form").each(function () { this.reset(); });
            //Prepare content
            $this.contentArea.show();
            $this.find(".temp").remove();
            $this.contentArea.find("label.error").hide();

            $this.isHidden = false;
            $this.each(function () {
                var $self = $(this);
                var height = options.height;
                $self.show().stop().animate({ "top": $self.position().top - height, "height": height }, options.transitionSpeed);
            });
        };

        return $this;
    }
})(jQuery);

$(document).ready(function () {
    window.subscribe = $(".subscribe").subscribe({
        "trigger": ".social li:eq(3) > a"
		, "height": 140
		, "transitionSpeed": 400
		, "url": "/remote/EmailSubscriptionService.aspx"
		, "oPosition": {
		    "of": ".social li:eq(3)"
								, "my": "left top"
								, "at": "left top"
								, "offset": "0 -3"
								, "collision": "none"
		}
    });

    window.languageList = $(".languageList").subscribe({
        "trigger": ".language a"
		, "height": 35
		, "transitionSpeed": 400
		, "oPosition": {
		    "of": ".language a"
								, "my": "right top"
								, "at": "right top"
								, "offset": "5 -10"
								, "collision": "none"
		}
    });
    $(".languageList").find("a").click(function (e) {
        //e.preventDefault();
        $(".language a").html($(this).html()).click();
    });
});
