﻿//nemTicker.js
//2008 Paul Graves paul.graves@nemisys.uk.com
//Nemisys ticker widget

//nemTicker class
var nemTicker = new Class({
	initialize: function(el) {
		this.ticker = el;
		this.tickerContent = el.getElement(".nemTickerContent");

		if (this.ticker.getStyle("height").toInt() < this.tickerContent.getStyle("height").toInt()) {
			this.pause = false;
			this.ticker.addEvent('mouseover', (function() { this.pause = true; }).bind(this));
			this.ticker.addEvent('mouseout', (function() { this.pause = false; }).bind(this));
			var scrollerWidth = 0;
			var scrollerHeight = 0;
			this.tickerContent.getElements('div').each((function(el) {
				if (this.ticker.hasClass('nemHorizontal')) {
					el.setStyle('float', 'left');
				}
				scrollerWidth += el.getSize().x + el.getStyle('margin-right').toInt() + el.getStyle('margin-left').toInt();
				scrollerHeight += el.getSize().y + el.getStyle('margin-top').toInt() + el.getStyle('margin-bottom').toInt();
			}).bind(this));
			this.direction = 1;
			if (this.ticker.hasClass('nemHorizontal')) {
				this.scrollY = 0;
				this.scrollX = 1;
				this.tickerContent.setStyle('width', scrollerWidth * 2);
			} else {
				this.scrollY = 1;
				this.scrollX = 0;
				this.tickerContent.setStyle('height', scrollerHeight);
			}

			if (this.ticker.hasClass('nemControls')) {
				this.scrollRight = new Element("SPAN", { 'html': '&nbsp;', 'class': 'nemScrollRight',
					'events': { 'mousedown': (function() { this.pause = false; this.direction = -1; }).bind(this),
						'mouseup': (function() { this.pause = true; this.direction = 1; }).bind(this),
						'mouseover': (function() { this.pause = true; }).bind(this),
						'mouseout': (function() { this.pause = false; }).bind(this)
					}
				});
				this.scrollRight.inject(this.ticker, 'before');
				this.scrollLeft = new Element("SPAN", { 'html': '&nbsp;', 'class': 'nemScrollLeft',
					'events': { 'mousedown': (function() { this.pause = false; }).bind(this),
						'mouseup': (function() { this.pause = true; }).bind(this),
						'mouseover': (function() { this.pause = true; }).bind(this),
						'mouseout': (function() { this.pause = false; }).bind(this)
					}
				});
				this.scrollLeft.inject(this.ticker, 'before');
			}


			this.tickerContent2 = this.tickerContent.clone();
			this.ticker.grab(this.tickerContent2, 'bottom');
			this.scrollTicker();
		}
	},
	scrollTicker: function() {
		if (!this.pause) {
			this.ticker.scrollTop += (this.scrollY * this.direction);
			this.ticker.scrollLeft += (this.scrollX * this.direction);
		}
		if (this.ticker.scrollTop > this.tickerContent.getStyle("height").toInt()) {
			this.ticker.scrollTop = 0;
		}
		if (this.ticker.scrollLeft * 2 > this.tickerContent.getStyle("width").toInt()) {
			this.ticker.scrollLeft = 0;
		}
		(function() { this.scrollTicker(); }).delay(30, this);
	}
});
