
var map;
var centerIcon;
var centerMarker;

function request(_url, func) {
	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) {
		if (func == 'list') {
			xmlhttp.onreadystatechange = update_list_pane;
		} else if (func == 'recent') {
			xmlhttp.onreadystatechange = update_list_pane_recent;
		} else if (func == 'param') {
			xmlhttp.onreadystatechange = get_frame_param;
		} else if (func == 'route') {
			xmlhttp.onreadystatechange = update_list_pane_route;
		}
		xmlhttp.open("GET",_url,true);
		xmlhttp.send(null);
	}
}

function xmlhttp_loading() {
	document.getElementById('the_list') .innerHTML = "<html><body><blink><b>Loading...</b></blink><br><img src=imgs/loading.gif></body></html>";
}

function update_list_pane_recent() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var result = xmlhttp.responseText;
		document.getElementById('the_list') .innerHTML = result;
		var jump = result.substring(4,18);
		if (jump !='00000000000000') {
			getFrameParams(jump);
		}
	} else if (xmlhttp.readyState == 1 || xmlhttp.readyState == 1 || xmlhttp.readyState == 1) {
		xmlhttp_loading();
	}
}

function update_list_pane_route() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var result = xmlhttp.responseText;
		document.getElementById('the_list') .innerHTML = result;
		
		var gps = result.indexOf("--RTS");
		if (gps != -1) {
			var gpe = result.indexOf("|RTE--");
			if (gpe != -1) {
				var gpsz = result.substring(gps+5,gpe);
				if (gpsz != '' ) {setRouteDatas(gpsz);}
			}
		}
		var jump = result.substring(4,18);
		if (jump !='00000000000000') {
			getFrameParams(jump);
		}
		
	} else if (xmlhttp.readyState == 1 || xmlhttp.readyState == 1 || xmlhttp.readyState == 1) {
		xmlhttp_loading();
	}
}

function update_list_pane() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var result = xmlhttp.responseText;
		document.getElementById('the_list') .innerHTML = result;
	} else if (xmlhttp.readyState == 1 || xmlhttp.readyState == 1 || xmlhttp.readyState == 1) {
		xmlhttp_loading();
	}
}

function get_frame_param() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		var result = xmlhttp.responseText;
		var resList = result.split(",");
		if (resList[0] == "00000") resList[0] = 139.725044 + ((Math.floor(Math.random() * 100)-50)/5000.0);
		if (resList[1] == "00000") resList[1] = 35.631375  + ((Math.floor(Math.random() * 100)-50)/5000.0);
		iconUpdate(resList[2]);
		setNewData(resList[0],resList[1],resList[2],resList[3]);
	}
}

function iconUpdate(ts) {
	for (var i=0; i < document.getElementsByTagName("img").length; i++) {
		var icons = document.getElementsByTagName("img").item(i);
		 if (icons.id.indexOf("icon") == 0) {
		 	icons.style.visibility = "hidden";
		}
	}
	try {
		document.getElementById("icon"+ts).style.visibility = "visible";
	} catch(e) {}
}

function setRouteDatas(gpsz) {
	map.zoomTo(9);
	var elmArray = gpsz.split("|");
	for (var i=0; i<elmArray.length;i++) {
		var e = elmArray[i].split(",");
		setNewDataDontGo(e[0],e[1],e[2],e[3]);
	}
}

function setNewDataDontGo(y,x,timeString,frameNum) {
	
	x = parseFloat(x);
	y = parseFloat(y);	
	dt = createTimeFormat(""+timeString);
	
	addMarker(map, x,y, "<center><span class=f12><a href=http://exonemo.com/TheRoadMovie/servlet/PDFGen?"+timeString+"><img src=imgs/bus.gif width=32 border=0><br>"+dt+"<br><nobr><b>Download &quot;Origami Bus&quot; pattern</b></a></nobr></span></center>",timeString);
	
}


