//opsIsDebugOn = 'con';

YAHOO.namespace("OPS");

YAHOO.OPS.AnimTextType = function(mgr, opts) {
    this.mgr = mgr;
    this.mgrID = this.mgr.add(this);
    this.createEvent('complete');
    if(YAHOO.util.Event.DOMReady)
	this.init('DOMReady', null, opts);
    else
	YAHOO.util.Event.onDOMReady(this.init, opts, this, true);
};

YAHOO.OPS.AnimTextType.prototype = {
    init: function(ev, other, opts) {
	myopts = {
	    el: '' // Must be defined
	    // Either speed or duration must be specified
	    , speed: 0 // Number of seconds between each letter
	    , duration: 0 // Total elpased time in seconds
	    , delay: 0
	};

	// Merge options.
	var debugout = '';
	for(var opt in myopts) {
	    if(typeof opts[opt] != 'undefined') {
		debugout += opt+'='+opts[opt]+"\n";
		myopts[opt] = opts[opt];
	    }
	}
	this.opts = myopts;
	if(!(this.opts.el instanceof Array))
	    this.opts.el = new Array(this.opts.el);

	this.els = new Array();
	for(var i in this.opts.el) {
	    if(typeof(this.opts.el[i]) != 'object') {
		var textEl = YAHOO.util.Dom.get(this.opts.el[i]);
		var speed = 1;
	    } else {
		var textEl = YAHOO.util.Dom.get(this.opts.el[i].id);
		var speed = this.opts.el[i].speed;
	    }
	    if(!textEl)
		continue;
	    var spanEl = document.createElement('span');
	    var typeEl = document.createElement('span');
	    //YAHOO.util.Dom.setStyle(spanEl, 'font-weight', 'bold');
	    YAHOO.util.Dom.setStyle(spanEl, 'visibility', 'hidden');
	    spanEl.innerHTML = textEl.innerHTML;
	    textEl.innerHTML = '';
	    textEl.appendChild(typeEl);
	    textEl.appendChild(spanEl);
	    var nSteps = spanEl.innerHTML.replace(' ', '').replace(/&nbsp;/, ' ').length;
	    this.els.push({textEl: textEl, spanEl: spanEl, typeEl: typeEl, speed: speed, nSteps: nSteps, lastChar: '', nextChar: ''});
	}

	this.initParams();
	this.mgr.queue(this.mgrID, this, this.opts.delay);
    }

    , complete: function(anim) {
	this.fireEvent('complete');
    }

    , tick: function(data, anim) {
	if(this.curEl >= this.els.length)
	    return;

	//opsDebug('++++ textype ++++ '+data.stepNo+' : '+data.duration+' , '+elRes);
	if(!this.els[this.curEl].spanEl.innerHTML) {
	    this.curEl++;
	    if(this.curEl >= this.els.length)
		return;
	}
	var elRes = 1/this.els[this.curEl].speed;
	if(elRes != 1 && (data.stepNo%elRes))
	    return;

	var nChars = 1;
	var shiftText = this.els[this.curEl].spanEl.innerHTML;
	if(this.els[this.curEl].nextChar == ' ')
	    this.els[this.curEl].lastChar = ' ';

	if(shiftText.indexOf('&nbsp;') == 0)
	    nChars = 6;
	if(this.els[this.curEl].lastChar == ' ')
	    this.els[this.curEl].typeEl.innerHTML += ' '+shiftText.substr(0, nChars);
	else
	    this.els[this.curEl].typeEl.innerHTML += shiftText.substr(0, nChars);
	this.els[this.curEl].lastChar = shiftText.substr(0, nChars);
	this.els[this.curEl].nextChar = shiftText.substr(nChars, 1);
	if(this.els[this.curEl].nextChar == ' ')
	    this.els[this.curEl].spanEl.innerHTML = shiftText.substr(nChars+1);
	else
	    this.els[this.curEl].spanEl.innerHTML = shiftText.substr(nChars);
    }

};

YAHOO.lang.augmentProto(YAHOO.OPS.AnimTextType, YAHOO.OPS.AnimBase);

