(function($){
	jQuery.fn.extend({
		gmap : function(options){

			var map ;
			var g = this ;
		//	var centerMarker;
		//	var centerMarkerOption;

			var config = jQuery.fn.extend({
				// 初期設定
				'width'         : 800,
				'height'        : 640,
				'centerLatLng'  : [33.590355, 130.401716],	// 福岡市役所
				'zoom'          : 10
			} , options) ;


			// 各メソッド
			jQuery.fn.extend(jQuery.fn.gmap , {

				// 地図上のピンを生成する
				createIcon : function (src, iconWidth, iconHeight, zindex) {

					width  = 20;
					height = 20;

					if(iconWidth)
						width = iconWidth;

					if(iconHeight) 
						height = iconHeight;
					

					var icon              = new GIcon();
					icon.image            = src;
					icon.iconSize         = new GSize (width, height);
					icon.iconAnchor       = new GPoint(width / 2, height);
					icon.infoWindowAnchor = new GPoint(Math.round(width / 2), Math.round(height / 2));

					return icon;
				},


				// 緯度・経度から、マーカーを生成する
				createMarker : function(lttd, lgtd, html, src, iconWidth, iconHeight, zindex){

					var marker;

					if(src) {
						if (navigator.userAgent.indexOf("Trident/4.0") != -1) {
							option = {'icon' : g.gmap.createIcon(src, iconWidth, iconHeight)};
						}
						else {
							option = {'icon': g.gmap.createIcon(src, iconWidth, iconHeight), 'zIndexProcess' : function() { return zindex;}};
						}
						marker = new GMarker(new GLatLng(lttd, lgtd), option);
					}
					else {
						if (navigator.userAgent.indexOf("Trident/4.0") == -1) {
							option = {'zIndexProcess' : function() { return zindex;}};
						}
					}

					marker = new GMarker(new GLatLng(lttd, lgtd), option);

					g.map.addOverlay(marker, zindex);

					if(html) {

						// ふきだしが開いたときと閉じたときに、マーカーにフラグを仕込む
						GEvent.addListener(marker, 'infowindowopen',  function() {
							marker.clickToggle = true;
						});
						GEvent.addListener(marker, 'infowindowclose', function() {
							marker.clickToggle = false;
						});

						GEvent.addListener(marker, 'click', function() {
						//	marker.openInfoWindowHtml(html);
							// ふきだしが開いてたら閉じ閉じてたら開く
							if(! marker.clickToggle){
								marker.openInfoWindowHtml(html);
							}
							else{
								marker.closeInfoWindow();
							}
						//	g.map.setCenter(new GLatLng(lttd, lgtd))
						});

					}

					return marker;
				},


				// マーカーを追加する
				addOverlay : function(marker){
					g.map.addOverlay(marker);
				},


				// マーカーを削除する
				clearOverlays : function (marker){
					if(! marker){
						g.map.clearOverlays();
					}
					else{
						g.map.removeOverlay(marker);
					}
				},


				// 特定の座標にパンするメソッド
				panTo : function(lttd, lgtd){
					if(lttd && lgtd){
						g.map.panTo(new GLatLng(lttd, lgtd));
					}
					else{
						g.map.panTo(new GLatLng(config.centerLatLng[0], config.centerLatLng[1]));
					}
				},


				// 地図をリサイズするメソッド
				resize : function(){
					g.map.checkResize();
				}

/*
				addPolygonToMap : function(lttd, lgtd){
	
					var point = new GLatLng(lttd,lgtd);

					var latlngs = g.gmap.createRectangle(point, 100, 32);
					var polygon = new GPolygon(latlngs, "#ff0000", 3, 1, "#fee9e2", 0.5);

					g.map.addOverlay(polygon);

					return polygon;

				},

				createRectangle : function(latlng, r, n){
					var point = g.map.fromLatLngToContainerPixel(latlng);

					var x = point.x;
					var y = point.y;

					var latlngs = [];

					for (var i = 0 ; i < n ; i++){
						var px = x + r * Math.cos(i * Math.PI/(n/ 2));
						var py = y + r * Math.sin(i * Math.PI/(n/ 2));
						var latlng = g.map.fromContainerPixelToLatLng(new GPoint(px, py));

						latlngs.push(latlng);
					}
					latlngs.push(g.map.fromContainerPixelToLatLng(new GPoint(x + r, y)));

					return latlngs;
				},
*/

			});


			// ================================================
			// リターン内容
			// ================================================
			return this.each(function(){

				// 地図を表示する領域のタテヨコを設定
				g.width(config.width).height(config.height);

				// 地図を生成
				g.map = new GMap2(this);

				center = new GLatLng(config.centerLatLng[0],config.centerLatLng[1]);
				g.map.setCenter(center, config.zoom);

				// MAPコントローラ
				g.map.addControl(new GSmallMapControl()); // マップコントローラを付ける
				g.map.addControl(new GMapTypeControl());  // マップタイプコントローラを付ける
				g.map.addControl(new GScaleControl());    // スケールコントローラを付ける
			//	map.enableScrollWheelZoom()             // ホイールズームを許可する
			//	map.removeMapType(G_HYBRID_MAP);        // マップタイプコントローラから地図+写真を削除する
			//	map.addMapType(G_PHYSICAL_MAP);         // マップタイプコントローラに地形を追加する
			//	map.setMapType(G_PHYSICAL_MAP);         // デフォルトの地図を地形図にする
			//	map.enableScrollWheelZoom();            // マウスホイールによるズーム処理を有効化

			});
		}
	});
})(jQuery);
