(function($) {
	/*
		jquery.twitter.js v1.5
		Last updated: 08 July 2009

		Created by Damien du Toit
		http://coda.co.za/blog/2008/10/26/jquery-plugin-for-twitter

		Licensed under a Creative Commons Attribution-Non-Commercial 3.0 Unported License
		http://creativecommons.org/licenses/by-nc/3.0/
	*/

	$.fn.getTwitter = function(options) {

		$.fn.getTwitter.defaults = {
			userName: null,
			numTweets: 5,
			loaderText: "Loading tweets...",
			slideIn: true,
			slideDuration: 750,
			showHeading: true,
			headingText: "Latest Tweets",
			showProfileLink: true,
			showTimestamp: true
		};

		var o = $.extend({}, $.fn.getTwitter.defaults, options);

		return this.each(function() {
			var c = $(this);

			// hide container element, remove alternative content, and add class
			c.hide().addClass("twitted");

			/*/ add heading to container element
			if (o.showHeading) {
				c.append("<h2>"+o.headingText+"</h2>");
			}*/
			
			var twitterFollow = "<p class=\"twitter_followers\"></p>";
			c.append(twitterFollow);
			var tf = $("#twitterFeed .twitter_followers");
			tf.hide();

			// add twitter list to container element
			var twitterListHTML = "<ul id=\"twitter_update_list\"><li></li></ul>";
			c.append(twitterListHTML);

			var tl = $("#twitter_update_list");

			// hide twitter list
			tl.hide();

			// add preLoader to container element
			var preLoaderHTML = $("<p class=\"preLoader\">"+o.loaderText+"</p>");
			c.append(preLoaderHTML);

			// add Twitter profile link to container element
			if (o.showProfileLink) {
				var profileLinkHTML = "<p class=\"profileLink\">Follow us: <a href=\"http://twitter.com/"+o.userName+"\">twitter.com/"+o.userName+" &raquo;</a></p>";
				c.append(profileLinkHTML);
				var pl = $("#twitterFeed .profileLink");
				pl.hide();
			}

			// show container element
			c.show();
			$.ajaxSetup({ cache: true });
			$.ajax({
			    url: "/service/services.asmx/getTwitter",
                type: "POST",
			    data: 'username='+o.userName+'&msgCount='+o.numTweets,
                contentType: "application/x-www-form-urlencoded",
                dataType: "xml",
			    success: function(data) {
			        //execute the returned string
			        //$.globalEval(data.childNodes[0].nodeValue);
			        var json = $.xml2json(data);
			        $.globalEval(json);
				    // remove preLoader from container element
				    $(preLoaderHTML).fadeOut(100, function(){
					    $(this).remove();
				    });

				    // remove timestamp and move to title of list item
				    if (!o.showTimestamp) {
					    tl.find("li").each(function() {
						    var timestampHTML = $(this).children("a");
						    var timestamp = timestampHTML.html();
						    timestampHTML.remove();
						    $(this).attr("title", timestamp);
					    });
				    }
				    // add twitter bird
				    tl.prepend("<li class=\"twitter_bird\"></li>");
				    var tb = $("#twitterFeed .twitter_bird");
				    tb.hide();
    				    
				    // show twitter list
				    if (o.slideIn) {
					    // a fix for the jQuery slide effect
					    // Hat-tip: http://blog.pengoworks.com/index.cfm/2009/4/21/Fixing-jQuerys-slideDown-effect-ie-Jumpy-Animation
					    var tlHeight = tl.data("originalHeight");

					    // get the original height
					    if (!tlHeight) {
						    tlHeight = tl.show().height();
						    tl.data("originalHeight", tlHeight);
						    tl.hide().css({height: 0});
					    }
					    tf.fadeIn(o.slideDuration);
					    pl.fadeIn(o.slideDuration);
					    //
					    tl.show().animate({height: tlHeight}, o.slideDuration, function() {
						    tb.fadeIn(400);
					    });
				    }
				    else {
					    tl.show();
				    }

				    // add unique class to first list item
				    tl.find("li:first").addClass("firstTweet");

				    // add unique class to last list item
				    tl.find("li:last").addClass("lastTweet");
			    },
                error: function() {                   
				    $(preLoaderHTML).fadeOut(100, function(){
					    $(this).remove();
				    });
				    tl.prepend("<div class=\"twitter_error\">Sorry an error occured retrieving this feed.</div>");
				    var tb = $("#twitterFeed .twitter_bird");
				    tb.hide();
				    var tlHeight = tl.data("originalHeight");

				    // get the original height
				    if (!tlHeight) {
					    tlHeight = tl.show().height();
					    tl.data("originalHeight", tlHeight);
					    tl.hide().css({height: 0});
				    }
				    tl.fadeIn(750);
                }
            });
		});
	};
})(jQuery);
