﻿var pageNum = "";
var sinceId = "";
var getUpdate = false;
var getURL = "";
var mode = "empTweet";

/*convert twitter hyperlink*/
function textFormat(texto) {
    //make links
    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    texto = texto.replace(exp, "<a href='$1' class='extLink' target='_blank'>$1</a>");
    var exp = /[\@]+([A-Za-z0-9-_]+)/ig;
    texto = texto.replace(exp, "<a href='http://twitter.com/$1' class='profileLink' target='_blank'>@$1</a>");
    var exp = /[\#]+([A-Za-z0-9-_]+)/ig;
    texto = texto.replace(exp, "<a href='http://juitter.com/#$1' onclick='$.Juitter.start({searchType:\"searchWord\",searchObject:\"$1\"});return false;' class='hashLink' target='_blank'>#$1</a>");
    // make it bold
    if (mode == "searchWord") {
        tempParam = param.replace(/&ors=/, "");
        arrParam = tempParam.split("+");
        $.each(arrParam, function(i, item) {
            regExp = eval('/ ' + item + '/ig');
            newString = new String(' <b>' + item + '</b> ');
            texto = texto.replace(regExp, newString);
        });
    }
    return texto;
}

/*make xhr call*/
function getTweets(update) {
    getUpdate = update;
    if (sinceId !== "") {
        getURL = "TwitterList.ashx?mode=" + mode + "&getUpdate=" + getUpdate;
        if (getUpdate == true) getURL = getURL + "&sinceId=" + sinceId;
    }
    else {
        getUpdate = false;
        getURL = "TwitterList.ashx?mode=" + mode + "&getUpdate=" + getUpdate;
    }
    xhrCall();

    function xhrCall() {
        if (mode == "empTweet") 
    {
        
            $.getJSON(getURL, function(data, textStatus) {
                $.each(data, function(i, item) {
                    mHTML = "<li style='display:none;' id='" + item.id + "'><a href='http://www.twitter.com/" + item.user.name + "'><img src='" + item.user.profile_image_url + "' alt='" + item.user.name + "' class='tumb' /></a><div class='dialogContent'><p><span class='name'>" + item.user.name + "</span><a href='http://www.twitter.com/" + item.user.screen_name + "' class='followBtn' target='_blank' title='followBtn'>follow</a><span class='timeStamp'>" + item.created_at + "</span><span class='message'>" + textFormat(item.text) + "</span></p> </div></li>";
                    (getUpdate) ? $("#tempTweets").prepend(mHTML) : $("#tempTweets").append(mHTML);
                    $("#"+ item.id).fadeIn('slow');
                })
                sinceId = $("#tempTweets li:first").attr("id");

            });
 
    }
    else 
    {

            $.getJSON(getURL, function(data, textStatus) {
            $.each(data.results, function(i, item) {
                    mHTML = "<li  style='display:none;' id='" + item.id + "'><a href='http://www.twitter.com/" + item.from_user + "'><img src='" + item.profile_image_url + "' alt='" + item.from_user + "' class='tumb' /></a><div class='dialogContent'><p><span class='name'>" + item.from_user + "</span><a href='http://www.twitter.com/" + item.from_user + "' class='followBtn' target='_blank' title='followBtn'>follow</a><span class='timeStamp'>" + item.created_at + "</span><span class='message'>" + textFormat(item.text) + "</span></p> </div></li>";
                    (getUpdate) ? $("#tempTweets").prepend(mHTML) : $("#tempTweets").append(mHTML);
                    $("#" + item.id).fadeIn('slow');
                })
                sinceId = $("#tempTweets li:first").attr("id");

            });
            
    }
    }

}


function updateXhr(total) {
    $("#tempTweets li").each(function() {
        $(this).fadeOut("slow", function() {
            $(this).remove();
            total = total - 1;
            if (total <= 0) {
                getTweets(false);
            }
        });
    });
 }


 $(function() {

     $("#pupTweet").click(function() {
         mode = "public";
         if ($(this).hasClass("current") !== true) {
             total = ($("#tempTweets").children().size());
             updateXhr(total);
         }
         else {
             return false;
         }
     });

     $("#empTweet").click(function() {
         mode = "empTweet";
         if ($(this).hasClass("current") !== true) {
             total = ($("#tempTweets").children().size());
             updateXhr(total);
         }
         else {
             return false;
         }
     });


     $("#nav a").each(function() {
         $(this).click(function() {
             $("#nav a").removeClass("current");
             $(this).addClass("current");
         })
     })
 })

getTweets(false);
setInterval("getTweets(true)", 30000);








		