// ダイス
// (C)TEXT.


var dicemouseX;
var dicemouseY;
var diceX;
var diceY;
var dx = 0; // x 方向の速度
var dy = 0; // y 方向の速度
var dragFlag = false;
var timerHandle;
var workCount = 0;
var rad;
var rotation;
var diceSpeed;
var firstplay = 0;


$(document).ready(function(){




	$("body").mousemove(function(e){
		myMouseMove(e);
		if (firstplay == 0){


			$("#dice").fadeIn(2000);
			$("#shadow").fadeIn(2000);

			firstplay = 1;
			diceSpeed = Math.floor(Math.random()*5+1);
			diceset();
		}
		return;
	});

	$("#dice").mousemove(function(e){
		if (workCount == 0){
			diceSpeed = Math.floor(Math.random()*15+5);
			diceset();
			return;
		}
	});






});


function diceset() {
	dragFlag = false;
	workCount = 1;
	diceX = dicemouseX;
	diceY = dicemouseY;
	rad  = Math.atan2(Math.floor(Math.random()*$(document).width()),Math.floor(Math.random()*$(document).height()));
	rotation = rad * 180 / Math.PI;
	dx = Math.cos(rotation * Math.PI / 180) * (diceSpeed*3);	
	dy = Math.sin(rotation * Math.PI / 180) * (diceSpeed*3);
	timerHandle = setInterval("diceMove()",20);
}


function drawInfo() {
/*
	$("#dx").text(dx);
	$("#dy").text(dy);
	$("#mx").text(dicemouseX);
	$("#my").text(dicemouseY);
	$("#rad").text(rad);
	$("#rotation").text(rotation);
	$("#speed").text(diceSpeed);
*/
}

function drawDicePt(type,dicePT){
	if (type == 0){
		if (dicePT == 1){
			$('#dice').css({ backgroundPosition: "0 0" });
		}
		if (dicePT == 2){
			$('#dice').css({ backgroundPosition: "-100px 0px" });
		}
		if (dicePT == 3){
			$('#dice').css({ backgroundPosition: "-200px 0px" });
		}
		if (dicePT == 4){
			$('#dice').css({ backgroundPosition: "-300px 0px" });
		}
		if (dicePT == 5){
			$('#dice').css({ backgroundPosition: "-400px 0px" });
		}
		if (dicePT == 6){
			$('#dice').css({ backgroundPosition: "-500px 0px" });
		}
	}
	// 斜め
	if (type == 1){
		if (dicePT == 1){
			$('#dice').css({ backgroundPosition: "0 -100px" });
		}
		if (dicePT == 2){
			$('#dice').css({ backgroundPosition: "-100px -100px" });
		}
		if (dicePT == 3){
			$('#dice').css({ backgroundPosition: "-200px -100px" });
		}
		if (dicePT == 4){
			$('#dice').css({ backgroundPosition: "-300px -100px" });
		}
		if (dicePT == 5){
			$('#dice').css({ backgroundPosition: "-400px -100px" });
		}
		if (dicePT == 6){
			$('#dice').css({ backgroundPosition: "-500px -100px" });
		}
	
	}
}

function diceMove() {

	diceX = diceX + dx;
	diceY = diceY + dy;

	var count;
	if (Math.abs(dx) > Math.abs(dy)){
		count = Math.abs(dx);
	} else {
		count = Math.abs(dy);
	}
	
	dx = dx * 0.95;
	dy = dy * 0.95;


	var jmpY = Math.sin(workCount)*(count*3);
	if (jmpY > 1) {
		if (Math.floor(Math.random()*2) == 0){
			var rnd = Math.floor(Math.random()*6+1);
			drawDicePt(1,rnd);
		}
	} else {
		var rnd = Math.floor(Math.random()*6+1);
		drawDicePt(0,rnd);
	}


	if(diceX < 50){
		diceX = 50;
		dx = dx * -1;
	}

	if(diceX > $(document).width()-150){
		diceX = $(document).width()-150;
		dx = dx * -1;
	}
	if(diceY < 50){
		diceY = 50;
		dy = dy * -1;
	}
	if(diceY > 700){
		diceY = 700;
		dy = dy * -1;
	}

	$('#dice').css('top', diceY-50+jmpY).css('left', diceX-50);
	$('#shadow').css('top', diceY-50+jmpY).css('left', diceX-50);


	if ((Math.abs(dx) > 1)||(Math.abs(dy) > 1)){
	} else {
		// 停止状態では斜めにならないようにする
		var rnd = Math.floor(Math.random()*6+1)
		drawDicePt(0,rnd);
		if (rnd == 1){
			$("body").css("color","#CC3333");
			$('#dice').fadeOut(100).fadeIn(100);


			$(".mainContents").animate({"margin-top":-10}, 50 ,"easeOutQuint").animate({"margin-top":10}, 50 ,"easeOutQuint").animate({"margin-top":-5}, 50 ,"easeOutQuint").animate({"margin-top":5}, 50 ,"easeOutQuint").animate({"margin-top":0}, 50 ,"easeOutQuint");

		}
		workCount = 0;
		clearInterval(timerHandle);
		return;
	}
	workCount++;
	drawInfo();
}


function getScrollTop(){
	var y = 0;
	var y1 = document.documentElement.scrollTop;
	var y2 = document.body.scrollTop;
	if(y1>y2){
		y = y1;
	}else{
		y = y2;
	}
	return y;
}
function getScrollLeft(){
	var x = 0;
	var x1 = document.documentElement.scrollLeft;
	var x2 = document.body.scrollLeft;
	if(x1>x2){
		x = x1;
	}else{
		x = x2;
	}
	return x;
}


function myMouseMove(e){
	dicemouseX = e.clientX + getScrollLeft();
	dicemouseY = e.clientY + getScrollTop();
	drawInfo();
}


function consoleLog(myStr){
	var getText = $("#exec_log").text();
	
	$("#exec_log").text(myStr + "\n" +getText);
}



