/**
 * refresh timeout in seconds
 */

var refresh_timeout = 25;

/**
 * web base where the script is installed
 * it is automatically set.
 */

var base = '';

/**
 * on document load set observers on links
 */

document.observe("dom:loaded", 
	function() { 
		a = $$("base");
		var url = a[0].readAttribute('href').substring(7);	
		base = url.substring(url.indexOf('/'));

		refresh(); 
	}
);

/**
 * refresh website with new tweets
 */

function refresh() {
	window.setTimeout(update, refresh_timeout * 1000);
}

/**
 * on click event load ajax content and show it with effects
 */

function update(event) { 
	var url = base+'update';

	new Ajax.Request(url, {
		method:'get',
		onSuccess: function(transport) {
			var text = transport.responseText;
			$('main').insert({top: text});
			var tmp = $$('.tweet');
			
			if(tmp.length>20) {
				r = tmp.length - 20;
				tmpl = tmp.length;
				for(i=0;i<r;i++) {
					tmp[tmpl-i-1].remove();
					new Effect.Highlight(tmp[i], { startcolor: '#ffff99',
					endcolor: '#ffffff' });
				}
				
				dates = $$('.date')
				for(i=(r);i<20;i++) {
					dates[i].update('more than '+refresh_timeout+' seconds ago.');
				}
				
			}
			refresh();
		}
	});
}
