var Dom = YAHOO.util.Dom;
var $ = Dom.get;
var box = {};

function WindowBox(box, className, windowContainer, slideWidth) {
	
	var output = null;
	this.box = box;
	this.boxEl = $(box);
	this.className = className || 'box-item';
	this.windowContainer = $(windowContainer) || this.boxEl.parentNode;
	var items = YAHOO.util.Dom.getElementsByClassName(this.className, null, this.boxEl);
	this.width = items[0].offsetWidth;
	output = "this.width = " + this.width;
	
	this.maxWidth = 0;
	for (var i = 0; i < items.length; i++)
		this.maxWidth += items[i].offsetWidth;
	output += "\nthis.maxWidth = " + this.maxWidth;
	
	
	this.movedBy = 0;
	
	this.windowSize = Math.floor(this.windowContainer.offsetWidth / this.width);
	output += "\nthis.windowContainer.offsetWidth = " + this.windowContainer.offsetWidth;
	output += "\nthis.width = " + this.width;
	output += "\nthis.windowSize = " + this.windowSize;
	
	this.slideWidth = slideWidth || this.windowSize - 1;
	output += "\nthis.slideWidth = " + this.slideWidth;
	YAHOO.log(output, "info")
	
	if ($('slideLeft') && $('slideRight')) {
		YAHOO.util.Event.on('slideLeft', 'click', function(e) { 
			if (!this.slideLeft()) {
				Dom.addClass('slideLeft', 'inactive');
			}
			
			Dom.removeClass('slideRight', 'inactive');
			YAHOO.util.Event.stopEvent(e);
		}, null, this);
		
		YAHOO.util.Event.on('slideRight', 'click', function(e) {
			if (!this.slideRight()) {
				Dom.addClass('slideRight', 'inactive');
			}
			
			Dom.removeClass('slideLeft', 'inactive');
			YAHOO.util.Event.stopEvent(e);
		}, null, this);
	}
}

WindowBox.prototype = {
	slideBy: function(delta) {
		var anim = new YAHOO.util.Motion(
			this.box, 
			{points: {by: [0, delta]}}, 
			0.3, 
			YAHOO.util.Easing.easeBoth
		);

		anim.animate();
	},
	
	slideLeft: function() {
		if (this.movedBy >= this.slideWidth*this.width) {
			this.slideBy(this.slideWidth*this.width);
			this.movedBy -= this.slideWidth*this.width;
		}
		
		return (this.movedBy >= this.slideWidth*this.width);
	},
	
	slideRight: function() {
		if (this.movedBy < this.maxWidth - this.slideWidth*this.width) {
			this.slideBy(-this.slideWidth*this.width);
			this.movedBy += this.slideWidth*this.width;
		}

		return (this.movedBy < this.maxWidth - this.slideWidth*this.width);
	}
}

YAHOO.util.Event.on(window, 'load', function() { box = new WindowBox('mybox', 'box-item', 'windowbox-container', 3)});
