/*
 * 	Sprokets JavaScript Library
 *  (c) 2007 Sprokets LLC
 *  
 *  Author: Minch Design Inc. <info@minchdesign.com>
 *  ScrollingViewPort.js, version .8.1
 *
 *  [ copyright statement  ]
 *
/*-------------------------------------------------------------------*/

var ScrollingViewPort = new Class({
						 
	initialize: function(el, options) {
		
		this.options = Object.extend({
            classLeftButton:               'leftNav',
			classRightButton:              'rightNav',
			scrollingContainerClass:       'scrollingContainer',
			scrollingItemsContainerClass:  'itemContainer',
			scrollingItemClass:            'item',
			disabledClass:                 'disabled',
			scrollingItemWidth:            110,
			maxInViewPort:                 4,
			slideAmount:                   10,
			slideDelay:                    '10'
        }, options || {});
		
		this.setOptions(this.options);
		
		this.itemCount = 0;
		this.maxMoves = 0;
		this.curPos = 1;
		this.inMotion = false;
		this.curLeft = 0;
		this.newLeft = 0;
		this.periodical;
		
		this.el = $(el);
		if(this.el) {
			this._setUp();
			this._updatePos('left');
		}
		
	},
	
	_setUp: function() {
		var el = this.el;
		var items = $ES('.'+this.options.scrollingItemClass, el);
		this.itemCount = items.length;
		this.maxMoves = items.length - this.options.maxInViewPort;
		
		// set up the buttons
		var left = $E('.'+this.options.classLeftButton, el);
		var right = $E('.'+this.options.classRightButton, el);
		
		var leftLink = $(left.getElementsByTagName("A")[0]);
		leftLink.href = "javascript:void(0);";
		leftLink.addEvent('click', this._buttonClickLeft.bindWithEvent(this, el));
		
		var rightLink = $(right.getElementsByTagName("A")[0]);
		rightLink.href = "javascript:void(0);";
		rightLink.addEvent('click', this._buttonClickRight.bindWithEvent(this, el));
		
	},
	
	_buttonClickLeft: function(event, el) {
		if(this.inMotion == true || this.curPos == 0) { return false; }
		this._updatePos('left');
		this.newLeft = this.curLeft + this.options.scrollingItemWidth; 
		this.inMotion = true;
		this.periodical = this._motionLeft.periodical(this.options.slideDelay, this);
	},
	
	_buttonClickRight: function(event, el) {
		if(this.inMotion == true || this.curPos == this.maxMoves) { return false; }
		this._updatePos('right');
		this.newLeft = this.curLeft - this.options.scrollingItemWidth;
		this.inMotion = true;
		this.periodical = this._motionRight.periodical(this.options.slideDelay, this);
	},
	
	_motionLeft: function() {
		if(this.curLeft >= this.newLeft) {
			$clear(this.periodical);
			this.inMotion = false;
		} else {
			var ele = $E('.'+this.options.scrollingItemsContainerClass, this.el);
			var updatedLeft = this.curLeft + this.options.slideAmount;
			this.curLeft = updatedLeft;
			ele.setStyle('margin-left', updatedLeft+'px');
		}
	},
	
	_motionRight: function() {
		if(this.curLeft <= this.newLeft) {
			$clear(this.periodical);
			this.inMotion = false;
		} else {
			var ele = $E('.'+this.options.scrollingItemsContainerClass, this.el);
			var updatedLeft = this.curLeft - this.options.slideAmount;
			this.curLeft = updatedLeft;
			ele.setStyle('margin-left', updatedLeft+'px');
		}
	},
	
	_updatePos: function(dir) {
		var left = $E('.'+this.options.classLeftButton, this.el);
		var right = $E('.'+this.options.classRightButton, this.el);
		
		if(dir == 'left') { this.curPos--; } else { this.curPos++; }
		if(this.curPos == this.maxMoves) { right.addClass(this.options.disabledClass); } else { right.removeClass(this.options.disabledClass); }
		if(this.curPos == 0) { left.addClass(this.options.disabledClass); } else { left.removeClass(this.options.disabledClass); }
	},
	
	
	blank: function() {} /* just so as I add more i dont have to do commas */
	
});

ScrollingViewPort.implement(new Options);
ScrollingViewPort.implement(new Events);
