//¸¶Ä¿¸¦ »ý¼ºÇÏ°í »çÀÌµå¹Ù µðÀÚÀÎ ²Ù¹Ì±â..
function createMarker(point, name, html, icon) {
    //¿äÃ»µÈ ¾ÆÀÌÄÜÀ¸·Î ¸¶Ä¿¸¦ ¸¸µéÀÚ
    var marker = new GMarker(point, gicons[icon]);

     GEvent.addListener(marker, "click", function() {
       marker.openInfoWindowHtml(html);
     });

		 // »çÀÌµå¹Ù¿¡¼­ »ç¿ëµÉ Á¤º¸¸¦ ÀÏ´Ü ÀúÀåÇØ ³õÀÚ
     gmarkers[i] = marker;
     htmls[i]    = html;

     // »çÀÌµå¹Ù¿¡ ¶óÀÎ Ãß°¡ÇÏ±â
     sidebar_html += '<ul style="padding:2px;margin:0px;clear:both;"><li><a style="font-size:12px;" href="javascript:epicclick(' + i + ')">' + name + '</a></li></ul><br>';
     i++;
     return marker;
}

//»çÀÌµå¹Ù¿¡¼­ Å¬¸¯ÇßÀ»¶§ Áöµµ¿¡ ÀÏÄ¡ ÇÏ´Â°Í Ç³¼± µµ¿ò¸» º¸¿©ÁÜ
function epicclick(i) {
  gmarkers[i].openInfoWindowHtml(htmls[i]);
}


//»çÀÌµå¹Ù¿¡ ÀÖ´Â htmlÄÝ·ºÆ® ÇØ¼­ ¸ÓÇÑ´Ù´Â°ÇÁö? ÆÄÆøÀº ±×³É µÇ´Âµ¥.. ÀÍ½º´Â ÀÌ°Å ¾øÀ¸´Ï±î ¿¡·¯³².
var sidebar_html = g_sidebar_html;
var sidebar_html_2 = "";
var no_geocode_description = "";
var no_geo = 0;

//»çÀÌµå¹Ù¿¡¼­ »ç¿ëµÈ ¸¶Ä¿ÇÏ°í htmlÄÚµå º¸°üÇÑ´Ù´Âµ¥.. ¿Ö? -¾øÀ¸¸é ¾ÈµÊ..   
var gmarkers = [];
var htmls    = [];
var i        = 0;



var marker_imgs = [];

// °ü·ÃµÈ ±¸±Û¸Ê ¾ÆÀÌÄÜÀ» »ý¼ºÇÔ
var gicons = [];



