// ajax-json_20090506.js

//<![CDATA[
    var infowindow_opening = false;
    var returning_latlng = false;
    var drawing_markers = false;
    var marker = [];
    var marker_msg = [];

    var arrow_marker = "";
    var arrow_point = "";
    var have_arrow_point = false;

    var rss_url = "/exec/rss-tb4gmap.rb?" + q;

    var map = new GMap(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GOverviewMapControl());
    map.setCenter(new GLatLng(start_lat, start_lon), start_zoom);
    map.setMapType(start_map_type);

     if (map.getCurrentMapType() == G_MAP_TYPE) {
       var map_type = "map";
     } else {
       var map_type = "sat";
     }

    var center_point = map.getCenterLatLng();
    var center_zoom = map.getZoomLevel();
    var point_stack = [center_point, center_zoom];


    var my_base_icon = new GIcon();
    my_base_icon.shadow = "/TB4Gmap/shadow_icon2.png";
    my_base_icon.iconSize = new GSize(22, 21);
    my_base_icon.shadowSize = new GSize(26, 21);
    my_base_icon.iconAnchor = new GPoint(0, 21);
    my_base_icon.infoWindowAnchor = new GPoint(11, 2);

    var my_txt_icon = new GIcon(my_base_icon);
    my_txt_icon.image = "/TB4Gmap/txt_icon2.png";

    var my_pict_icon = new GIcon(my_base_icon);
    my_pict_icon.image = "/TB4Gmap/pict_icon2.png";

    var my_new_icon = new GIcon();
    my_new_icon.shadow = "/TB4Gmap/shadow_icon2.png";
    my_new_icon.iconSize = new GSize(29, 31);
    my_new_icon.shadowSize = new GSize(29, 31);
    my_new_icon.iconAnchor = new GPoint(0, 31);
    my_new_icon.infoWindowAnchor = new GPoint(17, 6);

    var new_txt_icon = new GIcon(my_new_icon);
    new_txt_icon.image = "/TB4Gmap/txt_icon3.png";

    var new_pict_icon = new GIcon(my_new_icon);
    new_pict_icon.image = "/TB4Gmap/pict_icon3.png";

    var waiting_icon = new GIcon();
    waiting_icon.shadow = "/TB4Gmap/wait.png";
    waiting_icon.image = "/TB4Gmap/wait.png";
    waiting_icon.iconSize = new GSize(100, 62);
    waiting_icon.shadowSize = new GSize(100, 62);
    waiting_icon.iconAnchor = new GPoint(50, 62);
    waiting_icon.infoWindowAnchor = new GPoint(0, 50);

    var arrow_icon = new GIcon();
    arrow_icon.image = "/TB4Gmap/arrow.png";
    arrow_icon.shadow = "/TB4Gmap/arrow_shadow.png";
    arrow_icon.iconSize = new GSize(22, 32);
    arrow_icon.shadowSize = new GSize(23, 32);
    arrow_icon.iconAnchor = new GPoint(11, 32);

    function quick_jump(area_code){
      map.closeInfoWindow();
      if (area_code == "0") {
        map.centerAndZoom(new GPoint(136.45, 38.44), 12);
      } else if (area_code == "1") {
        map.centerAndZoom(new GPoint(142.62, 43.34), 11);
      } else if (area_code == "2") {
        map.centerAndZoom(new GPoint(140.32, 39.23), 11);
      } else if (area_code == "3") {
        map.centerAndZoom(new GPoint(139.44, 36.13), 10);
      } else if (area_code == "4") {
        map.centerAndZoom(new GPoint(137.47, 35.7), 10);
      } else if (area_code == "5") {
        map.centerAndZoom(new GPoint(135.3, 34.66), 10);
      } else if (area_code == "6") {
        map.centerAndZoom(new GPoint(133.37, 34.40), 10);
      } else if (area_code == "7") {
        map.centerAndZoom(new GPoint(130.68, 32.25), 10);
      } else if (area_code == "8") {
        map.centerAndZoom(new GPoint(126.09, 25.76), 10);
      }
    }

    // get point from stack and set it
    function pop_point(){
      map.recenterOrPanToLatLng(point_stack[0]);
      map.zoomTo(point_stack[1]);
    }

    // store point onto stack
    function push_point(){
      point_stack[0] = map.getCenterLatLng();
      point_stack[1] = map.getZoomLevel();
    }

  function savePoint() {
    // set span by msec
    var span = 30 * 24 * 60 * 60 * 1000;
    var expire_date = new Date();
    expire_date.setTime(expire_date.getTime() + span);

    if (save_point) {
      var point = map.getCenterLatLng();
      var zoom = map.getZoomLevel();

      var center = "" + point.y;
      center += "," + point.x;
      center += "," + zoom;
      center += "," + map_type;

      document.cookie = "tb4gmap_center=" + center + "; expires=" + expire_date.toGMTString();
    } else {
      document.cookie = "tb4gmap_center=void; expires=" + expire_date.toGMTString();
    }

    var relay_TB = "";
    if (tb_blogzine) {
      relay_TB += "/blgzn";
    }

    if (tb_hatena) {
      relay_TB += "/hatna";
    }

    if (tb_livedoor) {
      relay_TB += "/livdr";
    }
    document.cookie = "tb4gmap_relay=" + relay_TB + "; expires=" + expire_date.toGMTString();
  }

    function clk_save() {
      if (document.setup.save.checked) {
        save_point = true ;
      } else {
        save_point = false ;
      }
      savePoint();
    }

    function jump_to(lat, lon){
      map.closeInfoWindow();
      // move a little for place just behind flag
      infowindow_opening = false;
      map.recenterOrPanToLatLng(new GPoint(lon, lat));
    }

    function open_marker(id){
      map.closeInfoWindow();
      // move a little for place just behind flag
      //infowindow_closed = true;
      marker[id].openInfoWindowHtml(marker_msg[id]);
    }

    function insertTBAddress() {
      var keido = arrow_point.x;
      var ido = arrow_point.y;
      var location_tb = "" + ido.toFixed(7) + "%2C" + keido.toFixed(7);

      var location_form = "" + ido.toFixed(7) + "," + keido.toFixed(7);
      document.tb.location.value = location_form;

      // create random key
      rand_key = "00" + Math.round(Math.random() * 100)
      rand_str = rand_key.substr(rand_key.length - 3, 3);

      location_tb += "/skey=" + tb_key + "." + rand_str;
      document.tb.skey.value = tb_key + "." + rand_str;

      if (tb_blogzine) {
        location_tb += "/tb2blgzn";
      }

      if (tb_hatena) {
        location_tb += "/tb2hatna";
      }

      if (tb_livedoor) {
        location_tb += "/tb2livdr";
      }

      var tb_address = "トラックバックの送り先アドレスは、<br><span style=\"font-size: x-small\"><strong>http://www.platypus.st/TB2Gmap/" + location_tb + "</strong></span>";
      // works only on IE, need to know on IE or not
      var u_agent = navigator.userAgent.toLowerCase();
      if (u_agent.indexOf("msie") != -1) {
         // set clipboard but make error, plot last of function
         //window.clipboardData.setData("text", "http://www.platypus.st/TB2Gmap/" + location_tb);
        tb_address += " <br><input type=\"submit\" value=\"TBアドレスをコピー\" name=\"clipbrd\" onClick=\'JavaScript:window.clipboardData.setData(\"text\", \"http://www.platypus.st/TB2Gmap/" + location_tb + "\")\'>";
      }
      document.getElementById("tb-address").innerHTML = tb_address;
    }

    function plot_tb_mark() {
      if (have_arrow_point) {
        arrow_mark = new GMarker(arrow_point, arrow_icon);
        map.addOverlay(arrow_mark);

        insertTBAddress();
      }
    }

    function clk_blogzine() {
      if (document.setup.tb2blgzn.checked) {
        tb_blogzine = true ;
        document.tb.tb2blgzn.checked = true;
      } else {
        tb_blogzine = false ;
        document.tb.tb2blgzn.checked = false;
      }
      if (have_arrow_point) {
        insertTBAddress();
      }
      savePoint();
    }

    function clk_hatena() {
      if (document.setup.tb2hatna.checked) {
        tb_hatena = true ;
        document.tb.tb2hatna.checked = true;
      } else {
        tb_hatena = false ;
        document.tb.tb2hatna.checked = false;
      }
      if (have_arrow_point) {
        insertTBAddress();
      }
      savePoint();
    }

    function clk_livedoor() {
      if (document.setup.tb2livdr.checked) {
        tb_livedoor = true ;
        document.tb.tb2livdr.checked = true;
      } else {
        tb_livedoor = false ;
        document.tb.tb2livdr.checked = false;
      }
      if (have_arrow_point) {
        insertTBAddress();
      }
      savePoint();
    }

    function make_marker(marker_data) {
      var point = new GPoint(marker_data.lon, marker_data.lat);
      if (marker_data.mtype == "txt") {
        var marker = new GMarker(point, my_txt_icon);
      } else if (marker_data.mtype == "pict") {
	var marker = new GMarker(point, my_pict_icon);
      } else if (marker_data.mtype == "new_txt") {
        var marker = new GMarker(point, new_txt_icon);
      } else if (marker_data.mtype == "new_pict") {
	var marker = new GMarker(point, new_pict_icon);
      } else {
        var marker = new GMarker(point);
      }

      GEvent.addListener(marker, 'click', function() {
        document.getElementById("message").innerHTML = "clicked";
	marker.openInfoWindowHtml(marker_data.msg);
      });

      return marker;
    }

    function draw_marker(page) {
      if (drawing_markers) {} else {
        drawing_markers = true;

        document.getElementById("message").innerHTML = "begin draw marker";

        var point_center = map.getCenterLatLng();
        var waiting_marker = new GMarker(new GPoint(point_center.x, point_center.y), waiting_icon);

        savePoint();

        var bounder = map.getBoundsLatLng();
        var area_box = "N=" + bounder.maxY.toFixed(7) + "&E=" + bounder.maxX.toFixed(7) + "&W=" + bounder.minX.toFixed(7) + "&S=" + bounder.minY.toFixed(7) + "&page=" + page;
        document.getElementById("message").innerHTML = area_box;

        //var request = GXmlHttp.create();
        http_request = GXmlHttp.create();
        http_request.open("GET", "/exec/select-tb.rb?" + q + area_box, true);
        http_request.onreadystatechange = function() {
          if (http_request.readyState == 3) {
            document.getElementById("message").innerHTML = "Loadin Data ...";
          } else if (http_request.readyState == 4) {
            eval ("var res = " + http_request.responseText);
            document.getElementById("message").innerHTML = "Done";
            map.clearOverlays();
            // clearOverlays() called multi times, why?
            plot_tb_mark();
            marker = [];
            marker_msg = [];
            for (var i = 0; i < res.markers.length; i++) {
              marker_msg[res.markers[i].id] = res.markers[i].msg;
	      marker[res.markers[i].id] = make_marker(res.markers[i]);
              map.addOverlay(marker[res.markers[i].id]);
            }

            if (gmap2_mode) {
              var gmap2_marker = new GMarker(new GPoint(start_lon, start_lat));
              map.addOverlay(gmap2_marker);
            }

            document.getElementById("page_index").innerHTML = res.index_total + "<br />" + res.index_pages;
            document.getElementById("blog_entry_box").innerHTML = '<div id="blog_entry" style="float: left; width: 310px; height: 490px; overflow: auto;">' + res.blog_entry_html + '</div>';
            document.getElementById("page_index2").innerHTML = res.index_pages;
          drawing_markers = false;
          }
        }
        map.addOverlay(waiting_marker);
        http_request.send(null);
      }
    }

    function jump_page(page){
      map.closeInfoWindow();
      draw_marker(page);
    }

    function set_rss_url() {
      // make RSS URL
      var rss_bounder = map.getBoundsLatLng();
      var rss_param = "n=" + rss_bounder.maxY.toFixed(7) + "&e=" + rss_bounder.maxX.toFixed(7) + "&w=" + rss_bounder.minX.toFixed(7) + "&s=" + rss_bounder.minY.toFixed(7);
      document.getElementById("rss").innerHTML = "<strong>RSS2.0</strong>(<a href=\"" + rss_url + "&" + rss_param + "\">地図の範囲のみ</a> | <a href=\"" + rss_url + "\">全て</a>)";
    }

    GEvent.addListener(map, 'moveend', function() {
      if (infowindow_opening) {} else {
        if (returning_latlng) {
          returning_latlng = false;
        } else {
          // reset previous redraw request
          clearTimeout(timer_id);
          timer_id = setTimeout("draw_marker(1)",1000);
        }
      }

      set_rss_url();
    });

    GEvent.addListener(map, 'infowindowopen', function() {
      if (infowindow_opening) {} else {
        center_point = map.getCenterLatLng();
        center_zoom = map.getZoomLevel();
      }
      document.getElementById("message").innerHTML = "<strong>吹き出し窓が開いている間は、記事を検索しません。</strong>";
      infowindow_opening = true;
    });

    GEvent.addListener(map, 'infowindowclose', function() {
      if (center_zoom == map.getZoomLevel()) {
        returning_latlng = true;
        map.recenterOrPanToLatLng(center_point);
      } else {
        draw_marker(1);
        //map.centerAndZoom(center_point, center_zoom);
      }
      document.getElementById("message").innerHTML = "";
      infowindow_opening = false;
    });

    GEvent.addListener(map, 'click', function(overlay, point) {
     if (overlay) {
       if (overlay == arrow_mark) {
         have_arrow_point = false;
         arrow_point = "";
         document.tb.skey.value = "xxx.xxx";
         document.tb.location.value = "";
         document.getElementById("tb-address").innerHTML = "トラックバック先のアドレスは、<br>マップしたい場所をクリックすると表示されます。";

         map.removeOverlay(arrow_mark);
       }
     } else if (point) {
       // mark center and make trackback URL
       if (have_arrow_point) {
         map.removeOverlay(arrow_mark);
       }
       have_arrow_point = true;
       arrow_point = point;
       plot_tb_mark();
     }
    });

    GEvent.addListener(map, 'maptypechanged', function() {
     if (map.getCurrentMapType() == G_MAP_TYPE) {
       map_type = "map";
     } else {
       map_type = "sat";
     }
    });

    set_rss_url();
    timer_id = setTimeout("draw_marker(1)", 0);
//]]>
