// jTweets 1.0.0
// © Ahmed M. Ali. All rights reserved.
(function (d) {
    var c = /https?:\/\/[A-Za-z0-9\.\-\/]*(\?)?[A-Za-z0-9\-&=_]*/g;
    var a = /@[A-Za-z0-9_]+[,\n\s:\t\.]/g;
    var b = /#[A-Za-z0-9_]+[,\n\s:\t\.]/g;
    d.fn.jTweets = function (g) {
        var n = {
            searchTerm: "from:ahmedma",
            isRTL: false,
            pageSize: 5,
            maxPages: 10,
            refreshTime: 30000,
            asNewTweets: 0,
            photoWidth: 48,
            highlightedUser: "",
            highlightClass: ""
        };
        if (g) {
            d.extend(n, g)
        }
        var o = "left";
        var m = null;
        var h = false;
        var f = 0;
        var k = null;
        var j = null;
        var l = null;
        var e = new Array();
        var i = {
            initialize: function (p) {
                o = (n.isRTL == false) ? "left" : "right";
                p.css({
                    position: "relative",
                    overflow: "hidden"
                });
                m = d("<div></div>").width(p.width() * 3).css({
                    left: p.width() * -1,
                    position: "absolute"
                });
                p.append(m);
                i.loadTweetsPage(i.changeCurrentPage)
            },
            loadTweetsPage: function (p) {
                if (h == true) {
                    return
                }
                h = true;
                if (e.length > 0) {
                    d.each(e, function (q, r) {
                        clearTimeout(r)
                    });
                    e = new Array()
                }
                if (l != null) {
                    clearInterval(l);
                    l = null
                }
                d.getJSON("http://search.twitter.com/search.json?q=" + n.searchTerm + "&result_type=recent&rpp=" + ((f == 0) ? n.pageSize + n.asNewTweets : n.pageSize) + "&page=" + ((f == 0) ? ++f : f) + "&callback=?", function (s) {
                    var r = d("<ul></ul>").css({
                        top: "0px",
                        position: "absolute",
                        "direction": (n.isRTL == false) ? "ltr" : "rtl"
                    }).width(m.parent().width());
                    var q = s.results.length - n.pageSize;
                    d.each(s.results, function (t, u) {
                        if (t == 0 && f == 1) {
                            j = u.id
                        }
                        if (t < q) {
                            e.push(setTimeout(function () {
                                i.appendTweet(u)
                            }, 6000 * (q - t)))
                        } else {
                            r.append(i.formTweetItem(u))
                        }
                    });
                    r.append(i.formTweetsPager());
                    p(r);
                    r.width(r.width() - r.outerWidth() + r.width())
                })
            },
            changeCurrentPage: function (q) {
                var p = m.parent();
                q.css("left", p.width()).hide();
                m.append(q);
                q.fadeIn("slow");
                i.finalize(q);
                p.delay(100).animate({
                    height: q.outerHeight(true)
                }, "slow", "linear")
            },
            moveNextPage: function (q) {
                var p = m.parent();
                q.css("left", (n.isRTL == false) ? p.width() * 2 : "0");
                i.slidePage(q, p)
            },
            movePreviousPage: function (q) {
                var p = m.parent();
                q.css("left", (n.isRTL == false) ? "0" : p.width() * 2);
                i.slidePage(q, p)
            },
            slidePage: function (q, p) {
                m.append(q);
                m.animate({
                    left: q.position().left * -1
                }, "slow", "linear", function () {
                    if (k != null) {
                        k.remove()
                    }
                    q.css("left", p.width());
                    m.css("left", p.width() * -1)
                });
                p.delay(100).animate({
                    height: q.outerHeight(true)
                }, "slow", "linear", function () {
                    i.finalize(q)
                })
            },
            finalize: function (p) {
                if (k != null) {
                    k.fadeOut("slow", function () {
                        k.remove()
                    })
                }
                k = p;
                h = false;
                if (f == 1 && n.refreshTime != 0) {
                    l = setInterval(function () {
                        i.loadRecentTweets()
                    }, n.refreshTime)
                }
            },
            loadRecentTweets: function () {
                if (h == true) {
                    return
                }
                h = true;
                d.getJSON("http://search.twitter.com/search.json?q=" + n.searchTerm + "&since_id=" + j + "&result_type=recent&rpp=" + n.pageSize + "&page=" + f + "&callback=?", function (r) {
                    var q = d("<ul></ul>").css({
                        top: "0px",
                        position: "absolute"
                    }).width(m.parent().width());
                    for (var p = r.results.length - 1; p >= 0; p--) {
                        if (p == 0) {
                            j = r.results[p].id
                        }
                        i.appendTweet(r.results[p])
                    }
                });
                h = false
            },
            appendTweet: function (r) {
                var p = i.formTweetItem(r).hide();
                k.prepend(p);
                p.slideDown("slow");
                var q = k.find("li:not(.remove):not(.TweetsPager)").last().addClass("remove").slideUp("slow", function () {
                    q.remove();
                    m.parent().animate({
                        height: k.outerHeight(true)
                    }, "slow", "linear")
                })
            },
            formTweetItem: function (s) {
                s.text = s.text + "\n";
                var r = s.text.match(c);
                if (r) {
                    d.each(r, function (u, v) {
                        s.text = s.text.replace(v, '<a href="' + v + '" target="_blank">' + v + "</a>")
                    })
                }
                var q = s.text.match(a);
                if (q) {
                    d.each(q, function (u, v) {
                        var w = v.substring(1, v.length - 1);
                        s.text = s.text.replace(v, '@<a href="http://twitter.com/' + w + '" target="_blank">' + w + "</a>" + v.substring(v.length - 1))
                    })
                }
                var t = s.text.match(b);
                if (t) {
                    d.each(t, function (u, v) {
                        var w = v.substring(1, v.length - 1);
                        s.text = s.text.replace(v, '<a href="http://twitter.com/search?q=%23' + w + '" target="_blank">#' + w + "</a>" + v.substring(v.length - 1))
                    })
                }
                var p = d("<li></li>");
                if (n.photoWidth > 0) p.append(d('<img src="' + s.profile_image_url + '" />').css({
                    height: n.photoWidth,
                    width: n.photoWidth
                }));
                p.append(d('<span><a class="UserLink" href="http://twitter.com/' + s.from_user + '" target="_blank">' + s.from_user + "</a><br />" + s.text + "</span>"));
                p.append(d("<div></div>").css("clear", "both"));
                if (s.from_user == n.highlightedUser) {
                    p.addClass(n.highlightClass)
                }
                return p
            },
            formTweetsPager: function () {
                var p = d("<li></li>").addClass("TweetsPager");
                if (f < n.maxPages) {
                    p.append(d('<a href="#">></a>').addClass("NextLink").css("float", (n.isRTL == false) ? "right" : "left").click(function () {
                        f++;
                        i.loadTweetsPage(i.moveNextPage);
                        return false
                    }))
                }
                if (f > 1) {
                    p.append(d('<a href="#"><</a>').addClass("PreviousLink").css("float", (n.isRTL == false) ? "left" : "right").click(function () {
                        f--;
                        i.loadTweetsPage(i.movePreviousPage);
                        return false
                    }))
                }
                p.append("<span>Page " + f + "</span>");
                return p
            }
        };
        i.initialize(this.first());
        return this.first()
    }
})(jQuery);