function show_epic_map() {
  if (GBrowserIsCompatible()) {

    //metadata¸¦ ¾²ÀÚ
    document.getElementById("google-map-metadata").style.display = 'block';

    // ¸ÊÀ» Áß¾ÓÀ¸·Î º¸³»ÀÚ.
    var bounds = new GLatLngBounds();
    var map    = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());

    //À§¼º, ÇÏÀÌºê¸®µå, ±×³ÉÁöµµ ±¸ºÐÇÏ±â
    var type = G_HYBRID_MAP;
    if (g_view_mode == 'G_SATELLITE_MAP') {
      type = G_SATELLITE_MAP;
    } else if (g_view_mode == 'G_NORMAL_MAP') {
      type = G_NORMAL_MAP;
    }

    if (g_popular_places.length > 0) {
      // ¸¶Ä¿¸¦ Ãß°¡ÇÏ±â Àü¿¡ Áöµµ¸¦ Áß¾È¿¡ ¸ÂÃçÁÖ±â À§ÇØ¼­ ÇÊ¿äÇÏ´Ù. 
      map.setCenter(new GLatLng(0,0),0,type);

      // °¢°¢ÀÇ À§Ä¡¸¦ À§ÇØ¼­ ¸¶Ä¿¸¦ ¼ÂÆÃÇÏÀÚ.
      for (var j = 0; j < g_popular_places.length; j++) {
        var point = g_popular_places[j][0];
        var html  = g_popular_places[j][1];
        var label = g_popular_places[j][2];

        if (point) {
        var marker = createMarker(point, label, html, j);
        map.addOverlay(marker);

        // Æ÷ÀÎÆ®¸¦ Ã£À»¶§¸¶´Ù ¹Ù¿îµå¸¦ ³ÐÇôÁÖÀÚ
        bounds.extend(point);
        } 
        else {
          //À§µµ, °æµµ Á¤º¸ ¾øÀ» °æ¿ì ±×³É »çÀÌµå ¹Ù¿¡¸¸ Ãß°¡ÇÏÀÚ..
          sidebar_html_2 += '<ul style="padding:0;margin:0px;clear:both;"><li><a href="map_edit.php?lcc=' + html + '" style="font-size:12px;">' + label + '</a></li></ul><br>';
					no_geo++;
        }
      }

			if (g_epic_tune == 'yes'){
        // ¹Ù¿îµå¸¦ ÅëÇØ¼­ Áß½É°ú ÁÜ·¹º§À» ¾Ë¾Æ³»ÀÚ
        map.setZoom( g_epic_tune_zoom );

        var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
        var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
        map.setCenter(new GLatLng(clat,clng));
      // »çÀÌµå¹Ù¸¦ À§Ä¡½ÃÅ°ÀÚ.
      document.getElementById("sidebar").innerHTML = sidebar_html;
			}
			else
			{
				//¼Ó¿¡ Æ÷ÇÔµÈ Àå¼Ò°¡ ÇÑ°÷ »ÓÀÌ¶ó¸é.
	      if ( g_popular_places.length == 1) {
	        // µ¥ÀÌÅ¸¸¦ °¡Áö°í Áß½É°ú ÁÜ·¹º§À» ÀâÀÚ
	        map.setCenter(new GLatLng(g_latitude,g_longitude), g_zoom, type);
	
	      }
				//¼Ó¿¡ Æ÷ÇÔµÈ Àå¼Ò°¡ ¿©·¯°÷ÀÌÁö¸¸.. ÇÑ°÷µµ À§Ä¡Á¤º¸¸¦ °¡Áö°í ÀÖÁö ¾ÊÀ»¶§
	      else if ( (g_popular_places.length - no_geo)  == 1) {
	        // µ¥ÀÌÅ¸¸¦ °¡Áö°í Áß½É°ú ÁÜ·¹º§À» ÀâÀÚ
	        map.setCenter(new GLatLng(g_latitude,g_longitude), g_zoom, type);

	      	// »çÀÌµå¹Ù¸¦ À§Ä¡½ÃÅ°ÀÚ.
					if ( sidebar_html_2 != "")
					{
							no_geocode_description = "<br><span style=\"font-size:11px;\">Map this place</span>";
					}
		      document.getElementById("sidebar").innerHTML = sidebar_html + no_geocode_description + sidebar_html_2;

	      }
	      
	      else {
	        // °æ°è¼±À» ±âÁØÀ¸·Î Áß½É°ú ÁÜ·¹º§À» ÀâÀÚ
	        map.setZoom(map.getBoundsZoomLevel(bounds));
	
	        var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
	        var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
	        map.setCenter(new GLatLng(clat,clng));
	
	
      	// »çÀÌµå¹Ù¸¦ À§Ä¡½ÃÅ°ÀÚ.
				if ( sidebar_html_2 != "")
				{
						no_geocode_description = "<br><span style=\"font-size:11px;\">Map this place</span>";
				}
	      document.getElementById("sidebar").innerHTML = sidebar_html + no_geocode_description + sidebar_html_2;
	      }
			}

    } else { 
      // Áß½É°ú ÁÜ·¹º§À» Á¤ÇÏÀÚ
      map.setCenter(new GLatLng(g_latitude,g_longitude), g_zoom, type);

      // Ä¿½ºÅÒ ¾ÆÀÌÄÜÀ» ¼Â¾÷ÇÏÀÚ.
      var icon = new GIcon();
      icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
      icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
      icon.iconSize = new GSize(12, 20);
      icon.shadowSize = new GSize(22, 20);
      icon.iconAnchor = new GPoint(6, 20);
      icon.infoWindowAnchor = new GPoint(5, 1);

      var marker = new GMarker(g_point, icon);
      map.addOverlay(marker);
    }

    //À§µµ °æµµ ÁÜ ·¹º§À» Ä¸ÃÄÇÏÀÚ
    GEvent.addListener(map, "moveend", function() {
      var center   = map.getCenter();
      g_latitude   = center.y;
      g_longitude  = center.x;
      g_zoom       = map.getZoom();
    });

    // ºä¸ðµå¸¦ Ä¸ÃÄÇÏÀÚ
    GEvent.addListener(map, "maptypechanged", function() {
      var myMapType = map.getCurrentMapType();
      if (myMapType == G_SATELLITE_MAP) {
        g_view_mode = 'G_SATELLITE_MAP';
      }  
      if (myMapType == G_NORMAL_MAP) {
        g_view_mode = 'G_NORMAL_MAP';
      }
      if (myMapType == G_HYBRID_MAP) {
        g_view_mode = 'G_HYBRID_MAP';
      } 
    });
  } else {
    alert("Sorry, your browser does not support Goolge Map");
  }
}


