﻿// This is a self invoking function. 
// The $ parameter allows you to reference the jQuery item.
(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) {
				
				//place the tweet within a paragraph tag.
				holder.html("<div style='width: 71px; height: 97px; background: url(newImages/tweetie.gif) no-repeat right; float: right; margin-right: 10px;'></div><div style='width: 250px; float: right; font-size: 10px; margin: 10px 0 0 0;'><div style='width: 10px; height: 10px; background: url(newImages/tweetie_tr.gif) no-repeat right; float: right;'></div><div style='background-color: #e9f4f7; width: 230px; height: 10px; float: right;'></div><div style='width: 10px; height: 10px; background: url(newImages/tweetie_tl.gif) no-repeat left; float: left;'></div><div style='background-color: #e9f4f7; width: 230px; padding: 0 10px 0 10px; float: right;'>"+item.text.makeLinks()+"</div><div style='width: 10px; height: 10px; background: url(newImages/tweetie_br.gif) no-repeat right; float: right;'></div><div style='background-color: #e9f4f7; width: 230px; height: 10px; float: right;'></div><div style='width: 10px; height: 10px; background: url(newImages/tweetie_bl.gif) no-repeat left; float: left;'></div><div style='float:right; width: 250px; font-size: 12px; margin: 10px 0 10px 0;'><a href='http://twitter.com/MillerCreekBand' title='Follow Miller Creek on Twitter and never miss breaking news!' target='_blank' style='color: #009add; text-decoration: none; background: url(newImages/twitter_logo.gif) no-repeat right; padding: 2px 70px 0 10px;'>Read more and follow us on</a></div");
	
			});
	    });
	    
    }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);