//SearchSpot.php縮略小地圖
function LoadMap(lng,lat,url,elementID) {
	  var map;
      var icon = new GIcon();
	  icon.image="/images/google-icon-37.png";
      icon.iconSize = new GSize(37, 35);
      icon.iconAnchor = new GPoint(15, 15);
      icon.shadowSize = new GSize(40, 50);
      icon.shadow = "";

      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById(elementID));
	    map.addControl(new GSmallZoomControl());
        map.setCenter(new GLatLng(lng, lat), 11);
        map.disableDragging();
        map.setMapType(G_NORMAL_MAP);
        var marker = new GMarker(new GPoint(lat,lng), icon);
        map.addOverlay(marker);

	GEvent.addListener(marker, "click", function() {
	    window.open(url,"_self","");
	});

        GEvent.addListener(map, "click", function(overlay,point) {
          if (overlay == null || !(overlay instanceof GMarker)) {
			 window.open(url,"_self","");
          }
        });
      }
	 }


//DetailSpot.php縮略小地圖
    function DetailSpotLoadMap(lng,lat,zoom,url,elementid) {
		var map;
		var icon = new GIcon();
		icon.iconSize = new GSize(37, 35);
		icon.iconAnchor = new GPoint(7, 38);
		icon.image = "/images/google-icon-37.png";
		icon.shadowSize = new GSize(40, 50);
		icon.shadow = "";

      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById(elementid));
	    map.addControl(new GSmallZoomControl());
        map.setCenter(new GLatLng(lng, lat), zoom);
        map.disableDragging();
        map.setMapType(G_NORMAL_MAP);
        var marker = new GMarker(new GPoint(lat,lng), icon);
        map.addOverlay(marker);

      GEvent.addListener(marker, "click", function() {

		//window.open(url,"_blank","");
		window.open(url, '', 'width=850, height=740, left = 500,top = 0, toolbar= yes, menubar=yes, scrollbars=yes, resizable=yes, location=yes, status=yes,');
	});

        GEvent.addListener(map, "click", function(overlay,point) {
          if (overlay == null || !(overlay instanceof GMarker)) {
			//window.open(url,"_blank","");
			window.open(url, '', 'width=850, height=740, left = 500,top = 0, toolbar= yes, menubar=yes, scrollbars=yes, resizable=yes, location=yes, status=yes,');
          }
        });
		
		var street = document.getElementById("streetView");
		var myPano = new GStreetviewPanorama(street);
		var svOverlay = new GStreetviewOverlay();
		var client = new GStreetviewClient();  
		
		//map.addOverlay(svOverlay);
		client.getNearestPanoramaLatLng(new GLatLng(lng,lat),function(value){
			if (value){
				street.style.display='';
				pov = {zoom:1};
				myPano.setLocationAndPOV(value,pov);
			}
			else{
				street.style.display='none';
			}
		});
      }
	 }


