$.fn.extend({
    fadeOutAndRemove: function (a) {
        return this.each(function () {
            var c = $(this);
            c.fadeOut("fast", function () {
                c.remove()
            })
        })
    }
});

var notify = function () {
    var e = false;
    var f = 0;
    var d = -1;
    var h = "m";
    var a = function (j) {
        if (!e) {
            $("#notify-container").append('<table id="notify-table"></table>');
            e = true
        }
        var i = "<tr" + (j.messageTypeId ? ' id="notify-' + j.messageTypeId + '"' : "");
        i += ' class="notify" style="display:none"><td class="notify">' + j.text;
        if (j.showProfile) {
            var k = escape("/users/" + j.userId);
            i += ' See your <a href="/messages/mark-as-read?messagetypeid=' + j.messageTypeId + "&returnurl=" + escape(k) + '">profile</a>.'
        }
        i += '</td><td class="notify-close"><a title="dismiss this notification" onclick="notify.close(';
        i += (j.messageTypeId ? j.messageTypeId : "") + ')">&times;</a></td></tr>';
        $("#notify-table").append(i)
    };
    var c = function (i) {
        $.cookie(h, (i ? i : "0"), {
            expires: 90,
            path: "/"
        })
    };
    var g = function () {
        var i = parseInt($.cookie(h));
        if (isNaN(i)) {
            i = 0
        }
        if (i < 5) {
            $(".module.newuser").show();
            c(++i)
        }
    };
    return {
        showFirstTime: function () {
            if ($.cookie(h)) {
                g()
            } else {
                $(".module.newuser").show();
                $("body").css("margin-top", "2.5em");
                a({
                    messageTypeId: d,
                    text: 'Looking to trade up? Take advantage of our <a onclick="notify.closeFirstTime()">Cash for Clunkers</a> program!'
                });
                $(".notify").fadeIn("slow")
            }
        },
        showMessages: function (j) {
            for (var k = 0; k < j.length; k++) {
                a(j[k])
            }
            $(".notify").fadeIn("slow");
            f = j.length
        },
        show: function (i) {
            $("body").css("margin-top", "2.5em");
            a({
                text: i
            });
            $(".notify").fadeIn("slow")
        },
        close: function (i) {
            var k;
            var j = 0;
            if (i && i != d) {
                $.post("/messages/mark-as-read", {
                    messagetypeid: i
                });
                k = $("#notify-" + i);
                if (f > 1) {
                    j = parseInt($("body").css("margin-top").match(/\d+/));
                    j = j - (j / f)
                }
            } else {
                if (i && i == d) {
                    c()
                }
                k = $(".notify")
            }
            k.children("td").css("border-bottom", "none").end().fadeOut("fast", function () {
                $("body").css("margin-top", j + "px");
                k.remove()
            })
        },
        closeFirstTime: function () {
            c();
            document.location = "/cash-for-clunkers.html"
        }
    }
}();

jQuery.cookie = function (c, k, o) {
    if (typeof k != "undefined") {
        o = o || {};
        if (k === null) {
            k = "";
            o.expires = -1
        }
        var f = "";
        if (o.expires && (typeof o.expires == "number" || o.expires.toUTCString)) {
            var g;
            if (typeof o.expires == "number") {
                g = new Date();
                g.setTime(g.getTime() + (o.expires * 24 * 60 * 60 * 1000))
            } else {
                g = o.expires
            }
            f = "; expires=" + g.toUTCString()
        }
        var n = o.path ? "; path=" + (o.path) : "";
        var h = o.domain ? "; domain=" + (o.domain) : "";
        var a = o.secure ? "; secure" : "";
        document.cookie = [c, "=", encodeURIComponent(k), f, n, h, a].join("")
    } else {
        var e = null;
        if (document.cookie && document.cookie != "") {
            var l = document.cookie.split(";");
            for (var j = 0; j < l.length; j++) {
                var d = jQuery.trim(l[j]);
                if (d.substring(0, c.length + 1) == (c + "=")) {
                    e = decodeURIComponent(d.substring(c.length + 1));
                    break
                }
            }
        }
        return e
    }
};