/*
Marquee for MooTools
version 1.1.1 (c) Osem Websystems - www.osem.nl
*/

var Marquee = new Class({
	initialize: function(options) {
		this.setOptions({
			source: $empty,
			width: "",
			top: 0,
			margin: 0,
			padding: 0,
			speed: "",
			direction: "",
			pauseOnMouseOver: true
	    }, options);

		this.source = $(this.options.source);
		if (this.options.width == "") this.options.width = this.source.getAttribute("width") || "100%";
		if (!isNaN(this.options.width)) this.options.width += "px";
		if (this.options.speed == "") this.options.speed = this.source.getAttribute("scrolldelay") || 10;
		if (this.options.direction == "") this.options.direction = this.source.getAttribute("direction") || "left";

		if (this.source.innerHTML.trim() == "") return;

		this.height = this.options.top + this.source.getSize().y;

		this.scroller = new Element("div", {
			id: this.source.getAttribute("id") || "",
			styles: {
				position: "absolute",
				whiteSpace: "nowrap",
				top: this.options.top
			}
		});

		this.container = new Element("div", {
			styles: {
				float: "left",
				position: "relative",
				margin: this.options.margin,
				padding: this.options.padding,
				width: this.options.width,
				height: this.height,
				overflow: "hidden"
			}
		});

		this.scroller.innerHTML = this.source.innerHTML;
		this.source.innerHTML = "";
		this.container.inject(this.source, "before");
		this.container.adopt(this.scroller);
		this.source.dispose();

		this.startPos = this.scroller.getSize().x;
		this.limit = this.container.getSize().x;

		this.scroller.setStyle(this.options.direction == "left" ? "right" : "left", -this.startPos + "px");

		this.scroll();

		if (this.options.pauseOnMouseOver) {
			this.scroller.addEvents({
				mouseover: function() {
					$clear(this.timer);
				}.bind(this),
				mouseout: function() {
					this.timer = this.scroll.delay(this.options.speed, this);
				}.bind(this)
			});
		}
	},

	scroll: function() {
		var inc = this.scroller.getStyle(this.options.direction == "left" ? "right" : "left").toInt();
		this.scroller.setStyle(this.options.direction == "left" ? "right" : "left", (inc + 1) + "px");
		if (inc > this.limit){
			this.scroller.setStyle(this.options.direction == "left" ? "right" : "left", -this.startPos + "px");
		}
		this.timer = this.scroll.delay(this.options.speed, this);		
	}
});

Marquee.implement(new Options);
