// 日本語コメント

var ellapsedTime = 0;
var erapsedFrame = 0;
var tweeturl="http://api.twitter.com/1/statuses/user_timeline/buffer_renaiss.json?count=50&callback=?";
var maxID = 0;
var currentHash = "";

var bgPos = 0;
var bgWidth;
var bgHeight;

$(document).ready(function() {
	initEasing();
	
	$(".contents").each(function(node_id, node) {
		if(node_id != 0) {
			$(node).css({opacity : "0.0", "display" : "none"});
		}
	});
	
	$("#sidemenu li").each(function(node_id, node) {
		$(node).click(function() {
			if("#" + $(node).attr("id").replace("_menu", "") != window.location.hash) {
				if($(node).attr("id").replace("_menu", "") != "news") {
					window.location.hash = $(node).attr("id").replace("_menu", "");
				} else {
					window.location.hash = "";
				}
				$(".contents").each(function(node_id_, node_) {
					$(node_).css({opacity : "0.0", "display" : "none"});
				});
				
				$("#" + $(node).attr("id").replace("_menu", ""))
					.css({"display" : ""})
					.animate({opacity : "1.0"}, 500, "flashOut");
			}
		});
		
		$(node).children("p").each(function(subnode_id, subnode) {
			$(subnode).css({fontSize : "14px", marginLeft : "1px", "top" : 20 * subnode_id + "px", opacity : 1 - subnode_id});
		});
		
		$(node).hover(
			function() {
				$(node).animate({marginLeft : "10px"}, 300, "quart");
				$(node).children("p").each(function(subnode_id, subnode) {
					$(subnode).animate({fontSize : "16px", "top" : 20 * (subnode_id - 1) + "px", opacity : subnode_id}, 300, "quart");
				});
			},
			function() {
				$(node).animate({marginLeft : "1px"}, 300, "quart");
				$(node).children("p").each(function(subnode_id, subnode) {
					$(subnode).animate({fontSize : "14px", "top" : 20 * subnode_id + "px", opacity : 1 - subnode_id}, 300, "quart");
				});
			}
		);
	});
	
	$(".handle").each(function(node_id, node) {
		$(node).click(function(){
			if($(node).parent(".a_member").width() == 700) {
				$(".profiles_background")
					.children("li")
					.animate({height : "220px"}, {duration : 500, queue : false});
					
				$(node)
					.parent(".a_member")
					.animate({width : "12px"}, 500);
			} else {
				$(".handle").each(function(subnode_id, subnode) {
					if($(subnode) != $(node)) {
						$(subnode)
							.parent(".a_member")
							.animate({width : "12px"}, {duration : 500, queue : false});
							
						$("#" + $(subnode).attr("id") + "_bg")
							.animate({height : "0px"}, {duration : 500, queue : false});
					}
				});
				
				$(node)
					.parent(".a_member")
					.animate({width : "700px"}, {duration : 500, queue : false});
					
				$("#" + $(node).attr("id") + "_bg")
					.animate({height : "660px"}, 500);
			}
		});
	});
	
	$(".profiles_background").css({opacity : 0.8});
	$(".a_member").css({opacity : 0.9});
	
	$("#news_archive_list").each(function(){ 
		$("li > h4", this).each(function(index){
			var $this = $(this);
			
			if(index > 1) $this.next().hide();
	 		
			$this.click(function(){ 
				var params = {height:"toggle", opacity:"toggle"}; 
				$(this).next().animate(params).parent().siblings() 
					.children("ul:visible").animate(params);
			});
		});
	});
	
	$(".archive_picture_frame").each(function(node_id, node) {
		$(node).children("li").children(".archive_pics").click(function() {
			$wrapper = $(node).parents(".archive_contents_wrapper:first");
			$contents = $(node).parents(".archive_contents:first");
			
			if($wrapper.height() != $contents.height()) {
				var src_name = "";
				
				$wrapper.animate({"height" : $contents.height()}, 600, "easeInExpo", function() {
					$(node).children("li").each(function(_node_id, _node){
						var img_node = $(_node).children("img");
						if(_node_id == 0) {
							src_name = img_node.attr("src");
						} else {
							_src = src_name;
							img_node.attr("src", _src.replace("0.jpg", _node_id + ".jpg"));
						}
					});
					
					$(node).parents(".archive_contents_wrapper:first")
						.children(".archive_contents")
						.children(".archive_thumbnails")
						.children("li").each(function(_node_id, _node){
							var img_node = $(_node).children("img");
							if(_node_id != 0) {
								_src = src_name;
								img_node.attr("src", _src.replace("0.jpg", _node_id + ".jpg"));
							}
					});
				});
				$('html,body').animate({scrollTop : $(node).offset().top}, 1000, "quart");
			} else {
				$wrapper.animate({"height" : "80px"}, 600, "easeOutExpo");
			}
		});
		
		$(node).children().each(function(subnode_id, subnode) {
			if(subnode_id != 0) $(subnode).css({opacity : 0.0});
		});
	});
	
	$(".archive_thumbnails").each(function(node_id, node) {
		var $frame = $(node).parent().children(".archive_picture_frame:first");
		
		$(node).children().each(function(subnode_id, subnode) {
			var topVal = -Math.random() * 102.0;
			var leftVal = -Math.random() * 136.0;
			$(subnode).children(".archive_pics_thumb")
				.css({width : "272px", height : "204px", top : topVal + "px", left : leftVal + "px"}, 500);
		
			$(subnode).hover(
				function() {
					$(subnode).children(".archive_pics_thumb")
						.animate({width : "136px", height : "102px", top : "0px", left : "0px"}, 500, "quart");
				},
				function() {
					var topVal = -Math.random() * 102.0;
					var leftVal = -Math.random() * 136.0;
					$(subnode).children(".archive_pics_thumb")
						.animate({width : "272px", height : "204px", top : topVal + "px", left : leftVal + "px"}, 500, "flashOut");
				}
			);
			
//			$frame.children("li").not(":first").hide();
//			$frame.children("li:first").show();
			$frame.children("li").hide();
			$frame.children("li:first").show();
			
			$(subnode).click(function() {
				if($wrapper.height() != "480px") {
					$(node).parents(".archive_contents_wrapper")
						.animate({"height" : $(node).parents(".archive_contents:first").height()}, 600, "easeOutExpo");
				}
				
//				if($frame.children("li").children(":nth-child(" + (subnode_id + 1) + ")").css("opacity") > 0.9) return;
				
				$frame.children().not(":nth-child(" + (subnode_id + 1) + ")")
//					.animate({opacity : "0.0"}, 500, "flashOut");
					.animate({opacity : "0.0"}, 500, "flashOut", function() {
						$(this).hide();
					});
				$frame.children(":nth-child(" + (subnode_id + 1) + ")").show()
					.animate({opacity : "1.0"}, 500, "flashOut");
//					.animate({opacity : "1.0", "display" : "inline"}, 500, "flashOut");
			});
		});
	});
	
	initTweet();
	
	$("#graphics_list a").lightBox();
	
	$("#header").hover(glitchy, pass);
	
	$("body").css({"overflow" : "scroll"});
	$("#glitch_effect").animate({opacity : 0.0, "width" : "1600%", "height" : "1600%", "top" : "-800%", "left" : "-800%"}, 1000, "randomZeroOne", function() {
		$("#glitch_effect").hide();
	});
	
	checkHash();
	
	setInterval(checkHash, 500);
	
	bgWidth = $("#bg_effect_image").width();
	bgHeight = $("#bg_effect_image").height();
	
	setInterval(moveBg, 45);
});

