
Ticker = function () {
	if (! document.TEXT_NODE) {
		document.TEXT_NODE = 3;
	}
	var node_list = YAHOO.util.Dom.getElementsByClassName ('ticker', 'div');
	this._timers = [];
	for (var i = 0; i < node_list.length; ++i) {
		this._fix_whitespace (node_list[i]);
		this._set_style (node_list[i]);
		this._timers.push (YAHOO.lang.later (400, this, 'scroll', [node_list[i]], true));
	}	
};

Ticker.prototype = {

_fix_whitespace: function (node) {
	if (node.childNodes.length == 0)
		return;

	for (var i = 0; i < node.childNodes.length; ++i) {
		if (node.childNodes[i].nodeType == document.TEXT_NODE) {
			var t = node.childNodes[i].nodeValue;
			t = t.replace (/ /g, "&nbsp;");
			t = t.replace (/\s*/g, "");
			t = t.replace (/&nbsp;/g, "\u00a0");
			node.childNodes[i].nodeValue = t;
		}
		this._fix_whitespace (node.childNodes[i]);
	}
},

_set_style: function (node) {
	node.style.whiteSpace = "nowrap";
	node.style.overflow = "hidden";
},

scroll: function (node) {
	var fc = node.firstChild;

	if (fc.nodeType == document.TEXT_NODE) {
		var t = fc.nodeValue;
		if (t.length < 2) {
			// window.console.log ("scroll: removing empty text node");
			node.removeChild (fc);
		} else {
			var n = document.createTextNode (t.substr (1));
			node.replaceChild (n, fc);
		}
		
		var lc = node.lastChild;
		if (lc.nodeType == document.TEXT_NODE) {
			// window.console.log ("scroll: appending to last text node: '"+t.substr (0, 1)+"'");
			var n = document.createTextNode (lc.nodeValue + t.substr (0, 1));
			node.replaceChild (n, lc);
		} else {
			// window.console.log ("scroll: appending new text node");
			var nc = document.createTextNode (t.substr (0,1));
			node.appendChild (nc);
		}

	} else {
		var ftc = null;
		for (var t = fc; t != null; t = t.firstChild) {
			if (t.nodeType == document.TEXT_NODE) {
				ftc = t;
				break;
			}
		}

		if (! ftc) {
			// window.console.log ("Error: no text node found in "+fc);
			return;
		}

		var lc = node.lastChild;
		var found = null;
		for (var t1 = lc, t2 = fc; t1 != null && t2 != null; t1 = t1.lastChild, t2 = t2.firstChild) {
			if (t1.nodeName && t2.nodeName && t1.nodeName != t2.nodeName) {
				break;
			}

			if (t1.nodeType == document.TEXT_NODE && t2.nodeType == document.TEXT_NODE) {
				found = t1;
				break;
			}
		}
		
		var t = ftc.nodeValue;

		if (found) {
			// window.console.log ("scroll: appending to node "+found.parentNode+": '"+t.substr (0, 1)+"'");
			var n = document.createTextNode (found.nodeValue + t.substr (0, 1));
			found.parentNode.replaceChild (n, found);
		} else {
			var nt = null;
			for (var t1 = ftc; t1 != node; t1 = t1.parentNode) {
				if (t1.nodeType == document.TEXT_NODE) {
					nt = document.createTextNode (t.substr (0, 1));
					continue;
				}

				var t2 = t1.cloneNode (false);
				t2.appendChild (nt);
				nt = t2;
			}
			// window.console.log ("scroll: appending cloned tree "+nt);
			node.appendChild (nt);
		}

		if (t.length < 2) {
			var t1 = ftc;
			while (t1 != node) {
				var t2 = t1;
				t1 = t1.parentNode;
				// window.console.log ("scroll: removing empty node "+t2+" from "+t1);
				t1.removeChild (t2);
				if (t1.childNodes.length > 0) {
					// window.console.log ("scroll: stop removal, children left in node "+t1);
					break;
				}
			}
		} else {
			var n = document.createTextNode (t.substr (1));
			ftc.parentNode.replaceChild (n, ftc);
		}
	}
},

stop: function () {
	      for (var i = 0; i < this._timers.length; ++i) {
		      this._timers[i].cancel ();
	      }
      }
};

var t = null;

YAHOO.util.Event.onDOMReady (function () {
		t = new Ticker ();
		});