function edit_epic_map(latitude, longitude, zoom, view_mode, preview) {
  if (!latitude)  latitude  = g_latitude;
  if (!longitude) longitude = g_longitude;
  if (!zoom)      zoom      = g_zoom;
  if (!view_mode) view_mode = g_view_mode;
  zoom = parseInt(zoom);


  // ¸ÊÀ» Áß¾ÓÀ¸·Î 
  var map = new GMap2(document.getElementById("map"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());

  // ºä¸ðµå ¾Ë¾Æ³»ÀÚ.
  var type = G_HYBRID_MAP;
  if (view_mode == 'G_SATELLITE_MAP') {
    type = G_SATELLITE_MAP;
  } else if (view_mode == 'G_NORMAL_MAP') {
    type = G_NORMAL_MAP;
  }
  map.setCenter(new GLatLng(g_latitude,g_longitude), zoom, type);

  var form = '';
  form+= '<span class=click_to_move>Click to move the location.</span>';
  form += '<form name="saveForm" action="save_location.php" method="post">';
  form += '<input type="submit" name="submit_button_save" value="Save" />';
  form += g_cancel_link;
  form += '<div id="add-form" style="visibility:hidden">';
  form += 'Latitude: <input id="latitude" type="text" name="latitude" size="20"> ';
  form += 'Longitude: <input id="longitude" type="text" name="longitude" size="20"> ';
  form += '<input id="zoom" type="hidden" name="zoom">';
  form += '<input id="view_mode" type="hidden" name="view_mode">';
  form += '<input type="hidden" value="' + epic_loc + '" name="epic_loc">';
  form += '</div>';
  form += '</form>';

  // ÆûÀ» ¸¸µéÀÚ
  document.getElementById("google-map-metadata").style.display = 'block';
  document.getElementById("google-map-metadata").innerHTML = form;

  document.getElementById("latitude").value   = latitude;
  document.getElementById("longitude").value  = longitude;
  document.getElementById("zoom").value       = zoom;
  document.getElementById("view_mode").value  = view_mode;


  var pointpin = null;
  if (preview) {
    document.getElementById("add-form").style.display = 'block';

    pointpin = new GMarker(new GPoint(longitude, latitude));
    map.addOverlay(pointpin);
  }


  var icon        = new GIcon();
  icon.image      = "http://labs.google.com/ridefinder/images/mm_20_red.png";
  icon.shadow     = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
  icon.iconSize   = new GSize(12, 20);
  icon.shadowSize = new GSize(22, 20);
  icon.iconAnchor = new GPoint(6, 20);

  // Show marker at the place coordinace.
  var marker = new GMarker(g_point, icon);
  map.addOverlay(marker);

  GEvent.addListener(map, 'click', function(overlay, point) {
    if (!point) return;

    temp_pin = new GMarker(point);
    map.removeOverlay(marker); // Ã¹¹øÂ° ÇÉÀ» ¾ø¿¡¹ö¸®ÀÚ
    map.addOverlay(temp_pin);     // »õÇÉÀÌ ³õÀÌ±âÀü¿¡ ¸¶Áö¸· ÇÉÀ» ¾ø¾ÖÀÚ.

    // Center the map where the pin is.
    map.panTo(new GLatLng(point.y, point.x));

    // Write the form values.
    document.getElementById("longitude").value = point.x;
    document.getElementById("latitude").value  = point.y;

    if (pointpin) map.removeOverlay(pointpin);
    pointpin = temp_pin;
  });

  // ÁÜ·¹º§ Ä¸ÃÄÇÏÀÚ
  GEvent.addListener(map, "moveend", function() {
    document.getElementById("zoom").value = map.getZoom();
  });

  // ºä¸ðµå Ä¸ÃÄÇÏÀÚ
  GEvent.addListener(map, "maptypechanged", function() {
    var myMapType = map.getCurrentMapType();
    if (myMapType == G_SATELLITE_MAP) {
      document.getElementById("view_mode").value = 'G_SATELLITE_MAP';
    }
    if (myMapType == G_NORMAL_MAP) {
      document.getElementById("view_mode").value = 'G_NORMAL_MAP';
    }
    if (myMapType == G_HYBRID_MAP) {
      document.getElementById("view_mode").value = 'G_HYBRID_MAP';
    } 
  });
}

