var window_id;
var window_num = 0;

function detail(no)
{
	window_id = "mdetail" + no;
	
	if(document.getElementById(window_id)){return false;}
	window_num++;
	if(window_num > 5){return false;}

	newWin = document.createElement("div");
	newWin.className = "mdetail";
	newWin.id = window_id;
	newWin.style.zIndex = "3";
	newWin.style.border = "2px solid #333333";
	newWin.style.visibility = "hidden";
	newWin.style.backgroundColor = "#CCCCCC";
	newWin.style.position = "absolute";
	newWin.style.top = "0px";
	newWin.style.left = "0px";

	document.getElementById("iidxamd").appendChild(newWin);

	target = document.getElementById(window_id);
	target.innerHTML = "";
	var code_url = "iidxamd_detail.php?no=" + no + "&action=detail"
	getIndex(code_url);
	myGetBrowser();
	target.style.display = "block";
	
	
	/*
	document.getElementById("mdetail" + no).innerHTML = "";
	var code_url = "iidxamd_detail.php?no=" + no + "&action=detail"
	getIndex(code_url);
	myGetBrowser();
	document.getElementById("mdetail" + no).style.display = "block";
	//document.getElementById("mdetail").style.visibility = "visible";
	*/
}

function setDetail(){
	document.getElementById("vdetail").style.display = "block";
}

function closeDetail(){
	document.getElementById("vdetail").style.display = "none";
}


function getIndex(getFile)
{
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)
		{
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}

	if(xmlhttp)
	{
		xmlhttp.onreadystatechange = check;
		xmlhttp.open("GET", getFile, true);
		xmlhttp.send(null);
	}
}

function check()
{
	if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
	{
		//document.getElementById("mdetail").innerHTML = xmlhttp.responseText;
		target.innerHTML = xmlhttp.responseText;
		document.getElementById("mdetail_head").id = target.id + "_head";
		document.getElementById(target.id + "_head").onmousedown = DragMouseDown;
	}
	else{
		//document.getElementById("mdetail").innerHTML = "読み込み中...";
		target.innerHTML = "読み込み中...";
	}
}


/*window.document.onkeyup = menu_win;
if(!document.all){window.document.captureEvent(Event.KEYUP);}
function menu_win(evt){
	if(document.all){evt = event;}
	if(evt.keyCode == 32){
		close_menu_win();
		document.getElementById("selectver").style.display = "block";
		window.document.onkeyup = close_menu_win;
		if(!document.all){window.document.captureEvent(Event.KEYUP);}
	}
}

function close_menu_win(){
	document.getElementById("selectver").style.display = "none";
	window.document.onkeyup = menu_win;
	if(!document.all){window.document.captureEvent(Event.KEYUP);}
}*/


function close_data(id_name,h3_name){
	document.getElementById(id_name).style.display = "none";
	document.getElementById(h3_name).onclick = function(){open_data(id_name,h3_name);}
}

function open_data(id_name,h3_name){
	if(!document.all || window.opera){
		document.getElementById(id_name).style.display = "table-row-group";
	}
	else{
		document.getElementById(id_name).style.display = "block";
	}
	document.getElementById(h3_name).onclick = function(){close_data(id_name,h3_name);}
}

function s_all()//モード全選択
{
	var c;
	for(c = 0; c < 20; c++)
		document.ini.elements["s_mode[]"][c].checked = true;
}

function sd_all()//モード全解除
{
	var c;
	for(c = 0; c < 20; c++)
		document.ini.elements["s_mode[]"][c].checked = false;
}

function sp_all()//SPモード
{
	var c;
	for(c = 0; c < 20; c++)
	{
		if(c == 0 || c == 8 || c == 9 || c == 10 || c == 11 || c == 16 || c == 17 || c == 18 || c == 19)
			document.ini.elements["s_mode[]"][c].checked = false;
		else
			document.ini.elements["s_mode[]"][c].checked = true;
	}
}

function dp_all()//DPモード
{
	var c;
	for(c = 0; c < 20; c++)
	{
		if(c == 0 || c == 4|| c == 5 || c == 6 || c == 7 || c == 12 || c == 13 || c== 14 || c == 15)
			document.ini.elements["s_mode[]"][c].checked = false;
		else
			document.ini.elements["s_mode[]"][c].checked = true;
	}
}

function recommend()
{
	var c;
	for(c = 0; c < 20; c++)
	{
		if(c == 0 || c == 2 || c == 3)
			document.ini.elements["s_mode[]"][c].checked = false;
		else
			document.ini.elements["s_mode[]"][c].checked = true;
	}
}

function data_load(){
	php_cookie_read();
	var c,p = 0;
	for(c = 0; c < 20; c++)
		if(document.ini.elements["s_mode[]"][c].checked == true){
			p++;
		}
	if(p == 0){
		recommend();
	}
	var cookies = document.cookie;
	//var point = cookies.split("; ");
	if(cookies.match(/cat=2/)){
		all_close();
	}
	//setHanler();
}

