(function (b) {
    b.fn.ticker = function (p) {
        var e = b.extend({}, b.fn.ticker.defaults, p),
            i = "#" + b(this).attr("id"),
            j = b(this).attr("tagName");
        return this.each(function () {
            function f(c) {
                var d = 0,
                    k;
                for (k in c) c.hasOwnProperty(k) && d++;
                return d
            }
            function g(c) {
                if (e.debugMode) window.console && window.console.log ? window.console.log(c) : alert(c)
            }
            function l() {
                if (a.contentLoaded == false) {
                    if (e.ajaxFeed) g("Code Me!");
                    else if (e.htmlFeed) if (b(i + " LI").length > 0) b(i + " LI").each(function (c) {
                        a.newsArr["item-" + c] = {
                            type: e.titleText,
                            content: b(this).html()
                        }
                    });
                    else {
                        g("Couldn't find any content for the ticker to use!");
                        return false
                    } else {
                        g("Couldn't find any content for the ticker to use!");
                        return false
                    }
                    a.contentLoaded = true
                }
                b(a.dom.titleElem).html(a.newsArr["item-" + a.position].type);
                b(a.dom.contentID).html(a.newsArr["item-" + a.position].content);
                if (a.position == f(a.newsArr) - 1) a.position = 0;
                else a.position++;
                distance = b(a.dom.contentID).width();
                time = distance / e.speed;
                b(a.dom.wrapperID).find(a.dom.titleID).fadeIn().end().find(a.dom.titleElem).fadeIn("slow", q)
            }
            function q() {
                if (a.play) {
                    var c =
                    b(a.dom.titleElem).width() + 20;
                    b(a.dom.revealID).css("left", c + "px");
                    b(a.dom.revealElem).show(0, function () {
                        b(a.dom.contentID).css("left", c + "px").show();
                        b(a.dom.revealID).css("margin-left", "0px").delay(20).animate({
                            marginLeft: distance + "px"
                        }, time, "linear", m)
                    })
                } else return false
            }
            function m() {
                if (a.play) {
                    b(a.dom.contentID).delay(e.pauseOnItems).fadeOut("slow");
                    b(a.dom.revealID).hide(0, function () {
                        b(a.dom.tickerID).delay(e.pauseOnItems).fadeOut(e.fadeOutSpeed, function () {
                            b(a.dom.wrapperID).find(a.dom.titleElem + "," + a.dom.revealElem + "," + a.dom.contentID).hide().end().find(a.dom.tickerID + "," + a.dom.revealID + "," + a.dom.titleID).show().end().find(a.dom.tickerID + "," + a.dom.revealID + "," + a.dom.titleID).removeAttr("style");
                            l()
                        })
                    })
                } else b(a.dom.revealElem).hide()
            }
            function h() {
                a.play = false;
                b(a.dom.tickerID + "," + a.dom.revealID + "," + a.dom.titleID + "," + a.dom.titleElem + "," + a.dom.revealElem + "," + a.dom.contentID).stop(true, true);
                b(a.dom.revealID + "," + a.dom.revealElem).hide();
                b(a.dom.wrapperID).find(a.dom.titleID + "," + a.dom.titleElem).show().end().find(a.dom.contentID).show()
            }

            function n() {
                a.play = true;
                a.paused = false;
                m()
            }
            function o(c) {
                h();
                switch (c) {
                case "prev":
                    if (a.position == 0) a.position = f(a.newsArr) - 2;
                    else if (a.position == 1) a.position = f(a.newsArr) - 1;
                    else a.position -= 2;
                    b(a.dom.titleElem).html(a.newsArr["item-" + a.position].type);
                    b(a.dom.contentID).html(a.newsArr["item-" + a.position].content);
                    break;
                case "next":
                    b(a.dom.titleElem).html(a.newsArr["item-" + a.position].type);
                    b(a.dom.contentID).html(a.newsArr["item-" + a.position].content);
                    break
                }
                if (a.position == f(a.newsArr) - 1) a.position = 0;
                else a.position++
            }
            var a = {
                position: 0,
                time: 0,
                distance: 0,
                newsArr: {},
                play: true,
                paused: false,
                contentLoaded: false,
                dom: {
                    contentID: "#ticker-content",
                    titleID: "#ticker-title",
                    titleElem: "#ticker-title SPAN",
                    tickerID: "#ticker",
                    wrapperID: "#ticker-wrapper",
                    revealID: "#ticker-swipe",
                    revealElem: "#ticker-swipe SPAN",
                    controlsID: "#ticker-controls",
                    prevID: "#prev",
                    nextID: "#next",
                    playPauseID: "#play-pause"
                }
            };
            if (j != "UL" && e.htmlFeed === true) {
                g("Cannot use <" + j.toLowerCase() + "> type of element for this plugin - must of type <ul>");
                return false
            }(function () {
                b(a.dom.wrapperID).append('<div id="' + a.dom.tickerID.replace("#", "") + '"><div id="' + a.dom.titleID.replace("#", "") + '"><span style="display: none;"><!-- --\></span></div><p id="' + a.dom.contentID.replace("#", "") + '"></p><div id="' + a.dom.revealID.replace("#", "") + '"><span style="display: none;"><!-- --\></span></div></div>');
                b(a.dom.wrapperID).removeClass("no-js").addClass("has-js");
                b(a.dom.tickerElem + "," + a.dom.titleElem + "," + a.dom.contentID).hide();
                if (e.controls) {
                    b(a.dom.controlsID).live("click mouseover mousedown mouseout mouseup", function (c) {
                        var d = c.target.id;
                        if (c.type == "click") switch (d) {
                        case a.dom.prevID.replace("#", ""):
                            a.paused = true;
                            b(a.dom.playPauseID).addClass("paused");
                            o(d);
                            break;
                        case a.dom.nextID.replace("#", ""):
                            a.paused = true;
                            b(a.dom.playPauseID).addClass("paused");
                            o(d);
                            break;
                        case a.dom.playPauseID.replace("#", ""):
                            if (a.play == true) {
                                a.paused = true;
                                b(a.dom.playPauseID).addClass("paused");
                                h()
                            } else {
                                a.paused = false;
                                b(a.dom.playPauseID).removeClass("paused");
                                n()
                            }
                            break
                        } else if (c.type == "mouseover" && b("#" + d).hasClass("controls")) b("#" + d).addClass("over");
                        else if (c.type == "mousedown" && b("#" + d).hasClass("controls")) b("#" + d).addClass("down");
                        else if (c.type == "mouseup" && b("#" + d).hasClass("controls")) b("#" + d).removeClass("down");
                        else c.type == "mouseout" && b("#" + d).hasClass("controls") && b("#" + d).removeClass("over")
                    });
                    b(a.dom.wrapperID).append('<ul id="' + a.dom.controlsID.replace("#", "") + '"><li id="' + a.dom.playPauseID.replace("#", "") + '" class="controls"></li><li id="' + a.dom.prevID.replace("#", "") + '" class="controls"></li><li id="' + a.dom.nextID.replace("#", "") + '" class="controls"></li></ul>')
                }
                b(a.dom.contentID).mouseover(function () {
                    a.paused == false && h()
                }).mouseout(function () {
                    a.paused == false && n()
                });
                l()
            })()
        })
    };
    b.fn.ticker.defaults = {
        speed: 0.1,
        ajaxFeed: false,
        htmlFeed: true,
        debugMode: true,
        controls: true,
        titleText: "News:",
        pauseOnItems: 2E3,
        fadeInSpeed: 300,
        fadeOutSpeed: 300
    }
})(jQuery);
