﻿// JScript File

var twSinceId = null;
var twTimeout = null;
var newMsgFlag = false;
var timeOutSpace = 10;
var twitterUserName = null;
var firstMsgFlag = false;

function GetNewTwitterChatMsgs(time,tUsrName)
{   
    twitterUserName = tUsrName;
    StopNewTwitterChatMsgs();
    if(time==1)
    {
        GetTwitterChatMessages();
    }
    else
    {        
        twTimeout = setTimeout("GetTwitterChatMessages();", time);
    }
    fixCreateTime();
}

function StopNewTwitterChatMsgs() 
{
    if (twTimeout != null) 
    {
        clearTimeout(twTimeout);
    }
}

function fixCreateTime() 
{
    $each($$('.twCreateTime'), function(i,j){
	i.set('text', ' '+MsgCreateTime(i.get('alt')));
    });
}

function GetTwitterChatMessages() 
{   
    newMsgFlag = false;
    var twUrl = 'http://search.twitter.com/search.json?random=' + Math.random();
    if(twitterUserName.length>16)
    {
        twUrl += '&q=' + encodeURIComponent(twitterUserName.substring(1));
    }
    else
    {
        twUrl += '&q=' + encodeURIComponent(twitterUserName);
    }
    twUrl += '&callback=DrawTwitterChatMessages';    
    twUrl += '&rpp=10';
    if (twSinceId != null) 
    {
        twUrl += '&since_id=' + twSinceId;
    }
    var twScript = new Element('script');
    twScript.type = 'text/javascript';
    twScript.src = twUrl;
    
    $(document.body).grab(twScript);       
}

function DrawTwitterChatMessages(twInfo)
{   
    if (twSinceId == null || twInfo.max_id > twSinceId) 
    {
        twSinceId = twInfo.max_id;
        newMsgFlag = true;
        if(firstMsgFlag == true)
        {
            $g('TwitterMessagesDiv').innerHTML = "";
            firstMsgFlag = false;
        }        
    }
        
    if(twSinceId == -1)
    {
        $g('TwitterMessagesDiv').innerHTML = "<b style='color:#000000;text-align:center;width:480px;'>There are no messages for now</b>";
        firstMsgFlag = true;
    }

    var twMsgs = twInfo.results;   
    if(typeof(twMsgs)=="object")
    {
        for (var i=twMsgs.length-1;i>=0;i--) 
        {
            var twMsgTxt = "";
            var twMsg = twMsgs[i];        
            var twUserURL = 'http://twitter.com/' + twMsg.from_user;

            var outerDiv = new Element('div');           
            outerDiv.addClass('twOuterDiv');

            var twImgLink = new Element('a');
            twImgLink.target = "_blank";
            twImgLink.rel = "nofollow";
            twImgLink.href = twUserURL;        
            
            var twImg = new Element('img');
            twImg.src = twMsg.profile_image_url;
            twImg.width = 32;
            twImg.height = 32;
            twImg.alt = '';
            twImgLink.grab(twImg);        
            
            var twImgDiv = new Element('div');                        
            twImgDiv.addClass('twImgDiv');        
            twImgDiv.grab(twImgLink);
            
            outerDiv.grab(twImgDiv);
            
            var twUserName = new Element('b');
            twUserName.addClass('twUserName');
            twUserName.appendText(twMsg.from_user + " : ");
            
            var twMsgTextDiv = new Element('div');                        
            twMsgTextDiv.addClass('twMsgTextDiv');        
            twMsgTextDiv.grab(twUserName);                       
            
            var twTxtSpan = new Element('span');  
            twTxtSpan.addClass('twMsgText');   
            twMsgTxt = twMsg.text;
            twMsgTxt = twMsgTxt.replace(/ \(.* - http:\/\/blogtv.me\/.*\)/gi, ""); 
            twMsgTxt = twMsgTxt.replace(/ \(http:\/\/blogtv.me\/.*\)/gi, "");      
            twMsgTxt = replaceUrl2Link(twMsgTxt); 
            twTxtSpan.set('html', twMsgTxt);        
            twMsgTextDiv.grab(twTxtSpan);        
            
            var twCreateTime = new Element('span');   
            twCreateTime.addClass('twCreateTime');     
            var CreateTime = MsgCreateTime(twMsg.created_at);
            twCreateTime.set('alt', twMsg.created_at);
            twCreateTime.appendText(CreateTime);
            twMsgTextDiv.grab(twCreateTime);                       

            outerDiv.grab(twMsgTextDiv);

            $('TwitterMessagesDiv').grab(outerDiv, 'top');
            $$('.twOuterDiv.first').removeClass('first');
            $$('.twOuterDiv:first-child').addClass('first');        
        }
    }
    $each($$('.twOuterDiv'), function(obj, i) 
    {
    	if (i >= 100) 
    	{
    		obj.destroy();
    	}
    });

    if (newMsgFlag) {
    	timeOutSpace = timeOutSpace * 0.8;
    	if (timeOutSpace < 10) 
    	{
    		timeOutSpace = 10;
    	}
    } 
    else 
    {
    	timeOutSpace = timeOutSpace * 1.25;
    	if (timeOutSpace > 60) 
    	{
    		timeOutSpace = 60;
    	}
    }    
        
    GetNewTwitterChatMsgs(timeOutSpace*1000,twitterUserName);    
}

function MsgCreateTime(cTime) 
{
    var msgTime = new Date(cTime);
    var currentTime = new Date();
    var timeDifInSec = (currentTime - msgTime) / 1000;
    if (timeDifInSec < 60) 
    {
        return ' less than 1 minute ago';
    } 
    else if (timeDifInSec < 120) 
    {
        return ' about 1 minute ago';
    } 
    else if (timeDifInSec < 3600) 
    {
        var timeDifInMin = Math.floor(timeDifInSec / 60);
        return ' about ' + timeDifInMin + ' minutes ago';
    } 
    else if (timeDifInSec < 86400) 
    {
        var timeDifInHours = Math.floor(timeDifInSec / (3600));
        if (timeDifInHours == 1) 
        {
            return ' about 1 hour ago';
        } 
        else 
        {
            return ' about ' + timeDifInHours + ' hours ago';
        }
    } 
    else 
    {
        return ' ' + msgTime.toDateString();
    }
}

function replaceUrl2Link(txt)
{
    txt = txt.replace(/(ftp|http|https):\/\/[0-9a-zA-Z/.?#&]+(\b|$)(\/?)/gim,'<a href="$&" rel="nofollow" target="_blank">$&</a>');
    txt = txt.replace(/([^\/])(www[0-9a-zA-Z/.?#&]+(\b|$))(\/?)/gim, '$1<a href="http://$2" rel="nofollow" target="_blank">$2</a>');
    
    return txt;
}
 