// JavaScript Document
var numOfTwits = 3;

$(function() {
  setInterval(update, 30000);
});

function update() {
 $.ajax({
   type: "POST",
   url: "/includes/get.php",
   data: "name=None",
   success: function(result){
   	
	if (result == 1) {
		//alert (result);
		//new data so display it
	display(1);
	getTweetCount();
	}
	 }
 });
}



function display(update_data) {
	var jUrl = "/includes/display_tweets.php?numResults=" + numOfTwits;
$.getJSON(jUrl, function(tweetData){
	// alert("JSON Data: " + tweetData);
	 $.each(tweetData, function(i,item){
           //alert(item.id + " : " + item.text);
		   //var twitter_id = i;
		   if (update_data == 1) { //data exists so check before update
		   var hasFound = 0;
		   $( "#tweetContainer div" ).each(function () {

									 var title = this.id;
									 //alert (title);
									 if (title == item.id) {
										 //already exists in the doc
										 hasFound = 1;
									 }
		    }); 
		   
		  
		  
		   if (hasFound == 0) {
				// Not found so you can add a new one
		  		$("#tweetContainer").prepend("<div class='new' id=" + item.id + "><p>" + (item.text).replace(/(https?:\/\/[^ ;|\\*'"!,()<>]+\/?)/g,'<a href="$1" target="_blank">$1</a>') + "</p></div>");
				$("#tweetContainer div:first").hide();			
		   }
		   
		   } else { //fresh data just shove it all in!
		   
		   $("#tweetContainer").append("<div class='new' id=" + item.id + "><p>" + (item.text).replace(/(https?:\/\/[^ ;|\\*'"!,()<>]+\/?)/g,'<a href="$1" target="_blank">$1</a>') + "</p></div>");
		   $( "#tweetContainer div").hide();
		   
		   }
		  
          }); // end each tweetdata
	 

	$("#tweetContainer div.new:last").fadeItem(0, 500);

   });
}


function getUserData() {
	var userDataUrl = "/includes/twitter_user_detail.php";
$.getJSON(userDataUrl, function(userData){
						 //what to do with user data
						 //alert(userData[0].profile_image_url);
						 var imgUrl = userData[0].profile_image_url;
						 var followers_count = userData[0].followers_count;
						 var statuses_count = userData[0].statuses_count;
						 var friends_count = userData[0].friends_count;
						 var screen_name = userData[0].screen_name;
						 
						 $("#avatar").html("<a href='http://twitter.com/" + screen_name + "' target='_blank'><img src='" + imgUrl + "' /></a>");
						 
						
						 // $("#profile_info p.data").append(" " + followers_count + " followers");
						 //  $("#profile_info p.data").append(" " + friends_count + " friends");
						   
						 $("#profile_info p").html("Twitter<br /><a href='http://twitter.com/" + screen_name + "' target='_blank'>@" + screen_name + "</a><br /><span class='data'></span>");
						 $("#profile_info span.data").html("" + statuses_count + " tweets");
						 });
}


function getTweetCount() {
		var userDataUrl = "/includes/twitter_user_detail.php";
$.getJSON(userDataUrl, function(userData){
						 //what to do with user data
						 //alert(userData[0].profile_image_url);
						 var statuses_count = userData[0].statuses_count;
						 
						 $("#profile_info span.data").html("" + statuses_count + " tweets");
						 
						 });
}


$(document).ready(function() {
						   jQuery.fn.fadeItem = function(outspeed, inspeed) {
							   
							   //remove extra elements
	var numElements = $( "#tweetContainer div").size();
	//alert (numElements);
	if (numElements > numOfTwits) {
		$( "#tweetContainer div:last").fadeOut(outspeed, function() { $(this).remove(); });
		//numElements = $( "#tweetContainer div").size();
	}
	
    $(this).fadeOut(outspeed).fadeIn(inspeed, function()
    {
		 $(this).removeClass("new");
        $(this).prev().fadeItem(outspeed, inspeed);//pass all details on to the next item
    });
	}
				   			getUserData();
						   display(0);
						   
						   
						   
});


