//
// HorizontalScroller.js - (C) 2008 SevereLimitation.com
//
// HorizontalScroller was inspired by the Panic Coda (http://www.panic.com/coda/)
// scroller and coded using Panic Coda... what are the odds of that!
//
// Requires: Prototype 1.6 & Scriptaculous 1.8
// Author: Zuriel Barron

var HorizontalScroller = Class.create({

  initialize: function(scrollerName, leftButton, rightButton) {
	this.scrollerDiv = $(scrollerName);
	this.contentDiv = this.scrollerDiv.down();
	this.sections = this.scrollerDiv.select('.section');
	this.currentSection = 0;
	
	if(leftButton) {
		this.leftButton = $(leftButton);
		Event.observe(this.leftButton, 'click', this.movePanelLeft.bindAsEventListener(this));
	}
	if(rightButton) {
		this.rightButton = $(rightButton);
		Event.observe(this.rightButton, 'click', this.movePanelRight.bindAsEventListener(this));
	}
  },

  movePanelLeft: function(event) {
	this.scrollToPanel(this.currentSection - 1);
	Event.stop(event);
  },

  movePanelRight: function(event) {
	this.scrollToPanel(this.currentSection + 1)
	Event.stop(event);
  },
  
  scrollToPanel: function(targetSection) {
	if(targetSection < 0)
		targetSection = this.sections.length - 1;
	else if (targetSection >= this.sections.length)
		targetSection = 0;
		
	var theOffset = this.getOffset(targetSection);
	new Effect.Move (this.contentDiv,{ x: theOffset, y: 0, mode: 'relative', duration: 0.5, queue: 'end'});
	this.currentSection = targetSection;
  },
  
  getOffset: function(targetSection) {
	var offset = this.scrollerDiv.getWidth();
	var diff = this.currentSection - targetSection;
	return diff * offset;
  }
});