logging__ = false;

var map;
var icon = {};
var marker = {};
var xmlDoc;
var iwFlg = 0;

function startMap() {
  showElm("main");
  hideElm("list_onload");
  hideElm("map_onload");
  setIcon();
  loadMap();
  setXmlData();
//  setMenuHTML();
}

function loadMap() {
  var a = kitV.view;
  map = new GMap(getElm("map"));
  map.addControl(new GMapTypeControl());
  map.addControl(new GLargeMapControl());
  map.addControl(new GScaleControl());
  map.centerAndZoom(new GPoint(a.center.lng, a.center.lat), a.zoomLevel);
  if (!kitV.shop.sid) {
    GEvent.addListener(map, "moveend", function(){if(!iwFlg){resetPage();setXmlData();}});
    GEvent.addListener(map, "infowindowclose", function(){iwFlg = 0});
    GEvent.addListener(map, "infowindowopen", function(){iwFlg = 1});
  }
}

function setIcon() {
  var a = new GIcon();
  a.shadow = iconV.shadow;
  a.iconSize = new GSize(iconV.width,iconV.height);
  a.shadowSize = new GSize(iconV.width,iconV.height);
  a.iconAnchor = new GPoint(iconV.center,iconV.height);
  a.infoWindowAnchor = new GPoint(iconV.center,0);
  for (var i in iconV.image) {
    icon[i] = new GIcon(a);
	icon[i].image = iconV.image[i];
  }
}

//--------------------------------------------------
//--------------------------------------------------

function getMapSize() {
  var mapSize = new Object();
  if (window.navigator.userAgent.match(/Opera/)){
    getElm("all").style.height=document.body.clientHeight;
  }
  var H = getElm("all").offsetHeight;
  var W = getElm("all").offsetWidth;
  mapSize.height = H - headerH - footerH - mainMapB;
  mapSize.height = (mapSize.height < 0)? 0: mapSize.height;
  mapSize.width = W - mainMapX - mainMapB;
  return mapSize;
}

function setMapSize() {
  var mapSize = getMapSize();
  var listH = mapSize.height + mainMapB - mainListB;
  getElm("map").style.height = mapSize.height+"px";
  getElm("map").style.width = mapSize.width+"px";
  getElm("list").style.height = listH+"px";
}

function resizeMap() {
  setMapSize();
//  map.onResize(); // for GoogleMapsAPI v1
}

//--------------------------------------------------
//--------------------------------------------------
function setXmlData() {
  var xmlUri = makeQuery();
  loadXmlDoc(xmlUri);
}

function makeQuery() {
	var query = xmlFile+"?";
	var center = map.getCenterLatLng();
	var span = map.getSpanLatLng();
	query += "lat="+roundLatLng(center.y)+"&lng="+roundLatLng(center.x)+"&w="+roundLatLng(span.width)+"&h="+roundLatLng(span.height);
	query += "&g="+kitV.sKey.g;
	if (kitV.sKey.c) query += "&c="+kitV.sKey.c;
	if (kitV.sKey.f) query += "&f="+kitV.sKey.f;
	if (kitV.pInfo.page) query += "&page="+kitV.pInfo.page;
	if (kitV.pInfo.num) query += "&num="+kitV.pInfo.num;
	if (kitV.shop.sid) query += "&sid="+kitV.shop.sid;
	
	//getElm("test_output").value = query;
	return query;
}

function roundLatLng(num) {
	return Math.round(num*1000000)/1000000;
}

function loadXmlDoc(xmlUri) {
	var request = GXmlHttp.create();
	request.open("GET", xmlUri, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			xmlDoc = request.responseXML;
			setMarkers();
			if (kitV.shop.sid) openInfo(kitV.shop.sid);
			setList();
			setPageInfo();
		}
	}
	request.send(null);
}

function setMarkers() {
    var places = xmlDoc.documentElement.getElementsByTagName("place");
	var len = places.length;
	var newMarker = {};
	var id, lat, lng, point;
    for (var i = 0; i < len; i++) {
      id = places[i].getAttribute("id");
      if (marker[id]) {
        newMarker[id] = marker[id];
		delete marker[id];
	  }
      else {
        lat = parseFloat(places[i].getAttribute("lat"));
        lng = parseFloat(places[i].getAttribute("lng"));
        point = new GPoint(lng, lat);
        newMarker[id] = addMarker(point, icon[kitV.sKey.g], places[i]);
      }
    }
    for (var id in marker) {
      removeMarker(marker[id]);
    }
	marker = newMarker;
}

function removeMarker(markerObj) {
	GEvent.clearListeners(markerObj, "click");
	map.removeOverlay(markerObj);
}

function addMarker(point, myicon, xml) {
  var markerObj = new GMarker(point,myicon);
  if(xml.hasChildNodes()) {
    GEvent.addListener(markerObj, "click", function() {
      requestXsltProcess(xml, markerXslUri, function(a){markerObj.openInfoWindowHtml(a)});
    });
  }
  map.addOverlay(markerObj);
  return markerObj;
}

function setList() {
	  xml = xmlDoc.documentElement.getElementsByTagName("places").item(0);
      requestXsltProcess(xml, listXslUri, function(a){getElm("list").innerHTML = a});
}

function requestXsltProcess(xml, xslUri, callback) {
  var xslreq = GXmlHttp.create();
  xslreq.open("GET", xslUri, true);
  xslreq.onreadystatechange = function() {
    if (xslreq.readyState == 4) {
      callback(xsltProcess(xml, xslreq.responseXML));
    }
  }
  xslreq.send(null);
}

