$(document).ready(function(){

	/* {{{1
	 * Javascript Humane Dates
	 * Copyright (c) 2008 Dean Landolt (deanlandolt.com)
	 * Re-write by Zach Leatherman (zachleat.com)
	 *
	 * Adopted from the John Resig's pretty.js
	 * at http://ejohn.org/blog/javascript-pretty-date
	 * and henrah's proposed modification 
	 * at http://ejohn.org/blog/javascript-pretty-date/#comment-297458
	 *
	 * Modified by Jan Hnízdil for localization
	 *
	 * Licensed under the MIT license.
	 */
	function humanDate(date_str, hl) /*{{{1*/
	{
		var time = ('' + date_str).replace(/-/g,"/").replace(/[TZ]/g," "),
			dt = new Date,
			seconds = ((dt - new Date(time) + (dt.getTimezoneOffset() * 60000)) / 1000),
			token,
			i = 0,
			format;

		hl = hl[window.lang];

		if (seconds < 0) {
			seconds = Math.abs(seconds);
			token = '';
		}

		while (format = hl.formats[i++]) {
			if (seconds < format[0]) {
				token = i > 1 ? hl.token.text : '';
				if (format.length == 2) {
					if (hl.token.before) {
						return token + ' ' + format[1];
					}
					else {
						return format[1] + ' ' + token;
					}
				}
				else {
					if (hl.token.before) {
						return token + ' ' + Math.round(seconds / format[2]) + ' ' + format[1];
					}
					else {
						return Math.round(seconds / format[2]) + ' ' + format[1] + ' ' + token;
					}
				}
			}
		}

		// overflow for centuries
		if (seconds > 4730400000) {
			return Math.round(seconds / 4730400000) + ' Centuries' + token;
		}

		return date_str;
	};

	if (typeof jQuery != 'undefined') {
		jQuery.humanDate = humanDate;
	}

	Date.prototype.toISO8601String = function(format, offset) /*{{{1*/
	{
		/* accepted values for the format [1-6]:
		 1 Year:
		   YYYY (eg 1997)
		 2 Year and month:
		   YYYY-MM (eg 1997-07)
		 3 Complete date:
		   YYYY-MM-DD (eg 1997-07-16)
		 4 Complete date plus hours and minutes:
		   YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
		 5 Complete date plus hours, minutes and seconds:
		   YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
		 6 Complete date plus hours, minutes, seconds and a decimal
		   fraction of a second
		   YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
		*/
		if (!format) { var format = 6; }
		if (!offset) {
			var offset = 'Z';
			var date = this;
		} else {
			var d = offset.match(/([-+])([0-9]{2}):([0-9]{2})/);
			var offsetnum = (Number(d[2]) * 60) + Number(d[3]);
			offsetnum *= ((d[1] == '-') ? -1 : 1);
			var date = new Date(Number(Number(this) + (offsetnum * 60000)));
		}

		var zeropad = function (num) { return ((num < 10) ? '0' : '') + num; }

		var str = "";
		str += date.getUTCFullYear();
		if (format > 1) { str += "-" + zeropad(date.getUTCMonth() + 1); }
		if (format > 2) { str += "-" + zeropad(date.getUTCDate()); }
		if (format > 3) {
			str += "T" + zeropad(date.getUTCHours()) +
				   ":" + zeropad(date.getUTCMinutes());
		}
		if (format > 5) {
			var secs = Number(date.getUTCSeconds() + "." +
					   ((date.getUTCMilliseconds() < 100) ? '0' : '') +
					   zeropad(date.getUTCMilliseconds()));
			str += ":" + zeropad(secs);
		} else if (format > 4) { str += ":" + zeropad(date.getUTCSeconds()); }

		if (format > 3) { str += offset; }
		return str;
	}

	/*}}}1*/

	var
		o = ({ /*{{{1*/
			user: 'konferenceUX',
			count: 3,
			humanLocal: {
				cs: {
					centuries: 'stoletími',
					token: {
						before: true,
						text: 'před'
					},
					formats: [
						[60, 'právě teď'],
						[90, 'minutou'],
						[3600, 'minutami', 60],
						[5400, 'hodinou'],
						[86400, 'hodinami', 3600],
						[129600, 'dnem'],
						[604800, 'dny', 86400],
						[907200, 'týdnem'],
						[2628000, 'týdny', 604800],
						[3942000, 'měsícem'],
						[31536000, 'měsíci', 2628000],
						[47304000, 'rokem'],
						[3153600000, 'roky', 31536000],
						[4730400000, 'stoletím'],
					]
				},
				en: {
					centuries: 'centuries',
					token: {
						before: false,
						text: 'ago'
					},
					formats: [
						[60, 'Just Now'],
						[90, '1 Minute'],
						[3600, 'Minutes', 60],
						[5400, '1 Hour'],
						[86400, 'Hours', 3600],
						[129600, '1 Day'],
						[604800, 'Days', 86400],
						[907200, '1 Week'],
						[2628000, 'Weeks', 604800],
						[3942000, '1 Month'],
						[31536000, 'Months', 2628000],
						[47304000, '1 Year'],
						[3153600000, 'Years', 31536000],
						[4730400000, '1 Century'],
					]
				}
			}
		}), /*}}}1*/
		url = 'http://twitter.com/status/user_timeline/' +
			o.user + '.json?count=' + o.count + '&callback=?';

	$.getJSON(url, function(data){

		var
			frag = document.createDocumentFragment(),
			urlRe = new RegExp('' +
				'(https?:\\/\\/)?' +                                 // protocol
				'((([a-z\\d]([-a-z\\d]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
				'((\\d{1,3}\\.){3}\\d{1,3}))' +                      // OR ip (v4) address
				'(:\\d+)?(\\/[-a-z\\d%_.~+]*)*' +                    // port and path
				'(\\?[-;&a-z\\d%_.~+=]*)?' +                         // query string
				'(\\#[-a-z\\d_]*)?',                                 // fragment locater
			'ig');

		$.each(data, function(i, val){
			var
				text = val.text,
				created = new Date(
					val.created_at.replace(/^[A-Z][a-z]+\s([A-Z][a-z]+)\s(\d+)\s(\d\d:\d\d:\d\d).*(\d{4})$/, '$1 $2, $4 $3')
				);
			text = text.replace(urlRe,
				'<a href="$&">[link]</a>');
			text = text.replace(/(^|\s)@(\w+)/g,
				'$1@<a href="http://www.twitter.com/$2">$2</a>');
			text = text.replace(/(^|\s)#(\w+)/g,
				'$1#<a href="http://search.twitter.com/search?q=%23$2">$2</a>');
			frag.appendChild($('' +
				'<tr>' +
					'<td>' +
						'<div class="tweet">' +
							text + ' ' +
							'<a class="status" href="http://twitter.com/'+val.user.screen_name+'/statuses/'+val.id+'">' +
								$.humanDate(created.toISO8601String(), o.humanLocal) +
							'</a>' +
						'</div>' +
						'<div class="bottom"></div>' +
					'</td>' +
				'</tr>' +
			'')[0]);
		});

		$('#tweets .replace').replaceWith(frag);

	});

});

