(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().empty().addClass("twitted");

			// add heading to container element
			/*if (o.showHeading) {
				c.append("<h2>"+o.headingText+"</h2>");
			}*/
      
			// add twitter list to container element
			var twitterListHTML = "<div class=\"twitter_left_div\"><h2>"+o.headingText+"</h2><ul id=\"twitter_update_list\"><li></li></ul></div>";
			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\"><a href=\"http://twitter.com/"+o.userName+"\">http://twitter.com/"+o.userName+"</a></p>";
				c.append(profileLinkHTML);
			}

			// show container element
			c.show();

			$.getScript("http://twitter.com/javascripts/blogger.js");
			$.getScript("http://twitter.com/statuses/user_timeline/"+o.userName+".json?callback=twitterCallback2&count="+o.numTweets, function() {
				// remove preLoader from container element
				$(preLoaderHTML).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);
					});
				}
        var tt = 0;
        tl.find("span").each(function() {
       
//        userName = 'Tjanneke';        
        url = 'http://api.twitter.com/1/statuses/user_timeline/' + o.userName + '.json?callback=?';
//        var ProfileImage = "ProfileImageId"+tt;
        //alert(ProfileImage);
		$(this).attr("id", 'twittText');
				if($(this).text().length > 56)
				{
          $(this).text($(this).text().substring(0,'56',''))
        }
        /*$.getJSON(url, function (image) {        
       
            $("#"+ProfileImage).attr('src', image[0].user.profile_image_url);
            $("#"+ProfileImage).attr('width', '54');
            $("#"+ProfileImage).attr('height', '54');
			$("#"+ProfileImage).attr('border', '0');
            $("#"+ProfileImage).attr('style', 'float: left; padding-right: 0px;');
           // alert($("#"+ProfileImage))
        }); 
						ursRed = "http://twitter.com/"+ o.userName ;
    						//var myHTML = $(this).children("a");
    						if(ProfileImage){
    						 // $(this).append($('<img  src="" id='+ProfileImage+' />'))
    							$(this).append($('<a href="'+ursRed+'" target="_blank"><img  src="" id='+ProfileImage+' /></a>'))
							}
    					//	$(this).attr("title", imagehtml);
    		tt++;			*/
					
					});
tl.find("li").each(function() {
									url = 'http://api.twitter.com/1/statuses/user_timeline/' + o.userName + '.json?callback=?';
        var ProfileImage = "ProfileImageId"+tt;
        //alert(ProfileImage);
		var cliURL='http://twitter.com/';
		$(this).attr("id", 'twittText'+tt);
			
	
        $.getJSON(url, function (image) {        
       
            $("#"+ProfileImage).attr('src', image[0].user.profile_image_url);
            $("#"+ProfileImage).attr('width', '54');
            $("#"+ProfileImage).attr('height', '54');
			$("#"+ProfileImage).attr('border', '0');
            $("#"+ProfileImage).attr('style', 'float: left; padding-right: 0px;');
           // alert($("#"+ProfileImage))
        }); 
    						//var myHTML = $(this).children("a");
					ursRed = "http://twitter.com/"+ o.userName ;
    						if(ProfileImage)
							     $(this).append($('<a href="'+ursRed+'" target="_blank"><img  src="" id='+ProfileImage+' /></a>'))
//								 alert(dataF[tt])
	//							tl.find("span").remove('#twittText'+tt).text();
		//						$(this).append(dataF[tt])
								 
    							//$(this).html('<img  src=\"\" id=\"ProfileImage\" />');
    						
    					//	$(this).attr("title", imagehtml);
    		tt++;			
					
									});
      


				// 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});
					}

					tl.show().animate({height: tlHeight}, o.slideDuration);
				}
				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");
			});
		});
	};
})(jQuery);
