var mapCache = {};
var geocoder = null;

/**
 * id に該当するマップデータを取得する。
 * @param id	id
 * @param latId		緯度ID
 * @param lngId		経度ID
 * @return	マップデータ
 */
var getMap = function(id) {
	var map = new GMap2(document.getElementById(id));
	mapCache[id] = map;
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	//map.setUIToDefault();
  	map.enableGoogleBar();
    return map;
};

/**
 * 緯度経度を元に地図情報に設定を行う。
 * @param map		マップデータ
 * @param latlng	緯度経度
 * @param latId		緯度を保持するId
 * @param lngId		経度を保持するId
 * @return　マップデータ
 */
var setMap = function(map, array ,sort, arrName) {
	// 緯度経度が無効な場合
	if (!array || array.length == 0) {
		return map;
	} 
	
	// マップデータが無効な場合
	if (!map) {
		return map;
	}
	
	map.clearOverlays();
	
	var centerLatLng = null;
	var temp = "";
	$.each(array, function(i, latlng) {
		if(!sort){
			if (!centerLatLng) {
				centerLatLng = latlng;
			}
		}else{
			if (sort == i) {
				centerLatLng = latlng;
			}
		}
		
		var marker = null;
		if(i == sort){
			marker = new GMarker(latlng, {icon:createIcon()});
			
		}else{
			marker = new GMarker(latlng, {icon:createIconGreen()});
		}
		map.addOverlay(marker);
		var name = arrName[i];
		
		GEvent.addListener(marker, "click", function(){
			marker.openInfoWindowHtml(name);
		});
		
	});
	map.setCenter(centerLatLng, 15);
	
	
	return map;
};

/**
 * パラメータに該当する地図情報を表示する。
 * @param id					表示対象divのid
 * @param lat					緯度
 * @param lng					経度
 * @param latId					緯度を表す緯度id
 * @param lngId					経度を表す経度id
 * @param draggable				ドラッグ可能
 * @return	マップデータを返却する。
 */
var showMap = function(id, array, sort, arrName) {

	// マップが表示できない場合、nullを返却する。
	if (!'GBrowserIsCompatible' in window) return null;
	
    // 表示対象のエリアが宣言されていない場合、nullを返却する。
    if (document.getElementById(id) == null) return null;

    // マップデータを取得し、各種再設定を行う。
	var map = getMap(id);
	setMap(map, array, sort, arrName);
    return map;
};


/**
 * 位置表示を行うアイコンを作成する。
 * @return  icon
 */
var createIcon = function() {
	var icon = new GIcon();
	icon.image = 'http://maps.google.co.jp/mapfiles/ms/icons/red-dot.png' ;
	icon.iconSize = new GSize(32, 32);
	icon.shadowSize = new GSize(32, 32);
	icon.iconAnchor = new GPoint(9, 34);
	icon.infoWindowAnchor = new GPoint(9, 2);
	return icon;
};

/**
 * 位置表示を行うアイコンを作成する。
 * @return  icon
 */
var createIconGreen = function() {
	var icon = new GIcon();
	icon.image = 'http://maps.google.co.jp/mapfiles/ms/icons/green-dot.png' ;
	icon.iconSize = new GSize(32, 32);
	icon.shadowSize = new GSize(32, 32);
	icon.iconAnchor = new GPoint(9, 34);
	icon.infoWindowAnchor = new GPoint(9, 2);
	return icon;
};

/**
 * 住所による地図情報の取得を行う
 * @param id		地図情報表示Id
 * @param address	住所
 * @param latId		緯度ID
 * @param lngId		経度ID
 * @param draggable	ドラッグ可能
 * @return	マップデータ
 */
var setLatLngByAddress = function(address01, address02, latSelector, lngSelector) {
	
	address = address01 + address02;
	
	// GMap表示不可能な場合、nullを返却して終了
	if (!GBrowserIsCompatible()) return null;
	
	if (!geocoder) {
		geocoder = new GClientGeocoder();
	}
	
	geocoder.getLatLng(address, function(latlng) {
		if(latlng){
			$(latSelector).val(latlng.lat());
			$(lngSelector).val(latlng.lng());
			alert("経度と緯度は正常に入力されました。");
		}else{
			alert("対象住所の座標が有りません。\n\r再度入力するか、緯度と経度を直接入力してください。");
		}
	});
	
	return map
};

/**
 * 地図情報をアンロードする。
 */
var unloadMap = function() {
	$.each(mapCache, function(id, map) {
		$('#' + id).html('');
		map = null;
	});
	mapCache = {};
};

/**
 * 地図をクリアする。
 * @param id 対象Id
 */
var clearMap = function(id) {
	var map = mapCache[id];
	if (map != null) {
		$('#' + id).html('');
		map = null;
	}
};

