/*
Script: BarackSlideshow.js
	Lightweight slideshow script, based on Fx.MorphList

	License:
		MIT-style license.

	Authors:
		Guillermo Rauch
*/

var Carousel = new Class({
  
  // Extends: Fx.MorphList,
	Implements: [Events, Options],  
  
  
  options: {/*
    onShow: $empty,*/
    auto: false,
    autostart: false,
    autointerval: 2000,
    transition: 'fade',
    tween: { duration: 700 }
  },
  
  initialize: function(menu, slides, loader, options){
    var self = this;
		this.setOptions(options);    
    this.menu = $(menu);
    this.menuItems = this.menu.getChildren().addEvents({
			mouseenter : function(ev){ self.options.auto = false; self.onClick(ev,this); },
			mouseleave : function(ev){ self.options.auto = true; self.auto(); }
		});
    this.slides = $(slides);
    this.slideItems = this.slides.getChildren().fade('hide');
    $(loader).fade('in');
    
		this.setCurrent(this.menu.getElement('.current'));
    
    new Asset.images(this.slides.getElements('img').map(function(el) { return el.setStyle('display', 'none').get('src'); }), { onComplete: function() {
      this.loaded = true;
      $(loader).fade('out');
      if (this.current) this.show(this.menuItems.indexOf(this.current));
      else if (this.options.auto && this.options.autostart) this.progress();
    }.bind(this) });
    if ($type(this.options.transition) != 'function') this.options.transition = $lambda(this.options.transition);
  },
  
	setCurrent: function(el, effect){  
		if (el && !this.current){
			(effect) ? el.fade('in') : el.fade('show');
		}
		if (this.current) this.current.removeClass('current');
		if (el) this.current = el.addClass('current');    
		return this;
	},         
  
  auto: function(){
    if (!this.options.auto) return false;
    $clear(this.autotimer);
    this.autotimer = this.progress.delay(this.options.autointerval, this);
  },
  			
  onClick: function(event, item){
    this.setCurrent(item, true).fireEvent('click', [event, item]);
    event.stop();
    this.show(this.menuItems.indexOf(item));
    $clear(this.autotimer);
  },
  
  show: function(index) {
    if (!this.loaded) return;
    var slide = this.slideItems[index];    
		if (slide == this.curSlide) return;
    slide.set('tween', this.options.tween).dispose().inject(this.curSlide || this.slides.getFirst(), this.curSlide ? 'after' : 'before').fade('hide');
		slide.getElement('img').setStyle('display', 'block');
    var trans = this.options.transition.run(null, this).split('-');
    switch(trans[0]){
      case 'slide': 
        var dir = $pick(trans[1], 'left');
        var prop = (dir == 'left' || dir == 'right') ? 'left' : 'top';
        slide.fade('show').setStyle(prop, slide['offset' + (prop == 'left' ? 'Width' : 'Height')] * ((dir == 'bottom' || dir == 'right') ? 1 : -1)).tween(prop, 0); 
        break;
      case 'fade': slide.fade('in'); break;
    }
    slide.get('tween').chain(function(){ 
      this.auto();
      this.fireEvent('show', slide); 
    }.bind(this));    
    this.curSlide = slide;
    this.setCurrent(this.menuItems[index]);
		return this;
  },
  
  progress: function(){
    var curindex = this.slideItems.indexOf(this.curSlide);
    this.show((this.curSlide && (curindex + 1 < this.slideItems.length)) ? curindex + 1 : 0);
  }
  
});
