var xhr = null;

function TickerTape(url,cssClass,scrollInterval,targetDiv) {
	this.dataUrl = url;
	this.cssClassName = cssClass;
	this.scrollInterval = scrollInterval;
	this.targetDiv = targetDiv;
	this.lastId = 0;
	this.panes = new Array();	
	this.currentpane = 0;
	this.panewidth = 0;
	this.movespeed = 15;
	this.moving = 0;
	this.loading = 0;
	this.stilltoscroll = 0;
	this.init();
}


TickerTape.prototype.init = function() {
	var randomId = "tickerTape" + Math.floor(Math.random()*999);
	document.write("<div class=\"Tickerpane\" id=TickerPane1>&nbsp;</div>");
	document.write("<div class=\"Tickerpane\" id=TickerPane2>&nbsp;</div>");
	this.panes[0] = document.getElementById('TickerPane1');
	this.panes[1] = document.getElementById('TickerPane2');
	this.container = document.getElementById(this.targetDiv);
	this.panes[0].style.left = 0;
	this.panes[0].style.top = 0;
	this.panes[1].style.left = 760;
	this.panes[1].style.top = -50;
	this.update();
	this.startup();
}

TickerTape.prototype.update = function () {
	dl = new Date();
	this.loading = 1;
	var concatCharacter = this.dataUrl.indexOf('?') > -1 ? '&' : '?';
	var urlWithParams = this.dataUrl + concatCharacter + "dump=" + parseInt(dl.getTime()/1000);

	if (this.currentpane == 0) {
		this.targetObj = this.panes[1];
	} else {
		this.targetObj = this.panes[0];
	}
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xhr = new ActiveXObject(Microsoft.XMLHTTP);
	}
	
	xhr.onreadystatechange = this.updateCallback.simpleBind(this, xhr);
	xhr.open("GET", urlWithParams);
	xhr.send("");
}

TickerTape.prototype.updateCallback = function(e) {
	if (xhr.readyState != 4) {
		return;
	}
	this.loading = 0;
	var json = eval(xhr.responseText);
	if (! json) {
		this.targetObj.innerHTML = '!!!! no info received !!!!';
		return;
	}
	this.numberReturned = json.length;
	var tekst = '';
	tekst = '<font class=TickerTitle>' + json[0].Title + ': </font>' + json[0].LinkText;
	this.targetObj.innerHTML = tekst;
	return;
}

TickerTape.prototype.startup = function() {
	var timeoutCallback = this.scroll.simpleBind(this);
	window.setInterval(function() { timeoutCallback(); }, this.scrollInterval);
	window.setTimeout(function() { timeoutCallback(); }, 100);
}	

TickerTape.prototype.scroll = function() {
	this.panewidth = this.container.offsetWidth;
	if (! this.moving && ! this.loading) {
		this.moving = 1;
		var context = this;
		this.stilltoscroll = this.container.offsetWidth;
		
		this.scrollIntervalId = window.setInterval(function() {
			context.stilltoscroll -= context.movespeed;

			if (context.stilltoscroll < 0) {
				context.stilltoscroll = 0;
			}
			if (context.currentpane == 0) {
				context.panes[0].style.left = context.stilltoscroll - context.panewidth;
				context.panes[1].style.left = context.stilltoscroll;
			} else {
				context.panes[1].style.left = context.stilltoscroll - context.panewidth;
				context.panes[0].style.left = context.stilltoscroll;
			}
			if (context.stilltoscroll == 0) {
				context.moving = 0;
				if (context.currentpane == 0) {
					context.currentpane = 1;
				} else {
					context.currentpane = 0;
				}
				window.clearInterval(context.scrollIntervalId);
				context.scrollIntervalId = null;
				context.update();
			}
		}, 20);
	}	
}
	
Function.prototype.simpleBind = function() {

	var	__method = this;
	var args =	[arguments[1]];
	var object =	arguments[0];

	return function() {
		return __method.apply(object, args);
	}
}