//<![CDATA[

var map;
var xmlHttp;
var geocoder = new GClientGeocoder();
var spot = new Object();
var category = null;

var icon = new GIcon();

icon.shadow = "http://gourmet.kimama-labo.com/img/map/shadow.png";
icon.image = "http://gourmet.kimama-labo.com/img/map/food.png";
icon.iconSize = new GSize(34, 40);
icon.shadowSize = new GSize(51, 40);
icon.iconAnchor = new GPoint(17, 40);
icon.infoWindowAnchor = new GPoint(17, 2);
icon.infoShadowAnchor = new GPoint(34, 25);

function init(type, cate, address) {

    category = cate;

    map = new GMap2(document.getElementById("map"));
//map.setCenter(new GLatLng(34.722426197808446, 135.35455932617187), 12);
    map.setCenter(new GLatLng(35.644307, 139.699157), 13);
    map.addControl(new GLargeMapControl());
    map.addControl(new GOverviewMapControl());

//alert("init"+address);

    if(address != null){
	if (GBrowserIsCompatible()) {
		if (geocoder) {
			geocoder.getLatLng(
			  address,
			  function(point) {
				if (!point) {
				  //alert(address + " not found");
				} else {
				  map.setCenter(point, 16);
				}
			  }
			);
		}
	}
    }


    if(type == "small"){
    }else if(type == "category"){
    }else if(type == "top"){
        map.enableContinuousZoom(); 
        //map.addControl(new GMapTypeControl());
        GEvent.addListener(map, 'click', onGMapClick);
        map.addControl(new GScaleControl());
        setCross();
    }


    // マーカーを設定: XML データを読み込んで表示
    xmlHttp  = GXmlHttp.create(); // XmlHttpRequest オブジェクトを作成
    xmlHttp.open("GET", "http://gourmet.kimama-labo.com/gmap.xml",  true);
    xmlHttp.onreadystatechange = showMarkers;
    xmlHttp.send(null); 
}

function setCross() {

    //十字アイコン作成
    var cross_icon = new GIcon();
    cross_icon.image = "/img/cross.png";
    cross_icon.iconSize = new GSize(100, 100);
    cross_icon.iconAnchor = new GPoint(50, 50);
    var cross_marker = new GMarker(map.getCenter(), cross_icon);
    map.addOverlay(cross_marker);

    //地図移動で十字アイコン追随と、座標を表示
    GEvent.addListener(map, "move", function() { 
       cross_marker.setPoint(map.getCenter());
    });

    GEvent.addListener(map, 'moveend', function() {
       document.getElementById("show_x").innerHTML = map.getCenter().x;
       document.getElementById("show_y").innerHTML = map.getCenter().y;
    });

}

function showMarkers() {

//alert("showMarkers");

    if (xmlHttp.readyState == 4) {

        var xmlDoc = xmlHttp.responseXML;
        var marker = xmlDoc.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < marker.length; i++) {
            if( category == null ){
                    addMarker(marker[i],i);
            } else {
                if( marker[i].getAttribute("category") == category ){
                    addMarker(marker[i],i);
                }
            }
        } // End of for
    }
}

function addMarker(xml,i) {

//alert("addMarker");

	var point  = new GPoint(xml.getAttribute("lng"),
                            xml.getAttribute("lat"));
	var id     = xml.getAttribute("id");

	var ic = getIcon(xml.getAttribute("category"));

	spot[id] = new GMarker(point,ic);
	map.addOverlay(spot[id]);
	var html   = '<div id="InfoWindow" style="width:200px;font-size:12px;">'
               + '<a href="' + xml.getAttribute("link") + '">'
               + xml.getAttribute("title")
               + '</a>'
               + '<div style="margin-top:8px;">' + xml.getAttribute("rate") + ' / ' + xml.getAttribute("category") + '</div>'
               + '<div style="margin-top:8px;">' + xml.getAttribute("body") + '</div>'
               + '</div>';
	GEvent.addListener(spot[id], "click", function() {
		spot[id].openInfoWindowHtml(html);
	});
}

function showMap(lat,lon,category) {

	var map = new GMap2(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.setCenter(new GLatLng(lat, lon),19);

	var ic = getIcon(category);

	var point = new GLatLng(lat, lon);
	var marker = new GMarker(point,ic);
	map.addOverlay(marker);
}



function getIcon(category) {
    var ic     = icon;
    if( category == "カフェ" ){
       ic.image = "http://gourmet.kimama-labo.com/img/map/cafe.png";
    }else if(category  == "麺" ){
       ic.image = "http://gourmet.kimama-labo.com/img/map/noodle.png";
    }else if(category  == "焼肉" ){
       ic.image = "http://gourmet.kimama-labo.com/img/map/meat.png";
    }else if(category  == "カレー" ){
       ic.image = "http://gourmet.kimama-labo.com/img/map/curry.png";
    }else if(category  == "お好み焼き" ){
       ic.image = "http://gourmet.kimama-labo.com/img/map/okonomiyaki.png";
    }else{
       ic.image = "http://gourmet.kimama-labo.com/img/map/food.png";
    }
    return ic;
}

function setWeatherInfo() {

  var request = GXmlHttp.create();

  request.open("GET", "./mirror-weatherhacks.php", true);

  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      var rssitem = request.responseXML.getElementsByTagName("item");

      for (var i=0; i < rssitem.length; i++) {
        var cat = rssitem[i].getElementsByTagName("category");
        if (cat[0].childNodes[0].nodeValue == "PR") {
          continue;
        }

        var titleobj = rssitem[i].getElementsByTagName("title");
        var title = titleobj[0].childNodes[0].nodeValue;
        titles = title.split(" - ");
        titles2 = titles[0].split("]");

        //titles = title.split(" - ");
        //titles[0].match(/^(.+)\](.+)/);

        // get url of OTENKI icon
        var urlobj = rssitem[i].getElementsByTagName("url");
        var url = urlobj[0].childNodes[0].nodeValue;

        // get width of OTENKI icon
        var widthobj = rssitem[i].getElementsByTagName("width");
        var width = widthobj[0].childNodes[0].nodeValue;

        // get height of OTENKI icon
        var heightobj = rssitem[i].getElementsByTagName("height");
        var height = heightobj[0].childNodes[0].nodeValue;

        // create a marker with OTENKI icon
        var icon = new GIcon();
        icon.image = url;
        icon.iconSize = new GSize(width, height);
        icon.iconAnchor = new GPoint(0, 0);

        setIcon(titles2[1],icon);

      }
    }
  }
  request.send(null);
}




function setIcon(address,icon) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          //alert(address + " not found");
        } else {
          //map.setCenter(point, 13);
          var marker = new GMarker(point,icon);
          map.addOverlay(marker);
          //marker.openInfoWindowHtml(address);
        }
      }
    );
  }
}


function onGMapClick(overlay, point) {
  if (point) {
    var request = GXmlHttp.create();

    var url = "point.php" + "?x=" + point.x + "&y=" + point.y;

    request.open("GET", url, true);
    request.onreadystatechange = function() {
      if (request.readyState == 4) {
        var xmlDoc = request.responseXML;
        var xy = xmlDoc.documentElement.getElementsByTagName("xy");

        document.getElementById("show_x").innerHTML = xy[0].getAttribute("x");
        document.getElementById("show_y").innerHTML = xy[0].getAttribute("y");
      }
    }
    request.send(null);
  }
}
