/*
 * Desenvolvido por Leandro Sales
 * leandroasp at gmail dot com
 * +55 86 94275626
 * http://www.leandrosales.com.br
 *
 * Reprodução autorizada desde que mantenha os créditos
 */
var LSL;
if (!LSL) {
	LSL = {};
}

LSL.Carrocel = function(opt) {
	if (!opt) return;
	if (!opt.moveSize) return;
	if (!opt.totalInScreen) return;
	if (!opt.totalBox) return;
	if (!opt.carrocel) return;
	if (!opt.back) return;
	if (!opt.forward) return;
	
//	opt.back.css({visibility: 'hidden'});
	
//	if (opt.totalBox <= opt.totalInScreen) opt.forward.css({visibility: 'hidden'});
	
	var minLeft = (opt.totalInScreen-opt.totalBox)*opt.moveSize;
	var carrocelControlClick = function(t) {
		var left = opt.carrocel.css('marginLeft');
		left = parseInt(left);
		if (typeof(left) == 'undefined' || left == '' || isNaN(left) ) left = 0;
		
		var newLeft = left;
		if (t == 'back') {
			if (left < 0) newLeft += opt.moveSize;
			if (left > 0) newLeft = 0;
			if (newLeft % opt.moveSize != 0) newLeft -= newLeft % opt.moveSize;
		} else if (t == 'forward') {
			if (newLeft > minLeft) newLeft -= opt.moveSize;
			if (newLeft < minLeft) newLeft = minLeft;
			if (newLeft % opt.moveSize != 0) newLeft -= (opt.moveSize + newLeft % opt.moveSize);
		}
		if (newLeft < 0) {
//			opt.back.css({visibility: 'visible'});
		} else {
//			opt.back.css({visibility: 'hidden'});
		}
		if (newLeft == minLeft) {
//			opt.forward.css({visibility: 'hidden'});
		} else {
//			opt.forward.css({visibility: 'visible'});
		}	

		if (newLeft != left) {
			opt.carrocel.stop().animate({marginLeft: newLeft + "px"}, 500);
		}
	};
	opt.back.click(function() {
		carrocelControlClick('back');
	});
	opt.forward.click(function() {
		carrocelControlClick('forward');
	});
};

