$(function(){
	$('div.mod-slider').each(function(){
		var carrousel = $(this);
		var contents = carrousel.find('div.contents-inner');
		var unite = carrousel.find('div.unite');
		var uniteLength = unite.length;
		var uniteWidth = unite.outerWidth();
		var visibleUnite = 4;
		var controlL = carrousel.find('li.prev');
		var controlR = carrousel.find('li.next');
		var current = 0;
		
		contents.css('width', (uniteLength * uniteWidth) + 'px');
		
		if(uniteLength - visibleUnite < 0){
			controlL.hide();
			controlR.hide();
			carrousel.find('div.slider').hide();
			return;
		}
		
		else{
			
			function jump(target, carrousel){
				var range = target * uniteWidth;
				contents.animate({left : -1 * range + 'px'}, 'slow', '', function(){				
					controlL.removeClass('disable');
					controlR.removeClass('disable');
					if(target == 0){
						controlL.addClass('disable');
					}
					else if(target == uniteLength - visibleUnite){
						controlR.addClass('disable');
					}
				});
			}
			
			current = 0;
			jump(current, carrousel);
			
			controlL.click(function(){
				var o = check(current - 1);
				current = o.page;
				if(!o.over){
					jump(current, carrousel);
					sliderFix(current);
				}
			});
			
			controlR.click(function(){
				var o = check(current + 1);
				current = o.page;
				if(!o.over){
					jump(current, carrousel);
					sliderFix(current);
				}
			});
			
			/* group (or category) jump*/
			$(carrousel.find('div.group')).each(function(i){
				var groupHead = $("div.mod-slider.home-sliderContainer div.unite").index($(this).children('div:eq(0)'));
				$('ul.nav-group li:eq(' + i + ')').click(function(){
					var o = check(groupHead);
					current = o.page;
						jump(current, carrousel);
						sliderFix(current);
				});
			});
			
			
			
			function check(target){
				var over = false;
				var disable = null;
				if(0 > target) {
					target = 0;
					over = true;
				}
				else if(target > uniteLength - visibleUnite) {
					target = uniteLength - visibleUnite;
					over = true;
				}
				return {
					page : target,
					over : over
				};
			}
			
	
			// ----- slider -----
			
			
			var slider = carrousel.find('div.slider');
			var sliderWidth = slider.outerWidth()
			var handle = slider.find('div.handle');
			var handleWidth =　Math.floor(sliderWidth / (uniteLength - (visibleUnite - 1)));
			handle.css('width',handleWidth);
			var handlePointX = 0;
			var sliderX = slider.offset().left;
			var sliderMin = 0;
			var sliderMax = sliderMin + sliderWidth - handleWidth;
			var downStartPoint = 0;
			
			var sliderCells = [0];
			for(var i = 1; i < uniteLength - 1; i ++){
				sliderCells[i] = sliderCells[i-1] + handleWidth;
			}
			
			slider.mousedown(function(e){
				downStartPoint = e.pageX - sliderX
				//downStartPoint = e.pageX - sliderX - handleWidth / 2;
				//if(sliderMin < downStartPoint && downStartPoint < sliderMax){
				//	handle.css('left', downStartPoint);
				//}
				$('body').mousemove(handleMove).mouseup(posFix).mouseup(unbind);
				return false;
			});
			function handleMove(e) {
				var newX = e.pageX - sliderX - handleWidth / 2;
				if(sliderMin <= newX && newX <= sliderMax){
					handle.css('left', newX);
				
					var scale = (parseInt(contents.css('width')) - uniteWidth * (visibleUnite)) / (sliderWidth - handleWidth);
					var currentLeft = (e.pageX - sliderX - handleWidth / 2) * scale + 2;
					contents.css('left', -currentLeft);
				}
				return false;
			}
			function posFix(){
				$.each(sliderCells, function(i){
					var fixTargetPos = this;
					var max = fixTargetPos + handleWidth / 2;
					var min = fixTargetPos - handleWidth / 2;
					var handleX = parseInt(handle.css('left'));
					if(min < handleX && handleX < max){
						var o = check(i);
						current = o.page;
						jump(current, carrousel);
						sliderFix(current);
						false;
					}
				});
				return false;
			}
			
			function sliderFix(current){
				// min
				if(current == 0){
					handle.animate({left : sliderMin}, 'slow');
				}
				// max
				else if(current == uniteLength - visibleUnite){
					handle.animate({left : sliderMax}, 'slow');
				}
				else{
					handle.animate({left : current * handleWidth}, 'slow');
				}
			}
			
			function unbind() {
				$(this).unbind('mousemove', handleMove).unbind('mouseup', posFix).unbind('mouseup', unbind);
			}
		}
	});
})
