
var VerticalPager = Class.create();

VerticalPager.prototype = {
	// Constructor
  	initialize: function(pagerElemID) {
		this.pagerElemID = pagerElemID;

		this.options = Object.extend({
		 	animParameters:      {},
		    buttonStateHandler:  null,
			animHandler:         null,
		    initDoneHandler:     null,
		    queue:               pagerElemID,
		    size:                0,
		    prevElementID:       "prev-arrow",
		    nextElementID:       "next-arrow",
		    ajaxParameters:      null,
		    url:                 null
		}, arguments[1] || {});
		this.initDone = false;
		this.animRunning = "none";

		Object.extend(this.options.animParameters, {afterFinish:  this._animDone.bind(this), queue: { position:'end', scope: this.options.queue }});

		// Event bindings
		this.prevScroll = this._prevScroll.bindAsEventListener(this);
		this.nextScroll = this._nextScroll.bindAsEventListener(this);

		Event.observe(this.options.prevElementID, "click", this.prevScroll);
		Event.observe(this.options.nextElementID, "click", this.nextScroll);

		this.pager = $(pagerElemID);
		var temp = $(pagerElemID + '-pager');
		temp.setStyle({overflow:'hidden'});


		this.pane = document.getElementsByClassName("pane", $(pagerElemID))[0]

		this._init();
	},
	destroy: function() {
	  	Event.stopObserving(this.options.prevElementID, "click", this.prevScroll);
	  	Event.stopObserving(this.options.nextElementID, "click", this.nextScroll);
	},

	_init: function() {
		this._getWindowSize();
  		this._updateButtonStateHandler(this.options.prevElementID, false);

  		this._updateButtonStateHandler(this.options.nextElementID, this.paneSize > this.windowSize);
  		this.offset =0;
  		this.pager.style.overflow = "hidden";
  	},
	_prevScroll: function(event) {
	    if (this.animRunning != "none") return false;
	    if (this.offset > 0) {
	    	this._scroll(-this.windowSize);
	    }
	    return true;
	},
	_nextScroll: function(event) {
	    if (this.animRunning != "none") return false;
	    if (this.offset < this.paneSize - this.windowSize) {
	    	this._scroll(this.windowSize);
	    }
	    return true;
	},
	_scroll: function(delta) {
		this.animRunning = delta > 0 ? "prev" : "next";
		if (this.options.animHandler) this.options.animHandler(this.pagerElemID, "before", this.animRunning);
		delta = parseInt(delta * 0.8);
		new Effect.MoveBy(this.pane, -delta, 0, this.options.animParameters);
		this.offset += delta;
		this._updateButtonStateHandler(this.options.prevElementID, this.offset > 0);
		this._updateButtonStateHandler(this.options.nextElementID, this.offset < this.paneSize - this.windowSize);
	},
	_animDone: function(event){
		if (this.options.animHandler) this.options.animHandler(this.pager, "after", this.animRunning);
		this.animRunning = "none";
	},
	_getWindowSize: function() {
		this.windowSize = this.pager.getDimensions().height;
		this.paneSize = this.pane.getDimensions().height + parseFloat(this.pane.getStyle("margin-top")) + parseFloat(this.pane.getStyle("margin-bottom"));
	},
	_updateButtonStateHandler: function(button, state) {
		if (this.options.buttonStateHandler) this.options.buttonStateHandler(button, state)
	}
}


