jQuery.fn.glebsn_carousel = function(){
	var carousel_items = $("#photos").find("li").length;
	var item_width = $("#photos").find("li").width() + 0; // margin: 0
	var carousel_width = item_width*carousel_items;
	var clip_width = $("#carousel-clip").width();
	var carousel_list = $("#photos");
	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 div");
	var parent_prev = $("#prev div");
	var number = 0;
	var link = "";
	
	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(can_move) {
		$("#next div").removeClass('disabled');
	}
	
	/* functions */
	
	$("#next").click( function() {
		if(!parent_next.hasClass('disabled')) {
			left = left - item_width;
		
			if(can_move && (left > -carousel_width)) {
			
			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("disabled");
			}
			
			carousel_list.animate({"left": desctination},"fast", function() {
				// меняем ссылку
				number = - left / item_width; 
				link = $("#photos li:eq("+ number +")").find('a').attr('href');
				var html = link ? "<a href=\""+link+"\"></a>":"";
				$("#glamour").html(""+html+"");
			});
			parent_prev.removeClass("disabled");
		}	
		}	
	});
		
	$("#prev").click( function() {
		
		if(!parent_prev.hasClass('disabled')) {
			left = left + item_width;

			if(can_move && (-left >= 0)) {
			
			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("disabled");
			}
			
			carousel_list.animate({"left": desctination},"fast", function() {
				
				// ? - не нужна как я понимаю эта строчка if (desctination == first_delta) parent_prev.addClass("disabled");
				number = - left / item_width; 
				link = $("#photos li:eq("+ number +")").find('a').attr('href');
				var html = link ? "<a href=\""+link+"\"></a>":"";
				$("#glamour").html(""+html+"");
			});
			parent_next.removeClass("disabled");
		}
		}
	});
	
	/*
	$("#prev").mouseout( function() {
		carousel_list.stop();
	});
	
	$("#next").mouseout( function() {
		carousel_list.stop();
	});
	*/
	
	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/3000)*1000;
		
		
		return current_speed; 
	}
};
