﻿Slide = Class.create(Abstract, {

	initialize: function (scroller, slides,controls, options) {

		this.scrolling	= false;
		this.scroller	= scroller;
		this.slides		= slides;
		this.controls = controls;
	
		this.options    = Object.extend({ duration: 1.0, frequency: 8,controlClassName: 'btn',selectedClassName:'sel' }, options || {});

		this.slides.each(function(slide, index) {
			slide._index = index; 
			});
			
		if (this.controls) {
		
			this.controls.invoke('observe', 'click', this.click.bind(this));
			
		}
		this.options.auto = true;
		
		if (this.options.auto) {
			this.start();
			}

		},
	
	click: function (event) {

		this.stop();
       
		var element = event.findElement('div');
        var rel = element.readAttribute('rel');

        
		if (!element.hasClassName('disabled') && this.scrolling	== false) {
		
			this.deactivateControls();	
			
			if (element.hasClassName(this.options.controlClassName)) {
		
				this.moveTo(element, rel,'click');
				
			}
		}

		event.stop();

		},

	moveTo: function (trigger, element,from) {


		if (this.controls && this.options.selectedClassName) {
			this.controls.each((function (elm) { elm.removeClassName(this.options.selectedClassName); }).bind(this));
			trigger.addClassName(this.options.selectedClassName);
			}

		this.previous= this.current ? this.current : this.slides[0];
		this.current = $(element);
	
		this.scrolling	= true;
		
		    new Effect.Appear($(element),{ 
		            duration: this.options.duration,
					afterFinish: (function () { 
					
					this.scrolling	= false;
					if (this.controls) {
						this.activateControls(); 
						this.hideElements($(element));
						if(from == "click"){
						
						  this.start();
						  
						}
						
						 
						}
					 }).bind(this)});
					 
				 
			
					

		return false;

		},

	hideElements: function(element){
  
	  var elName = element.identify();
	  
	  this.slides.each(function(slide, index) {
			if(slide.identify() != elName){		
			  slide.hide();
			}
	  });
	

    },	
		
	prev: function () {
		if (this.current) {
			var currentIndex = this.current._index;
			var prevIndex = (currentIndex == 0) ? this.slides.length - 1 : currentIndex - 1;
			} else { 
				var prevIndex = this.slides.length - 1;
			}

		this.moveTo(this.controls ? this.controls[prevIndex] : false, this.slides[prevIndex]);
		},		

	next: function () {
		if (this.current) {
			var currentIndex = this.current._index;
			var nextIndex = (this.slides.length - 1 == currentIndex) ? 0 : currentIndex + 1;      
			} else {
				var nextIndex = 1;
				}
		
		this.moveTo(this.controls ? this.controls[nextIndex] : false, this.slides[nextIndex]);
		},
    scrollToPos: function(index){
	  if (this.current) {
			var currentIndex = this.current._index;
			}

	  this.moveTo(this.controls[index], this.slides[index]);	
    },
	first: function () {
		var firstIndex = 0;
		if (this.current) {
			var currentIndex = this.current._index;
			}

		this.moveTo(this.controls[firstIndex], this.slides[firstIndex]);	
		},

	last: function () {
		var lastIndex = (this.slides.length - 1);
		if (this.current) {
			var currentIndex = this.current._index;
			}

		this.moveTo(this.controls[lastIndex], this.slides[lastIndex]);
		},
	
	toggle: function () {
		if (this.previous) {
			this.moveTo(this.controls[this.previous._index], this.slides[this.previous._index]);
			} else {
				return false;
				}
		},

	stop: function () { clearTimeout(this.timer);  this.timer = null;},
	
	start: function () { this.periodicallyUpdate(); },
		
	pause: function (event) {
		this.stop();
		this.activateControls();
		},

	resume: function (event) {
		if (event) {
			var related = event.relatedTarget || event.toElement;	
			if (!related || (!this.slides.include(related) && !this.slides.any(function (slide) { return related.descendantOf(slide); }))) {
				this.start();
				}
			} else {
				this.start();
				}
		},
	
	periodicallyUpdate: function () {
		if (this.timer != null) {
			clearTimeout(this.timer);
			this.next();
			}
		this.timer = setTimeout(this.periodicallyUpdate.bind(this), this.options.frequency * 1000);
		},

	deactivateControls: function () {
		this.controls.invoke('addClassName', 'disabled');
		},

	activateControls: function () {
		this.controls.invoke('removeClassName', 'disabled');
		}
	
});
