/*
 * (c) Nicolas Pajon et cie. 2008-2009
 * 
 * 
 * 
 */

(function($)
{
	$.fn.yzCarousel = function(settings)
	{

		var version = '0.1b';
		var timer = null;
		var options = {interval: 2500, size: 240, set_margins: true, direction:-1};


		$.extend(options, settings);

		options.count = $(this).size();
		options.parent_width = $(this).parent().width();
		options.parent_height = $(this).parent().height();
		options.current = 0;

		var myself = this;

		var rotate = function (){

			if (options.direction==-1){

				rotateRight();

			} else {

				rotateLeft();
			}

		}

		var rotateRight = function(){

			options.current ++;

			myself.each(function(i){

				var pos = ((options.count-1 - ((i + options.current) % options.count))*options.size);

				if (pos>=(options.count-1)*options.size){

					$(this).animate({'top': 0-options.size},"normal",null,function(){$(this).css('top',pos).show()} );

				} else{

					$(this).animate({'top': pos });

				} // if

			}); // myself.each

		} // function rotate

	var rotateTop = function(){

			options.current ++;

			myself.each(function(i){

				var pos = ((( i + options.current) % options.count)*options.size);

				if (pos==0){

					$(this).hide().css('top',0-options.size).show().animate({'top': pos });

				} else{

					$(this).animate({'top': pos });

				} // if

			}); // myself.each

		} // function rotate


		if (options.count*options.size>options.parent_width){

			var timer = setInterval(function(){rotate();}, options.interval);

		}

		return this.each(function(i){

		if (options.direction == -1){

				$(this).css({'top': ((options.count-1-i)*options.size)});

			} else {

				$(this).css({'top': (i*options.size)});

			}

			if (options.set_margins==true){
				var th = $(this).height();
				var mt = Math.round((options.parent_height-th)/2);
				$(this).css('margin-top',mt);
				$(this).css('margin-bottom',options.parent_height-th-mt);
			} // if

		}); // return this.each

 } // function $.fn.yzCarousel

})(jQuery);
