if (!window.NewsTicker)
	window.NewsTicker = {};

NewsTicker.Page = function(controlId, tickerData, backgroundUrl, animationSpeed) 
{
	this.controlId = controlId;
	this.tickerData = tickerData;
	this.backgroundUrl = backgroundUrl;
	this.animationSpeed = animationSpeed;
}

NewsTicker.Page.prototype =
{
	newsIndex: 0,
	
	handleLoad: function(control, userContext, rootElement) 
	{
		this.control = control;
		this.root = rootElement;
		
		this.control.content.findName("imgBackground").Source = this.backgroundUrl;
		this.txtTicker1 = this.control.content.findName("txtTicker1");
		this.txtTicker2 = this.control.content.findName("txtTicker2");
		this.Fade1 = this.control.content.findName("TickerFade1");
		this.Fade2 = this.control.content.findName("TickerFade2");
		
		this.txtTicker2.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleNewsClick));

		var x = 0;
		var y = this.tickerData.length - 1;
		this.newsIndex = x + Math.floor(Math.random() * (y - x + 1));
		this.nextNews();
	},
	
	
	// Nächste News einblenden
	nextNews: function() {
		if (this.tickerData.length == 0) return;
		if (this.newsIndex >= this.tickerData.length) this.newsIndex = 0;
		
		this.txtTicker1.Text = this.txtTicker2.Text;
		this.txtTicker2.Text = this.tickerData[this.newsIndex].Text;
		this.txtTicker2.Tag = String(this.newsIndex);
		this.Fade1.Begin();		
		this.Fade2.Begin();
		
		this.newsIndex++;
		// Nächste News automatisch aufrufen
		window.setTimeout(this.controlId + ".nextNews()", this.animationSpeed);		
	},
	
	// Auf News geklickt
	handleNewsClick: function(sender, eventArgs) 
	{
		var index = parseInt(this.txtTicker2.Tag);
		window.location.href = this.tickerData[index].Link;
	}
}