// Wpl news ticker - called with an array of items and urls like this:
//
// items = [
//   ['this is the title', 'http://ross.com'],
//   ['ross is elite & stuff ', 'http://ross.com']
// ];
//
//
Wpl.Ticker = function (items, el) {
	this._items = items;
	
	var a = html.a("Loading");
	el.appendChild(a);

	this._el = a;
	this._itemPosition = 0;
	this._strPosition = 0;
	var thiss = this;
	Event.observe(window, "load", function () {thiss.start()});
}

Wpl.Ticker.prototype = {};
Wpl.Ticker.PAUSE_COUNT = 80;
Wpl.Ticker.TYPE_DELAY = 80;
Wpl.Ticker.BLINK_DELAY = 80;
Wpl.Ticker.prototype.start = function () {
	this._prepareEl();
	this._type();
}
Wpl.Ticker.prototype._prepareEl = function () {
	this._el.href = this._currentItem()[1];
	this._setText('');
}
Wpl.Ticker.prototype._currentItem = function () {
	return this._items[this._itemPosition];
}
Wpl.Ticker.prototype._type = function () {
	var thiss = this;
	if (this._currentItem()[0].length == this._strPosition) {
		// need to pause then move to the next item!
		this._startPause();
		return;
	}
	this._strPosition++;
	var text = this._currentItem()[0].substring(0, this._strPosition);
	if (this._strPosition % 2 == 0) {
		text += '-';
	} else {
		text += '_';
	}
	this._setText(text);
	
	setTimeout(function () {thiss._type()}, Wpl.Ticker.TYPE_DELAY);
}
Wpl.Ticker.prototype._nextItem = function () {
	// need to move to the next item!
	this._itemPosition++;
	if (this._itemPosition == this._items.length) {
		// start back at the beginning!
		this._itemPosition = 0;
	}
	this._strPosition = 0
	this._prepareEl();
	this._type();
}
Wpl.Ticker.prototype._startPause = function () {
	this._pauseCount = 0;
	var thiss = this;
	this._setText(this._currentItem()[0]);
	this._span = html.span("_");
	this._el.appendChild(this._span);
	setTimeout(function () {thiss._doPause()}, Wpl.Ticker.BLINK_DELAY);
}

Wpl.Ticker.prototype._doPause = function () {
	var thiss = this;
	this._pauseCount++;
	if (this._pauseCount == Wpl.Ticker.PAUSE_COUNT) {
		this._endPause();
	} else {
		if (this._pauseCount%2 == 0) {
			this._span.style.visibility='visible';
		} else {
			this._span.style.visibility='hidden';
		}
		setTimeout(function () {thiss._doPause()}, Wpl.Ticker.BLINK_DELAY);
	}
}

Wpl.Ticker.prototype._endPause = function () {
	this._el.removeChild(this._span);
	this._nextItem();
}

Wpl.Ticker.prototype._setText = function (text) {
	this._el.firstChild.nodeValue = text;
}



