var _LONG_V = false;
var _SHORT_V = false;
var _LONG_H = false;
var _SHORT_H = false;
var currentShort;

var _CURRENT_W;
var _CURRENT_H;
var _OFFSETX;
var _OFFSETY;
var _CENTER = {};
var _DEFAULT_LOCATION;

var _INTERVAL = null;
var dInterval = null;
var _FADE = null;

var _IMG = 256;
var _ZOOM = 15;
var _IGNORE_FLG = 0;
var _FUNC_FLG = false;

var _HOL_INIT_FLG = false;

var _gmap;
var _gls;

function mapInit() {
	setWindowConst();
	rotate("cws");
//	var it = setTimeout("rotate('cws')", 3000);
	if (GBrowserIsCompatible()) {
		_gls = new GlocalSearch();
		_gls.setSearchCompleteCallback(null, onLocalSearch);
		_gmap = new GMap2(document.getElementById("map"));
		setInitLocation();
		_gmap.setCenter(_DEFAULT_LOCATION, _ZOOM);
		document.getElementById("nw").onclick = function(){_gmap.panDirection(1, 1);}
		document.getElementById("n").onclick = function(){_gmap.panDirection(0, 1);}
		document.getElementById("ne").onclick = function(){_gmap.panDirection(-1, 1);}
		document.getElementById("w").onclick = function(){
			if(_FUNC_FLG){
				_gmap.zoomOut();
				_ZOOM--;
			}else{
				_gmap.panDirection(1, 0);
			}
		}
		document.getElementById("e").onclick = function(){
			if(_FUNC_FLG){
				_gmap.zoomIn();
				_ZOOM++;
			}else{
				_gmap.panDirection(-1, 0);
			}
		}
		document.getElementById("sw").onclick = function(){_gmap.panDirection(1, -1);}
		document.getElementById("s").onclick = function(){_gmap.panDirection(0, -1);}
		document.getElementById("se").onclick = function(){_gmap.panDirection(-1, -1);}
		document.getElementById("c").onclick = function(){
		var move = document.getElementById("move");
		var search = document.getElementById("search");

		if(window.navigator.userAgent.match("Opera 8") || window.navigator.userAgent.match("Opera 7")){
			move.style.background = none;
		}

			if(_FUNC_FLG){
//				move.style.opacity = 0;
				triggerFade("move", 0, 2, 5);
				document.getElementById("w").style.background = "none";
//				document.getElementById("c").style.background = "none";
				document.getElementById("e").style.background = "none";
				_FUNC_FLG = false;
				search.style.display = "none";
			}else{
				triggerFade("move", 0.7, 2,5);
//				move.style.opacity = 0.7;
				_FUNC_FLG = true;
				document.getElementById("w").style.background = "url(img/minus.png) center center no-repeat";
//				document.getElementById("c").style.background = "url(img/close2b.png) center center no-repeat";
				document.getElementById("e").style.background = "url(img/plus.png) center center no-repeat";
//				search.style.left = Math.round((window.innerWidth / 2) - 50) + "px";
				search.style.display = "block";
			}
		}
	}
}

function localSearch(q) {
//	alert(q);
	_gls.execute(q);
	return false;
}

function onLocalSearch() {
//alert(_gls.results.length);
	window.scrollTo(0, 60);
 if (_gls.results.length == 0){
 	alert("Not found.\nPlease try again with another keyword.");
 	 return;
 }else if(_gls.results.length == 1){
	goTo(0);
 }else{

 	var sel = document.select.dist;
	sel.innerHTML = "";
 	var i;
 	for(i = 0; i < _gls.results.length; i++){
 		var option = document.createElement("option");
 		option.setAttribute("value", i);
 		var txt = document.createTextNode(_gls.results[i].streetAddress);
 		option.appendChild(txt);
 		sel.appendChild(option);
 	}
	var option = document.createElement("option");
	option.setAttribute("value", "cancel");
	var txt = document.createTextNode("Cancel");
	option.appendChild(txt);
	sel.appendChild(option);

	document.s.style.display = "none";
 	document.select.style.display = "block";

 }
}

function setInitLocation(){
	var _location = "New York";
	_DEFAULT_LOCATION = new GLatLng(40.714510, -74.007140);
	switch(navigator.language){
		case "ja-JP-mac":
		case "ja-jp":
		case "ja":
		case "jp":
			_location = "Tokyo";
			_DEFAULT_LOCATION = new GLatLng(35.681099, 139.767084);
		break;
		case "sv-se":
		case "sv":
			_location = "Stockholm";
			_DEFAULT_LOCATION = new GLatLng(59.332726, 18.064454);
		break;
		case "zh-cn":
		case "cn":
			_location = "Beijing";
			_DEFAULT_LOCATION = new GLatLng(39.937758, 116.387215);
		break;
		case "ko-kr":
		case "ko":
		case "kr":
			_location = "Seoul";
			_DEFAULT_LOCATION = new GLatLng(37.532308, 126.957440);
		break;
		case "da-dk":
		case "dk":
			_location = "Copenhagen";
			_DEFAULT_LOCATION = new GLatLng(55.676294, 12.568116);
		break;
		case "nb-no":
		case "no":
			_location = "Oslo";
			_DEFAULT_LOCATION = new GLatLng(59.913820, 10.738741);
		break;
		case "de-de":
		case "de":
			_location = "Berlin";
			_DEFAULT_LOCATION = new GLatLng(52.523480, 13.411494);
		break;
		case "fr":
			_location = "Paris";
			_DEFAULT_LOCATION = new GLatLng(48.856558, 2.350966);
		break;
	}
	_gls.setCenterPoint(_location);	
}

