
var BuzzReader = function(options){
	var url = "http://buzz.googleapis.com/feeds/{user}/public/posted";
	jQuery.extend(this,options || {});
	
	if(this.user === "") throw "The 'user' property is required";
	if(this.renderTo === "") throw "The 'renderTo' property is required";
	
	if(this.url === "")this.url = url.replace(/{user}/g,this.user);
	
	this.read();
};

BuzzReader.prototype = {
	renderTo: "",
	proxy	: "readfeed.php",
	user	: "",
	url		: "",
	items	: 10,
	onLoad	: function(){},
	onRender: function(){},
	
	render	: function(element){
		this.onRender.call(this,this);
		
		var html = [];
		html.push("<ul>");
		
		for(var i = 0; i < this.items || i < this.data.lenght;i++){
			html.push("<li><strong><a href=\""+this.data[i].uri+"\">"+this.data[i].author+"</a></strong><span>"+this.data[i].published+"</span>"+this.data[i].content+"</li>");
		}
		html.push("</ul>");
		
		this.el.append(html.join(""));
	},
	
	read	: function(){
		this.el = jQuery(this.renderTo);
		this.loader = this.el.append("<div class=\"buzz-loading\"></div>");
		jQuery.ajax({
			url		: this.proxy,
			data	: "url="+this.url,
			context	: this,
			success	: this.parse
		});
	},
	
	parse	: function(xml,status){
		var that = this;
		var nodes = jQuery("entry",xml);
		this.el.empty();
		var info = [];
		nodes.each(function(){
			var date = that.createDate(jQuery("published",this).text());
			info.push({
				title		: jQuery("title",this).text(),
				author		: jQuery("author > name",this).text(),
				uri			: jQuery("author > uri",this).text(),
				summary 	: jQuery("summary ").text(),
				content		: jQuery("content:first",this).text(),
				published	: that.format(date),
				updated		: jQuery("updated",this).text(),
				date		: date,
				reply		: jQuery("link[rel=replies]",this).attr("href")
			});
		});
		this.data = info;
		this.onLoad.call(this,info);
		this.render(this.renderTo);
	},
	
	format		: function(date){
		var diff   = (((new Date()).getTime() - date.getTime()) / 1000),
			days   = Math.floor(diff / 86400),
			months = Math.floor(days / 31);

		if (isNaN(days) || days < 0)return date.toString();
		
		if(days == 0){ 
			if(diff < 60)return "przed chwila";
			if(diff < 120)return "minute temu";
			if(diff < 3600)return Math.floor( diff / 60 ) + " minut temu";
			if(diff < 7200)return "godzine temu";
			if(diff < 86400)return  Math.floor( diff / 3600 ) + " godzin temu";
		}else if(days < 31){
			if(days == 1)return "wczoraj";
			if(days < 7)return days + " dni temu";
			if(days < 31)return Math.ceil( days / 7 ) + " tygodni temu";
		}else{
			if(months == 1)return "miesiac temu";
			if(months < 12)return Math.ceil( days / 31 ) + " miesiecy temu";
			if(months >=12)return Math.floor( days / 365 ) + " lat temu";
		}
	},
	
	createDate	: function(str){
		str = str.substring(0,19).replace(/[ZT]/," ").replace(/\-/g,"/");
		return new Date(str);
		var date = new Date();
		date.setDate(str.substring(8,10));
		date.setMonth(str.substring(5,7) - 1);
		date.setFullYear(str.substring(0,4));
		date.setUTCHours(str.substring(11,13));
		date.setUTCMinutes(str.substring(14,16));
		date.setUTCSeconds(str.substring(17,19));
		return date;
		
	} 
};



jQuery.fn.buzzReader = function(options){
	return this.each(function(){
		var opts = options || {};
		opts.renderTo = this;
		new BuzzReader(opts);
	});
};

