/**
 * imagesScrolelr.js
 *
 * @version: 1.0
 * @author: ForForce Team (www.forforce.com)
 *
 */

Scroller = new Class({
	Implements: [Options, Events],

	options: {
		'container':			null
	},

	initialize: function(options) {
		this.setOptions(options);

		this.options.container						= $(this.options.container);

		this.imagesHolder							= null;
		this.images									= [];
		this.currentImage							= 0;
		this.nextImage							    = 0;
		this.prevImage							    = 0;
		this.imageScrollPrev						= null;
		this.imageScrollNext						= null;
		this.currentIndicator                       = null;
		this.totalIndicator                         = null;

		this.initElements();

		this.initEvents();

	},

	destroy: function() {
		this.images.each(function(el) {
			el.removeEvents();
			el.destroy();
		});
		this.images.empty();

		this.imagesHolder.scrollTo(0,0);

		this.imagesHolder	  =
		this.images			  =
		this.currentImage     =
		this.nextImage        =
		this.prevImage        =
		this.currentIndicator =
		this.totalIndicator   =
		this.imageScrollPrev  =
		this.imageScrollNext  = null;

		return this;
	},
	
	initEvents : function() {
		this.imageScrollNext.addEvent('click',this.onNextClick.bind(this));
		this.imageScrollPrev.addEvent('click',this.onPrevClick.bind(this));
	},
	
	initElements : function() {
		this.imagesHolder		= this.options.container.getElement('.images_holder');
		this.images			    = this.imagesHolder.getElements('div.image');
		this.currentIndicator   = this.options.container.getElement('.current');
		this.imageScrollPrev	= this.options.container.getElement('.button_scroll_left');
		this.imageScrollNext	= this.options.container.getElement('.button_scroll_right');
		this.totalIndicator     = this.options.container.getElement('.total');
		
		this.nextImage = 1;
		this.currentImage = 0;
		this.prevImage = this.images.length - 1;

		if (this.images.length == 1) {
			this.imageScrollNext.setStyle('visibility', 'hidden');
			this.imageScrollPrev.setStyle('visibility', 'hidden');
		} else {
			this.imageScrollNext.setStyle('visibility', 'visible');
			this.imageScrollPrev.setStyle('visibility', 'visible');
		}
		this.totalIndicator.set('html',this.images.length);
		this.showImage(0);
		
		return this;
		
	},
	
	showImage : function(i) {
		this.images.each(function(item){
			item.fade('hide');
			item.setStyle('display','none');
		});
		this.images[this.prevImage].setStyle('display','inline');
		this.images[this.prevImage].fade(0);
		this.images[this.prevImage].setStyle('display','none');
		this.images[i].setStyle('display','block');
		this.images[i].fade(1);
//		this.images[this.prevImage].fade(0);
//		this.images.each(function(item){
//			item.setStyle('display','none');
//		});
//		this.images[i].setStyle('display','block');
//		this.images[i].fade(1);
	},
	
	onNextClick: function() {
		
		this.prevImage = this.currentImage;
		this.currentImage++;
		if (this.currentImage == this.images.length) {
		   this.currentImage = 0;	
		}
		this.showImage(this.currentImage);
		this.currentIndicator.set('html',this.currentImage+1);
	},
	
	onPrevClick: function() {
		this.prevImage = this.currentImage;
		this.currentImage--;
		if (this.currentImage < 0) {
			this.currentImage = this.images.length - 1;
		}
		this.showImage(this.currentImage);
		this.currentIndicator.set('html',this.currentImage+1);
	}
});

window.addEvent('domready', function() {
	$$('.images_scroller').each(function(item){
        new Scroller({container: item});
    });
    //if (document.getElementById('images-scroller')) {
	//	window.Scroller = new Scroller({ 'container': $('images-scroller') });
	//}
});