var dx = -1;

function moveBg() {
	$("#bg_effect_image").css("left", (bgPos += dx) + "px");
//	$("#bg_effect").css("right", (25 + 5 * (Math.cos(ellapsedTime / 20.0) + 1)) + "px");
//	$("#bg_effect").width(145 + 25 * Math.cos(ellapsedTime / 11.0));
	
	if(Math.random() < 0.01) {
		bgPos = -Math.random() * ($("#bg_effect_image").width() + $("#bg_effect").width());
		dx = Math.random() < 0.5 ? 1 : -1;
	}
	
	if(Math.random() < 0.1) {
		var topVal = parseInt($("#bg_effect_image").css("top")) + ((Math.random() < 0.5) ? Math.random() * 15 : Math.random() * -15);
		topVal = Math.max(Math.min(0, topVal), $('html, body').innerHeight() - $("#bg_effect_image").height());
		$("#bg_effect_image").css("top", topVal + "px");
	}
	
	if($("#bg_effect_image").width() < -(bgPos - $("#bg_effect").width()) || 0 < bgPos) {
		bgPos = -Math.random() * ($("#bg_effect_image").width() + $("#bg_effect").width());
		dx = Math.random() < 0.5 ? 1 : -1;
	}
	
	erapsedFrame++;
}

