﻿window.onload = setHandler;

var offsetX;
var offsetY;
var target;
var closeFlag = {"outline":0,"research":0,"digital":0,"member":0,"access":0};
list = Array("outline","research","digital","member","access");

function DragMouseDown(e){
	target = document.getElementById(this.id.slice(0,-5));
	if(closeFlag[target.id]){
		close(target.id);
	}
	else{
		document.onmouseup = DragMouseUp;
		document.onmousemove = DragMouseMove;
	}
	
	for(i = 0; i < list.length; i++){
		document.getElementById(list[i]).style.zIndex = 0;
	}
	document.onselectstart = function(e){return false}
	document.ondragstart = function(e){return false}
	target.style.zIndex = 3;
	if(window.opera){
		offsetX = window.event.offsetX;
		offsetY = window.event.offsetY;
	}
	else if(e){
		offsetX = e.layerX;
		offsetY = e.layerY;
	}
	else if(event){
		offsetX = event.offsetX;
		offsetY = event.offsetY;
	}
	return false;
}

function DragMouseMove(e){
	var x,y;
	if(!target){return true;}
	if(e){
		x = e.pageX;
		y = e.pageY;
	}
	else if(event){
		x = event.clientX + document.body.scrollLeft;
		y = event.clientY + document.body.scrollTop;
	}
	x -= offsetX;
	y -= offsetY;
	if(window.innerWidth && window.innerWidth < target.offsetWidth + x){
		x = window.innerWidth - target.offsetWidth;
	}
	else if(document.body.clientWidth < target.offsetWidth + x){
		x = document.body.clientWidth - target.offsetWidth;
	}
	if(window.innerHeight && window.innerHeight < target.offsetHeight + y){
		y = window.innerHeight - target.offsetHeight;
	}
	else if(document.documentElement.clientHeight < target.offsetHeight + y){
		y = document.documentElement.clientHeight - target.offsetHeight;
	}
	target.style.position = "absolute";
	target.style.left = x + "px";
	target.style.top = y + "px";
}

function DragMouseUp(e){
	target = null;
	document.onmouseup = null;
	document.onmousemove = null;
	document.onselectstart = null;
	document.ondragstart = null;
}

var startX;
var startY;
var targetWidth;
var targetHeight;

function ResizeMouseDown(e){
	document.onmouseup = ResizeMouseUp;
	document.onmousemove = ResizeMouseMove;
	document.onselectstart = function(e){return false}
	document.ondragstart = function(e){return false}
	//target = this;
	target = document.getElementById(this.id.slice(0,-4));

	for(i = 0; i < list.length; i++){
		document.getElementById(list[i]).style.zIndex = 0;
	}
	target.style.zIndex = 3;
	target = document.getElementById(this.id.slice(0,-4) + "_main");

	if(target.style.width == "" || target.style.height == ""){
		target.style.width = "450px";//document.getElementById(target.id.slice(0,-5)).offsetWidth + "px";
		target.style.height = "400px";//document.getElementById(target.id.slice(0,-5)).offsetHeight + "px";
	}
	targetWidth = parseInt(target.style.width.match(/\d+/),10);
	targetHeight = parseInt(target.style.height.match(/\d+/),10);
	if(e){
		startX = e.pageX;
		startY = e.pageY;
	}
	else if(event){
		startX = event.clientX + document.body.scrollLeft;
		startY = event.clientY + document.body.scrollTop;
	}
	return false;
}

