// JavaScript Document FancySlider v2

var FancySlider = new Class({
	options : {
		transition : 'back:out',
		duration : 1000,
		mode : 'slide',
		plane : 'vertical',
		nextButton : false,
		previousButton : false,
		containerHeight : 'variable',
		firstElement : 0,
		numberDisplayed : 1,
		containerStyles : {},
		buttonDefaultDisplay:'inline',
		container: false,
		fixedWidth:false
        
	},

	initialize: function( elements, options ){
		this.setOptions(options);
                if( $type(elements) == 'function' ){
                     this.elements = elements();
               }else{
       		      this.elements = $$(elements);
                }
		this.current = this.options.firstElement;
		if(this.options.container !== false){
			this.container = $(this.options.container);
		}else{
			this.container = new Element('div');
			this.container.inject(this.elements[0], 'before');
			this.elements.each(function(el){this.container.grab(el);}, this );
		}
		this.buttons = [];
		this.inProgress = false;
       
		
		var styles = { 'overflow' : 'hidden'};
        if(this.options.containerHeight != 'variable') styles['height'] = this.options.containerHeight;                      
		styles = $merge(this.options.containerStyles, styles);
		this.container.setStyles(styles);
		if(this.options.mode == 'scroll'){
			this.scroller = new Fx.Scroll(this.container, { 
				duration: this.options.duration,
				transition: this.options.transition,
				onComplete: this.adjustHeight.bindWithEvent(this),
				wheelStops: false,
	            link:'ignore'
			});
		}else if(this.options.mode == 'slide'){
			this.slidingDiv = new Element('div');
			this.slidingDiv.inject(this.container, 'top');
			this.slidingDiv.set('tween', {
				duration: this.options.duration,
				transition: this.options.transition,
				onComplete: this.adjustHeight.bindWithEvent(this),
	            link:'ignore'
			});
			this.property = this.options.plane == 'vertical' ? 'margin-top' : 'margin-left';
			this.xy = this.options.plane == 'vertical' ? 'y' : 'x';
			this.elements.each(function(el){ 
				this.slidingDiv.grab(el); 
				var p = el.getPosition(this.slidingDiv)[this.xy];
				el.store('position', p);
				}, this );
			
			
		}else{return;}
		if(this.options.nextButton != false) this.addButton(this.options.nextButton, 'next');
		if(this.options.previousButton != false)this.addButton(this.options.previousButton, 'previous');
		this.scrollToElement(this.options.firstElement);
	},
	
	adjustHeight:function(){
		if(this.options.containerHeight == 'variable'){
			var nh = this.elements[this.current].getSize().y;
			this.container.set('tween', {dur:500, transition:'linear'});
			this.container.tween('height', nh + 'px');
		}
		this.inProgress = false;
	},

	scrollToElement: function(index){
		this.inProgress = true;
		if(this.options.mode == 'scroll'){
			if(this.options.fixedWidth){
		        this.scroller.start(this.options.fixedWidth * index, 0);
			}else{this.scroller.toElement(this.elements[index]);}
			this.current = index;
		}else if(this.options.mode == 'slide'){
			var p = this.elements[index].retrieve('position');
			var p = p - (p * 2);
			this.slidingDiv.tween(this.property, p + 'px');
			this.current = index;
		}
		this.sortOutButtons();
	},

	scrollToFirst: function(){this.scrollToElement(0);},

	scrollToLast: function(){
		this.scrollToElement(this.elements.length - 1);
	},

	scrollToNext: function(){
		if(this.current + this.options.numberDisplayed >= (this.elements.length - 1) ){
			this.inProgres = false; 
			return;
		}
		this.scrollToElement(this.current + this.options.numberDisplayed);
	},

	scrollToPrevious: function(){
		if(this.current == 0){ 
			this.inProgress = false; 
			return;
		}
		this.scrollToElement(this.current - this.options.numberDisplayed);
	},

	addButton: function(element, action){
		var el = $(element);
		el.store('action', action);
		if(el.getStyle('display') == 'none'){
			el.store('display', this.options.buttonDefaultDisplay);
		}else{
			el.store('display', el.getStyle('display') );
		}
        this.buttons.push(el);
		el.addEvent('click', this.handleClick.bindWithEvent(this, el) );
	},

	handleClick: function(e, el){
		if(this.inProgress) return;
		var action = el.retrieve('action');
		switch(action){
			case 'next': this.scrollToNext(); break;
			case 'previous' : this.scrollToPrevious(); break;
			case 'first' : this.scrollToFirst(); break;
			case 'last': this.scrollToLast(); break;
			default: this.scrollToElement(action); break;
		}
                
	},

    getPosition: function(){
		if(this.current == 0){
		     return 'first';
		}else if(this.current == this.elements.length - 1){
			return 'last';
		}else{
			return this.current;
		}
    },

    hideButton:function(el){
        $(el).setStyle('visibility', 'hidden');
    },

    showButton:function(el){
        $(el).setStyle('visibility', 'visible');
    },
     
    hideAllButtons:function(){
        this.buttons.each( function(el){
        el.setStyle('visibility', 'hidden')
        }, this);
    },

    showAllButtons:function(){
        this.buttons.each( function(el){
        el.setStyle( 'visibility', 'visible' );
        }, this);
    },

    sortOutButtons:function(){
        this.showAllButtons();
		if(this.current + this.options.numberDisplayed >= (this.elements.length - 1) ){this.hideButton(this.options.nextButton);}else if(this.current == 0){this.hideButton(this.options.previousButton);}
		
    }

});
FancySlider.implement(new Options);	