/*
 * Helperfunction to get document.content from iframes
 */
HELPERS = {
	getIframeContent :function(iframe) {
		var ret = null;
		if(iframe.contentDocument) // Firefox, Opera
			ret = iframe.contentDocument;
		else if(iframe.contentWindow) // Internet Explorer
			ret = iframe.contentWindow.document;
		else if(iframe.document) // Others?
			ret = iframe.document;

		return ret;
	},
	addLoadEvent : function(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
        }
		else {
			window.onload = function()  { if (oldonload) { oldonload(); } func(); }
        }
    }
}

/**
 * A little helper for date functions
 */
MONTHS = {
	Jan:'01',Feb:'02',Mar:'03',Apr:'04',May:'05',Jun:'06',Jul:'07',Aug:'08',Sep:'09',Oct:'10',Nov:'11',Dec:'12'
}

function Ticker(url) {
	this.ticker = $('ticker');
	this.tickerwidth = this.ticker.getWidth();
	this.padding = 10;
	this.step = 2;
	this.fps = 25;

	/**
	 * Get the blog-feed from rss and start animating on success
	 */
	new Ajax.Request(url, {
		method: 'get',
		onSuccess:this.initialize.bind(this)
	});
}

Ticker.prototype.initialize = function(transport) {
	this.rss = transport.responseXML;
	this.rsslist = this.rss.getElementsByTagName('item');
	this.rsslistlength = this.rsslist.length;
	this.list = new Array();

	for (var i = 0; i<this.rsslistlength; i++) {
		var item = this.rsslist[i];
		var title,link,date = false;
		for (var e = 0; e < item.childNodes.length; e++) {
			if (item.childNodes[e].nodeType === 1) {
				switch(item.childNodes[e].nodeName) {
					case 'title' :
						title = item.childNodes[e].firstChild.data;
						break;
					case 'link' :
						link = item.childNodes[e].firstChild.data;
						break;
					case 'pubDate' :
						date = item.childNodes[e].firstChild.data;
						break;
				}
			}

			if (title !== false && link !== false && date !== false) break;
		}
		var d = /^[a-zA-Z]{3}..(\d{1,2}).([a-zA-Z]{3}).(\d{1,2})/.exec(date);
		date = d[1]+'.'+MONTHS[d[2]]+'.'+d[3]+':\u00A0';
		this.list.push(new Element('a',{href:link}).insert(date+title));
		this.list.push(new Element('span').insert('+++'));
	}
	this.listlength = this.list.length;

	for (var i = 0; i<this.listlength; i++) {
		this.ticker.insert(this.list[i]);
		var width = this.list[i].getWidth();
		this.list[i].setStyle({
			position:'absolute',
			width:width+"px",
			left:this.tickerwidth+"px"
		});
	}
 	this.currentString = 0;
 	this.animate();
}

/*
 * animate:
 * main animation loop.
 * move the main string, move the following strings
 * if the main-string is all inside the window
 */
Ticker.prototype.animate = function() {
	//clearInterval(this.animator);

	// The main-string:
	var i = this.currentString;

	//Loop trough and move all visible strings.
	do {
		var doAnother = false;
		var newLeft = parseInt(this.list[i].getStyle('left'),10)-this.step;
		var stringWidth = parseInt(this.list[i].getStyle('width'),10);

		if (stringWidth+newLeft > -5) {
			this.list[i].setStyle({left:newLeft+"px"});
		}
		else {
			/**
			 * Set new main-string (for next execution) if this one goes out of visibility
			 * the other strings are moved according to the main string.
			 */
			// Reset current string:
			this.list[i].setStyle({left:this.tickerwidth+"px"});
			if (++this.currentString == this.listlength) this.currentString = 0;
		}

		// Continue loop to next string if main-string/this string is all inside window.
		if (stringWidth + newLeft < this.tickerwidth-this.padding) {
			if (++i >= this.listlength) i = 0;
			doAnother = true;
		}
	} while(doAnother) //If we should move another string on this execution

	//And re-execute
	this.animator = window.setTimeout(this.animate.bind(this),(1000/this.fps));
}

document.observe('dom:loaded',function() {
		//var host = window.location.toString().match(/^http:\/\/[^\/]*/);
		new Ticker(www_root+"feeds/blog");
		// HACK
		//Remove the ugly frame around menuentries when you click
		$('mainMenu').observe('click',function(e){ e.element().blur(); });
});