function all_close(){
	id_all = new Array("1st","ss","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","red","hsk","dd","gold","djt","emp","sir","cs",
						"num","abcd","efgh","ijkl","mnop","qrst","uvwxyz","jp",
						"bpm100u","bpm100","bpm130","bpm160","bpm190","bpm220",
						"L0","L1","L2","L3","L4","L5","L6","L7","L8","L9","L10","L11","L12",
						"notes0","notes300","notes600","notes900","notes1200","notes1500");
	for(i = 0; i < id_all.length; i++){
		h3_name = "h3_" + id_all[i];
		if(!document.getElementById(id_all[i])){
			continue;
		}
		else{
			close_data(id_all[i],h3_name);
		}
	}
}

function all_open(){
	id_all = new Array("1st","ss","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","red","hsk","dd","gold","djt","emp","sir","cs",
						"num","abcd","efgh","ijkl","mnop","qrst","uvwxyz","jp",
						"bpm100u","bpm100","bpm130","bpm160","bpm190","bpm220",
						"L0","L1","L2","L3","L4","L5","L6","L7","L8","L9","L10","L11","L12",
						"notes0","notes300","notes600","notes900","notes1200","notes1500");
	for(i = 0; i < id_all.length; i++){
		h3_name = "h3_" + id_all[i];
		if(!document.getElementById(id_all[i])){
			continue;
		}
		else{
			open_data(id_all[i],h3_name);
		}
	}
}

function php_cookie(){
	getFile = "iidxamd_cookie.php?mode=save";
	//var headers = new Array("ver","bpm","artist","genre","spn","sph","spa","dpn","dph","dpa","n7","h7","a7","n14","h14","a14");
	var headers = new Array("ver","bpm","artist","genre","spn","sph","spa","spd","dpn","dph","dpa","dpd","n7","h7","a7","d7","n14","h14","a14","d14");
	var fpp = "";

	for(c = 0; c < 20; c++){//mscはCookie有無の確認
		if(document.ini.elements["s_mode[]"][c].checked == true){
			fpp += "&" + headers[c] + "=1";
		}
		else{
			fpp += "&" + headers[c] + "=0";
		}
	}
	fpp += "&skin=" + document.ini.skin.value;
	fpp += "&cat=" + document.ini.cat.value;
	getFile += fpp;
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)
		{
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}

	if(xmlhttp)
	{
		xmlhttp.onreadystatechange = function (){
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
				time = (new Date()).getSeconds();
				if(location.search){
					location.href = document.URL + "&css=" + time;
				}
				else{
					location.href = document.URL + "?css=" + time;
				}
			}
		}
		xmlhttp.open("GET", getFile, true);
		xmlhttp.send(null);
		//time = (new Date()).getSeconds();
		//location.href(document.URL + "&css=" + time);
	}
}

function php_cookie_read(){
	getFile = "iidxamd_cookie.php";
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)
		{
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}

	if(xmlhttp)
	{
		xmlhttp.onreadystatechange = function (){
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
				//var headers = new Array("ver","bpm","artist","genre","spn","sph","spa","dpn","dph","dpa","n7","h7","a7","n14","h14","a14");
				var headers = new Array("ver","bpm","artist","genre","spn","sph","spa","spd","dpn","dph","dpa","dpd","n7","h7","a7","d7","n14","h14","a14","d14");
				var cookie_data = xmlhttp.responseText;
				//alert(cookie_data);
				point = cookie_data.split(",");
				for(i = 0; i < 20; i ++){//mscはCookie有無の確認
					if(point[i] == 1){
						document.ini.elements["s_mode[]"][i].checked = "checked";
					}
				}
				if(point[20] != ""){
					document.ini.skin.value = point[20];
				}
				if(point[21] != ""){
					document.ini.cat.value = point[21];
				}
			}
		}
		xmlhttp.open("GET", getFile, true);
		xmlhttp.send(null);
	}
}


//window.onload = setHandler;

var offsetX;
var offsetY;
var target;
//var window_num = 0;
//list = Array("mdetail0","mdetail1","mdetail2","mdetail3","mdetail4");

function DragMouseDown(e){
	document.onmouseup = DragMouseUp;
	document.onmousemove = DragMouseMove;

	//target = document.getElementById(this.id.slice(0,-5));
	//target = document.getElementById("mdetail");
	window_id = this.id.slice(0,-5);
	target = document.getElementById(window_id);
	//target = document.getElementById()
	/*for(i = 0; i < list.length; i++){
		document.getElementById(list[i] + 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(window.opera){
		//x = window.event.clientX + window.pageXOffset;
		//y = window.event.clientY + window.pageYOffset;
		if(document.body.scrollTop != 0){
			x = event.clientX + document.body.scrollLeft;
			y = event.clientY + document.body.scrollTop;
		}
		else{
			x = event.clientX + document.documentElement.scrollLeft;
			y = event.clientY + document.documentElement.scrollTop;
		}
	}*/
	if(e){
		x = e.pageX;
		y = e.pageY;
	}
	else if(event){
		if(document.body.scrollTop != 0){
			x = event.clientX + document.body.scrollLeft;
			y = event.clientY + document.body.scrollTop;
		}
		else{
			x = event.clientX + document.documentElement.scrollLeft;
			y = event.clientY + document.documentElement.scrollTop;
		}
	}
	x -= offsetX;
	y -= offsetY;
	target.style.position = "absolute";
	target.style.left = x + "px";
	target.style.top = y + "px";
	//alert(offsetX+","+x);
}

