// JavaScript Document

var ContentVerticalRoller = Class.create({
	initialize:function(elm,options){
		this.elm = $(elm);
		this.options = Object.extend({
			step: 3,
			delay: 100,
			direction: 'left'
		}, options || {});
		
		this.margin_patten = new RegExp("^([-0-9]+)(px|em|pt)$");
		
		Element.setStyle(this.elm, {
			'overflow':'hidden',
			'white-space':'nowrap'
		});
		
		//this.fillNode.defer();
		
		Element.childElements(this.elm).first().setStyle({
			'marginLeft':'0px'
		});
		
		Event.observe(this.elm, 'mouseover', this.stop.bindAsEventListener(this));
		Event.observe(this.elm, 'mouseout', this.play.bindAsEventListener(this));

		this.play();
	},
	fillNode: function() {
		var offset = Element.cumulativeOffset(this.elm);
		var d = Element.getDimensions(this.elm);
		
		var lastNodeOffset = Element.childElements(this.elm).last().cumulativeOffset();
		
		if (lastNodeOffset.left < offset.left + d.width) {
			Element.childElements(this.elm).each(function(e) {
				Element.up(e).appendChild(e.cloneNode(true))
			}.bind(this));
			this.fillNode();
		}
	},
	play: function() {
		this.stop();
		this.move();
		//if (Prototype.Browser.IE)
			this.intarval = setInterval(this.move.bind(this), this.options.delay);
		//else
		//	this.intarval = setInterval(this.move.defer.bind(this), this.options.delay);
	},
	stop: function() {
		clearInterval(this.intarval);
	},
	move: function() {
		var f = function() {
		var m = Element.childElements(this.elm).first().style.marginLeft.match(this.margin_patten);

		Element.childElements(this.elm).first().style.marginLeft = (m[1]-this.options.step) + m[2];
		
		if (m[1] <= 0 - Element.childElements(this.elm).first().getWidth()) {
			this.elm.appendChild(Element.childElements(this.elm).first().cloneNode(true));
			Element.childElements(this.elm).last().setStyle({
				'marginLeft':'0' + m[2]
			});
			Element.childElements(this.elm).first().remove();
			
			Element.childElements(this.elm).first().setStyle({
				'marginLeft':'-' + this.options.step + m[2]
			});
		}
		}.bind(this);
		f.defer();
	}
});