function setNewData(y,x,timeString,frameNum) {
	
	x = parseFloat(x);
	y = parseFloat(y);	
	dt = createTimeFormat(""+timeString);
	
	goToThePoint(x,y,timeString,frameNum,"<center><span class=f12><a href=http://exonemo.com/TheRoadMovie/servlet/PDFGen?"+timeString+"><img src=imgs/bus.gif width=32 border=0><br>"+dt+"<br><nobr><b>Download &quot;Origami Bus&quot; pattern</b></a></nobr></span></center>");
	
	document.getElementById('info_time').innerHTML = dt;
	document.getElementById('info_n').innerHTML = y;
	document.getElementById('info_e').innerHTML = x;
	document.getElementById('info_no').innerHTML = createNumberFormat(""+frameNum,5);
	document.getElementById('download').innerHTML = "<span id=e style=position:absolute;><BR><nobr><TABLE border='0' cellspacing='0' ><TR><TD><a href=http://exonemo.com/TheRoadMovie/servlet/PDFGen?"+timeString+"><IMG src='imgs/downloadE.gif' border='0'><BR><BR></TD></TR></TABLE></nobr></span><span id=j style=position:absolute;visibility:hidden;><BR><nobr><TABLE border='0' cellspacing='0'><TR><TD><a href=http://exonemo.com/TheRoadMovie/servlet/PDFGen?"+timeString+"><IMG src='imgs/downloadJ.gif' border='0'><BR><BR></TD></TR></TABLE></nobr></span><br><BR>";
	
	document.getElementById("info_pane").style.visibility = 'hidden';
	setTimeout(function() {
		je_switch(je_visi);
		document.getElementById("info_pane").style.visibility = 'visible';
	}, 500);
}



function goToThePoint(x,y,timeString,frameNum, html) {
	var gp = new GPoint(x, y)
	map.recenterOrPanToLatLng(gp);
	addMarker(map, x,y, html,timeString);
}

// Creates a marker whose info window displays the given number
	function addMarker(map, lat, lng, html, timeString) {
		var point = new GPoint(lat, lng);
		var icon = new GIcon();
		icon.image = "imgs/xs.png";
		icon.iconSize = new GSize(15,15);
		//icon.shadow = "shadow.png";
		//icon.shadowSize = new GSize(75,75);
		icon.iconAnchor = new GPoint(7,7);
		icon.infoWindowAnchor = new GPoint(7,7);
		var marker = new GMarker(point,icon);
		
		marker.myTimeString = timeString;
		marker.myHtml = html;
		
		if (centerMarker != null) {
			map.removeOverlay(centerMarker);
		}
		centerMarker = new GMarker(point,centerIcon);
		
		// Show this marker's index in the info window when it is clicked
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(marker.myHtml);
			getFrameParams(marker.myTimeString);
		});
		
		map.addOverlay(centerMarker);
		map.addOverlay(marker);
		
	}


function searchNo() {
	clearButtonColor();
	var no = document.getElementById('search_no').value;
	
	if (no.length >0 && no.length < 5) {
		no = createNumberFormat(no,5);
		document.getElementById('search_no').value = no;
	}
	if (no != '' ) {
		request("http://exonemo.com/RM/list.php?no=" + no,"recent");
	} else {
		document.getElementById('search_no').value = '00000';
	}
}


function clearButtonColor(){
	document.getElementById("bestButtonE").style.color = "#3366CC";
	document.getElementById("bestButtonE").style.border = "#3366CC 2px solid";
	document.getElementById("bestButtonJ").style.color = "#3366CC";
	document.getElementById("bestButtonJ").style.border = "#3366CC 2px solid";
	document.getElementById("recentButtonE").style.color = "#3366CC";
	document.getElementById("recentButtonE").style.border = "#3366CC 2px solid";
	document.getElementById("recentButtonJ").style.color = "#3366CC";
	document.getElementById("recentButtonJ").style.border = "#3366CC 2px solid";
}

function setButtonColor(b){
	if (b == "best") {
		document.getElementById("bestButtonE").style.color = "#FFFF00";
		document.getElementById("bestButtonE").style.border = "#FFFF00 2px solid";
		document.getElementById("bestButtonJ").style.color = "#FFFF00";
		document.getElementById("bestButtonJ").style.border = "#FFFF00 2px solid";
	} else {
		document.getElementById("recentButtonE").style.color = "#FFFF00";
		document.getElementById("recentButtonE").style.border = "#FFFF00 2px solid";
		document.getElementById("recentButtonJ").style.color = "#FFFF00";
		document.getElementById("recentButtonJ").style.border = "#FFFF00 2px solid";
	}
}

function searchRoot() {
	
	var rootNo = document.form1.roots.options[document.form1.roots.selectedIndex].value;
	if (rootNo != "") {
		clearButtonColor();
		request("http://exonemo.com/RM/list.php?root="+rootNo + "&last=1" ,"route");
	}
}

function searchDate() {
	clearButtonColor();
	var mon = document.form1.mon.options[document.form1.mon.selectedIndex].value;
	var day = document.form1.day.options[document.form1.day.selectedIndex].value;
	var hour = document.form1.hour.options[document.form1.hour.selectedIndex].value;
	request("http://exonemo.com/RM/list.php?date=2005" +mon+day+hour ,"list");
}

