// JavaScript Document

function loadGmap( selector,hotelLat,hotelLng,hotelName,ico ){
	var options = {
		markers:[{
			nam:hotelName,
			lat:hotelLat,
			lng:hotelLng,
			ico: ico?ico:'hotel'
			}]
	};
	return loadGmap2(selector, options);
}

/**
options = {
	center:{lat:,lng:},
	zoomLevel:13,
	markers:[{
		nam:,
		lat:,
		lng:,
		ico:,
		lnk:
	}]
};
*/
function loadGmap2( selector, options, bOpenInfoWindow, cbProHtml ){
	var zIndex = 10000 ;
	//3、设置google 地图
	if (GBrowserIsCompatible()) {
		var map = new GMap2($j(selector)[0]);
		
		//设置最小的分辨率，8城市
		var mts = map.getMapTypes();
		for(var i=0;i<mts.length;i++)
			mts[i].getMinimumResolution=function(){return 8;}
			
		//启用连续缩放、启用鼠标滚轮缩放
		map.enableContinuousZoom();
		
		//加入缩放、位置移动控件
		map.addControl(new GLargeMapControl(),new GControlPosition(G_ANCHOR_TOP_LEFT)); 
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl(),new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(0,10)));		//地图中心
		
		var zoomLevel = 13 ;
		if( options.zoomLevel ) {
			zoomLevel =  parseInt(options.zoomLevel) ;
		}

		var gcenter = false ;
		if( options.center ) {
			var fcLat = parseFloat(options.center.lat) ;
			var fcLng = parseFloat(options.center.lng) ;
			if( Math.abs(fcLat) > 2 && Math.abs(fcLng) > 2 ) {
				var latlng = new GLatLng(fcLat,fcLng );
				map.setCenter(latlng, zoomLevel); 
				map.savePosition();
				gcenter = true ;
			}
		}
		
		if( options.markers ) {
			var gmarkers = new Array();
			for( var i=0; i<options.markers.length; i++ ) {
				var marker = options.markers[i] ;
				if(marker.nam) {
					var latlng = new GLatLng(marker.lat, marker.lng );
					if(!gcenter) {
						map.setCenter(latlng, zoomLevel); 
						map.savePosition();
						gcenter = true ;
					}

					//放置图标
					var gmarker = new GMarker(latlng,{'icon': getIcon( marker.ico ),title:marker.nam,zIndexProcess:function(){return zIndex--},clickable:true});
					map.addOverlay(gmarker); 
					if( bOpenInfoWindow == true ) {
						gmarker._UserIndex = gmarkers.length ;
						gmarkers[gmarker._UserIndex] = gmarker ;
						GEvent.addListener( gmarker, "click", function() {   
							if( $j('#showGmapmarkerLi_'+this._UserIndex).length > 0 )
							this.openInfoWindowHtml( $j('#showGmapmarkerLi_'+this._UserIndex).html() );

							if( typeof(cbProHtml) == 'function' ) {
								this.openInfoWindowHtml( cbProHtml( $j('#showGmapmarkerLi_'+this._UserIndex).html() ) );
							}
							else {
								this.openInfoWindowHtml( $j('#showGmapmarkerLi_'+this._UserIndex).html() );
							}
						});
					}
				}
			}
			if( bOpenInfoWindow == true ) {
				$j(selector).data( 'gmarkers', gmarkers );
			}
		}
		GEvent.addListener(map, 'dragstart', function(){			
			var nodes = $j(selector)[0].getElementsByTagName('a') ;
			for( var i=nodes.length-1; i>0; i-- ){
				if( nodes[i].title.indexOf( '点击可在 Google 地图上参看该区域' ) >= 0 || nodes[i].innerHTML.indexOf( '使用条款' ) >= 0) {
					nodes[i].innerHTML = nodes[i].title = '' ;
					nodes[i].href = '#' ;
				}		
			}
			var nodes = $j(selector)[0].getElementsByTagName('span') ;
			for( var i=nodes.length-1; i>0; i-- ){
				if( nodes[i].innerHTML.indexOf( '?' ) >= 0 ) {
					//nodes[i].innerHTML = '' ;
				}			
			}	
		}) ;
		return map ;
	}
		
}


function getIcon( ico ){
	var icon = new GIcon();
	if( ico == 'hotel' ) {
		icon.image = "/img/ico_gmaps_hotel.png";
		icon.iconSize = new GSize(20, 24);	//图片 resize的大小
		icon.shadow = "/img/ico_gmaps_hotel_s.png";
		icon.shadowSize = new GSize(32, 24);	//阴影 resize的大小
		icon.iconAnchor = new GPoint(10, 24);	//地图上指定的点对应图标上的左上角，如果需要偏移图片，设定此值，如把图片向左移一半，向上移全部（22/2,29）相对于图片(22, 29)
		icon.infoWindowAnchor = new GPoint(15, 3);
	}
	else if( ico == 'metro' || ico == 'location' ) {
		icon.image = "/img/ico_gmaps_" + ico + ".gif";
		icon.iconSize = new GSize(32, 32);	//图片 resize的大小
		icon.shadow = "/img/ico_gmaps_metro_s.png";
		icon.shadowSize = new GSize(48, 32);	//阴影 resize的大小
		icon.iconAnchor = new GPoint(16, 32);	//地图上指定的点对应图标上的左上角，如果需要偏移图片，设定此值，如把图片向左移一半，向上移全部（22/2,29）相对于图片(22, 29)
		icon.infoWindowAnchor = new GPoint(15, 3);
	}
	else if( ico && (parseInt(ico) > 0 || ico.substr(0,1)=='g' && parseInt(ico.substr(1)) > 0) ){
		icon.image = "/img/ico_gmaps_" + ico + ".gif";
		icon.iconSize = new GSize(16, 20);	//图片 resize的大小
		icon.shadow = "/img/ico_gmaps_s.png";
		icon.shadowSize = new GSize(32, 20);	//阴影 resize的大小
		icon.iconAnchor = new GPoint(8, 20);	//地图上指定的点对应图标上的左上角，如果需要偏移图片，设定此值，如把图片向左移一半，向上移全部（22/2,29）相对于图片(22, 29)
		icon.infoWindowAnchor = new GPoint(10, 3);
	}
	else {
		return G_DEFAULT_ICON ;
	}
	return icon ;
}