function checkHash() {
	if(currentHash != window.location.hash) {
		switch(window.location.hash) {
			case "#about" :
			case "#archive" :
			case "#member" :
			case "#twitter" :
			case "#link" :
				$(".contents").not(window.location.hash).each(function(node_id_, node_) {
					$(node_).css({opacity : "0.0", "display" : "none"});
				});
				$(window.location.hash)
					.css({"display" : ""})
					.animate({opacity : "1.0"}, 1000, "flashOut");
				break;
			case "#" :
			case "" :
				$(".contents").not(window.location.hash).each(function(node_id_, node_) {
					$(node_).css({opacity : "0.0", "display" : "none"});
				});
				$("#news")
					.css({"display" : ""})
					.animate({opacity : "1.0"}, 100, "flashOut");
				break;
			default :
				break;
		}
	}
	currentHash = window.location.hash;
}

function glitchy() {
	$("#glitch_effect").show();
	$("#glitch_effect").css({opacity : 1.0, "width" : "100%", "height" : "100%", "top" : "0", "left" : "0"});
	$("#glitch_effect").animate({opacity : 0.0, "width" : "1600%", "height" : "1600%", "top" : "-800%", "left" : "-800%"}, 300, "randomZeroOne", function() {
		$("#glitch_effect").hide();
	});
}

function tweetMove() {
	var size = $("#twitter_view li").size();
	$("#twitter_view li").each(function(node_id, node){
		$(node).css({
			"margin-left" : ((Math.cos(1.4 * ellapsedTime + 4 * 3.14 * (size - node_id) / size) + 1.0) * 20.0) + "px",
			"margin-bottom" : ((Math.sin(1.2 * ellapsedTime + 1.5 * 3.14 * (size - node_id) / size) + 1.0) * 3.25) + "px"
		});
		ellapsedTime += 0.0015;
	});
}

function initTweet() {
	$.getJSON(tweeturl, function(data){
		var newMaxID = maxID;
		var prependText = "";
		$.each(data, function(i, item){
			newMaxID = Math.max(newMaxID, parseInt(item.id));
			var txt = item.text
				.replace(/(https?:\/\/[-a-z0-9._~:\/?#@!$&\'()*+,;=%]+)/ig,'<a href="$1">$1</a>')
				.replace(/@+([_A-Za-z0-9-]+)/ig, '<a href="http://twitter.com/$1">@$1</a>')
				.replace(/#+([_A-Za-z0-9-]+)/ig, '<a href="http://search.twitter.com/search?q=$1">#$1</a>');
			var time = Date.parse(item.created_at);
			var date = new Date();
			date.setTime(time);
			var time_at = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
			prependText += '<li>' + txt + " <span style=\"width : 600px; font-size : 90%; text-align : right;\">[" + time_at + "]</span>" + '</li>' + "\n";
			
		});
		$("#twitter_view").prepend(prependText);
		maxID = newMaxID;
	});
	
	var ellapsedTime = 0.0;
	
	setInterval(loadTweet, 20000);
	setInterval(tweetMove, 60);
}

function loadTweet() {
	$.getJSON(tweeturl, function(data){
		var newMaxID = maxID;
		var prependText = "";
		$.each(data, function(i, item){
			newMaxID = Math.max(newMaxID, parseInt(item.id));
			if(maxID < parseInt(item.id)) {
				var txt = item.text
					.replace(/(https?:\/\/[-a-z0-9._~:\/?#@!$&\'()*+,;=%]+)/ig,'<a href="$1">$1</a>')
					.replace(/@+([_A-Za-z0-9-]+)/ig, '<a href="http://twitter.com/$1">@$1</a>')
					.replace(/#+([_A-Za-z0-9-]+)/ig, '<a href="http://search.twitter.com/search?q=$1">#$1</a>');
				var time = Date.parse(item.created_at);
				var date = new Date();
				date.setTime(time);
				var time_at = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
				prependText += '<li>' + txt + " <span style=\"width : 600px; font-size : 90%; text-align : right;\">[" + time_at + "]</span>" + '</li>' + "\n";
			}
		});
		$("#twitter_view").prepend(prependText);
		maxID = newMaxID;
	});
}

function pass() {}

function initEasing() {
	jQuery.easing.flashOut = function (x, t, b, c, d) {
		if(t == d) return c;
		else return Math.random() * Math.random() * (t / d * c + (1.0 - t / d) * b);
	};
	
	jQuery.easing.flashIn = function (x, t, b, c, d) {
		if(t == d) return b;
		else return Math.random() * Math.random() * (t / d * b + (1.0 - t / d) * c);
	};
	
	jQuery.easing.randomZeroOne = function (x, t, b, c, d) {
		if(t == 0) return 1.0; 
		var r = Math.random();
		return r * r * r * r;
	};

	
	jQuery.easing.quart = function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	};
}
