/////////////////////////////////////////////////////////
// vertical Newsticker
// author: Dominik Scholz <dominik.scholz@petitio.de>
// copyright: petitio gmbh werbeagentur
/////////////////////////////////////////////////////////

var newsticker = {
	
	// private vars
	_container: null,
	_news: [],
	_counter: 0,
	_delay: 5000,
	
	
	// print current page
	start: function()
	{
		this._container = document.getElementById('newsticker');
		if (this._getNews() > 0) this._fadeIn(0);
	},
	
	
	// fade container in
	_fadeIn: function(n)
	{
		var item = this._news[n];
		this._counter += .05;
		
		this._opacity(item.style, this._counter * 100);
		item.style.top = (20 - this._magic(this._counter) * 20) + 'px';
		item.style.display = 'block';
		
		if (this._counter < 1) window.setTimeout(this._fadeIn.bind(this, n), 50);
		else window.setTimeout(this._fadeOut.bind(this, n), this._delay);
	},
	
	
	// fade container out
	_fadeOut: function(n)
	{
		var item = this._news[n];
		this._counter -= .05;
		
		this._opacity(item.style, this._counter * 100);
		item.style.top = (this._magic(this._counter) * 20 - 20) + 'px';
		
		if (this._counter > 0) window.setTimeout(this._fadeOut.bind(this, n), 50);
		else {
			item.style.display = 'none';
			window.setTimeout(this._fadeIn.bind(this, (n + 1) == this._news.length ? 0 : (n + 1)), 50);
		}
	},
	
	
	// get news container into array
	_getNews: function()
	{
		var children = this._container.childNodes;
		
		for (var i = 0; i < children.length; i++)
		{
			var child = children[i];
			if (child.className == 'newsticker-item')
			{
				this._news.push(child);
			}
		}
		
		return this._news.length;
	},
	
	
	// set opacity for a given object
	_opacity: function(o, p)
	{
		var pInt       = Math.ceil(p);
		o.filter       = 'Alpha(opacity=' + pInt + ')';
		o.MozOpacity   = '' + pInt / 100;
		o.KTHMLOpacity = '' + pInt / 100;
		o.opacity      = '' + pInt / 100;
	},
	
	
	// convert with sinus-function for smoother animation
	_magic: function(pos)
	{
		return ((-Math.cos(pos * Math.PI) / 2) + 0.5);
	}
	
}

