/**
 * YPulse v0.9
 * Copyright(C) 2008
 * Author: Kent Johnson <kent13304@yahoo.com>
 * License: MIT (http://www.opensource.org/licenses/mit-license.php)
 * 
 * Register now at www.trendics.com to monitor your servers and 
 * to support this free software.
 */
YAHOO.namespace('squarebits');
YAHOO.squarebits.YPulse = function(element, attributeName, startValue, endValue, s2eDuration, s2eWait, e2sDuration, e2sWait, s2eMethod, e2sMethod) {	

	// Create _s2eAnim
	var _s2eAttributeValues = {};
	if (startValue) _s2eAttributeValues['from'] = startValue;
	if (endValue) _s2eAttributeValues['to'] = endValue;
	
   	var _s2eAttributes = {};
	_s2eAttributes[attributeName] = _s2eAttributeValues;

   	var _s2eAnim;
   	if (attributeName.indexOf('olor')>0) {
   		_s2eAnim = new YAHOO.util.ColorAnim(element, _s2eAttributes, s2eDuration, s2eMethod);
   	}
	else {
   		_s2eAnim = new YAHOO.util.Anim(element, _s2eAttributes, s2eDuration, s2eMethod);
	}
   	_s2eAnim.onComplete.subscribe(function() {
   		YAHOO.lang.later(s2eWait*1000.0, null, function() {
   			_nextAnimation();
   		});
   	});
   	
	// Create _e2sAnim
	var _e2sAttributeValues = {};
	if (startValue) _e2sAttributeValues['to'] = startValue;
	if (endValue) _e2sAttributeValues['from'] = endValue;	

	var _e2sAttributes = {};
	_e2sAttributes[attributeName] = _e2sAttributeValues;

	var _e2sAnim;
	if (attributeName.indexOf('olor')>0) {
		_e2sAnim = new YAHOO.util.ColorAnim(element, _e2sAttributes, e2sDuration, e2sMethod);
	}
	else {
		_e2sAnim = new YAHOO.util.Anim(element, _e2sAttributes, e2sDuration, e2sMethod);
	}
   	_e2sAnim.onComplete.subscribe(function() {
   		YAHOO.lang.later(e2sWait*1000.0, null, function() {
   			_nextAnimation();
   		});
   	});
	
	// Define shared private variables
   	var _repeat;
	var _repeatCount;
	var _stopped = false;
	
	// Define private methods
	function _nextAnimation() {
		if (_repeat==null || _repeatCount<_repeat) {
			if (_repeatCount % 2 == 0) {
				if (!_stopped) {
					_s2eAnim.animate();
				}
			}
			else {
				_e2sAnim.animate();
			}
			_repeatCount++;
		}
	}
		
	return {
		go: function(repeat) {
			_stopped = false;
			_repeat = repeat;
			_repeatCount = 0;
			_nextAnimation();
		},
		
		stop: function() {
			_stopped = true;
		}
	}
};