$(function(){
	//Scroll
	$('#main').mousewheel(function(event, delta){
		if (delta > 0){
			if (moving == false){
				moving = true;
				scrollUp();
			}
		} else if (delta < 0) {
			if (moving == false){
				moving = true;
				scrollDown();
			}
		}
	});
	$(".up").mousedown(function(){
		if (moving == false){
			moving = true;
			scrollUp();
			clearInterval(timeout);
			timeout = setInterval(scrollUp, 400);
		}
		return false;
	});
	$(".up").mouseup(function(){
		clearInterval(timeout);
	});
	$(".down").mousedown(function(){
		if (moving == false){
			moving = true;
			scrollDown();
			clearInterval(timeout);
			timeout = setInterval(scrollDown, 400);
		}
		return false;
	});
	$(".down").mouseup(function(){
		clearInterval(timeout);
	});
});

//Scroll
var timeout;
var moving = false;
var scroll_height = 106;
var scroll_show = 5;

function scrollUp()
{
	var topActual = scroll_top();
	if (topActual < 0)
	{
		scroll_moving(topActual + scroll_height);
	}
	else
	{
		moving = false;
	}
}

function scrollDown()
{
	var topActual = scroll_top();
	var last_li = $("#boxScroll ul").find("li:last").offset().top;
	var limit = $("#scroller").offset().top + ( (scroll_show-1) * scroll_height )
	
	if ( $("#boxScroll ul").height() > $("#scroller").height() && last_li > limit )
	{
		scroll_moving(topActual - scroll_height);
	}
	else
	{
		moving = false;
	}
}

function scroll_moving (position)
{
	$("#boxScroll ul").animate({top: position}, 300, null, function(){moving = false;});
}

function scroll_top ()
{
	return $("#boxScroll ul").offset().top - $("#scroller").offset().top;
}