ThePoint.SimpleCarousel = Behavior.create({
  initialize:function(direction, visibleSelector, collectionClass, scrollOffsetClass){
    this.direction = direction;
    this.visibleSelector = visibleSelector; // active/visible element
    this.collectionClass = collectionClass; // siblings of active
    this.collectionSelector = "." + this.collectionClass;
		this.scrollOffsetClass = scrollOffsetClass;
		var offset = $$(this.scrollOffsetClass).first().innerHTML; // ff3 hack

		this.absTop = "0px";
	  this.absLeft = offset;
	  this.absRight = "-" + offset;
  }, 
  onclick:function(e){
    e.stop();
    // only scroll if previous scroll is complete
    if(this.scrollEnabled()){this._prepareElement();} 
  },
	_prepareElement:function(){
	  if($$(this.visibleSelector).first() != null){
	    this._scrollDisable();
	    this.visibleElement = $$(this.visibleSelector).first();
  		var el = this.direction == "next" ? this.visibleElement.next() : this.visibleElement.previous();
      if(el && el.hasClassName(this.collectionClass)){
        this._prepareMove(el);
      } else { // end of the line; jump to first or last element to seamlessly 'wrap' carousel
        if($$(this.collectionSelector).length > 1){
  				if(this.direction == 'next'){
  					this._prepareMove($$(this.collectionSelector).first()); 
  				}else if(this.direction =="previous"){
  					this._prepareMove($$(this.collectionSelector).last()); 
  				}
        } 
      }
	  }
	},
  _prepareMove:function(el){
    var dims = this.visibleElement.getDimensions();
    var width = dims.width;
    if(this.direction == "next"){      
      this._move(el, this.absLeft, -width); // positioned to left of active; move backwards (left)
    } else {
      this._move(el, this.absRight, width); // positioned to right of active; move forwards (right)
    }
    this._toggleActiveState(this.visibleElement);
      this._toggleActiveState(el);
  },
  _move:function(el,left,width){
		el.absolutize();
		el.setStyle({left:left, top:this.absTop});
		el.relativize();
		new Effect.Parallel([new Effect.Move(el,{x:width}),new Effect.Move(this.visibleElement,{x:width})],{afterFinish:this._scrollEnable});
  },  
  _scrollDisable:function(){
    $$('a.scroll').each(function(elem){elem.addClassName('scroll_disabled');});
  },
  _scrollEnable:function(){
    // tried this by setting a property in the behavior but ran into scope issues when called from effect callback
    $$('a.scroll').each(function(elem){elem.removeClassName('scroll_disabled');});
  },
  scrollEnabled:function(){
    return $$('a.scroll').first().hasClassName('scroll_disabled') ? false : true
  },
  _toggleActiveState:function(el){
    if(el.hasClassName('active')){
      el.removeClassName('active');
      el.addClassName('hidden');
    }else if(el.hasClassName('hidden')){
      el.removeClassName('hidden');
      el.addClassName('active');
    }
  }
});