/**
 * gmap.js
 * @version	1.7 - 12:43 2009/06/25
 *
 * Google Maps API リファレンス - Google Maps API - Google Code
 * http://code.google.com/intl/ja/apis/maps/documentation/reference.html
 *
 */

Kaas.googlemaps = {
	
	init: function(c) {
		
		for (var j = 0, jl = this.mark.length; j < jl; j++) {
			
			var gmaps = (document.getElementById(this.mark[j].container));
			gmaps.innerHTML = '<div id="gContainer' + j + '" class="gbox"></div>';
				 
			this.gmap = new GMap2(document.getElementById('gContainer'+j));
	
			
			var center;
			center = this.mark[j].latlng;
			
			// 中心位置を設定
			this.gmap.setCenter(new GLatLng(center[0], center[1]), this.defaultZoom);
			
			// コントロール作成
			this.gmap.addControl(new GLargeMapControl());
			this.gmap.addControl(new GMapTypeControl());
	
			
			// マップタイプ設定
			this.gmap.setMapType(this.mapType);
			// マーカー作成
			var marker = this.createGMarker(j);
			this.gmap.addOverlay(marker);
			
			if (this.mark[j].id != undefined) this.onSetCenter(j, marker);
				
		}
		
	},
	

	/**
	 * マーカー作成
	 * @param	i	:	mark のインデックス
	 * @return	{GMarker}
	 */
	createGMarker: function(i) {
		
		var m = this.mark[i];
		// Icon
		var iconsrc = (m.iconsrc != undefined)? m.iconsrc : this.iconsrc;
		var markerOpt = (iconsrc)? { icon: this.createGIcon(iconsrc) } : {};
		
		var marker = new GMarker(new GLatLng(m.latlng[0], m.latlng[1]), markerOpt);
		// infoWindow 幅の指定
		var infoWindowOpt = this.gmap.getInfoWindow();
		infoWindowOpt.maxWidth = this.infoWindowMaxWidth;
		
		if (m.html != undefined) GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml(m.html, infoWindowOpt); });
		
		return marker;
		
	},
	
	/**
	 * カスタムアイコン作成
	 * @todo	とりあえず影のパスやサイズ等は固定（変えたい場合はカスタマイズ）
	 * @param	src	:	アイコンのパス
	 * @return	{GIcon}
	 */
	createGIcon: function(src) {
		
		var icon = new GIcon(G_DEFAULT_ICON);
		icon.image = Kaas.root() + this.directory + src;
		icon.shadow = Kaas.root() + this.directory + 'img/gmap_shadow.png';
		icon.iconSize = new GSize(25, 32);
		icon.shadowSize = new GSize(25, 38);
		icon.iconAnchor = new GPoint(13, 32);
		
		return icon;
		
	},
	
	/**
	 * クリックイベント
	 * マップコントロールのハンドラとなる要素をクリックするとセンターを移動
	 * @param	i		:	mark のインデックス
	 * @param	marker	:	対応するマーカー
	 * @return	{Void}
	 */
	onSetCenter: function(i, marker) {
		
		var m = this.mark[i];
		if (!document.getElementById(m.id)) return;
		
		var g = this.gmap;
		
		var a = this.createCenterControlElement(i);
		a.i = i;
		
		anchor = this.anchorToGMap;
		
		a.onmouseover = function() {
			Kaas.addClass(this, 'mapControllerOver');
		};
		a.onmouseout = function() {
			Kaas.removeClass(this, 'mapControllerOver');
		};
		a.onclick = function() {
			
			g.panTo(new GLatLng(m.latlng[0], m.latlng[1]));
			
			
			if (m.html != undefined) {
				var w = this.infoWindowMaxWidth;
				var infoWindowOpt = g.getInfoWindow();
				infoWindowOpt.maxWidth = w;
				var timerId = setTimeout(function() {
					marker.openInfoWindowHtml(m.html, infoWindowOpt);
				}, 300);
			}
			// 
			if (anchor) {
				window.location.href = anchor;
				return false;
			}
			
		};
		
	},
	
	/**
	 * マップコントロールのハンドラとなる要素を作成
	 * @param	i	:	mark のインデックス
	 * @return	{Object}
	 */
	createCenterControlElement: function(i) {
		
		var m = this.mark[i];
		var div = document.createElement('div');
		div.id = m.id + 'mapController';
		div.className = 'mapController';
		div.innerHTML = this.mapControllerInnerHTML;
		
		return document.getElementById(m.id).appendChild(div);
		
	},
	
	/**
	 * body の class名と連動させて最初の表示位置を変える時
	 * @return	{Array}
	 */
	getCenter: function() {
		
		var bodyClassNames = document.getElementsByTagName('body')[0].className.split(' ');
		var bodyClassName = bodyClassNames[bodyClassNames.length - 1];
		var m = this.mark;
		
		for (var i = 0, l = m.length; i < l; i++) {
			if (m[i].page == bodyClassName) return m[i].latlng;
		}
		
	},
	
	/**
	 * 緯度・経度をユーザープロンプトで出力
	 * @param	address	:	緯度・経度を調べたい住所
	 * @return	{Void}
	 */
	getGeoCoder: function(address) {
		
		var geocoder = new GClientGeocoder();
		var g = this.gmap;
		
		geocoder.getLatLng(address, function(e) {
			if (e != null) {
				g.panTo(e);
				var str = e.toString().replace(/\(/, '');
				prompt(address, str.replace(/\)/, ''));
			} else {
				alert('指定した住所はありません\n' + address);
			}
		});
		
	},
	
	
	gmap: {}
	
};
