/* malihu custom scrollbar plugin - http://manos.malihu.gr */

var slideFlag = 1;
var nowSection = 0;
var newSection = 0;
var idNum = 0;
var idFlag = 0;


var totalHeight;
var contentsHeight;
var scrollbarHeight;
var listHeight;
var listNum;
var listPadding;

var scrollbarTopMaxNum;

var dir;
var tmpDir;

(function ($) {
	$.fn.scrollbar = function (){
		totalHeight = $("div#scrollArea").attr("clientHeight");
		contentsHeight = $("div#contents").height();
		scrollbarHeight = $("div#scrollbar").height();
		listHeight = $("div#sub li").height();
		listNum = $("div#sub li").length;
		listPadding = ((totalHeight / listNum) - listHeight) / 2;
		
		scrollbarTopMaxNum = contentsHeight - scrollbarHeight;
		
		tmpDir = location.href.split("#s");
		dir = tmpDir[0];
		
		if (isNaN(tmpDir[tmpDir.length - 1]) == false) {
			$("div#scrollbar").animate({ "top": scrollbarHeight * (tmpDir[tmpDir.length - 1]) + "px" }, 500, "easeInOutExpo", completeScrollMain(tmpDir[tmpDir.length - 1]));
			idNum = tmpDir[tmpDir.length - 1];
			nowSection = parseInt(tmpDir[tmpDir.length - 1]);
			
			
			/*
			location.href = tmpDir[tmpDir.length - 2];
			
			var Kanshi = function(){
				var pages = document.getElementBy("contents");
				
				if(pages == undefined){
					setTimeout(Kanshi, 100);
				}
				
				// 要素の生成が完了したときの処理をココに書く
				
				newSection = tmpDir[tmpDir.length - 1];
			}
			*/
		}
		
		var wheelFlag = 1;
		$("div#contents").mousewheel(function(event, delta) {
			if (slideFlag == 1 && wheelFlag == 1) {
				var mousewheelNum = 0;
				
				if (delta > 0) {
					mousewheelNum = -1;
				}
				else if (delta < 0) {
					mousewheelNum = 1;
				}
				
				if (nowSection + mousewheelNum >= 0 && nowSection + mousewheelNum < listNum) {
					newSection = nowSection + mousewheelNum;
					scrollMain(newSection);
				}
				
				wheelFlag = 0;
				var wheelTimer = setInterval(function() { wheelFlag = 1; clearInterval(wheelTimer); }, 300);
				
				return (false);
			}
		});
		
		$("a").click(function(event) {
			tmpDir = this.href.split("#s");
			
			dir = tmpDir[0];
			
			if (tmpDir[1] == undefined) {
				tmpDir[1] = 0;
			}
			else {
				$("div#main").css("margin-top", 0 + "px");
				nowSection = parseInt(tmpDir[1]);
				idNum = parseInt(tmpDir[1]);
				
				fixScrollbar(idNum);
			}
			

/*
			$("div#main").css("margin-top", 0 + "px");
			
			tmpDir = this.href.split("#s");
			
			dir = tmpDir[0];
			
			if (tmpDir[1] == undefined) {
				tmpDir[1] = 0;
			}
			
			nowSection = parseInt(tmpDir[1]);
			idNum = parseInt(tmpDir[1]);
			
			fixScrollbar(idNum);
			
			//animate({ "margin-top": (450 * idNum) + (450 * (-inSectionNum)) + "px" }, 500, "easeInOutExpo");
*/
		});
		
		$("div#sub li").click(function(event) {
			if (slideFlag == 1) {
				newSection = $("div#sub li").index(this);
				scrollMain(newSection);
			}
		});
		
		
		$("div#scrollbar").draggable({ axis: "y", containment: "parent" });
		
		var draggFlag = 0;
		$("div#scrollbar").mousedown(function() {
			draggFlag = 1;
		});
		$("body").mouseup(function() {
			if (slideFlag == 1 && draggFlag == 1) {
				var scrollbarTop = parseInt($("div#scrollbar").css("top"));
				
				newSection = Math.round(scrollbarTop / scrollbarHeight);
				scrollMain(newSection);
				
				draggFlag = 0;
			}
		});
		
		function scrollMain(inSectionNum) {
			if (slideFlag == 1) {
				$("div#scrollbar").clearQueue();
				$("div#main").clearQueue();
				
				//window.alert(parseInt(idNum) + " * 450 + " + parseInt(inSectionNum) + " * (-450)");
				
				$("div#scrollbar").addClass("select");
				$("div#sub li").removeClass("select");
				$("div#scrollbar").animate({ "top": scrollbarHeight * (inSectionNum) + "px" }, 500, "easeInOutExpo", completeScrollMain(inSectionNum));
				$("div#main").animate({ "margin-top": (450 * idNum) + (450 * (-inSectionNum)) + "px" }, 500, "easeInOutExpo");
				
				$("div#leftTitle section").removeClass("up");
				$("div#leftTitle section").removeClass("down");
				
				if (nowSection > inSectionNum) {
					$("div#leftTitle section").addClass("up");
					
				}
				else if (nowSection < inSectionNum) {
					$("div#leftTitle section").addClass("down");
				}
				
				nowSection = inSectionNum;
				
				var timer = setInterval(function() {
					clearInterval(timer);
					$("div#leftTitle section").removeClass("up");
					$("div#leftTitle section").removeClass("down");
				}, 300);
			}
		}
		
$("li.clickS0").click(function(){if (slideFlag == 1){newSection = 0; scrollMain(newSection);}});
$("li.clickS1").click(function(){if (slideFlag == 1){newSection = 1; scrollMain(newSection);}});
$("li.clickS2").click(function(){if (slideFlag == 1){newSection = 2; scrollMain(newSection);}});
$("li.clickS3").click(function(){if (slideFlag == 1){newSection = 3; scrollMain(newSection);}});
$("li.clickS4").click(function(){if (slideFlag == 1){newSection = 4; scrollMain(newSection);}});
$("li.clickS5").click(function(){if (slideFlag == 1){newSection = 5; scrollMain(newSection);}});
$("li.clickS6").click(function(){if (slideFlag == 1){newSection = 6; scrollMain(newSection);}});
$("li.clickS7").click(function(){if (slideFlag == 1){newSection = 7; scrollMain(newSection);}});
$("li.clickS8").click(function(){if (slideFlag == 1){newSection = 8; scrollMain(newSection);}});
$("li.clickS9").click(function(){if (slideFlag == 1){newSection = 9; scrollMain(newSection);}});
		
	}
})(jQuery);


function setSlideFlag(inSlideFlag) {
	slideFlag = inSlideFlag;
}

function completeScrollMain(inSectionNum) {
	$("div#scrollbar").removeClass("select");
	$("div#sub li").eq(inSectionNum).addClass("select");
}

function fixScrollbar(inIdNum) {
	idNum = parseInt(inIdNum);
	$("div#scrollbar").animate({ "top": scrollbarHeight * (idNum) + "px" }, 500, "easeInOutExpo", completeScrollMain(inIdNum));
	$("div#scrollbar").removeClass("select");
	
	$("div#sub li").removeClass("select");
	$("div#sub li").eq(inIdNum).addClass("select");
	nowSection = idNum;
}


