// JavaScript Document
var BannerJama = new Class({
		Implements : [Options],
		options : {
			images : [],
			duration : 3,
			holderClass : 'banner-jama-holder'
		},
		elements : {
			images:[]
		},
		index : 0,
		initialize : function(banner, options)
		{
			this.setOptions(options);
			this.elements.container = document.id(banner);
			this.options.images.reverse();
			return this;
		},
		start : function()
		{
			this.show();
			this.periodical = this.show.periodical(this.options.duration * 1000, this);
		},
		pause : function()
		{
			clearInterval(this.periodical);
		},
		show : function()
		{
			if(this.options.images.length > 0)
			{
			  var currentImage = this.options.images.pop();
			  var image = Asset.image(currentImage, {
				  onLoad : function() {
					  this.index = this.elements.images.push(new Element('div', {
						  'class' : this.options.holderClass,
						  'styles' : { 'opacity' : 0 }
					  }).grab(image).inject(this.elements.container)) -1;
					 
					 this.transition();
					 
				  }.bind(this),
				  onError : this.show.bind(this)
				  
			  });
			}
			else
			{
				this.transition();
			}
			
		},
		transition : function()
		{
			
			if(this.elements.images.length > 1)
			{
				var previous = ((this.index - 1) >= 0) ? this.index - 1 : this.elements.images.length - 1;
				this.elements.images[previous].tween('opacity',0);
				this.elements.images[this.index].tween('opacity',1);
				this.index = ((this.index + 1) <= this.elements.images.length - 1) ? this.index + 1 : 0;
			
			}
			else
			{
				this.elements.images[this.index].tween('opacity',1);
			}
		}
	
	});
	
