/******USAGE*******
PHP files (config.php, getTweet.php, TweetWithOAuth.class.php, OAuth.php, twitteroauth.php) must be required.

HTML CODE
<ul id="unique_id">Loading Tweets ... </ul>

JAVASCRIPT CODE
$('#unique_id').getTweetsList({
	url:'path/to/getTweet.php',
	screenName:'twitter screen name',
	options
});

---REQUIRED PARAMETERS---
url: (string) path to getTweet.php
screenName: (string) 

---OPTIONS---
duration: (int) duration (ms) to change next tweet / only single = true
speed: (int) speed (ms) to appear or disappear tweet / only single = true
showDate: (boolean) true to show tweet date
refresh: (int) time to refresh tweets using ajax (minutes)
count: (int) number of tweet to load
display: (int) number of tweet to display / only single = false
listMargin: (int) margin from list to ul frame
single: (boolean) true: show only 1 tweet, false: show tweets as list

******************/
(function($) {
	$.fn.getTweetsList = function(userArgs) {
		if(!this.length) return false;
		if(!this.is('ul') && !this.is('ol')) return this;
		
		var args = {
			duration:4000,
			speed:1000,
			url:'getTweet.php',
			showDate:true,
			refresh:0,
			count:5,
			display:5,
			screenName:'!!!',
			listMargin:5,
			single:true
		}
		$.extend(true, args, userArgs);
		
		var frame = this.css({'position':'relative','overflow':'hidden'});
		var currentItem = 0;
		var itemTotal = 0;
		var changeTimer;
		var ajaxTimer;
		
		connection();
		
		if(args.refresh > 0) {
			ajaxTimer = setInterval(function() {
				if(itemTotal > 1 && args.single) {
					clearInterval(changeTimer);
				}
				frame.empty();
				connection();
			}, args.refresh*60000);
		}
		
		function connection() {
			$.ajax({
				url:args.url+'?count='+args.count+'&screen_name='+args.screenName,
				dataType:'json',
				success:function(data) {
					if(!data.error) {
						makeList(data);
					}
				}
			});
			return false;
		}
		
		function makeList(data) {
			frame.empty();
			if(args.single)
				itemTotal = data.length;
			else
				itemTotal = args.display;
				
			for(var i=0; i<itemTotal; i++) {
				if(data[i]) {
					var content = data[i].text;
					if(args.showDate) content += '<br/><span>'+data[i].created_at+'</span>';
					var list = $('<li></li>').html(content);
					if(args.single) 
						list.css({'position':'absolute','top':0,'left':0,'opacity':0,'padding':args.listMargin+'px'});
					else
						list.css({'padding':args.listMargin+'px'});
					frame.append(list);
				}
			}
			content = null;
			list = null;
	
			if(args.single)
				frame.find('li').eq(0).animate({'opacity':1}, args.speed);
			
			if(itemTotal > 1 && args.single) {
				changeTimer = setInterval(function() {
					changeItem();
				}, args.duration);
			}
		}
		
		function changeItem() {
			frame.find('li').eq(currentItem).animate({'opacity':0}, args.speed, function() {
				currentItem++;
				$(this).css('opacity',0);
				if(currentItem >= itemTotal) currentItem = 0;
				frame.find('li').eq(currentItem).animate({'opacity':1}, args.speed);
			});
		}
		
		return this;
	}
})(jQuery);
