var time = 0;
var contentsH2EllapseFreq = 0;
var worksDegOffset = new Array();
var contentsH2Freq = new Array();

var outoutlineClickCount = 0;
var noiseNum = 0;

var w, h;

var noisePos = 0;

function setDegree(index, elem) {
	deg = -(index + 1);
	transformOrigin = "50% 50%";
	$(elem).css("-webkit-transform", "rotate(" + deg + "deg)");
	$(elem).css("-moz-transform", "rotate(" + deg + "deg)");
	$(elem).css("transform", "rotate(" + deg + "deg)");
	$(elem).css("-webkit-transform-origin", transformOrigin);
	$(elem).css("-moz-transform-origin", transformOrigin);
	$(elem).css("transform-origin", transformOrigin);
}

function init(){
	w = $(document).attr("width");
	h = $(document).attr("height");
	$("#outoutline").css("height", $(document).attr("height") - 30 + "px");
	$("#outoutline").animate({rotate : (Math.random() * -10.0 - 5.0)+ "deg"});
	$("#outoutline").mousedown(function() {
		outoutlineClickCount++;
		if(outoutlineClickCount % 5 == 0) {
			$("#outoutline").animate({rotate : (Math.random() > 0.5 ? "+" : "-") + "=89deg"});
		}
	});
	$(".contents").each(setDegree);
	$("#logo").animate({scale : 2}, 120000);
	
	$(".contents").each(function(index, elem) {
		$(elem).mouseover(function() {
			$(elem).animate({rotate : 360 - (index + 1) + 'deg'}, {duration : 1000}, 0);
		});
	});
	
	$("#worksItem h2").mousedown(function() {
		$(".worksItemBox").each(function(index, elem) {
			$(elem).animate({rotate : '0deg'}, {duration : 3000}, 0);
		});
	});
	
	$(".worksItemBox").each(function(index, elem) {
		worksDegOffset[index] = Math.random() * 40.0 - 20.0;
		$(elem).mouseover(function() {
			worksDegOffset[index] += (Math.random() * 17.5 + 17.5) * ((Math.random() > 0.8) ? -1.0 : 1.0);
			$(elem).animate({rotate : worksDegOffset[index] + "deg"});
		});
	});
	
	$("#menu li").each(function(index, elem) {
		$(elem).mousedown(function() {
			$(".contents").animate({scale : 1.0}, 0);
			$("#" + ($(elem).attr('id')).replace("Menu", "Item")).animate({scale : 1.2}, 50);
		});
		$(elem).mouseover(function() {
			$(elem).scale(4.0);
		});
		$(elem).mouseout(function() {
			$(elem).scale(1.0);
		});
	});
	
	$("#logo").mousedown(function() {
		$(".contents").each(function(index, elem) {
			$(elem).animate({rotate : - (index + 1) + 'deg'}, {duration : 1000}, 0);
		});
	});
	
	setInterval(
		function() {
			$("#outline").animate({rotate: Math.sin(time+=1 / 360.0) * 5 + 'deg'}, 50);
			$("#logo").animate({rotate: Math.sin(time / 360.0) * -5 + 'deg'}, 0);
			$(".worksItemBox").each(function(index, elem) {
				$(elem).css("padding", Math.sin(time / 360.0) * 5.0 + "px");
			});
		}, 50
	);
		
	$(".contents h2").each(function(index, elem) {
		contentsH2Freq[index] = 1.0 / (Math.random() * 0.5 + 0.5);
	});
	
	setInterval(
		function() {
			contentsH2EllapseFreq += 0.1;
			$(".contents h2").each(function(index, elem) {
				$(elem).css("padding-left", (Math.sin(contentsH2EllapseFreq * contentsH2Freq[index]) * 5 + 10) + "px");
				$(elem).css("margin-top", (Math.sin(contentsH2EllapseFreq * contentsH2Freq[index]) * 5 + 10) + "px");
			});
//			ci = Math.floor(Math.random() * 64) + 192;
//			c = "0123456789abcdef"[Math.floor(ci / 16)] + "0123456789abcdef"[ci % 16];
//			$("body").css("background-color", "#" + c + c + c);
			$("#outoutline").css("-moz-border-radius", (Math.sin(contentsH2EllapseFreq * contentsH2Freq[0]) * 10 + 10) + "px");
			$("#outoutline").css("-webkit-border-radius", (Math.sin(contentsH2EllapseFreq * contentsH2Freq[0]) * 10 + 10) + "px");
			$("#outoutline").css("border-radius", (Math.sin(contentsH2EllapseFreq * contentsH2Freq[0]) * 10 + 10) + "px");
		}, 30
	);
	
	setInterval(
		function() {
			n = Math.floor(Math.random() * 14.0);
			$(".contents").each(function(index, elem) {
				if(n / 2 == index) {
					$(elem).animate({rotate: ((n % 2 == 0)?"+":"-") + "=" + "180deg"}, 1500);
					return false;
				}
			});
		}, 2500
	);
}

$(init);
