/*
 * JavaScript Pretty Date
 * Copyright (c) 2008 John Resig (jquery.com)
 * Licensed under the MIT license.
 * -- modified by Martin Tajur
 */
function prettyDate(date) {
	var offset = new Date;
	offset = offset.getTimezoneOffset()*60;

	diff = (((new Date()).getTime() - date.getTime()) / 1000) + offset,
	day_diff = Math.floor(diff / 86400);

	if (date.getTime() == 0) {
		return ' ';
	} else {
		return day_diff == 0 && (
			diff < 60 && "just praegu" ||
			diff < 120 && "1 minut tagasi" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minutit tagasi" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " tundi tagasi") ||
		day_diff == 1 && "Eile" ||
		day_diff < 0 && "just praegu" ||
		day_diff < 7 && day_diff + " päeva tagasi" ||
		day_diff < 13 && Math.floor( day_diff / 7 ) + " nädal tagasi" ||
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " nädalat tagasi" ||
		day_diff < 365 && Math.abs(Math.ceil( day_diff / 31 )) + " kuu tagasi" ||
		day_diff < 750 && "1 aasta tagasi" ||
		day_diff >= 730 && Math.abs(Math.floor( day_diff / 365 )) + " kuud tagasi";
	}
}

// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if ( typeof jQuery != "undefined" )
jQuery.fn.prettyDate = function(){
	return this.each(function(){
		var d;
		if (!jQuery(this).hasClass('done')) {
			if (d = Date.parseExact(trim(jQuery(this).text()), ['yyyy-MM-dd HH:mm:ss', 'yyyy-MM-dd'])) {
				jQuery(this).attr('title', d.toString('dddd, MMMM d, yyyy HH:mm'));
				var date = prettyDate(d);
				if (date) {
					jQuery(this).text(date);
					jQuery(this).addClass('done');
				}
			}
		}
	});
};