function getRecentList() {
	clearButtonColor();
	setButtonColor("recent");
	request("http://exonemo.com/RM/list.php?recent=1&last=1&disp=10","recent");
}
function getFrameParams(d) {
	request("http://exonemo.com/RM/getFrameParam.php?f=" + d,"param");
}
function getBestShots() {
	clearButtonColor();
	setButtonColor("best");
	request("http://exonemo.com/RM/list.php?best=1&last=1&disp=10","recent");
}

function checkSearchOrRecent() {
	if (location.search != '') {
		var val = location.search.substring(1);
		if (val != '') {
			request("http://exonemo.com/RM/list.php?no=" + val,"recent");
		} else {
			getBestShots();
		}
	} else {
		getBestShots();
	}
}

function init_GMAP() {
		try {
		    map = new GMap(document.getElementById("map"));
		} catch(xx) { return; }
		map.setMapType(G_SATELLITE_MAP);
		map.addControl(new GLargeMapControl());
	    map.addControl(new GMapTypeControl());
	    map.centerAndZoom(new GPoint(139.725044,35.631375), 10);
		
		centerIcon = new GIcon();
		centerIcon.image = "imgs/xx.gif";
		centerIcon.iconSize = new GSize(19,19);
		//icon.shadow = "shadow.png";
		//icon.shadowSize = new GSize(75,75);
		centerIcon.iconAnchor = new GPoint(9,9);
		
		
		//マウスホイールのイベントを追加
		if( navigator.userAgent.match( "MSIE"   ) ){ document.getElementById( "map" ).attachEvent( "onmousewheel" , mouseWheelZooming ); }
		if( navigator.userAgent.match( "Gecko"  ) ){ document.getElementById( "map" ).addEventListener( "DOMMouseScroll" , mouseWheelZooming , false ); }
		if( navigator.userAgent.match( "Safari" ) ){ document.getElementById( "map" ).onmousewheel = mouseWheelZooming; }
		//マウスホールによるズーム処理
	
	
	
	
	function mouseWheelZooming( event ){
		//マウスホイールの上／下の取得と、スクロールのキャンセル
		if( navigator.userAgent.match( "MSIE"   ) ){ var delta = event.wheelDelta;   event.returnValue = false; }	//IE
		if( navigator.userAgent.match( "Gecko"  ) ){ var delta = event.detail * -1;  event.preventDefault();    }	//Gecko
		if( navigator.userAgent.match( "Safari" ) ){ var delta = event.wheelDelta;   event.returnValue = false; }	//Safari

		//ズーム処理
		map.zoomTo( map.getZoomLevel() + ( delta < 0 ? -1 : 1 ) );
	}
}

function createTimeFormat(timeString) {
	var year = timeString.substring(0,4);
	var mon = timeString.substring(4,6);
	var day =timeString.substring(6,8);
	var hour = timeString.substring(8,10);
	var min = timeString.substring(10,12);
	var sec = timeString.substring(12,14);
	
	return year + "/" + mon + "/" + day + " " + hour + ":" + min + ":" + sec;
}
function createNumberFormat(valString,num) {
	while(valString.length < num) {
		valString = "0" +valString;
	}
	return valString;
}

function openAboutJ() {
	window.open("aboutJ.html","about","width=450,height=700,status=no,directories=no,toolbars=no,scrollbars=yes");
}
function openAboutE() {
	window.open("aboutE.html","about","width=450,height=700,status=no,directories=no,toolbars=no,scrollbars=yes");
}

function openCredit() {
	window.open("credit.html","credit","width=481,height=260,status=no,directories=no,toolbars=auto");
}

function openHowto() {
	window.open("howtolist.html","howto","width=840,height=600,status=no,directories=no,toolbars=no,scrollbars=no");
}

function openRequireE() {
	window.open("requirementsE.html","Requirements","width=400,height=380,status=no,directories=no,toolbars=no");
}

function openRequireJ() {
	window.open("requirementsJ.html","Requirements","width=400,height=380,status=no,directories=no,toolbars=no");
}
function openHelpJ() {
	window.open("helpJ.html","about","width=450,height=770,status=no,directories=no,toolbars=no, scrollbars=no");
}
function openHelpE() {
	window.open("helpE.html","about","width=450,height=770,status=no,directories=no,toolbars=no, scrollbars=no");
}