//放大的google map
    function LoadLargerMap(countryID,lat,long,v,streetLat,streetLong) {
      if (GBrowserIsCompatible()) {
	  	var mapblock=document.getElementById("map");
        var map= new GMap2(mapblock);
        map.setCenter(new GLatLng(long, lat), v);
		//map.setUI(map.getDefaultUI());
		var iconP=new GIcon();
		//iconP.shadow圖標的陰影圖像 URL
		iconP.shadow="";
		iconP.image="images/google-icon-45.png";
		iconP.iconSize=new GSize(45,42);
		iconP.iconAnchor=new GPoint(15,40);

		markerOptions = { icon:iconP };
		var latlng = new GLatLng(long, lat);
		map.addOverlay(new GMarker(latlng, markerOptions));
		if(mapblock.getAttribute("control")=='large')
		map.addControl(new GLargeMapControl());
		else if(mapblock.getAttribute("control")=='small')
		map.addControl(new GSmallMapControl());
		if(mapblock.getAttribute("dragging")=='no')
		map.disableDragging();
		if(mapblock.getAttribute("minimap")=='yes'){
			map.addControl(new GOverviewMapControl(new GSize(200,170)));
		}
		if(mapblock.getAttribute("mapUrl")){
			GEvent.addListener(map,"click",function(){window.open(document.getElementById("map").getAttribute("mapUrl"),'_self');});}
		if(mapblock.getAttribute("typecontrol")){
			map.addControl(new GMapTypeControl());
		}
		if(mapblock.getAttribute("followme")){
			window.followMe();
			var fm=document.getElementById("map");
			if(fm)fm.style.display="block";fm=null;
		}
		
		/*if (countryID){
			for (i=0; i<streetLat.length; i++){
				streetView(countryID,"streetView"+(i+1),streetLong[i],streetLat[i],map);
			}
		}*/
		//else{
			streetView(countryID,"streetView",lat,long,map);
		//}
      }
    }
	
	//Google Map Street view
	function streetView(countryID,id,lat,long,map){
	    var street = document.getElementById(id);
		var streetView_spot = document.getElementById(id+"_spot");
		var myPano = new GStreetviewPanorama(street);
		var svOverlay = new GStreetviewOverlay();
		var client = new GStreetviewClient();  
		
		client.getNearestPanoramaLatLng(new GLatLng(long,lat),function(value){
			if (value){
				street.style.display='';
				if (countryID) streetView_spot.style.display='';
				myPano.setLocationAndPOV(value);
			}
			else{
				street.style.display='none';
			}
		});
		
		
		var iconP = new GIcon();
		iconP.shadow="";
		iconP.image="images/icon_googlemap_0401.png";
		iconP.iconSize=new GSize(32,34);
		iconP.iconAnchor=new GPoint(32,34);

		markerOptions = {icon:iconP,clickable:true,draggable:true};
		var latlng = new GLatLng(long, lat);
		var marker = new GMarker(latlng,markerOptions);
		map.addOverlay(marker);
		
		//Mouse over function
		GEvent.addListener(marker,"mouseover",function(){
			if (countryID){ 
				/*if (id == "streetView1") color = "red";
				if (id == "streetView2") color = "yellow";
				if (id == "streetView3") color = "green";*/
				color="red";
				street.style.border='3px solid '+color;
			}
		});
		
		GEvent.addListener(marker,"mouseout",function(){
			if (countryID) street.style.border="3px solid white";
		});
		//End mouse over function
		
		//Marker click and drag
		GEvent.addListener(marker,"dragstart",function(){
			map.addOverlay(svOverlay);
			if (countryID){ 
				/*if (id == "streetView1") color = "red";
				if (id == "streetView2") color = "yellow";
				if (id == "streetView3") color = "green";*/
				color="red";
				street.style.border='3px solid '+color;
			}
		});
		
		GEvent.addListener(marker,"dragend", function(latlng) {
			if (countryID) streetView_spot.style.display='none';
			map.removeOverlay(svOverlay);
			if (countryID){ 
				/*if (id == "streetView1") color = "red";
				if (id == "streetView2") color = "yellow";
				if (id == "streetView3") color = "green";*/
				color="red";
				street.style.border='3px solid '+color;
			}
			client.getNearestPanoramaLatLng(latlng,function(value){
				if (value){
					street.style.display='';
					myPano.setLocationAndPOV(value);
				}
				else{
					street.style.display='none';
				}
			});
		});
		//End marker click and drag
		
		//Map clicked
		GEvent.addListener(map,"click", function(overlay,latlng) {
			if (countryID) streetView_spot.style.display='none';
			client.getNearestPanoramaLatLng(latlng,function(value){
				if (value){
					street.style.display='';
					myPano.setLocationAndPOV(value);
				}
				else{
					street.style.display='none';
				}
			});
			marker.setLatLng(latlng);
		});
	}
