
function LoadMap () {
  var Flg = 0;
  $( 'map_form' ).getElements().each( function ( item ) {
            if ( item.id == "prefecture") {
              Flg = 1;
              return;
            }
        } );

  if (Flg) {
    var params =$F("prefecture").split(',');
  } else {
    var params =$F("region").split(',');
  }

  onLoad(new GLargeMapControl());
  if (parseFloat(params[0]) > 90) {
    Move(new GLatLng( parseFloat(params[1]), parseFloat(params[0]) ), parseFloat(params[2]) );
  } else {
    Move(new GLatLng( parseFloat(params[0]), parseFloat(params[1]) ), parseFloat(params[2]) );
  }
  GEvent.addListener( map, 'dragend', LoadMarker );
  GEvent.addListener( map, 'zoomend', LoadMarker );
}


function LoadSpotMap () {
    //  onLoad(new GLargeMapControl());
  onLoad(new GSmallMapControl());
  LoadSpotMarker();
}

function LoadMarker () {
  var bounds = map.getBounds();
  var sw     = bounds.getSouthWest();
  var ne     = bounds.getNorthEast();
  var position = sw.lat() + ',' + ne.lat() + ',' +  sw.lng() + ',' + ne.lng();
  var shop_hidden_zoom = $F('shop_hidden_zoom');
  var now_zoom = map.getZoom();

  $( 'position' ).value = position;
  params = $( 'map_form' ).serialize();
  new Ajax.Request( 'map_json.php', { parameters: params, onComplete: function ( req ) {
    eval( 'var markers = ' + req.responseText );
    map.clearOverlays();
    for ( var i = 0; i < markers.length-1; i++ ) {
      var item = markers[ i ];
      if (item.shop == 1) {
      	if (now_zoom >= shop_hidden_zoom) {
          var point = new GPoint( item.tel, item.tnl );
          var marker = CreateShopMarker( point, item.html);
          map.addOverlay( marker );
        }
      } else {
        var point = new GPoint( item.longitude, item.latitude );
        var subcategory_id = item.subcategory[0];
//      var marker = CreateMarker( point, item.spot_id, item.spot_name, item.description_marker, item.image, subcategory_id, item.count, item.promoted_image_id, item.image_url_m);
        var marker = CreateMarker( point, subcategory_id, item.html);
        map.addOverlay( marker );
      }
    }
    document.getElementById("map_list").innerHTML = markers[i];
  } } );

}

function LoadSpotMarker () {
       
    var lat  = parseFloat( $F('latitude') );
    var lng  = parseFloat( $F('longitude') );
    var subcategory_id = $F('subcategory_id');
    map.setCenter( new GLatLng( lat, lng ), 14 );
    
    var point = new GPoint( lng, lat );
    var marker = LocateMarker( point, subcategory_id);
    map.addOverlay( marker );

}

function LocateMarker ( point, subcategory_id ) {
  var icon = new GIcon();
  icon.image = '/DATA/SUBCATEGORY/' + subcategory_id + '.gif';
  icon.iconSize = new GSize( 22, 28 );
  icon.iconAnchor = new GPoint( 10, 24 );
  icon.infoWindowAnchor = new GPoint( 5, 1 );
  return new GMarker( point, icon );
}

function Move ( latlng, zoom ) {
  map.setCenter( latlng, zoom );
  LoadMarker();
}

//function CreateMarker ( point, spot_id, title, description, imgflg, subcategory_id, com_count, promoted_id, image_url) {
function CreateMarker ( point, subcategory_id, html ) {

	var icon = new GIcon();
	icon.image = '/DATA/SUBCATEGORY/' + subcategory_id + '.gif';
	icon.iconSize = new GSize( 22, 28 );
	icon.iconAnchor = new GPoint( 10, 24 );
	icon.infoWindowAnchor = new GPoint( 5, 1 );

	//add
	var marker = new GMarker( point, icon );

	GEvent.addListener( marker, 'click', function() {
		marker.openInfoWindowHtml( html );
	} );
	return marker;
	//add
/*
	if (promoted_id) {
		imgpath = image_url;
	} else if ( imgflg == 1 || imgflg == '1') {
		imgpath = 'http://drive.nissan.co.jp/C/IMAGEC/1/' + spot_id + '/' + spot_id + '_1_m.jpg';
	}

	permalink = '../SPOT/detail.php?spot_id=' + spot_id;

	var marker = new GMarker( point, icon );
	var width  = 0;
	var height = 0;
	var onimg  = '';
	var html   = '';

	if ( imgflg == 1 || imgflg == '1') {
		onimg = '<div style="width:160px; height:120px; border:0px;  margin:0px 10px 0px 0px; padding:0px; top:auto; bottom:auto; overflow:hidden; float:left;"><a href="' + permalink + '" target="_spot_detail"><img src="' + imgpath + '" alt="' + title + '" width="160"/></a></div>';
	}

	html = '<div id="inmap">';
	html += '<a href="' + permalink + '" target="_spot_detail" style="font-weight:bold; margin:0px 0px 10px 0px;">' + title +'</a><br />';
	if ( imgflg == 1 || imgflg == '1') {
		html +=  onimg;
	}
	html += '<p style="font-size:12px; color:#999999; display:block; width:200px; float:left">' + '<a href="' + permalink + '" target="_spot_detail">' + description + '</a><br>';
	html += '<a href="' + permalink + '" target="_spot_detail">' + '&gt;&gt;詳しく見る' + '</a></p>';
	html += '<br style="clear:both;"></div>';
	GEvent.addListener( marker, 'click', function() {
		marker.openInfoWindowHtml( html );
	} );
	return marker;
*/
}

function CreateShopMarker ( point, html ) {

	var icon = new GIcon();
	icon.image = '/08IMAGES/SHOP/shop.gif';
	icon.iconSize = new GSize( 22, 28 );
	icon.iconAnchor = new GPoint( 10, 24 );
	icon.infoWindowAnchor = new GPoint( 5, 1 );

	//add
	var marker = new GMarker( point, icon );

	GEvent.addListener( marker, 'click', function() {
		marker.openInfoWindowHtml( html );
	} );
	return marker;
}

// google
var points = [];
var markers = [];
var counter = 0;
var sidebar_html = "";
var marker_html = [];
var to_htmls = [];
var from_htmls = [];
var map = null;
function onLoad( ctrl ) {
  if (GBrowserIsCompatible()) {
    var mapObj = document.getElementById("map");
    if (mapObj != "undefined" && mapObj != null) {
      map = new GMap2(document.getElementById("map"));
      map.addControl(ctrl);
      map.addControl(new GMapTypeControl());
      map.addControl(new GScaleControl());
    }
  } else {
    alert("Sorry, the Google Maps API is not compatible with this browser.");
  }
}


function check() {
     chk = 0;
     for(i = 0; i<document.map_form.elements.length; i++) {
      if((document.map_form.elements[i].name == "subcategory[]") && (document.map_form.elements[i].checked)) { chk = 1;}
     }
     if(chk) { document.getElementById("c_box_recommend").style.display = "";
      } else {
       document.getElementById("c_box_recommend").style.display = "none";
     }
   }
   
