// slides.js v1.0
//
// Copyright (c) 2007 stickmanlabs | http://www.stickmanlabs.com
// 	- Kevin P Miller
// 
// Slides is freely distributable under the terms of the MIT-style license.
//

/*-----------------------------------------------------------------------------------------------*/

if(typeof $ == 'undefined')
  throw ("slides.js requires including the prototype.js library!");

var Slides = Class.create({

  slides: null,
  panel: {
    height: 0,
    width: 0
  },

	initialize: function(container, options) {
		this.options = Object.extend({
			selectors: {
				slide: 'slide'
			},
			size: {
			  height: 'auto',
			  width: 'auto'
			},
			direction: 'horizontal',
			defaultIndex: 0,
			defaultOffset: 0,
			delay: 0
		}, options || {});
		if (!$(container)) {
	    throw (container+" doesn't exist!");
	    return false;
	  } else {
			this.container = container;		
		}
		if (this.options.direction != 'vertical' && this.options.direction != 'horizontal') {
	    throw ("You must specify a valid direction!");
	    return false;		  
		}
		this.setup();
	},
	
	setup: function() {
	  this.review();

	  var slides = $(this.container).innerHTML;
	  $(this.container).setStyle({
      overflow: 'hidden',
	    position: 'relative',
	    height: this.options.size.height+'px',
	    width: this.options.size.width+'px'
	  });
	  
	  var panel = new Element('div', {
      id: this.container+'_inner'
    }).setStyle({
      position: 'absolute',
      top: (this.options.direction == 'horizontal' ? 0 : this.options.defaultOffset)+'px',
      left: (this.options.direction == 'horizontal' ? this.options.defaultOffset : 0)+'px',
      height: (this.options.direction == 'horizontal' ? this.panel.maximumHeight : this.panel.totalHeight)+'px',
      width: (this.options.direction == 'horizontal' ? this.panel.totalWidth : this.panel.maximumWidth)+'px'
    });

	  this.wrap('#'+this.container+' .'+this.options.selectors.slide, panel);

    this.review();
    if (this.options.defaultIndex > 0 || this.options.defaultOffset != 0) {
      setTimeout(this.slide.bind(this, this.options.defaultIndex), this.options.delay);
	  }
	},
		
	review: function() {
	  this.slides = $$('#'+this.container+' .'+this.options.selectors.slide); 
	  var panel = {
      maximumHeight: 0,
      maximumWidth: 0,
      totalHeight: 0,
      totalWidth: 0
    };
    var height = width = 0;
	  for (var x = 0; x < this.slides.length; x++) {
	    height = parseInt(this.slides[x].getHeight());
	    width = parseInt(this.slides[x].getWidth());
	    panel = {
	      maximumHeight: height > panel.maximumHeight ? height : panel.maximumHeight,
	      maximumWidth: width > panel.maximumWidth ? width : panel.maximumWidth,
	      totalHeight: panel.totalHeight + height,
	      totalWidth: panel.totalWidth + width
	    }
	    if (this.options.direction == 'horizontal') {
	      this.slides[x].setStyle({
	        cssFloat: 'left'
	      });
	    }
	  }
	  this.panel = panel;
	},
  
  slide: function(index) {
    $(this.container+'_inner').setStyle({
      top: -(this.options.direction == 'horizontal' ? 0 : this.slides[index].positionedOffset()[1])+'px',
      left: -(this.options.direction == 'horizontal' ? this.slides[index].positionedOffset()[0] : 0)+'px'
    });
  },
  
  wrap: function(selector, wrapper) {
    elements = $$(selector);
		if (elements[0].parentNode) {
  		elements[0].parentNode.appendChild(wrapper);
  	}
		for (var x = 0; x < elements.length; x++) {
			wrapper.appendChild(elements[x]);
		}
		return wrapper;
	}
  
});

var twSlides = Class.create(Slides, {

  initialize: function($super, container, options) {
    $super(container, options);
	},
	
  slide: function(index) {
    var effects = [];

    effects.push(
      new Effect.Morph(this.container+'_inner', {
        sync: true,
        style: {
          top: -(this.options.direction == 'horizontal' ? 0 : this.slides[index].positionedOffset()[1])+'px',
          left: -(this.options.direction == 'horizontal' ? this.slides[index].positionedOffset()[0] : 0)+'px'
        }
      })
    );
    
    new Effect.Parallel(effects, {
      duration: 1.0,
      queue: {
        position: 'end',
        scope: 'slides_animation'
      },
      transition: Effect.Transitions.BouncePast
    });
  }	
});