/**
 * @author Stéphane Roucheray 
 * @extends jquery
 */



jQuery.fn.carousel = function(previous, next, options){
	var sliderList = jQuery(this).children()[0];

	if (sliderList) {
		var increment = jQuery(sliderList).children().outerWidth("true"),
		elmnts = jQuery(sliderList).children(),
		numElmts = elmnts.length,
		sizeFirstElmnt = increment,
		shownInViewport = Math.round(jQuery(this).width() / sizeFirstElmnt),
		firstElementOnViewPort = 1,
		isAnimating = false;
		
		for (i = 0; i < shownInViewport; i++) {
			jQuery(sliderList).css('width',(numElmts+shownInViewport)*increment + "px");
			jQuery(sliderList).append(jQuery(elmnts[i]).clone());
		}

		jQuery(previous).mouseover(function(event){
		    $(this).everyTime(500, function(){
			    if (!isAnimating) {
				    if (firstElementOnViewPort == 1) {
					    jQuery(sliderList).css('left', "-" + numElmts * sizeFirstElmnt + "px");
					    firstElementOnViewPort = numElmts;
				    }
				    else {
					    firstElementOnViewPort--;
				    }
				
				    jQuery(sliderList).animate( { left: "+=" + increment, y: 0, queue: true }, "linear", function(){isAnimating = false;} );
				    isAnimating = true;
			    }
			});
		}).mouseout(function(event){
		    $(this).stopTime();
		});
		
		jQuery(next).mouseover(function(event){
		    $('.dresslist .dressitems').scrollTo({top: '0px', left: '100%'}, {duration: ($(".dresslist .dressitems .viewport > div").size() * 200)});
		}).mouseout(function(event){
		    //$(this).stopTime();
		});
	}
};

