﻿// This is a self invoking function. 

// The $ parameter allows you to reference the jQuery item.

function fnShowProps(obj, objName){
   var result = "";
   for (var i in obj) // обращение к свойствам объекта по индексу
       result += objName + "." + i + " = " + obj[i] + "<br />\n";
   
//  alert(result);     
//       document.write(result);
}


function formatTwitString(str)
{
	str=' '+str;
	str = str.replace(/((ftp|https?):\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?)/gm,'<a href="$1" target="_blank">$1</a>');
	str = str.replace(/([^\w])\@([\w\-]+)/gm,'$1@<a href="http://twitter.com/$2" target="_blank">$2</a>');
	str = str.replace(/([^\w])\#([\w\-]+)/gm,'$1<a href="http://twitter.com/search?q=%23$2" target="_blank">#$2</a>');
	return str;
}


function relativeTime(pastTime)
{	
	var origStamp = Date.parse(pastTime);
	var curDate = new Date();
	var currentStamp = curDate.getTime();
	
	var difference = parseInt((currentStamp - origStamp)/1000);

//	alert(difference);
	if(difference < 0) return false;

	if(difference <= 5)		return "Just now";
	if(difference <= 20)		return "Seconds ago";
	if(difference <= 60)		return "A minute ago";
	if(difference < 3600)		return parseInt(difference/60)+" minutes ago";
	if(difference <= 1.5*3600) 	return "One hour ago";
	if(difference < 23.5*3600)	return Math.round(difference/3600)+" hours ago";
	if(difference < 1.5*24*3600)	return "One day ago";
	
	
	var dateArr = pastTime.split(' ');
	var strR = dateArr[4].replace(/\:\d+$/,'')+' '+dateArr[2]+' '+dateArr[1]+(dateArr[3]!=curDate.getFullYear()?' '+dateArr[3]:'');

	
	return strR.replace('+0000','');
	
	
}



(function($){



// fn is essentially the same as prototype.

// so this will be used like $('#jqueryItem).twitterize(username)

$.fn.twitterize = function(username,options){

	

	//check to see if the username has been set.

	if (username){

		

		//create an object full of your default settings.

		var defaultSettings = {

			count:'1'

		}

		

		// Finds which default settings have been overwritten by the options object

		// and creates a new object with all the new and untouched settings.

		var settings = $.extend(defaultSettings, options);

	

		// The URL for the Twitter JSON API.

		var url = "http://twitter.com/status/user_timeline/"+username+".json?count="+settings.count+"&callback=?";

		

		//Variable to get around scope problem in callback function.

		var holder = this;

		

		$.getJSON(url,

		

		//This function is called when twitter responds with the appropriate information.

	    function(data){

	    	

	    	//Step through each tweet.

			$.each(data, function(i, item) {

				
//fnShowProps(item);
//alert(item.id_str);
var str = '	<div class="tweet">\
<table width="316" border="0" cellspacing="0" cellpadding="0" id="twitter-td">\
<tr>\
<td height="96" style="background-color:#09030d;"><div class="txt" style="padding-left:7px;padding-right:30px;padding-bottom:3px;">'+formatTwitString(item.text)+'<br /><br /><a target="_blank" href="http://twitter.com/#!/nlagency/status/'+item.id_str+'">'+relativeTime(item.created_at)+'</a> via '+item.source+'&nbsp;&nbsp;&nbsp;<a target="_blank" href="http://twitter.com/intent/favorite?tweet_id='+item.id_str+'">Favorite</a>&nbsp;&nbsp;&nbsp;<a target="_blank" href="http://twitter.com/intent/retweet?tweet_id='+item.id_str+'">Retweet</a>&nbsp;&nbsp;&nbsp;<a target="_blank" href="http://twitter.com/intent/tweet?in_reply_to='+item.id_str+'">Reply</a></div></td>\
</tr>\
<tr>\
<td height="1" style="background:url(twitter/bg_line.gif) 0 0 repeat-x;">\
<img src="twitter/bg_line.gif" />\
</td>\
</tr>\
</table>\
</div>';		

holder.append(str);

//
//
//
//
//undefined.coordinates = null<br />
//undefined.in_reply_to_user_id = 62520727<br />
//undefined.favorited = false<br />
//undefined.in_reply_to_status_id_str = null<br />
//undefined.user = [object Object]<br />
//undefined.created_at = Thu Aug 11 22:12:21 +0000 2011<br />
//undefined.place = null<br />
//undefined.in_reply_to_screen_name = nlagency<br />
//undefined.truncated = false<br />
//undefined.in_reply_to_user_id_str = 62520727<br />
//undefined.contributors = null<br />
//undefined.retweeted = false<br />
//undefined.source = web<br />
//undefined.id_str = 101778004857978881<br />
//undefined.retweet_count = 0<br />
//undefined.in_reply_to_status_id = null<br />
//undefined.id = 101778004857978880<br />
//undefined.geo = null<br />
//undefined.text = @nlagency bringing another great urban tour to #Australia, coming up @TheRealEve Sep 21 - 25. Tasmania, Melbourne, Gold Coast, Brisbane<br />



				//place the tweet within a paragraph tag.

//				holder.append("<p>"+item.text.makeLinks()+"</p>");

	

			});

	    });

	    

    }else{

    	//Username paramater hasn't been set.

    	console.debug("jquery plugin twitterize requires a username! Check your parameters");



    }

    

    

    //Changes urls within the tweet into links.

    String.prototype.makeLinks = function() {

		return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(str) {

    	return str.link(str);

 		});

 	}; 



	

//Return itself

return this;



};



// We pass "jQuery" to the self invoking function 

// so we can use whatever alias of jQeury that we like. 

// So instead of "$" you could also use any other 

// valid JavaScript variable name.



})(jQuery);
