jQuery.fn.reverse=Array.prototype.reverse;
String.prototype.linkify=function()
{
    return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g,function(m)
    {
        if (m.length > 25) {
            var n = m.substring(0, 23);
            n = n + "..";
        }
        else {
            var n = m;
        }
        return n.link(m);
    });
};
String.prototype.linkuser=function()
{
    return this.replace(/[@]+[A-Za-z0-9-_]+/g,function(u)
    {
        var username=u.replace("@","")
        return u.link("http://twitter.com/"+username);
    });
};
String.prototype.linktag=function()
{
    return this.replace(/[#]+[A-Za-z0-9-_]+/,function(t)
    {
        var tag=t.replace("#","%23")
        return t.link("http://search.twitter.com/search?q="+tag);
    });
};
    
function reInitFacebox(objPass)
{ 
    jQuery(document).ready(function($) { 
        $('#' + objPass + ' a[rel*=facebox]').facebox(); 
    })
}    
        
function limitChars(textid, limit, infodiv)
{
    var text = $('#'+textid).val();
    var userlabel = $('#u_label').text();
    // argh.. IE includes the textarea text within the label, other browsers don't...
    var temp = new Array();
    temp = userlabel.split(':');
    userlabel = temp[0] + ': ';
    var textlength = text.length + userlabel.length + 1;
    if(textlength > limit)
    {
        $('#' + infodiv).html('You cannot send more than '+limit+' characters!');
        $('#' + textid).val(text.substr(0,text.length-1));
        return false;
    }
    else
    {
        $('#' + infodiv).html((limit - textlength));
        return true;
    }
}
function fetch_tweets(elem)
{
    elem=$(elem);
    input=elem.attr('htitle');
    lang=elem.attr('lang');
    geocode=elem.attr('geocode');
    loggedin=elem.attr('loggedin');
    topic=elem.attr('topic');
    catview=elem.attr('catview');
    catid=elem.attr('catid');
    ruser=elem.attr('ruser');
    if (ruser == null) { ruser = "ReallyTworried"; }
    limit_tweets = 4;
    if (catview == "1" || catview == "2") {  limit_tweets = 8; }
    rrp = limit_tweets;
    if(input!=window.tweets['text-'+input])
    {
        window.tweets['last_id'+input]=0;
        window.tweets['text-'+input]=input;
        window.tweets['count-'+input]=limit_tweets;
        window.tweets['topic-'+input]=topic;
        window.tweets['ruser-'+input]=ruser;
    ;
    }
    if(window.tweets['count-'+input]>4)
    {
        // elem.prepend('<div class="tweet"><img src="http://monitter.com/widget/favicon.gif" align="absmiddle" />real time twitter by: <a href="http://monitter.com" target="_blank">monitter.com</a></div>');
        window.tweets['count-'+input]=0;
    }
    var url="http://search.twitter.com/search.json?q="+input+"&lang="+lang+"&rpp="+rrp+"&since_id="+window.tweets['last_id'+input]+"&callback=?";
    if (geocode != null) {
       url = url + "&geocode=" + geocode;
    }
    var thistopic = window.tweets['topic-'+input];
    var thisruser = window.tweets['ruser-'+input];
    $.getJSON(url,function(json)
    {
        $('div.tweet:gt('+window.tweets['limit']+')',elem).each(function(){
        $(this).fadeOut('slow')});
        var tweetcnt = 0;
        elem.empty();
        $(json.results).reverse().each(function()
        {
            if($('#tw'+this.id,elem).length==0)
            {
                window.tweets['count-'+input]++;
                tweetcnt++;
                var thedate=new Date(Date.parse(this.created_at));
                var tmpmin = thedate.getMinutes();
                tmpmin = tmpmin + "";
                if (tmpmin.length == 1) { tmpmin = "0" + tmpmin;  }
                var thedatestr=thedate.getHours()+':'+tmpmin;
                var divstr = '';
                var classstr = '';
                if (tweetcnt == 1) { classstr = "tweet last"; }
                else { classstr = "tweet"; }
                
                if (loggedin == "no") {
                    if (catview == "1") {
                        divstr='<li class="tweet"><img width="48" height="48" alt="" src="'+this.profile_image_url+'" ><p><a href="http://twitter.com/'+this.from_user+'" target="_blank">'+this.from_user+'</a> '+this.text.linkify().linkuser().linktag()+'<a class="reply" title="Reply to this tweet" href="SignIn.aspx?ReturnURL=BrowseCategory.aspx?t=c%26q='+catid+'">Reply</a> <b>'+thedatestr+'</b></p></li>';
                    }
                    else if (catview == "2") {
                        divstr='<li class="tweet"><img width="48" height="48" alt="" src="'+this.profile_image_url+'" ><p><a href="http://twitter.com/'+this.from_user+'" target="_blank">'+this.from_user+'</a> '+this.text.linkify().linkuser().linktag()+'<a class="reply" title="Reply to this tweet" href="SignIn.aspx?ReturnURL=BrowseCategory.aspx?t=s%26q='+catid+'">Reply</a> <b>'+thedatestr+'</b></p></li>';
                    }
                    else {
                        divstr='<div id="tw'+this.id+'" class="'+classstr+'"><img width="48" height="48" alt="" src="'+this.profile_image_url+'" ><p><a href="http://twitter.com/'+this.from_user+'" target="_blank">'+this.from_user+'</a> '+this.text.linkify().linkuser().linktag()+' <b>'+thedatestr+'</b><a class="reply" title="Reply to this tweet" href="SignIn.aspx?ReturnURL=Default.aspx?tw=1">Reply</a></p></div>';
                    }              
                }              
                else {
                    if (catview == "1" || catview == "2") {
                        divstr='<li class="tweet"><img width="48" height="48" alt="" src="'+this.profile_image_url+'" ><p><a href="http://twitter.com/'+this.from_user+'" target="_blank">'+this.from_user+'</a> '+this.text.linkify().linkuser().linktag()+' <b>'+thedatestr+'</b></p><a class="reply" title="Reply to this tweet" onclick="javascript:setVisible(\'tweet-box\', \''+this.from_user+'\', \'tworries-'+tweetcnt+'\', \''+ thisruser +'\');">Reply</a></li>';
                    }
                    else {
                        divstr='<div id="tw'+this.id+'" class="'+classstr+'"><img width="48" height="48" alt="" src="'+this.profile_image_url+'" ><p><a href="http://twitter.com/'+this.from_user+'" target="_blank">'+this.from_user+'</a> '+this.text.linkify().linkuser().linktag()+'<a class="reply" title="Reply to this tweet" onclick="javascript:setVisible(\'tweet-box\', \''+this.from_user+'\', \''+tweetcnt+'-'+thistopic+'Summary_latestWorries\', \''+ thisruser +'\');">Reply</a> <b>'+thedatestr+'</b></p></div>';
                    }
                }

                window.tweets['last_id'+input]=this.id;
                if (tweetcnt < limit_tweets + 1)
                {
                    elem.prepend(divstr);
                    $('#tw'+this.id,elem).hide();
                    $('#tw'+this.id+' img',elem).hide();
                    $('#tw'+this.id+' img',elem).fadeIn(4000);
                    $('#tw'+this.id,elem).fadeIn('slow');
                    
                }
            }
        });
        // add empty tweet divs if less than limit_tweets results
        /*
        if (tweetcnt < limit_tweets) 
        {
            empties = limit_tweets - tweetcnt;
            for (i=1; i<=empties; i++)
            {
                if (i == empties)
                {
                    elem.append('<div class="tweet last"></div>');
                }
                else 
                {
                    elem.append('<div class="tweet"></div>');
                }
            }
        }
        */
        input=escape(input);
        rrp=limit_tweets;
        setTimeout(function(){
            fetch_tweets(elem)},90000);
        });
    return(false);
}
$(document).ready(function(){
    window.tweets={ };
    $('.tweets').each(function(e){
        rrp=8;
        fetch_tweets(this);
    });
});



