/**
 * ProductBand
 *
 * @require 	MooTools
 * @require 	MooTools.NB
 * @require 	MooTools.NB.Custom
 *
 * @author 		Sebastian Sommer <s.sommer@netzbewegung.com>
 * @version 	1.00 alpha
 */
MooTools.NB.Custom.ProductBand = new Class
({
	initialize: function(outer)
	{
		this.linkLeft 		= outer.getElements('a.left')[0];
		this.linkRight 		= outer.getElements('a.right')[0];
		this.innerWrapper	= outer.getFirst('.inner');
		
		this.showElements	= 4;
		this.elementCount	= this.innerWrapper.getChildren('.entry').length;
		this.elementSize	= this.innerWrapper.getFirst('.entry').getDimensions().width;
		
		this.maxMargin = ((this.elementCount*this.elementSize)-(this.showElements*this.elementSize))*-1;
		this.fxObj = new Fx.Tween(this.innerWrapper,{'fps': 50, 'transition': 'back:inOut', 'duration': 700});
		
		this.linkRight.addEvent('click',this.moveRight.bindWithEvent(this));
		//this.linkRight.addClass('act'); init arrow by php
		
		this.linkLeft.addEvent('click',this.moveLeft.bindWithEvent(this));
		
		if(this.elementCount <= this.showElements)
		{
			this.linkRight.removeClass('act');
		}
	}
});

MooTools.NB.Custom.ProductBand.implement
({
	moveRight: function(event)
	{
		event.stop();
		intMargin = this.innerWrapper.getStyle('margin-left').toInt()-this.elementSize;
		this.controller(intMargin);
	},
	
	moveLeft: function(event)
	{
		event.stop();
		intMargin = this.innerWrapper.getStyle('margin-left').toInt()+this.elementSize;
		this.controller(intMargin);
	},
	
	controller: function(intMargin)
	{
		if(this.maxMargin > intMargin)
		{
			this.linkRight.removeClass('act');
			return false;
		}
		
		if(this.maxMargin < intMargin)
		{
			this.linkRight.addClass('act');
		}
		
		if(this.maxMargin == intMargin)
		{
			this.linkRight.removeClass('act');
		}
		
		if(intMargin < 0)
		{
			this.linkLeft.addClass('act');
		}
		
		if(intMargin > 0)
		{
			return false;
		}
		
		if(intMargin == 0)
		{
			this.linkLeft.removeClass('act');
		}
		
		this.fxObj.start('margin-left', this.innerWrapper.getStyle('margin-left').toInt(), intMargin);
	}
})

window.addEvent('domready',function(){
	var productBandItem = $('product-band');
	
	if(productBandItem)
	{
		var ProductBandObj = new MooTools.NB.Custom.ProductBand(productBandItem);
	}
});
