function NewsTicker(oAppendTo) 
{
	var oThis = this;
	this.timer = null;
	this.feeds = [];
	this.tickerContainer = document.createElement("div");
	this.ticker = document.createElement("div");
	this.tickerContainer.className = "newsTickerContainer";
	this.ticker.className = "newsTicker";
	
	this.tickerContainer.onmouseover = function () {
		oThis.stopTick();
	};
	this.tickerContainer.onmouseout = function () {
		oThis.tick();
	};
	
	this.tickerContainer.appendChild(this.ticker);
	var oToAppend = (oAppendTo)? oAppendTo:document.body;
	oToAppend.appendChild(this.tickerContainer);
	
	this.ticker.style.left = this.tickerContainer.offsetWidth + "px";
	this.tick();
	//more code to come
}

NewsTicker.prototype.tick = function () {
	var iTickerLength = this.ticker.offsetWidth;
	var oThis = this;
	
	if (this.ticker.innerHTML) {
		if (this.ticker.offsetLeft > -iTickerLength) {
			var iNewLeft = this.ticker.offsetLeft - 1;
			this.ticker.style.left = iNewLeft + "px";
		} else {
			this.ticker.style.left = this.tickerContainer.offsetWidth + "px";
		}
	}
	var doSetTimeout = function() {
		oThis.tick();
	};
	this.timer = setTimeout(doSetTimeout,1);
};
	
NewsTicker.prototype.stopTick = function () {
	clearTimeout(this.timer);
	this.timer = null;
};

NewsTicker.prototype.add = function (sUrl) {
	//document.write("feed added...."+sUrl);
	this.feeds.push(new NewsTickerFeed(this, sUrl));
};

NewsTicker.prototype.dispose = function () {
	for (var i = 0; i < this.feeds.length; i++) {
		this.feeds[i].dispose();
	
		this.stopTick();
		this.tickerContainer.parentNode.removeChild(this.tickerContainer);
		this.ticker = null;
		this.tickerContainer = null;
	}
};

function NewsTickerFeed(oParent, sUrl) {
	this.parent = oParent;
	this.url = sUrl;
	this.timer = null;
	this.container = null;
	this.poll();
}

NewsTickerFeed.prototype.poll = function () {
	var oThis = this;
	var sFullUrl = "news-ticker.php?url=" + encodeURIComponent(this.url);
	xparser.getFeed(sFullUrl, this.populateTicker, this);
	
	var doSetTimeout = function () {
		oThis.poll();
	};
	this.timer = setTimeout(doSetTimeout, 90000);
};

NewsTickerFeed.prototype.stopPolling = function () {
	clearTimeout(this.timer);
	this.timer = null;
};

NewsTickerFeed.prototype.populateTicker = function (oParser) {
	var spanTickerLinks = document.createElement("span");
	var aFeedTitle = document.createElement("a");
	aFeedTitle.className = "newsTicker-feedTitle";
	aFeedTitle.href = oParser.link.value;
	aFeedTitle.target = "_new";
	aFeedTitle.innerHTML = oParser.title.value;
	spanTickerLinks.appendChild(aFeedTitle);
	
	for (var i = 0; i < oParser.items.length; i++) {
		var item = oParser.items[i];
		var aFeedLink = document.createElement("a");
		aFeedLink.href = item.link.value;
		aFeedLink.target = "_blank";
		aFeedLink.className = "newsTicker-feedItem";
		aFeedLink.innerHTML = item.title.value;
		spanLinkContainer.appendChild(aFeedLink);
	}
	if (!this.container) {
		this.container = document.createElement("span");
		this.container.className = "newsTicker-feedContainer";
		this.parent.ticker.appendChild(this.container);
	} else {
		this.container.removeChild(this.container.firstChild);
	}
	this.container.appendChild(spanLinkContainer);
};

NewsTickerFeed.prototype.dispose = function () {
	if (this.timer) this.stopPolling();
	if (this.container) {
		this.parent.ticker.removeChild(this.container);
		this.container = null;
	}
	this.parent = null;
};