function DragMouseUp(e){
	target = null;
	document.onmouseup = null;
	document.onmousemove = null;
	document.onselectstart = null;
	document.ondragstart = null;
}


function mdetail_front(){
	if(target == null){target = document.getElementById(window_id);}
	divtags = document.getElementsByTagName("div");
	for(i = 0; i < divtags.length; i++){
		divtags[i].style.zIndex = 1;
	}
	//alert(target.id);
	target.style.zIndex = 3;
}

function window_change(){
	if(window_id == "all"){
		for(i = 0; i < list.length; i++){
			document.getElementById(list[i]).style.display = "none";
		}
		document.getElementById('mdetail1').style.display = "block";
	}
	else{
		es = document.getElementById(window_id).style
		if(es.display == "none"){
			es.display = "block";
		}
		else{
			es.display = "none";
		}
	}
}




//以下、「イヌでもわかるJavaScript講座」を参考に制作


//myID = "mdetail";                  // DIVタグで付けたID

myObj = 0;           // 使用ブラウザのSTYLEオブジェクト用

function myGetBrowser(){            // ブラウザを判断する
   myID = window_id;
   myOP = window.opera;             //OP
   myN6 = document.getElementById;  // N6 or IE
   myIE = document.all;             // IE
   myN4 = document.layers;          // N4

   if (myOP){                          // OP?
      document.onclick = myClickOP;
	myClickOP();
   }else if (myIE){                    // IE?
      document.onclick = myClickIE;
   }else if (myN6){                    // N6?
      //window.addEventListener("click",myClickN6,true);
	document.onclick = myClickN6;
   }else{                    // N4?
      window.captureEvents(Event.CLICK);
      window.onclick = myClickN4;
   }
}

function myClickOP(){  // OPで左クリック
  myObj=document.getElementById(window_id);
  myObj.style.left = window.event.clientX + window.pageXOffset + 10 + "px";
  myObj.style.top  = window.event.clientY + window.pageYOffset - 10 + "px";
  myObj.style.visibility = "visible";
  document.onclick = null;
  //myObj.onclick = div_stop;
}
function myClickN6(myEvent){  // N6でクリック
  myObj=document.getElementById(window_id);
  myObj.style.left = myEvent.clientX + window.pageXOffset + 10 + "px";
  myObj.style.top  = myEvent.clientY + window.pageYOffset - 10 + "px";
  myObj.style.visibility = "visible";
  document.onclick = null;
  //myObj.onclick = div_stop;
}
function myClickIE(){ // IEでクリック
  myObj=document.all[window_id];
  if(document.body.scrollTop != 0){
    myObj.style.left = window.event.clientX + document.body.scrollLeft + 10 + "px";
    myObj.style.top  = window.event.clientY + document.body.scrollTop - 10 + "px";
  }
  else{
  	myObj.style.left = window.event.clientX + document.documentElement.scrollLeft + 10 + "px";
  	myObj.style.top  = window.event.clientY + document.documentElement.scrollTop - 10 + "px";
  }
  //if(window.event.clientY + document.body.scrollTop < 450 && window.event.clientX + document.body.scrollLeft < 350)
//	myObj.style.left = "350px";
  myObj.style.visibility = "visible";
  document.onclick = null;
  //myObj.onclick = div_stop;
}
function myClickN4(myEvent){ // N4でクリック
  myObj=document[window_id];
  myObj.left = myEvent.x ;
  myObj.top  = myEvent.y ;
  myObj.visibility = "visible";
  window.onclick = null;
  //myObj.onclick = div_stop;
}

function close_win(){
  if(target == null){target = document.getElementById(window_id);}
  target.innerHTML = "";
  target.display = "none";
  target.style.visibility = "hidden";
  document.getElementById("iidxamd").removeChild(target);
  window_num--;
}

//function div_stop(){
//  document.onclick = "";
//  myObj.onclick = "";
//  myObj.innerHTML = myObj.innerHTML.replace(/-stop-/i,"-move-");
//  myObj.innerHTML = myObj.innerHTML.replace(/div_stop/i,"div_move");
//}
//
//function div_move(){
//  myObj.innerHTML = myObj.innerHTML.replace(/-move-/i,"-stop-");
//  myObj.innerHTML = myObj.innerHTML.replace(/div_move/i,"div_stop");
//  myGetBrowser();
//}