/*
 *	a2Carousel 2.0 - Организация карусели списка с кнопками вниз/вверх
 *
 *	Examples/Docs: www.alekstest.ru/a2Carousel/
 *
 *  Coprright(c) 2009 Petrov Aleksandr - alekstest.ru
 *
 *  Dual licensed under the MIT and GPL licenses:
 *  http://www.opensource.org/licenses/mit-license.php
 *  http://www.gnu.org/licenses/gpl.html
*/
(function($){
	$.fn.a2Carousel = function(Options){
		var options = $.extend({
			class_carousel: "a2-carousel",			//класс карусели
			class_prefix_prevnext: "a2-carousel-",	//классы или префикс кнопок прокрутки
			class_active: "active",					//класс активной кнопки прокрутки
			list_ul: "ul",							//список
			list_li: "li",							//элементы списка
			bottom_relatively_top: false,			//считать сдвиг вниз относительно верхнего края стороны
			scrolling_speed: 500					//скорость прокрутки в миллисекундах
		},Options);

		return this.each(function(){
			var block_carousel = $(this);
			var inner_block_carousel = block_carousel.find("."+options.class_carousel);
			var list_ul = inner_block_carousel.find(options.list_ul);
			var list_li = list_ul.find(options.list_li);
			var class_prev
			var class_next;
			if(typeof options.class_prefix_prevnext == "string"){
				class_prev = options.class_prefix_prevnext+"prev";
				class_next = options.class_prefix_prevnext+"next";
			}
			else{
				class_prev = options.class_prefix_prevnext[0];
				class_next = options.class_prefix_prevnext[1];
			}
			var button_prev = block_carousel.find("."+class_prev);
			var button_next = block_carousel.find("."+class_next);
			block_carousel.find("."+class_prev+", ."+class_next).bind("click", function(){
				if(typeof list_ul.data("ismove") == "undefined" || list_ul.data("ismove") == false){
					var now_element = $(this);
					if(now_element.hasClass(options.class_active)){
						var shift, new_shift, for_i;
						if(now_element.hasClass(class_next)){
							shift = Math.abs(parseInt(list_ul.position().top)) + inner_block_carousel.outerHeight(true);
							for(for_i=list_li.length-1; for_i>=0; for_i--){
								if(parseInt($(list_li[for_i]).position().top) <= shift){
									if(options.bottom_relatively_top){
										new_shift = Math.abs(parseInt(list_ul.position().top)) + parseInt($(list_li[for_i]).outerHeight(true));
									}
									else{
										new_shift = parseInt($(list_li[for_i]).position().top)+$(list_li[for_i]).outerHeight(true)-inner_block_carousel.outerHeight(true);
									}
									if(for_i == list_li.length-1){
										var disactivate_next = true;
									}
									break;
								}
							}
						}
						else{
							shift = Math.abs(parseInt(list_ul.position().top));
							for(for_i=0; for_i<list_li.length; for_i++){
								if(parseInt($(list_li[for_i]).position().top) >= shift){
									new_shift = parseInt($(list_li[for_i-1]).position().top);
									if(for_i-1 == 0){
										var disactivate_prev = true;
									}
									break;
								}
							}
						}
						list_ul.data("ismove", true);
						list_ul.animate( {top:"-"+new_shift+"px" }, options.scrolling_speed, function(){
							list_ul.data("ismove", false);
							if(disactivate_next){
								now_element.removeClass(options.class_active);
							}
							if(disactivate_prev){
								now_element.removeClass(options.class_active);
							}
							if(now_element.hasClass(class_next)){
								button_prev.addClass(options.class_active);
							}
							else{
								button_next.addClass(options.class_active);
							}
						});
					}
				}
			});
			var height_list_li = 0;
			list_li.each(function(){
				height_list_li+= $(this).outerHeight(true);
			});
			if(height_list_li > inner_block_carousel.outerHeight(true)){
				button_next.addClass(options.class_active);
			}
		});
	};
})(jQuery);