function goTo(idx){
		document.getElementById("c").focus();
	 	document.s.style.display = "block";
	 	document.select.style.display = "none";
	if(idx == "cancel"){
	 	return false;
	}
	var tgt = _gls.results[idx];
	var lat = parseFloat(tgt.lat);
	var lng = parseFloat(tgt.lng);
//	rotate("cws");
//	var it = setTimeout("panTo(" + lat + "," + lng + ")", 2000);
 	panTo(lat, lng);
 	return false;
}

function panTo(lat, lng){
	_gmap.setCenter(new GLatLng(lat, lng), _ZOOM);
}

function setWindowConst(){
	var h = window.innerHeight;
	var w = window.innerWidth;
	if(h > w){
		_LONG_V = h;
		_SHORT_V = w;
	}else{
		_LONG_H = w;
		_SHORT_H = h;
	}
}

function rotate(){
	var itv = false;
	if(arguments[0] != "cws"){
		itv = true;
	}
	if(_INTERVAL != null){
		clearInterval(_INTERVAL);
		_INTERVAL = null;
	}
	var h = window.innerHeight;
	var w = window.innerWidth;
	var x = window.pageXOffset;
	var y = window.pageYOffset;
	_OFFSETX = x;
	_OFFSETY = y;
//	document.getElementById("wrapper").style.height = ((h * 1) + 60) + "px";
	if(h > w){
		if(!_LONG_V){
			_LONG_V = h;
			_SHORT_V = w;
		}
		currentShort = _SHORT_V;
		document.getElementById("wrapper").style.height = (_LONG_V + 60)  + "px";
		document.getElementById("map").style.height = (_LONG_V + 60)  + "px";
		document.getElementById("move").style.height = (_LONG_V + 60)  + "px";
	}else{
		if(!_SHORT_H || _HOL_INIT_FLG){
			if(!_HOL_INIT_FLG){
				_LONG_H = w;
				_SHORT_H = h;
			}
			var px = _SHORT_H;
			_HOL_INIT_FLG = true;
		}else{
			var px = _SHORT_H + 60;
		}
		document.getElementById("wrapper").style.height = px + "px";
		document.getElementById("map").style.height = px + "px";
		document.getElementById("move").style.height = px + "px";
		currentShort = px;
	}
//	document.getElementById("map").style.height = ((h * 1) + 60)  + "px";
	window.scrollTo(0, 60);
	replaceMovePanels();
	if(itv){
		_INTERVAL = setInterval("waitWinMove()", 333);
	}
}

function replaceMovePanels(){
	var w = Math.floor(currentShort / 3);

	var div = document.getElementById("move").getElementsByTagName("div");
	var i;

	var wh = window.innerHeight;
	var ww = window.innerWidth;

	var left = 0;
	var center = Math.round((ww / 2) - (w / 2));
	var right = ww - w;
	var top = 0;
	var middle = Math.round((wh / 2) - (w / 2));
	var bottom = wh - w;
	
	var hScheme = [left, center, right, left, center, right, left, center, right];
	var vScheme = [top,top,top,middle,middle,middle,bottom,bottom,bottom]
	
	for(i = 0; i < div.length; i++){
		div[i].style.width = w + "px";
		div[i].style.height = w + "px";
		div[i].style.left = hScheme[i] + "px";
		if(i < 6){
			div[i].style.top = vScheme[i] + "px";
		}else{
			div[i].style.bottom = "0px";
		}
	}
}

function triggerFade(tgtId, opacity, speed, ms){
	_FADE = setInterval("fade('" + tgtId + "'," + opacity + "," + speed + ")", ms);
}

function fade(tgtId, opacity, speed){
	speed = speed / 10;
	tgt = document.getElementById(tgtId);
	var op = parseFloat(tgt.style.opacity);
	if(!opacity){
		opacity = 0;
	}
	if(op == opacity){
		clearInterval(_FADE);
		_FADE = null;
		return;
	}else	if(!op){
		op = 0;
	}
	if(opacity > op){
		if(op + speed > opacity){
			tgt.style.opacity = opacity;	
		}else{
			tgt.style.opacity = op + speed;
		}

	}else if(opacity < op){
		if(op - speed < opacity){
			tgt.style.opacity = opacity;	
		}else{
			tgt.style.opacity = op - speed;
		}
	}
}

function waitWinMove(){
	var h = window.innerHeight;
	var w = window.innerWidth;
	if(parseInt(document.getElementById("map").style.height.replace("/px/", "")) == h){
//alert("xxx"+h);
//	window.scrollTo(0, 60);
		clearInterval(_INTERVAL);
		_INTERVAL= null;
		_CURRENT_H = h;
		_CURRENT_W = w;	
		execCheckWin();
	}
}

function checkWinState(){
	var h = window.innerHeight;
	var w = window.innerWidth;
//	document.getElementById("map").innerHTML = "<br><br><br><br>" + _IGNORE_FLG + "<br>" + _CURRENT_H + "<br>" + h; 
	if(_CURRENT_H != h || _CURRENT_W != w){
		if(_CURRENT_H == undefined || _CURRENT_W == undefined){
			_CURRENT_W = w;	
			_CURRENT_H = h;
			return;
		}
		_CURRENT_W = w;	
		_CURRENT_H = h;
		rotate("cws");
	}
}

function execCheckWin(){
	_INTERVAL = setInterval("checkWinState()", 500);
}
addEvent(window, "load", mapInit);
addEvent(window, "resize", rotate);
addEvent(window, "load", execCheckWin);
addEvent(window, "scroll", replaceMovePanels);
