(function($) {
    $.fn.emailAFriend = function(opts) {
        $.fn.emailAFriend.defaults = {
            linkClass: 'email-friend-link',
            contentClass: 'email-friend-content',
            callbackUrl: '/email-a-friend.aspx',
            closeDelay: 100
        };
        var opts = $.extend({}, $.fn.emailAFriend.defaults, opts);

        return this.each(function(idx) {
            if ($(this).get(0).tagName != "A") return;
            var closeTimer = null;

            $(this).mouseenter(function() {
                var a = this;

                var linkBox = $('<div class="' + opts.linkClass + '">' + $(this).parent().html() + '</div>')
                                            .css("position", "absolute")
                                            .click(function() { return false; })
                                            .mouseleave(function() { closeTimer = window.setTimeout(function() { Close(a); }, opts.closeDelay); })
                                            .mouseenter(function() { window.clearTimeout(closeTimer); });
                $(this).parent().append(linkBox);
                var contentBox = $('<div class="' + opts.contentClass + '"><p>Forward this page to a friend.</p><p><label>From: <input class="from email required" type="text" name="from" value="" /></label></p><p><label>To: <input class="to email required" type="text" name="to" value="" /></label></p><p class="right"><span class="error">Error</span>&nbsp;&nbsp;<input type="button" class="submit" value="Submit" /></p></div>').css("position", "absolute");
                contentBox.insertAfter(linkBox)
                                            .mouseleave(function() { closeTimer = window.setTimeout(function() { Close(a); }, opts.closeDelay); })
                                            .mouseenter(function() { window.clearTimeout(closeTimer); });
                $(contentBox).find("input.submit").click(function() {
                    Submit($(contentBox), closeTimer, a);
                }).end().find("input[type=text]").keypress(function(e) {
                    if (e.keyCode == 13) {
                        Submit($(contentBox), closeTimer, a);
                        return false;
                    }
                });

            }).click(function() { return false; });
        });
        function Submit(contentBox, timer, a) {
            var from = $(contentBox).find("input[name=from]").val();
            var to = $(contentBox).find("input[name=to]").val();
            var error = $(contentBox).find("span.error");
            if ($.IsBlank(from) || $.IsBlank(to)) {
                error.text("Please complete both fields").fadeIn("fast");
            } else if (!$.IsEmail(from)) {
                error.text('"From" address is invalid').fadeIn("fast");
            } else if (!$.IsEmail(to)) {
                error.text('"To" address is invalid').fadeIn("fast");
            } else {
                error.text("").fadeOut("fast");
                $(contentBox).children().fadeOut("fast", function() { $(this).remove(); });
                $('<img src="/virtua/images/gallery/loading9.gif" />').css({ display: "none", position: "absolute", left: 109, top: 40 }).appendTo($(contentBox)).fadeIn("fast");
                window.clearTimeout(timer);
                $.ajax({
                    url: opts.callbackUrl,
                    data: { "j": "1", "to": to, "from": from, "url": location.href },
                    cache: false,
                    type: 'GET',
                    success: function(data, status, xhr) {
                        Close(a, true);
                    },
                    error: function(xhr, status, error) {
                        $(contentBox).children()
                                     .fadeOut("fast")
                                     .end()
                                     .append('<p><strong>We\'re sorry, an error has occurred.</strong></p><p>Please try again in a few moments.</p><p style="float:right"><a class="close" href="#">Close</a></p>')
                                     .find("a.close").click(function() {
                                         Close(a, true);
                                     });
                    },
                    dataType: "html"
                });

            }
        }
        function Close(a, fade) {
            var stuff = $(a).parent().children("." + opts.linkClass + ",." + opts.contentClass);
            if (fade) stuff.fadeOut("fast", function() { $(this).remove() });
            else stuff.remove();
        }
    };
})(jQuery);