/*
 * Tiny Carousel 1.73
 *
 * Copyright (c) 2010 Maarten Baijs
 *
 * Date: 20 / 05 / 2010
 * Library: jQuery
 *
 */
(function($){
	$.fn.productscarousel = function(options){
		var defaults = { 
			animation: true, // false is instant, true is animate.
			duration: 1000 // how fast must the animation move in ms?
		};
		var options = $.extend(defaults, options);  

		var oSlider = $(this);
		var oViewport = $('.viewport', oSlider);
		var oContent = $('.line_products', oSlider);
		var oPages = oContent.children('.line_product');
		var oBtnNext = $('.next', oSlider);
		var oBtnPrev = $('.prev', oSlider);
		var iPageSize, iCurrent, iLeftover;

		return this.each(function(){
			initialize();
		});
		function initialize(){
			iPageSize = $(oPages[0]).outerWidth(true);
			iLeftover = Math.ceil((oViewport.outerWidth() / iPageSize) -1);
			iCurrent = -1;
			oContent.css('width', (iPageSize * oPages.length));
			setEvents();
			move(1);
		}
		function setButtons(){
			oBtnPrev.toggleClass('disable', !(iCurrent > 0));
			oBtnNext.toggleClass('disable', !(iCurrent+iLeftover+1 < oPages.length));
		}
		function setEvents(){
			if(oBtnPrev.length > 0 && oBtnNext.length > 0) {
				oBtnPrev.click(function(){move(-1); return false;});
				oBtnNext.click(function(){move( 1); return false;});
			}
		}
		function move(iDirection) {
			if(iCurrent > 0 || iCurrent +1 < oPages.length){
				iCurrent += iDirection;
				var oPosition = {};
				oPosition['left'] = -(iCurrent * iPageSize);
				oContent.animate(oPosition,{
					queue: false,
					duration: options.animation ? options.duration : 0
				});
				setButtons();
			}
		}
	};
})(jQuery);


