/**
 * @author Bruno Bornsztein <bruno@missingmethod.com>
 * @copyright 2007 Curbly LLC
 * @package Slider
 * @license MIT
 * @url http://www.missingmethod.com/projects/slider/
 * @version 0.0.1
 * @dependencies prototype.js, effects.js
 */

Slider = Class.create();
Object.extend(Object.extend(Slider.prototype, Abstract.prototype), {
	initialize: function(wrapper, options){
		this.scrolling 	= false;
    this.wrapper 		= $(wrapper);
		this.scroller 	= $$('div#'+this.wrapper.id+' div.scroller')[0];
    this.options 		= Object.extend({duration:1, debug:false}, options || {});
		this.sections 	= $$('div#'+this.wrapper.id+' div.section');
		this.section_ids = this.mapSectionIds();
		this.current;
		this.setupControls();
	},
	
	mapSectionIds: function(){
		array = []
		for(i=0; i< this.sections.length; i++){
			array.push(this.sections[i].id);
		}		
		return array
	},

	setupControls: function() {
		controls = $$('#'+this.wrapper.id+' div.controls a');
		if (this.options.debug){
			console.log("Controls:", controls);			
		}
		
		for(i=0; i< controls.length; i++){
			control = controls[i]
			if(this.options.debug){ console.log("control:", control) }
			Event.observe(control, 'click', this.handleControlClick.bindAsEventListener(this) );			
		}
	},
	
	handleControlClick: function(event) {
		element = Event.findElement(event, 'a');
		if(this.options.debug){ 
			console.log("event:", event);
			console.log("element:",element);
		}
		if (this.scrolling){
			this.scrolling.cancel();
		}
		this.scroller.background = "#ffffff"
		this.moveTo(element.href.split("#")[1], this.scroller, {duration:this.options.duration} );			
		Event.stop(event);
	},
	
	moveTo: function(element, container, options){
		this.current = element;
		options 		= Object.extend({duration: 1}, options || {});	
		Position.prepare();
	  var container_x = Position.cumulativeOffset(container)[0];
	  var container_y	= Position.cumulativeOffset(container)[1];
	  var element_x	 	= Position.cumulativeOffset($(element))[0];
	  var element_y  	= Position.cumulativeOffset($(element))[1];		
	  this.scrolling 	= new Effect.Scroll(container, {duration:options.duration, x:(element_x-container_x), y:(element_y-container_y)});
	  return false;
	},
	
	next: function(){
		if (this.current){
			currentIndex 	= this.section_ids.indexOf(this.current);
			if(currentIndex == (this.sections.length-1)){
				nextSection 	= this.sections[0];
			} else {
				nextSection 	= this.sections[currentIndex+1];
			}
		} else {
			nextSection 	= this.sections[1];
		}
		this.moveTo(nextSection.id, this.scroller, {duration:this.options.duration} );		
	},
	
	previous: function(){
		if (this.current){
			currentIndex 	= this.section_ids.indexOf(this.current);
			if(currentIndex == 0){
				prevSection 	= this.sections[this.sections.length-1];
			} else {
				prevSection 	= this.sections[currentIndex-1];
			}
		} else {
			prevSection 	= this.sections[this.sections.length-1];
		}
		this.moveTo(prevSection.id, this.scroller, {duration:this.options.duration} );
	}
});

Effect.Scroll = Class.create();
Object.extend(Object.extend(Effect.Scroll.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    var options = Object.extend({
      x:    0,
      y:    0,
      mode: 'absolute'
    } , arguments[1] || {}  );
    this.start(options);
  },
  setup: function() {
    if (this.options.continuous && !this.element._ext ) {
      this.element.cleanWhitespace();
      this.element._ext=true;
      this.element.appendChild(this.element.firstChild);
    }
   
    this.originalLeft=this.element.scrollLeft;
    this.originalTop=this.element.scrollTop;
   
    if(this.options.mode == 'absolute') {
      this.options.x -= this.originalLeft;
      this.options.y -= this.originalTop;
    } else {
   
    }
  },
  update: function(position) {   
    this.element.scrollLeft = this.options.x * position + this.originalLeft;
    this.element.scrollTop  = this.options.y * position + this.originalTop;
  }
});