function ResizeMouseMove(e){
	if(!target){return true;}
	var endX,endY;
	if(e){
		endX = e.pageX;
		endY = e.pageY;
	}
	else if(event){
		endX = event.clientX + document.body.scrollLeft;
		endY = event.clientY + document.body.scrollTop;
	}
	rWidth = (targetWidth + endX - startX > 350) ? targetWidth + endX - startX : 350;
	rHeight = (targetHeight + endY - startY > 100) ? targetHeight + endY - startY : 100;
	if(window.innerWidth && window.innerWidth < rWidth){
		rWidth = window.innerWidth;
	}
	else if(document.body.clientWidth < rWidth){
		rWidth = document.body.clientWidth;
	}
	if(window.innerHeight && window.innerHeight < rHeight){
		rHeight = window.innerHeight;
	}
	else if(document.documentElement.clientHeight < rHeight){
		rHeight = document.documentElement.clientHeight;
	}
	target.style.width = rWidth + "px";
	target.style.height = rHeight + "px";

	if(target.offsetWidth != document.getElementById(target.id.slice(0,-5)).offsetWidth){
		document.getElementById(target.id.slice(0,-5)).style.width = target.offsetWidth + "px";
	}
	if(target.style.width != document.getElementById(target.id.slice(0,-5)).style.width){
		document.getElementById(target.id.slice(0,-5)).style.width = target.offsetWidth + "px";
	}
}

function ResizeMouseUp(e){
	target = null;
	document.onmouseup = null;
	document.onmousemove = null;
	document.onselectstart = null;
	document.ondragstart = null;
}

function click_window(){
	for(i = 0; i < list.length; i++){
		document.getElementById(list[i]).style.zIndex = 0;
	}
	this.style.zIndex = 3;
}

function setHandler(){
	if(!document.getElementById){location.href = "./old_lab/index.html";}
	else{
		if((window.innerWidth < 950 || window.innerHeight < 600) || (document.documentElement.clientHeight < 600 || document.body.clientWidth < 950)){
			document.getElementById("link").innerHTML += "<p style=\"clear:both\">表示範囲が狭くて見難い場合は<a href=\"./old_lab/index.html\">こちら</a>をどうぞ。</p>";
		}
		if(navigator.appVersion.charAt(0)<=4){document.getElementById("link").style.position = "absolute";}
		for(i = 0; i < list.length; i ++){
			document.getElementById(list[i]).onmousedown = click_window;
			document.getElementById(list[i]+"_head").onmousedown = DragMouseDown;
			document.getElementById(list[i]+"_btm").onmousedown = ResizeMouseDown;
			document.getElementById(list[i]+"_btm").style.cursor = "nw-resize";
			document.getElementById(list[i]).style.display = "none";
			//document.getElementById(list[i]).style.zIndex = "1";
		}
		//document.getElementById("outline").style.zIndex = "3";
		//document.getElementById("research").style.zIndex = "2";
		document.getElementById("outline").style.display = "block";
		document.getElementById("research").style.display = "block";
	}
}

function close(window_id){
	document.getElementById(window_id).style.display = "none";
	document.getElementById('link_' + window_id).src = "./img/link_" + window_id + ".gif";
	closeFlag[window_id] = false;
}

function cf(window_id){
	closeFlag[window_id] = true;
}

function window_change(window_id){
	if(window_id == "all"){
		for(i = 0; i < list.length; i++){
			document.getElementById(list[i]).style.display = "none";
			document.getElementById(list[i]).style.left = "30%";
			document.getElementById(list[i]).style.top = "10%";
			document.getElementById('link_' + list[i]).src = "./img/link_" + list[i] + ".gif";
		}
		document.getElementById('outline').style.display = "block";
		document.getElementById('link_outline').src = "./img/link_outline_on.gif";
	}
	else{
		es = document.getElementById(window_id).style;
		
		//click_windowと被ってる
		for(i = 0; i < list.length; i++){
			document.getElementById(list[i]).style.zIndex = 0;
		}
		es.zIndex = 3;

		if(es.display == "none"){
			es.display = "block";
			document.getElementById('link_' + window_id).src = "./img/link_" + window_id + "_on.gif";
		}
		else{
			es.display = "none";
			document.getElementById('link_' + window_id).src = "./img/link_" + window_id + ".gif";
		}
	}
}

function openHTML(filename){
	 
}