(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/
		Z moimi przerobkami
	*/

	$.fn.getTwitter = function(options) {

		$.fn.getTwitter.defaults = {
			userName: null,
			numTweets: 4,
			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 ttl = $("#twitter_update_list");
			var len_ttl =ttl.children().size();
			if(len_ttl>0){
				var found = 0;
			 	ttl.find("li").each(function(i) {
					if( $(this).css('display') == 'inline' && i<len_ttl ){
						found = i;
						ttl.children().css('display', 'none');
						$(this).next().css('display', 'inline'); 
						return false;
					}					
				});
				if(found<len_ttl-1) {
					return 0;
				}
			}
			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 = "<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\"><a href=\"http://twitter.com/"+o.userName+"\">http://twitter.com/"+o.userName+"</a></p>";
					c.append(profileLinkHTML);
				}
	
				// show container element
				c.show();
				tl.show();
				//alert(tl.children().size());
				
				
				
			$.ajax({
                type: "GET",
                url: "/twitter",
                dataType: "xml",
				beforeSend: function(xhr){
					xhr.setRequestHeader('Cache-Control', 'max-age=600');
				},
                error: function(XMLHttpRequest, textStatus, errorThrown){
					//alert(textStatus+errorThrown);
				
				},
                success: function(xml) {
					//alert("POBRANO "+xml)
					var statusHTML = [];
				    $(xml).find('status').each(function(iteration ){
						var username = $(this).find('user').find('screen_name').text(); 
	                    var twitter_text = $(this).find('text').text();	
						var twitter_id = $(this).find('id:first').text();	
						var created_at =  $(this).find('created_at:first').text();	
						var status = twitter_text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
							return '<a href="'+url+'">'+url+'</a>';
						}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
							return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
						});
						var display ='';
						if(iteration>0) {
							display = 'style="display: none"';
						}
						statusHTML.push('<li '+display+' ><span>'+status+'</span> <a style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+twitter_id+'">'+relative_time(created_at)+'</a></li>');
					});
					document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
	              
				   
						// 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);
	    					});
	    				}

	    				// 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");
	    				
			}			
   		    }); //close $.ajax(	
		});
	};
})(jQuery);
function relative_time(time_value) {
	  var values = time_value.split(" ");
	  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	  var parsed_date = Date.parse(time_value);
	  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	  delta = delta + (relative_to.getTimezoneOffset() * 60);

	  if (delta < 60) {
	    return 'mniej niż minutę temu';
	  } else if(delta < 120) {
	    return 'około minutę temu';
	  } else if(delta < (60*60)) {
	    return (parseInt(delta / 60)).toString() + ' minut temu';
	  } else if(delta < (120*60)) {
	    return 'około godzinę temu';
	  } else if(delta < (24*60*60)) {
	    return 'około ' + (parseInt(delta / 3600)).toString() + ' godzin temu';
	  } else if(delta < (48*60*60)) {
	    return '1 day ago';
	  } else {
	    return (parseInt(delta / 86400)).toString() + ' dni temu';
	  }
}	
