/*
 * Ticker 0.1 - A news ticker - in stable alpha stade!
 *
 * @requires Transformer.js
 * @param id unique id of the target element (block element!)
 * @param feed array of { a: "url", d: "description" }
 * @param speed value * 1ms/px = total sec;
 * @param popupstyle parameters for window.open() (if omitted then no popup but normal link)
 */

// requires Transformer.js
function Ticker(id,feed,speed,popupstyle) {

	// init params
	this.target = document.getElementById(id);
	this.feed = feed;
	this.popupstyle = popupstyle;
	this.speed = speed; // * 1ms/px = total sec;

	// event handlers + system hook
	if (!("ticker" in document)) document.ticker = [];
	document.ticker[id] = this;
	this.click = function (evt) {
		if (("isIE" in window) && isIE) target = evt.srcElement; // ie workaround
		else target = evt.target;
		var p = document.ticker[target.parentNode.id].popupstyle;
		if (p) window.open("","TickerPopup",p);
	};
	this.mouseover = function (evt) {
		if (document.all) target = evt.srcElement; // ie workaround
		else target = evt.target;
		document.ticker[target.parentNode.id].animator.stop();
	};
	this.mouseout = function (evt) {
		if (document.all) target = evt.srcElement; // ie workaround
		else target = evt.target;
		document.ticker[target.parentNode.id].animator.play();
	};
	this.appendNews = function (item) {
		var a = document.createElement("A");
//		a.href = item.a;
		if (this.popupstyle) a.target = "TickerPopup";
		if (("isIE" in window) && isIE) { // ie workaround
			a.attachEvent("onclick",this.click);
//			a.attachEvent("onmouseover", this.mouseover);
//			a.attachEvent("onmouseout", this.mouseout);
		} else {
			a.addEventListener("click",this.click, false);
//			a.addEventListener("mouseover", this.mouseover, false);
//			a.addEventListener("mouseout", this.mouseout, false);
		}
		a.appendChild(document.createTextNode(item.d));
		this.target.appendChild(a);
		this.target.appendChild(document.createTextNode(" + + + "));
	};

	// create container
	this.target.id = undefined;
	var s = document.createElement(("isIE" in window) && isIE ? "DIV" : "SPAN"); // todo: add workaround for IE
	s.className = "Text";
	s.id = id;
	this.target.appendChild(s);
	this.target = s;
		
	var si;
	// create news
	var sil = this.feed.length;
	for (si=0; si<sil; si++) {
		this.appendNews(this.feed[si]);
	}
	// Warning: offsetWidth = DOM Level 0!! There exists no spec here DOM 3 will be implemented!!
	var loopWidth = this.target.offsetWidth; 
	var windowWidth = this.target.parentNode.offsetWidth;
	this.target.style.width = loopWidth+"px"; 
	// ---------------------------------------------------------------------------
	for (si=0; (this.target.offsetWidth-loopWidth) < windowWidth; si++) {
		if (si >= sil) si = 0;
		this.appendNews(this.feed[si]);
	}
	
	// create renderer
	this.animator = new Transformer(id,{ fps: 40, length: 2, loop: true,
		0: { duration: Math.round(loopWidth*this.speed), style: { left: "0px" }},
		1: { duration: undefined, style: { left: "-"+loopWidth+"px" }}
	});
	// and animate!
	this.animator.play();
};
