var map = null;
var markers = new Array();

function load() {

	if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
	    map.addControl(new GSmallMapControl());
	    map.addControl(new GMapTypeControl());
		point_x = 127.68590569496155;
		point_y = 26.215512485239405;
		map.setCenter(new GLatLng(point_y, point_x),16,G_NORMAL_MAP);
		
		move();
		
		GEvent.addListener(map, "moveend", function() {
			move();
		});
	}else{
		alert("お使いのブラウザは、Google Maps に対応しておりません。");
	}
}

function move(){
	
	var cObj = map.getCenter();
	
	point_x = cObj.x;
	point_y = cObj.y;
	zoom =map.getZoom();
	
	addMarkerMove();
}

function addMarkerMove() {
	var Bounds = map.getBounds();
	var minX = Bounds.getNorthEast().lat();
	var minY = Bounds.getNorthEast().lng();
	var maxX = Bounds.getSouthWest().lat();
	var maxY = Bounds.getSouthWest().lng();
	
	var request = GXmlHttp.create();
	
	request.open('GET','/map_marker.php?mode=photo&minX='+minX+'&minY='+minY+'&maxX='+maxX+'&maxY='+maxY, true);
	
	
	//国際通りフォトギャラリーをマーキング
	var request = GXmlHttp.create();
	request.open('GET','/map_marker.php?mode=photo&minX='+minX+'&minY='+minY+'&maxX='+maxX+'&maxY='+maxY, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
		
			var html = request.responseText;
			if(html != ""){
				var lines = html.split("\n");
				var inner_html = "";
				for(var i=0;i<lines.length;i++){
					var line = lines[i];
					
					if(line != ""){
						var prms = line.split(",");
						var marker = addMarker(prms[0],prms[1],prms[3],prms[4],prms[5]);
					}
				}
			}
		}
	}
	request.send(null);
}

function addMarker(lat,lng,id,ext,gbody) {
	//alert(lat+","+lng+","+gbody);
	var markerIcon = new GIcon();
	markerIcon.image = '/thumbnai_photo.php?pid='+ id +'&ext='+ext;
	markerIcon.iconSize = new GSize(30,23);
	markerIcon.iconAnchor = new GPoint(14, 22);
	markerIcon.iconShadoAnchor = new GPoint(18, 24);
	markerIcon.infoWindowAnchor=new GPoint(18,10);
	
	var point = new GLatLng(lat, lng);
	var marker = new GMarker(point,markerIcon);
	
	GEvent.addListener(marker, "click", function() {
		info_html  = "";
		info_html = infoHtml(id,ext,gbody);
		marker.openInfoWindowHtml(info_html);
		imageDataChg(id,ext);
	});
	map.addOverlay(marker);
	return marker;
}

function popupMarker(id){
	marker = markers[id];
	var html = "";
	var request = GXmlHttp.create();
	request.open('GET', "/map_marker.php?pid="+id, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			html = request.responseText;
			
			if(html != ""){
				var prms = html.split(",");
				
			
				
				var marker = addMarker(prms[0],prms[1],prms[3],prms[5],prms[4]);
				
				info_html  = "";
				info_html = infoHtml(prms[3],prms[4],prms[5]);
		
				marker.openInfoWindowHtml(info_html);
			}
		}
	}
	request.send(null);
}

function infoHtml(id,ext,gbody){
	info_html  = "";
	info_html += "<div class='marker_info_photo'>";
	info_html += "<img src='/_photo/"+ id +"_140."+ ext +"' width='140' height='111' /><p><strong>" + gbody + "</strong></p>";
	info_html += "</div>";

	return info_html;
}

