(function($){
	$.fn.twitter = function(options) {
	
		var defaults = {
			duration: 2000,
			user: 'TimeshareSale',
			pathToCSS: '/css/twitter.css',
			headBackgroundColor: '#8EC1DA',
			headTextColor: '#FFFFFF',
			linkColor: '#F78E1E',
			backgroundColor: '#C1D2EC'
		};
		
		var options = $.extend(defaults, options);
		
		return this.each(function() {
			var obj = $(this);
			buildFramework(options, obj);
			buildCustomCSS(options, obj);
			$.getJSON("http://twitter.com/statuses/user_timeline.json?screen_name="+options.user+"&callback=?", function(posts){
				renderViewer(posts, options, obj)
			});
		});
	};
})(jQuery);

function renderViewer(posts, options, obj){
	renderPost(posts[0], 0);
	var i = 1;
	var something = setInterval(function(){
		if(typeof posts[i] != 'undefined'){
			renderPost(posts[i], i)
			i++;
		}
		else {
			clearInterval(something);
			renderViewer(posts, options, obj);
		}
	}, options.duration);
}

function renderPost(posts, i){
	var replaced = posts.text.replace(/((http(s?):\/\/)|(www.))+([\w.1-9\+\&=#?\-~%;\/]+)/gi, '<a href="http$3://$4$5">$4$5</a>');
	replaced = replaced.replace(/(@(\w{1,15}))+/gi, '<a href="http://twitter.com/$2">$1</a>');
	var insert = 
		'<div id="tweet-id-'+i+'" class="twtr-tweet">'
			+'<div class="twtr-tweet-wrap">'
				+'<div class="twtr-tweet-text">'
					+'<p>'+replaced+'</p>'
				+'</div>'
			+'</div>'
		+'</div>'
	;
	$('#tweet-id-'+i).remove();
	$('.twtr-reference-tweet').append(insert);
	var h = $('.twtr-reference-tweet .twtr-tweet').height();
	$('.twtr-reference-tweet .twtr-tweet').prependTo('.twtr-tweets').css('height', '0px').animate({ height: h });
}

function buildFramework(options, obj){
	var framework = 
		'<a href="http://twitter.com/leisurelinkgolf"><div id="twitterHeader"><img src="/images/twitterFinal.gif" alt="Follow Leisure Link on Twitter!" /></div></a>'
		+'<div id="twtr-widget-1" class="twtr-widget twtr-paused">'
			+'<div style="width: 172px;" class="twtr-doc">'
			
				+'<div class="twtr-hd">'
					+'<div class="twtr-spinner twtr-inactive"></div>'
				+'</div>'
				
				+'<div class="twtr-bd">'
					+'<div style="height: 300px;" class="twtr-timeline">'
						+'<div class="twtr-tweets">'
							+'<div class="twtr-reference-tweet"></div>'
							
							/* Tweets go here */
							
						+'</div>'
					+'</div>'
				+'</div>'
				
			+'</div>'
		+'</div>'
		
		+'<a href="http://www.twitter.com/'+options.user+'"><div id="twitterFooter"></div></a>'
	;
	$('head').append('<link rel="stylesheet" type="text/css" href="'+options.pathToCSS+'" />')
	obj.prepend(framework);
}

function buildCustomCSS(options, obj){
	var css = 
		'<style type="text/css">'
			+'#twtr-widget-1 .twtr-doc, #twtr-widget-1 .twtr-hd a, #twtr-widget-1 h3, #twtr-widget-1 h4 {'
				+'background:'+options.headBackgroundColor+' none repeat scroll 0 0 !important;'
				+'color:'+options.headTextColor+' !important;'
			+'}'
			+'#twtr-widget-1 .twtr-tweet a {'
				+'color:'+options.linkColor+' !important;'
			+'}'
			+'#twtr-widget-1 .twtr-new-results, #twtr-widget-1 .twtr-results-inner, #twtr-widget-1 .twtr-timeline {'
				+'background:'+options.backgroundColor+' none repeat scroll 0 0 !important;'
			+'}'
		+'</style>'
	;
	$('head').append(css);
}
