window.addEvent('domready', function() {
	new Rotator('banner', { delay: 15000, speed: 500 });
	new Request({
		method: 'get',
		url: '/web-service/share-price.aspx',
		onComplete: function(response) {
			var b = $('body');
			b.getElement('div.share-price span.date').set('html', Share.Date);
			b.getElement('div.share-price p.price').set	 ('html', Share.Price);
			b.getElement('div.share-price p.percent').set('html', Share.Percent).addClass(Share.Direction);
		}
	}).send();
});

Rotator = new Class({
	options: {
		delay: 10000,
		speed: 500
	},
	initialize: function(el, options){ 
		this.setOptions(options);
		this.id				= el;
		this.el				= $(el);
		this.items			= this.el.getElements('li');
		this.height			= this.el.getSize().y;
		this.count			= this.items.length;
		this.paused			= false;
		this.complete		= true;
		this.index			= 0;
		this.next			= 0;
		this.previous		= -1;
		this.initialized	= $defined(this.el) && this.count > 1 && this.options.speed < this.options.delay;
		
		if (this.initialized) {
			
			this.items.each(function(item, index) {
				item.setStyles({
					display: 'block',
					opacity: 0
				});
				
				var a = item.getElement('a');
				if (a != null && a.href.contains('http://')) {
					a.addEvent('click', function() {
						window.open(a.href, '_blank'); 
						return false;
					});
				}
			}.bind(this));
			
			this.start();
		}// else
		 //	alert('Banner \'' + this.id + '\' was not initialized. Please ensure all parameters are correct and refresh the page');
	},
	go: function(index) { if (!this.complete) return;
	
		this.index		= $defined(index) ? index : this.next;
		this.next		= this.index + 1;
		this.next		= this.next < 0 || this.next >= this.count ? 0 : this.next;
		this.paused		= true; //console.log('previous:' + this.previous + ', index:' + this.index + ', next:' + this.next);
		this.fx			= new Fx.Tween(this.items[this.index], {
			duration: this.options.speed,
			onStart: function() {
				this.complete = false;
				this.items[this.index].setStyle('z-index',999);
				
				if (this.previous != -1)
					this.items[this.previous].setStyle('z-index',0);
			}.bind(this)
		}).start('opacity', 0, 1).chain(function() {
			if (this.previous != -1)
				this.items[this.previous].setStyle('opacity', 0);
				
			this.previous	= this.index;
			this.complete	= true;
			this.paused		= false;
		}.bind(this));
	},
	start: function() {
		if (!this.paused)
			this.go();
		this.start.bind(this).delay(this.options.delay);	
	}
});
Rotator.implement(new Options);
