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

function WindowBox(box, className, windowContainer, slideWidth) {
	this.box = box;
	this.boxEl = $(box);
	this.className = className || 'boxItem';
	this.windowContainer = $(windowContainer) || this.boxEl.parentNode;
	var items = YAHOO.util.Dom.getElementsByClassName(this.className, null, this.boxEl);
	this.width = items[0].offsetWidth;
	
	this.maxWidth = 0;
	for (var i = 0; i < items.length; i++)
		this.maxWidth += items[i].offsetWidth;
	
	this.movedBy = 0;
	
	this.windowSize = Math.floor(this.windowContainer.offsetWidth / this.width);
	this.slideWidth = slideWidth || this.windowSize - 1;
	
	if ($('slideRight')) {//$('slideLeft') && 
		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: [delta, 0]}}, 
			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;
		}
		//alert(this.movedBy);
		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;
		}
		//alert(this.movedBy);
		return (this.movedBy < this.maxWidth - this.slideWidth*this.width);
	}
}

function checkMarginSlot () {
   if ($('margincontent') && $('margincontent').offsetHeight < 23) {
      YAHOO.util.Dom.setStyle('margincontent', 'display', 'none'); 
   }
}

YAHOO.util.Event.on(window, 'load', function() { box = new WindowBox('mybox')});
YAHOO.util.Event.on(window, 'load', checkMarginSlot );