/**
 * Google Maps 表示用（jQuery Plugin）
 */
(function($) {
	function GMap() {
		this.mapTypeId = google.maps.MapTypeId.ROADMAP;
		this.scaleControl = true;
		this.scrollwheel = false;

		this._defaults = {
			zoom: 16,
			lat: 0,
			lng: 0,
			title: "",
			address: "",
			tel: ""
		};
	}

	$.extend(GMap.prototype, {
		_attach: function(element) {
			this._target = element;

			/* 中央に表示する座標を設定 */
			var centerPos = new google.maps.LatLng(this._get("lat"), this._get("lng"));

			/* 地図を表示 */
			var mapOptions = {
				zoom: this._get("zoom"),
				mapTypeId: this.mapTypeId,
				scaleControl: this.scaleControl,
				scrollwheel: this.scrollwheel,
				center: centerPos
			};
			var map = new google.maps.Map(this._target.get(0), mapOptions);

			/* 地図にマーカを表示 */
			var markerOptions = {
				position: centerPos,
				map: map
			};
			var marker = new google.maps.Marker(markerOptions);

			/* 吹き出しを表示 */
			var infoOptions = {
				position: centerPos,
				content: this._createInfoContent()
			};
			var infowindow = new google.maps.InfoWindow(infoOptions);
			google.maps.event.addListener(marker, 'click', function() {
				infowindow.open(map, marker);
			});
			infowindow.open(map, marker);
		},
		_createInfoContent: function() {	/* 吹き出し用HTMLの生成 */
			var infoContent = "";

			infoContent += "<div>";
			infoContent += "<h4 style=\"margin:0 0 10px 0;font-size:120%;font-weight:bold;\">" + this._get("title") + "</h4>";
			infoContent += "<dl style=\"width:320px;font-size:90%;\">";
			infoContent += "<dt style=\"float:left;width:70px;\">住所</dt>";
			infoContent += "<dd style=\"float:left;width:250px;\">" + this._get("address") + "</dd>";
			infoContent += "<dt style=\"float:left;width:70px;\">電話番号</dt>";
			infoContent += "<dd style=\"float:left;width:250px;\">" + this._get("tel") + "</dd>";
			infoContent += "</dl>";
			infoContent += "</div>";

			return infoContent;
		},
		_get: function(key) {				/* 設定値の取得 */
			return this._defaults[key];
		}
	});

	$.fn.gMap = function(options) {
		$.extend($.gMap._defaults, options);
		$.gMap._attach(this);

		return this;
	};

	$.gMap = new GMap();
	window.GMAP = $;

})(jQuery);
