var jwscroller = null;
/*	
Script: jwscroller.js
	Builds a scroll effect to slide
		
Author: Luca Scarpa, <luscarpa [at] joomlaworks [dot] gr>
Project page:  http://www.joomlaworks.gr

Copyright (c) 2006 - 2007 JoomlaWorks.gr - http://www.joomlaworks.gr

Arguments:
	options - optional, an object containing options.

*/

var JWscroller = new Class({
	initialize: function(container, options){
		this.container = $(container);
		if(!this.container.hasClass('hasJWscroller')){
			this.container.addClass('hasJWscroller');
			this.slides = [];
			this.buttons = [];
			this.options = Object.extend({
				slidesSelector: ".jwscroller-content",
				buttonsSelector: ".jwscroller-link",
				wrapperSelector: "jwscroller-wrapper",
				nextSelector: "jwscroller-next",
				prevSelector: "jwscroller-prev",
				rotateAction: "click",
				transitionDuration: 500,
				startIndex: 0,
				buttonOffClass: 'off',
				buttonOnClass: 'selected',
				wait: false
			}, options || {});
			this.currentSlide = -1;
			this.slides = this.container.getElements(this.options.slidesSelector);
			this.buttons = this.container.getElements(this.options.buttonsSelector);
			this.createScroll(this.options.rotateAction);
			return this;
		} else return false;
	},
	
	/*	
Property: createScroll
	*Private internal function; do not call directly.*
	create scroll effect and add action to all link	
	
	Arguments:
	action - action to give to link
*/

	createScroll: function(action){
		this.scroll = new Fx.Scroll(this.options.wrapperSelector, { wait: this.options.wait, duration: this.options.transitionDuration});
		
		// Add action to link slide
		this.buttons.each(function(el,index){
			$(el).addEvent(action, function(event) {
				event = new Event(event).stop();
				this.selected(index);
			}.bind(this));
		}, this);
		
		// Add action to next link
		//$(this.options.nextSelector).addEvent(action, function(event) { this.next(event); }.bind(this));
		// Add action to prev link
		//$(this.options.prevSelector).addEvent(action, function(event) { this.prev(event); }.bind(this));
		
		this.selected(this.options.startIndex);

	},
	
	/*	
Property: next
	*Private internal function; do not call directly.*
	Progresses to the next slide.	*/
	next: function(event){
		event = new Event(event).stop();

		current = this.currentSlide;
		next = (current+1 >= this.slides.length) ? 0 : current+1;
		this.selected(next);
	},
	
		/*	
Property: prev
	*Private internal function; do not call directly.*
	Progresses to the prev slide.	*/
	prev: function(event){
		event = new Event(event).stop();
		
		current = this.currentSlide;
		prev = (current-1 < 0) ? this.slides.length-1 : current-1;
		this.selected(prev);
	},
	
	/*
Property: selected
	*Private internal function; do not call directly.*
	Select the slide and add it class */ 
	selected: function(slideIndex){
		if(slideIndex!=this.currentSlide){
			this.buttons.each(function(el,index){
				if(index == slideIndex && index != this.currentSlide){ //show
					$(this.buttons[index]).removeClass(this.options.buttonOffClass).addClass(this.options.buttonOnClass);
				} else {
					$(this.buttons[index]).removeClass(this.options.buttonOnClass).addClass(this.options.buttonOffClass);
				}
			}, this);
			this.currentSlide = slideIndex;
			this.scroll.toElement(this.slides[slideIndex]);
		}
	}	
	
});