
function getTweets(user_name,tweet_container,limit){
    // Declare variables to hold twitter API url and user name
    var twitter_api_url = 'http://search.twitter.com/search.json';
    var twitter_user    = user_name;
    var counter = 0;

    // Enable caching
    $.ajaxSetup({ cache: true });

    // Send JSON request
    // The returned JSON object will have a property called "results" where we find 
    // a list of the tweets matching our request query
    //console.log(twitter_api_url+'?callback=?&page=1&since_id=19585392648&&rpp=10&q=from:' + twitter_user);
    $.getJSON(
    twitter_api_url + '?callback=?&rpp=10&q=from:' + twitter_user,
    function(data) {
        $.each(data.results, function(i, tweet) {
            //if(counter <= limit){
                // Uncomment line below to show tweet data in Fire Bug console
                // Very helpful to find out what is available in the tweet objects
                //console.log(tweet);

                // Before we continue we check that we got data
                if(tweet.text !== undefined) {
                    // Calculate how many hours ago was the tweet posted
                    var date_tweet = new Date(tweet.created_at);
                    var date_now   = new Date();
                    var date_diff  = date_now - date_tweet;
                    var hours      = Math.round(date_diff/(1000*60*60));

                    // Build the html string for the current tweet

                    var tweet_html = '';

                    /*
                    var tweet_html = '<div class="tweet_text">';
                    tweet_html    += '<a href="http://www.twitter.com/';
                    tweet_html    += twitter_user + '/status/' + tweet.id + '">';
                    tweet_html    += tweet.text + '<\/a><\/div>';
                    tweet_html    += '<div class="tweet_hours">' + hours;
                    tweet_html    += ' hours ago<\/div>';*/

                    var message = hyperlinks(tweet.text);
                    message = twitter_users(message);

                    tweet_html += '<br /><b>@'+twitter_user+': </b>'+message+'<br /> <a href="http://twitter.com/'+user_name+'/statuses/'+tweet.id+'"> '+$.timeago(date_tweet); +' hours ago</a><br /><br />';

                    // Append html string to tweet_container div
                    $('#'+tweet_container).append(tweet_html);
                    counter++;
                }
           // }
        });
    }
    );
}


function hyperlinks(text) {
    text = text.replace(/\s(http:\/\/[a-z][a-zA-Z0-9\/\*\-\?\.\&\%\$]*)/ig," <a href=\"$1\" class=\"twitter-link\">$1</a>");
    text = text.replace(/\s([a-zA-Z]+:\/\/[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)([\s|\.|\,])/ig," <a href=\"$1\" class=\"twitter-link\">$1</a>$2");
    // match www.something.domain/path/file.extension?some=variable&another=asf%
    text = text.replace(/\s(www\.[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)([\s|\.|\,])/ig," <a href=\"http://$1\" class=\"twitter-link\">$1</a>$2");      

    return text;
}

function twitter_users(text) {
    text = text.replace(/([\.|\,|\:|\¡|\¿|\>|\{|\(]?)@{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/ig, "$1<a href=\"http://twitter.com/$2\" class=\"twitter-user\">@$2</a>$3 ");
    return text;
}

/*
function sendAjaxRequest(url, eventhandler) {
if (window.ActiveXObject) {
var xhr = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
var xhr = new XMLHttpRequest();
}

xhr.onreadystatechange = function() {
if(xhr.readyState == 4)
if(xhr.status == 200) {
eventhandler(xhr);
} else {
//document.body.innerHTML = xhr.responseText;
}
}

xhr.open("POST", url, true );
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send("");
}

function getAjaxTwits(url, url2, itemCount) {
//var twitter = document.getElementById('AjaxTwits');
var twitter = $('#AjaxTwits');
var twitterload = $('#AjaxTwitsLoader');
var allTwittes = "";
sendAjaxRequest(url, function(ajaxRequest) {
if (ajaxRequest.responseText) {

var ans = eval('(' + ajaxRequest.responseText + ')');

if(ans['@attributes']['update'] == true) {
updateTwitter(url2);
}

if (ans.item) {
twitterload.remove();
var itemCount = ans.item.length;

for (var i = 0; i < itemCount; i++) {

var message = hyperlinks(ans.item[i].description);
message = twitter_users(message);

allTwittes += '<b>@'+ans.item[i].user+': </b>'+message+'<br /> '+ans.item[i].date+'<br /><br />';

}
//twitter.html('asdasdasdas');
twitter.html(allTwittes);
//$('homeBoxContent').html('labas123');
//alert(allTwittes);
}
}
});	
}

function updateTwitter(url) {
sendAjaxRequest(url, function(ajaxRequest) {
if (ajaxRequest.responseText) {
//console.log("twitter has been updated with the new xml");
}
});
}

function hyperlinks(text) {
text = text.replace(/\s(http:\/\/[a-z][a-zA-Z0-9\/\*\-\?\.\&\%\$]*)/ig," <a href=\"$1\" class=\"twitter-link\">$1</a>");
text = text.replace(/\s([a-zA-Z]+:\/\/[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)([\s|\.|\,])/ig," <a href=\"$1\" class=\"twitter-link\">$1</a>$2");
// match www.something.domain/path/file.extension?some=variable&another=asf%
text = text.replace(/\s(www\.[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)([\s|\.|\,])/ig," <a href=\"http://$1\" class=\"twitter-link\">$1</a>$2");      

return text;
}

function twitter_users(text) {
text = text.replace(/([\.|\,|\:|\¡|\¿|\>|\{|\(]?)@{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/ig, "$1<a href=\"http://twitter.com/$2\" class=\"twitter-user\">@$2</a>$3 ");
return text;
}
*/

