
jQuery.extend( jQuery.easing, {
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	}
});


jQuery.fn.carousel = function(options) {

	var options = jQuery.extend({
		turn: 1,		// по сколько элементов будет листаться
		timer: 0,		// количество милисекунд для автоматичекого пролистывания
		vertical: 0		// горизонтальная карусель
	}, options);


	return this.each(function() {

		var carousel		= this;
		var carousel_ul		= $('.mid ul', this);
		var timer;
		var ul_left			= 0;

		if (options.vertical)
		{
			var li_height		= $('li', carousel_ul)[0].clientHeight;		// высота одного элемента
			var mid_height		= $('.mid', this)[0].clientHeight;			// высота div.mid
			var count_in_mid	= Math.floor(mid_height / li_height);			// количество элементов вмещающихся в div.mid
		}
		else
		{
			var li_width		= $('li', carousel_ul)[0].clientWidth;		// ширина одного элемента
			var mid_width		= $('.mid', this)[0].clientWidth;			// ширина div.mid
			var count_in_mid	= Math.floor(mid_width / li_width);			// количество элементов вмещающихся в div.mid
		}
		
		
		
		if (options.vertical)
		{
			$('div.up a', carousel).bind('click', function(){ turn_vert(false, true); return false; });
			$('div.down a', carousel).bind('click', function(){turn_vert(true, true); return false; });
		}
		else
		{
			$('a.prev', carousel).bind('click', function() { turn(false, true); return false; });
			$('a.next', carousel).bind('click', function() { turn(true, true); return false; });
		}
		
		

		if (options.timer) timer = setInterval(function() { turn(true, false); }, options.timer);

		if (options.vertical)
		{
			if (carousel_ul.css('marginTop') != 'auto') ul_up = parseInt(carousel_ul.css('marginTop'), 10);	
		}
		else
		{
			if (carousel_ul.css('left') != 'auto') ul_left = parseInt(carousel_ul.css('left'), 10);	
		}


		function turn(direct, stop_timer)
		{
			var left_px, is_end, w, l_min;

			if (timer && stop_timer)
			{
				clearInterval(timer);
				timer = undefined;
			}

			is_end = false;	
			w = li_width * options.turn;
			l_min = (count_in_mid - $('li', carousel_ul).length) * li_width;
			if (l_min > 0) l_min = 0;

			if (direct)
				if (ul_left <= l_min)
				{
					ul_left = 0;
					is_end = true;
				} else {
					ul_left -= w;
					if (ul_left < l_min) ul_left = l_min;
				}
			else
				if (ul_left >= 0)
				{
					ul_left = l_min;
					is_end = true;
				} else {
					ul_left += w;
					if (ul_left > 0) ul_left = 0;
				}

			carousel_ul.animate({left:ul_left+'px'}, (is_end ? 1300 : 'slow'), (is_end ? 'easeInOutBack' : 'easeInOutCubic'));
		}
		
		function turn_vert(direct, stop_timer)
		{
			var left_px, is_end, w, l_min;

			if (timer && stop_timer)
			{
				clearInterval(timer);
				timer = undefined;
			}

			is_end = false;	
			w = li_height * options.turn;
			l_min = (count_in_mid - $('li', carousel_ul).length) * li_height;
			if (l_min > 0) l_min = 0;

			if (direct)
				if (ul_up <= l_min)
				{
					ul_up = 0;
					is_end = true;
				} else {
					ul_up -= w;
					if (ul_up < l_min) ul_up = l_min;
				}
			else
				if (ul_up >= 0)
				{
					ul_up = l_min;
					is_end = true;
				} else {
					ul_up += w;
					if (ul_up > 0) ul_up = 0;
				}

			carousel_ul.animate({marginTop:ul_up+'px'}, (is_end ? 1300 : 'slow'), (is_end ? 'easeInOutBack' : 'easeInOutCubic'));
		}

	});

};

