﻿/*
 * 地図を描画
 */
function createGoogleMap(id, let, lng, zoom) {

	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById(id));
		map.enableDoubleClickZoom();
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(let, lng), zoom);
		
		drawMaker();
	}

	createClickFunction(map);
	
}


/*
 * リンクをクリックしたときに地図上にInfoWindowを描画するfunctionを生成
 * 第2引数にズームレベルがあった場合は地図を再描画
 */
function createClickFunction(map) {
	$(".gmapitem", $("#gmapitems")).click(function() {
		
		var point = $(this).attr("id");
		var zoom = point.split(",")[2];
		
		$a = $("a", $(this));
		$img = $("img", $a);
		var html = createInfoHtml( $a.attr("rel"), $img.attr("alt"), $a.attr("title") );
		map.openInfoWindowHtml(getGLatLng(point), html);
		
		if (zoom != "" && zoom != "undefined") {
			map.setCenter(getGLatLng(point), eval(zoom));
		} else
		{
			//map.setCenter(getGLatLng(point));
			map.panTo(getGLatLng(point));
		}
		
	});
}

/*
 * マーカーオブジェクトを生成
 */
function createMaker(point, html) {
	
	var marker = new GMarker(getGLatLng(point), createIcon());
	GEvent.addListener(marker, "mouseover", function() {
		marker.openInfoWindowHtml(html);
	});
	return marker;

}


/*
 * マーカーを地図に配置
 */
function drawMaker() {
	
	map.clearOverlays();
	var $target = $("#gmapitems");
	$(".gmapitem", $target).each(function(){
		
		$a = $("a", this);
		$img = $("img", $a);
		var html = createInfoHtml( $a.attr("rel"), $img.attr("alt"), $a.attr("title") );
		map.addOverlay(createMaker($(this).attr("id"), html));
		
	});
}


/*
 * 座標を展開しGLatLngへ格納
 */
function getGLatLng(letlng) {
	var let = letlng.split(",")[0];
	var lng = letlng.split(",")[1];
	return new GLatLng(let, lng);	
}


/*
 * アイコンを生成
 */
function createIcon() {
	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.image = "/share/imgs/pin_gmap.gif";
	icon.iconSize = new GSize(20, 30);
	//icon.shadowSize = new GSize(22, 20);
	icon.iconAnchor = new GPoint(10, 30);
	icon.infoWindowAnchor = new GPoint(10, 15);
	
	
	//var point = new GLatLng(let, lng);
	return icon;
	
}

/*
 * InfoWindow内のHTMLを生成
 */
function createInfoHtml(url, title, comment) {
	
	var _array = url.split(",");
	if (_array.length != 2 && title == "") {
		return '';
	}
	
	var ahref = _array[0];
	var imgsrc = _array[1];
	
	if (ahref == 'undefined') ahref = "";
	if (imgsrc == 'undefined') imgsrc = "";
	if (comment == 'undefined') comment = "";
	
	var html = '';
	if (ahref != '' && imgsrc != '' && title != '') {
		html += '<p class="infowindow"><a href="' + ahref + '">' + title + '<br />';
		html += '<img src="' + imgsrc + '" /></a></p>';
	} else
	if (ahref != '' && imgsrc == '' && title != '') {
		html += '<p class="infowindow"><a href="' + ahref + '">' + title + '</a></p>';
	} else
	if (ahref == '' && imgsrc != '' && title != '') {
		html += '<p class="infowindow"><img src="' + imgsrc + '" /></p>';
	} else
	if (ahref == '' && imgsrc == '' && title != '') {
		html += '<p class="infowindow">' + title + '</p>';
	}
	
	if (comment != '' && comment != null) {
		html += '<p class="infowindow">' + comment + '</p>';
	}
	return html;
	
}




/* ----------------------------------------------------------------------
 * シングルポイント用メソッド群
 ---------------------------------------------------------------------- */
function createSinglePointGoogleMap(id, let, lng, zoom, html) {

	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById(id));
		map.enableDoubleClickZoom();
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(let, lng), zoom);
		
		drawSinglePointMaker(let, lng, html);
	}

}

function drawSinglePointMaker(let, lng, html) {
	
	map.clearOverlays();
	map.addOverlay(createSinglePointMaker(let, lng, html));

}

function createSinglePointMaker(let, lng, html) {
	
	var marker = new GMarker(new GLatLng(let, lng), createIcon());
	if (html != "") {
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(html);
		});
	}
	return marker;

}


