/**
 * @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
ScrolledText.prototype.init = function(){
	try {
		this.el2 = document.createElement(this.el.tagName);
		this.el2.innerHTML = this.el.innerHTML;
		this.el2.className = this.el.className;
		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.el2.style.left = '0';
	}
	catch(e) {
        return false;
	}
	return true;
}

ScrolledText.prototype.start = function() {
	this.currentSpeed = 1;
}
	
ScrolledText.prototype.stop = function() {
	this.currentSpeed = 0;
}	
ScrolledText.prototype.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';
}
