jQuery.fn.glebsn_carousel = function(param){
	var param = param;
	var carousel_items_array = $("#carousel-list").find("span.title");
	var carousel_items = $("#carousel-list").find("li").length;
	var item_width = $("#carousel-list").find("li").width() + 115; // margin: 116 - may change
	var carousel_width = item_width*carousel_items;
	var clip_width = $("#carousel-clip").width();
	var carousel_list = $("#carousel-list");
	/*
	var first_delta = -19;
	var last_delta = 19;
	krosh
	*/
	var first_delta = 0;
	var last_delta = 0;
	var current = $('#current').length ? $('#current').attr('value'):0;
	var speed = 2000;
	var left = 0;
	
	var parent_next = $("#next").parents('div.next');
	var parent_prev = $("#prev").parents('div.prev');
	
	var title_next  = parent_next.find('span');
	var title_prev  = parent_prev.find('span');
	
	if(clip_width<carousel_width) {
		parent_next.removeClass("empty");
	}
	
	
	var top_preloader = $("#kitchen").offset().top + 257;
 	var left_preloader =$("#kitchen").offset().left + 465;
	
	

	var can_move = (clip_width) < (carousel_width - first_delta + last_delta);
	
	/* we can move if carousel width minus deltas is bigger than carousel clip */
	/*
	if (current && can_move) {
		
		move_carousel_to_position(current);
	}
	*/
	
	if(param) {
		move_carousel_via_param(param);
	}
	
	/* functions */
	$("#next_area").click( function() {
		if(!parent_next.hasClass('empty')) {
		left = left - item_width;
		var number = (-left/item_width)+1;
		var number_ = number-1;
		
		if(can_move && (left > -carousel_width)) {
			
			change_kitchen(number, number_,"next");
			
			$("#collection-section div.imgbox").removeClass('selected');
			$("#collection-section div.imgbox").eq(-left/item_width).addClass("selected");
			
			
			var how_long = Math.min(item_width,carousel_width-clip_width-last_delta);
				
			var desctination = -how_long+last_delta;
			last_delta = last_delta - how_long;
			var current_speed = change_speed(desctination);
			
			if ((left - item_width) == -carousel_width) {
				parent_next.addClass("empty");
				title_next.empty();
				title_prev.html(carousel_items_array[(-left/item_width)-1].innerHTML);
				//
			} else {
				
				title_prev.html(carousel_items_array[(-left/item_width)-1].innerHTML);
				title_next.html(carousel_items_array[(-left/item_width)+1].innerHTML);
			}
			
			carousel_list.animate({"left": desctination},/*current_speed*/"fast", function() {});
			parent_prev.removeClass("empty");
		}	
		}	
	})
	
	$("#prev_area").click( function() {
		
		if(!parent_prev.hasClass('empty')) {
		left = left + item_width;
		var number = (-left/item_width)+1;
		var number_ = number+1;

		if(can_move && (-left >= 0)) {
			
			change_kitchen(number, number_,"prev");
			
			$("#collection-section div.imgbox").removeClass('selected');
			$("#collection-section div.imgbox").eq(-left/item_width).addClass("selected");
			
			var how_long = Math.min(item_width, -last_delta);
			var desctination =  how_long+last_delta;
			last_delta = last_delta + how_long;
			var current_speed = change_speed(desctination);
			
			if (left == 0) {
				parent_prev.addClass("empty");
				title_prev.empty();
				title_next.html(carousel_items_array[(-left/item_width)+1].innerHTML);
				//
			} else {
				title_prev.html(carousel_items_array[(-left/item_width)-1].innerHTML);
				title_next.html(carousel_items_array[(-left/item_width)+1].innerHTML);
			}
			
			carousel_list.animate({"left": desctination},/*current_speed*/"fast", function() {
				if (desctination == first_delta) parent_prev.addClass("empty");
			});
			parent_next.removeClass("empty");
		}
		}
	});
	
	/* controls */
	
	$("#collection-section img").click( function() { 
		var number_ = parseInt($("#collection-section").find("div.selected img").attr('number'));
		
		$("#collection-section div.imgbox").removeClass('selected');
		var parent = $(this).parents('div.imgbox');
		parent.addClass("selected");
		
		// как то определить номер кликнутой картинки - можно тупо индексом, но это не вариант
		
		
		
		if(can_move) move_carousel_to_position($(this).attr('number'), number_,"");
	});
	
	function move_carousel_via_param(param) {
		param = param.replace(/_/g," ");
		$.each(carousel_items_array, function (key,val) {
			if(val.innerHTML==param && key!=0) {
				move_carousel_to_position(key+1,1,"direct");
				$("#collection-section div.imgbox").removeClass('selected');
				$("#collection-section div.imgbox").eq(key).addClass("selected");
			}
		});
	}
	
	/* this function moves carousel list to the defined position */
	function move_carousel_to_position(number, number_, param) {
		
		
		var delta = -(item_width*(number-1)) + first_delta;
		
		if (delta > first_delta) delta = first_delta; // не левее левого
		
		var right_border = delta + carousel_width - last_delta;
		
		if (right_border < clip_width) {
			delta = clip_width-carousel_width+last_delta;
		}
		left = delta;
		last_delta = delta;
		
		if(last_delta==0) {
			parent_prev.addClass("empty");
			title_prev.empty();
		}
		else {
			parent_prev.removeClass("empty");
			title_prev.html(carousel_items_array[(-left/item_width)-1].innerHTML);
		}
		
		if(last_delta==-carousel_width+item_width) {
			parent_next.addClass("empty");
			title_next.empty();
		}
		else {
			parent_next.removeClass("empty");
			title_next.html(carousel_items_array[(-left/item_width)+1].innerHTML);
		}
		
		change_kitchen(number, number_,param);
		
		
		//var current_speed = change_speed(delta);
		
		carousel_list.animate({"left": delta}, "fast");
	}
	
	function change_speed(destination) {
		var current_left = parseInt(carousel_list.css("left"));
		var distance = Math.abs(destination-current_left);
		
		var current_speed = parseInt(speed * Math.pow(distance / parseInt(item_width), 1/1.3) );
		//current_speed = current_speed < 2000 ? 2000:current_speed;
		
		current_speed = Math.round(current_speed/1000)*1000;
		
		
		return current_speed; 
	}
	
	function change_kitchen(number, number_, postfix) {
		
		var image = $('#kitchen-' + number).attr('src');
		
		/*
 		$("#ex-1-container").css({
  			top: top_preloader+"px",
  			left: left_preloader+"px"
 		});
 		*/
        /*
 		$("#ex-1").jdCrazyDots({
			"speed": 60,
		    "size": 48,
		    "dotWidth": "13%",
		    "dotHeight": "26%",
		    "empty": true
		});
		*/
       
        $("#uploader3d").css({
  			top: top_preloader+"px",
  			left: left_preloader+"px"
 		}).show('fast', function() {
 		
 			
		if(image=='/i/spacer.gif') {
			
			
			$('#kitchen-' + number).attr('src',$('#kitchen-source-' + number).html()).load( function() {
  				
				
				//setTimeout( function() {
					//alert("f");
					$('#kitchen-' + number_).fadeOut("slow");
					$('#kitchen-' + number).fadeIn("slow");
					$('#kitchen-box-' + number_).addClass('hide');
					$('#kitchen-box-' + number).removeClass('hide');
					//$("#ex-1").empty();
					$("#uploader3d").hide();
				//}
				//,"1000")
  			});
  		
		} else {
		
			$('#kitchen-' + number_).fadeOut("slow");
			$('#kitchen-' + number).fadeIn("slow");
			$('#kitchen-box-' + number_).toggleClass('hide');
			$('#kitchen-box-' + number).toggleClass('hide');
			//$("#ex-1").empty();
			$("#uploader3d").hide();

		}	
		
		var title = new String(carousel_items_array[number-1].innerHTML);
		title = title.replace(/ /g,"_");
				
		window.location="/presentation/"+"#"+title;
	
		
		var pageTracker = _gat._getTracker('UA-17765479-1');
		pageTracker._trackPageview("/presentation/"+title+"/"+postfix);
		 			
 		});

	}
};
