$(function() {	 
	getSearch();
});

var timer,
	timerOn = 0;
	x = 0;

function tweetSlider() {
	t = setTimeout('startTimer()', 8000);	
}

function startTimer(a){
	var divCount = $("#recentTweets > div").length,
		firstDiv = $("#recentTweets div.tweet:first-child"),
		nextDiv = $("#recentTweets div.tweet:nth-child(4)");	
			
	firstDiv.fadeTo(500, 0, function() {
		$(this).slideUp(800, function() {
			$(this).remove();
			nextDiv.animate({"height": "toggle"}, "slow").removeClass('hiddenTweet');
		});
	}); 
	
	if (x == 6) {
		clearTimeout(timer);
		return false;
	} else {
		x++;
		timer = setTimeout('startTimer(x)', 8000);	
	}
}

function timeSince(yourTimestamp) {
	var e = new Date(yourTimestamp);
	dt = e.getDate();
	mn = e.getMonth();
	yr = e.getFullYear();
	hr = e.getHours();
	min = e.getMinutes();
	sec = e.getSeconds();
	
	ms = Date.UTC(yr,mn,dt,hr,min,sec);
	ms = Math.round(ms/1000); 
	
	cur = new Date();
	theDate = cur.getTime();
	theDate = Math.round((theDate/1000) + 3600); //ADD 3600 when observing BST
	
	seconds = theDate - ms;
    minutes = 0;
    hours = 0;
    days = 0;
    weeks = 0;
    months = 0;
    years = 0;
    if ( seconds == 0 ) {
    	seconds = 1;
    }
    if ( seconds > 60 ) {
        minutes =  seconds/60;
    } else {
        return add_s(seconds,'second');
    }

    if ( minutes >= 60 ) {
        hours = minutes/60;
    } else {
        return add_s(minutes,'minute');
    }

    if ( hours >= 24) {
        days = hours/24;
    } else {
        return add_s(hours,'hour');
    }

    if ( days >= 7 ) {
        weeks = days/7;
    } else {
        return add_s(days,'day');
    }
	if ( weeks >= 4 ) {
        months = weeks/4;
    } else {
        return add_s(weeks,'week');
    }

    if ( months >= 12 ) {
        years = months/12;
        return add_s(years,'year');
    } else {
        return add_s(months,'month');
    }
}

function add_s(num,word) {
	val = num*1;
    val = Math.round(val);
    if ( num == 1 ) {
        return val+' '+word+' ago';
    } else {
        return val+' '+word+'s ago';
    }
}

function linkify(text){
    if (text) {
        text = text.replace(
            /((https?\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi,
            function(url){
                var full_url = url;
                if (!full_url.match('^https?:\/\/')) {
                    full_url = 'http://' + full_url;
                }
                return '<a href="' + full_url + '">' + url + '</a>';
            }
        );
    }
    return text;
}


function getSearch() {
	$.getJSON(
		"http://search.twitter.com/search.json?callback=?&q=everythingconf&rpp=10",
		function(data){
			var theDiv = $("#recentTweets");
			$.each(data.results, function(index, item) {
				var result = timeSince(item.created_at),
				content = item.text,
				thisTweet;
				
				content = linkify(content);
				//content.replace(/(^|\s)@(\w+)/, '\1@<a href="http://www.twitter.com/\2">\2</a>');
				//content.replace(/(^|\s)#(\w+)/, '\1#<a href="http://search.twitter.com/search?q=%23\2">\2</a>');
				
				if (item.from_user !== "liulze"){
					thisTweet = "<div class='tweet'><a href='http://www.twitter.com/"+item.from_user+"'>"+item.from_user+"</a>: "+content+"<br /><p class='postData'></i>Posted "+result+"</div>";
					if (index < 10) {	
						if (index > 2) {
							var thisTweet = "<div class='hiddenTweet tweet'><a href='http://www.twitter.com/"+item.from_user+"'>"+item.from_user+"</a>: "+content+"<br /><p class='postData'></i>Posted "+result+"</div>";
						} else {
							var thisTweet = "<div class='tweet openTweet' rel=T"+index+"'><a href='http://www.twitter.com/"+item.from_user+"'>"+item.from_user+"</a>: "+content+"<br /><p class='postData'></i>Posted "+result+"</div>";
						}
					}
				}
				theDiv.append(thisTweet);			
			});
			tweetSlider();
		}
	);
}
