/**
 * @author Knishman Eduard
 */

/**
 * Create scrolled text
 * Usage:
 * var scrolledNews = new ScrolledText(document.getElementById("scroll-news-inner"), 0.05);
 * 			if (scrolledNews.init()) {
 * 				scrolledNews.move();
 * 				setInterval(move, scrolledNews.duration);
 * 			}
 * 	function move() {scrolledNews.move();}
 * 	YAHOO.util.Event.addListener(scrolledNews.el.parentNode, 'mouseover', function(){scrolledNews.stop()});
 * 	YAHOO.util.Event.addListener( scrolledNews.el.parentNode, 'mouseout', function(){scrolledNews.start()});
 * 
 * @param {HTMLObject} el - text element
 * @param {int} speed - in seconds
 */ 
var ScrolledText = function(el, duration) {
	this.duration = duration * 1000;
	this.currentSpeed = 1;
	this.el = el;
	this.elY = null;
	this.el2Y = null;
	this.elHeight = null;
	//inits scroll
	this.init = function(){
		try {
			this.el2 = document.createElement(el.tagName);
			this.el2.innerHTML = el.innerHTML;
			this.elHeight = this.el.offsetHeight;
			this.elY = 0;
			this.el2Y = this.elHeight;
			this.el.parentNode.appendChild(this.el2);
			this.el.parentNode.style.position = 'relative';
			this.el.style.position = 'absolute';
			this.el.style.top = '0';
			this.el.style.left = '0';
			this.el2.style.position = 'absolute';
			this.el2.style.top = this.el2Y + 'px';
			this.el.style.left = '0';
		}
		catch(e) {
			alert(e.message);
			return false;
		}
		return true;
	}
	
	this.start = function() {
		this.currentSpeed = 1;
	}
	
	this.stop = function() {
		this.currentSpeed = 0;
	}
	
	this.move = function() {
		this.elY -= this.currentSpeed;
		this.el2Y -= this.currentSpeed;
		if (this.elY < -5 - this.elHeight) {
			this.elY += 2 * this.elHeight;
		}
		this.el.style.top = this.elY + 'px';
		if (this.el2Y < -5 - this.elHeight) {
			this.el2Y += 2 * this.elHeight;
		}
		this.el2.style.top = this.el2Y + 'px';
	}
	
}

