
var Carousel = new Class({
	
	Extends: Fx,
	
	options: {
		step: 1,
		auto: false,
		direction: 1,
		duration: 3,
		
		langScrollPrev: 'Scroll to previous thumbnail',
		langEnter: 'Click to enter gallery',
		langScrollNext: 'Scroll to next thumbnail'
	},
	
	scroll: null,
	items: [],
	links: [],
	activeItem: 0,
	itemsSize: 0,
	itemsNumber: 0,
	
	
	initialize: function(element,options) {
		this.setOptions(options);
		this.element = this.pass = $(element);
		if (!this.element) return;
		
		if ($type(this.element)!='element') this.element = $(this.element.getDocument().body);
		
		this.build();
		
		//---------------------------------------------------------------------------------
		
		/**
		 * Script
		 */
		this.scroll = new Fx.Scroll(this.carouselScroll);
		
		$$(this.itemsList.childNodes).each(function(element,index) {
			this.items[index] = element;
			this.links[index] = element.getAttribute("rel");
			this.itemsSize += element.offsetWidth;
		}.bind(this));
		
		this.itemsNumber = this.items.length-1;
		
		this.itemsList.style.width = this.itemsSize+'px';
		this.carouselItems.style.width = this.itemsSize+'px';
		
		this.prevButton.addEvent("click",function(){
			this.goPrev();
		}.bind(this));
		
		this.nextButton.addEvent("click",function(){
			this.goNext();
		}.bind(this));
		
		this.scroll.toElement(this.items[0]);
		this.items[this.activeItem].addEvent('click',function() {
			goHref(this.links[0]);
		}.bind(this));
		//---------------------------------------------------------------------------------
		
		if (this.options.auto==true) this.roll();
	},
	
	build: function() {
		
		this.carouselClass = this.element.className.substring(this.element.className.lastIndexOf('-')+1);
		this.carouselTitle = this.element.get('title');
		
		
		this.carousel = new Element('div',{
			'class': 'carousel-'+this.carouselClass
		});
		this.carousel.injectInside(this.element.parentNode);
		
		if (this.carouselTitle!='') {
			this.titleDiv = new Element('div',{
				'class': 'carousel-title-'+this.carouselClass
			});
			this.titleDiv.setHTML(this.carouselTitle);
			this.titleDiv.injectInside(this.element.parentNode);
		}
		
		this.prevButton = new Element('div',{
			'class': 'carousel-prev-'+this.carouselClass,
			'rel': 'cloud',
			'title': this.options.langScrollPrev
		});
		this.prevButton.injectInside(this.carousel);
		this.addCloud(this.prevButton);
		
		this.carouselContainer = new Element('div',{
			'class': 'carousel-container-'+this.carouselClass
		});
		this.carouselContainer.injectInside(this.carousel);
		
		this.carouselScroll = new Element('div',{
			'class': 'carousel-scroll-'+this.carouselClass,
			'rel': 'cloud',
			'title': this.options.langEnter
		});
		this.carouselScroll.injectInside(this.carouselContainer);
		this.addCloud(this.carouselScroll);
		
		this.carouselItems = new Element('div',{
			'class': 'carousel-items-'+this.carouselClass
		});
		this.carouselItems.injectInside(this.carouselScroll);
		
		this.itemsList = this.element.cloneNode(true);
		this.itemsList = this.carouselItems.appendChild(this.itemsList);
		this.element.parentNode.removeChild(this.element);
		
		this.nextButton = new Element('div',{
			'class': 'carousel-next-'+this.carouselClass,
			'rel': 'cloud',
			'title': this.options.langScrollNext
		});
		this.nextButton.injectInside(this.carousel);
		this.addCloud(this.nextButton);
	},
	
	goPrev: function() {
		if(this.activeItem==0){
			this.activeItem = this.itemsNumber;
			this.scroll.toRight();
		} else{
			this.activeItem -= this.options.step;
			this.activeItem = this.activeItem<0?0:this.activeItem;
			this.scroll.toElement(this.items[this.activeItem]);
		}
		this.items[this.activeItem].addEvent('click',function() {
			goHref(this.links[this.activeItem]);
		}.bind(this));
	},
	
	goNext: function() {
		if(this.activeItem==this.itemsNumber) {
			this.activeItem = 0;
			this.scroll.toLeft();
		} else{
			this.activeItem += this.options.step;
			this.activeItem = this.activeItem>this.itemsNumber?this.itemsNumber:this.activeItem;
			this.scroll.toElement(this.items[this.activeItem]);
		}
		this.items[this.activeItem].addEvent('click',function() {
			goHref(this.links[this.activeItem]);
		}.bind(this));
	},
	
	roll: function() {
		this.timer = setInterval(function() {
			if (this.options.direction==1) {
				this.goNext();
			} else {
				this.goPrev();
			}
		}.bind(this),this.options.duration*1000);
	},
	
	addCloud: function(element) {
		var Cloud = new Tips(element, {
			rel: 'cloud',
			initialize:function(){
				this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 200, wait: false}).set(0);
			},
			onShow: function(toolTip) {
				this.fx.start(1);
			},
			onHide: function(toolTip) {
				this.fx.start(0);
			}
		});
	}
});