function openInfo(id) {
  GEvent.trigger(marker[id],"click");
}

function setPageInfo() {
	var page = parseInt(xmlDoc.documentElement.getElementsByTagName("page").item(0).firstChild.nodeValue);
	var num = parseInt(xmlDoc.documentElement.getElementsByTagName("num").item(0).firstChild.nodeValue);
	var count = parseInt(xmlDoc.documentElement.getElementsByTagName("count").item(0).firstChild.nodeValue);
	if (kitV.pInfo.page == null && kitV.pInfo.num == null) {
		setCounter(getElm("counter"), counterHtml2, page, num, count);
		if (isShow("pager")) {
			hidePager("pager", "list");
		}
	}
	else {
		setCounter(getElm("counter"), counterHtml, page, num, count);
		if (!isShow("pager")) {
			showPager("pager", "list");
		}
		setPager(getElm("pager"),  page, num, count);
	}
}

function setCounter(docElm, html, page, num, count) {
	docElm.innerHTML = createCounter(html, page, num, count);
}

function createCounter(html, page, num, count) {
	var a = count;
	var b = (count > 0)? (page - 1) * num + 1: 0;
	var pn = page * num;
	var c = (pn < count)? pn: count;
	return eval(html);
}

function showPager(pagerId, containerId) {
	var container = getElm(containerId);
	container.style.height = container.offsetHeight - getElm(pagerId).offsetHeight + "px";
	showElm(pagerId);
}

function hidePager(pagerId, containerId) {
	var container = getElm(containerId);
	hideElm(pagerId);
	container.style.height = container.offsetHeight + getElm(pagerId).offsetHeight + "px";
}

function setPager(docElm, page, num, count) {
	if (count > 0) {
		docElm.innerHTML = createPreviousPage(page) + createPageIndex(page,num,count) + createNextPage(page, num, count);
	}
	else {
		docElm.innerHTML = emptyDataHtml;
	}
}

function createPreviousPage(page) {
	var a = page - 1;
	return (a > 0)? eval(previousPageHtml): dummyPageHtml;
}

function createNextPage(page, num, count) {
	var a = page + 1;
	var b = Math.ceil(count / num);
	return (a <= b)? eval(nextPageHtml): dummyPageHtml;
}

function createPageIndex(page,num,count) {
	var a = page, p = page, n = page;
	var b = eval(currentNumberHtml);
	var c = 1;
	var s = " ";
	var end = Math.ceil(count / num);
	var l = (end < kitV.pInfo.maxLength)? end: kitV.pInfo.maxLength;
	while (c < l) {
		if (n < end) {
			n ++;
			a = n;
			b = b + s + eval(pageNumberHtml);
			c ++;
		}
		if (p > 1 && c < l) {
			p --;
			a = p;
			b = eval(pageNumberHtml) + s + b;
			c ++;
		}
	}
	var ahead = (p > 1)? continueHtml: separatorHtml;
	var behind = (n < end)? continueHtml: separatorHtml;
	return ahead + b + behind;
}

function changePage(num) {
	if (kitV.shop.sid) return;
	kitV.pInfo.page = num;
	map.closeInfoWindow();
	setXmlData();
}

function resetPage() {
	if (kitV.pInfo.page !== null) kitV.pInfo.page = 1;
}
//--------------------------------------------------
//--------------------------------------------------

function changeGenre(num) {
  if (kitV.shop.sid) return;
  kitV.sKey = {g:num,c:"",f:""};
  resetPage();
  map.clearOverlays();
  marker = {};
  setXmlData();
//  setMenuHTML();
}

function setMenuHTML() {
	var cMenuUri = "menu/c_"+kitV.sKey.g+".html";
	var fMenuUri = "menu/f_"+kitV.sKey.g+".html";
    loadMenuHTML(getElm("cmenu"), cMenuUri);
    loadMenuHTML(getElm("fmenu"), fMenuUri);
}

function loadMenuHTML(docElm, menuUri) {
  var request = GXmlHttp.create();
  request.open("GET", menuUri, true);
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      docElm.innerHTML = request.responseText;
    }
  }
  request.send(null);
}

//--------------------------------------------------
//--------------------------------------------------

function onSearch(menuId) {
  closeMenu(menuId);
  convSKey(menuId);
  resetPage();
  map.closeInfoWindow();
  setXmlData();
}

function convSKey(menuId) {
	var sKey = "";
	var elements = document.forms[menuId].elements;
	for (var i = 0; i < elements.length; i++) {
		if (elements[i].checked) {
			sKey += elements[i].value+",";
		}
	}
	kitV.sKey[menuId.charAt(0)] = sKey.substring(0,sKey.length-1);
}

function switchMenu(menuId) {
	if (kitV.shop.sid) return;
	var menuId2 = (menuId == "cmenu")? "fmenu": "cmenu";
	if (isShow(menuId)) {
		closeMenu(menuId);
	}
	else {
		if (isShow(menuId2)) {
			closeMenu(menuId2);
		}
		openMenu(menuId);
	}
}

function closeMenu(menuId) {
  hideElm(menuId);
}

function openMenu(menuId) {
  showElm(menuId);
}

function hideElm(id) {
  getElm(id).style.visibility = "hidden";
}

function showElm(id) {
  getElm(id).style.visibility = "visible";
}

function isShow(id) {
  var result;
  switch (getElm(id).style.visibility) {
    case "visible": result = true; break;
    case "hidden": result = false; break;
    default: result = null;
  }
  return result;
}

function getElm(id) {
    return document.getElementById(id);
}

