// JavaScript Document FancySlider v2



var FancySlider = new Class({

	options : {

		transition : 'back:out',

		duration : 500,

		mode : 'slide',

		plane : 'vertical',

		nextButton : false,

		previousButton : false,

		containerHeight : 'variable',

		firstElement : 0,

		numberDisplayed : 1,

		containerStyles : {},

		buttonDefaultDisplay:'inline',

		container: false,

		ieFix: 0,
               fixedWidth: 339,

		

		// EVENTS

		

		onSlideBegin: $empty,

		onSlideEnd: $empty,

		onEnd: $empty,

		onStart: $empty

        

	},

	

	Implements: Events,





	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];

				if(Browser.Engine.trident) p = p + this.options.ieFix;

				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;

		this.fireEvent('slideEnd');

		if( this.atTheEnd() ) this.fireEvent('end');

		if( this.atTheBeginning() ) this.fireEvent('start');

	},

	

	atTheEnd: function(){

		return this.current >= (this.elements.length - this.options.numberDisplayed) ? true : false;

	},

	

	atTheBeginning: function(){

		return this.current == 0 ? true : false;

	},



	scrollToElement: function(index){

		this.inProgress = true;

		this.fireEvent('slideBegin');

		if(this.options.mode == 'scroll'){
                        if(this.options.plane == 'horizontal' && Browser.Engine.trident){
                            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;

		}

	},



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



	scrollToLast: function(){

		this.scrollToElement(this.elements.length - 1);

	},



	scrollToNext: function(){

		if( this.atTheEnd() ){

			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) );

	},

	

	removeButton: function(element){

		var el = $(element);

		el.removeEvent('click', this.handleClick);

		this.buttons.erase(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('display', 'none');

    },



    showButton:function(el){

        $(el).setStyle('display', el.retrieve('display') );

    },

     

    hideAllButtons:function(){

        this.buttons.each( function(el){

        el.setStyle('display', 'none')

        }, this);

    },



    showAllButtons:function(){

        this.buttons.each( function(el){

        el.setStyle( 'display', el.retrieve('display') );

        }, this);

    },



    sortOutButtons:function(el, action){

        /*this.showAllButtons();

		if(action != ('next' || 'previous') ) this.hideButton(el);

        if( this.getPosition() == 'first' && this.options.nextButton != false ){this.hideButton(this.options.nextButton);}else if( this.getPosition() == 'last' && this.options.previousButton != false){this.hideButton(this.options.previousButton);}*/

    }



});

FancySlider.implement(new Options);	