function ShowGmap( point_x, point_y, zoom ) {
	if( GBrowserIsCompatible() ){
		map = new GMap2( document.getElementById( "gmap" ) );
		map.addControl( new GLargeMapControl() );
		map.addControl( new GOverviewMapControl() );
		map.setCenter( new GLatLng( point_x, point_y ), zoom );
	}
}

var gmarkers = [];

function addMarker( infoMarker ){
	var ptStudio = new GLatLng( infoMarker[ 0 ], infoMarker[ 1 ] );
	var marker = new GMarker( ptStudio );
	GEvent.addListener( marker, 'click', function(){
		marker.openInfoWindowHtml( infoMarker[ 2 ] );
	});
	return marker;
}
	
function AddStudioMarker( filename, nSelectIndex ){
	var request = GXmlHttp.create();
	request.open( "GET", filename, true );
	request.onreadystatechange = function() {
		if( request.readyState == 4 ){
			var xmlDoc = request.responseXML;
			var studioElement = xmlDoc.documentElement.getElementsByTagName( "StudioInfo" );
			
			for( i = 0; i < studioElement.length; i++ ){
				var pointElement = studioElement[ i ].getElementsByTagName( "point" );
				if( pointElement.length ){
					var infoElement = studioElement[ i ].getElementsByTagName( "info" );
					if( infoElement.length ){
						var id = infoElement[ 0 ].getAttribute( "id" );
						var name = infoElement[ 0 ].getAttribute( "name" );
						var url = infoElement[ 0 ].getAttribute( "url" );
						
						var infoHtml = "<div class=\"gmap_fukidasi\"><a href=\"index.php?com=detail&id=" + id + "\">" + name + "</a><a href=\"" + url + "\"><img src=\"image/home_icon.gif\"></a></div>";
						var infoMarker = new Array( pointElement[ 0 ].getAttribute( "x" ), pointElement[ 0 ].getAttribute( "y" ), infoHtml );
						var mkObj = addMarker( infoMarker );
						gmarkers[ i ] = mkObj;
						map.addOverlay( mkObj );
					}
				}
			}
			if( nSelectIndex > -1 ){
				SelectStudio( 0 );
			}
		}
	}
	request.send(null);
}

function getMapPoint( address ){
	// GClientGeocoderšś»
	geocoder = new GClientGeocoder();
	if( geocoder ){
		geocoder.getLatLng( address,
			function(point) {
				if( !point ){
					alert(address + " not found");
				}else{
					map.setCenter(point, 13);
					var marker = new GMarker(point);
					map.addOverlay(marker);
					document.form_info["point_x"].value = point.lat();
					document.form_info["point_y"].value = point.lng();
				}
			}
		);
	}
}

function SelectStudio( index ){
	map.panTo( gmarkers[ index ].getPoint() );
	GEvent.trigger( gmarkers[ index ], "click" );
}

function getMapsByArea( area_id, point_x, point_y, zoom ){
	document.cookie = "area=" + String( area_id );
	
	ShowGmap( point_x, point_y, zoom );
	AddStudioMarker( "getstudioxmlbyarea.php", -1 );
}

function getMapsByStudio( studio_id ){
	document.cookie = "studio=" + String( studio_id );
	
	ShowGmap( 33.591467, 130.398891, 15 );
	AddStudioMarker( "getstudioxmlbystudio.php", 0 );
}