//<![CDATA[  
var map;
var mapMk;
var present = null;   
var geocoder=null;
var zoomIn=16;	// ขนาด Zoom ตอนแสดง
var zoomOut=9;	// ขนาด Zoom ภาพกว้าง
// lat และ lng ของประเทศไทย
var iniLat=13.75;		
var iniLng=100.517;

var markers;	 // ตำแหน่ง maker
var cicon = []; // custom icon

var disMarkers=[];
var disMarkersNum=0;

function initial_Icon(){
var baseIcon = new GIcon();
	baseIcon.iconSize=new GSize(32,32);
	baseIcon.shadowSize=new GSize(32,32);
				  //baseIcon.shadowSize=new GSize(56,32);
	baseIcon.iconAnchor=new GPoint(16,32);
	baseIcon.infoWindowAnchor=new GPoint(16,0);

	cicon['house'] = new GIcon(baseIcon, "../images/icon_map/icon_house.png", null, "../images/icon_map/icon_houses.png");  	
	cicon['townhouse'] = new GIcon(baseIcon, "../images/icon_map/icon_townhouse.png", null, "../images/icon_map/icon_townhouses.png");  	
	cicon['twinhouse'] = new GIcon(baseIcon, "../images/icon_map/icon_twinhouse.png", null, "../images/icon_map/icon_twinhouses.png");  	
	cicon['condo'] = new GIcon(baseIcon, "../images/icon_map/icon_condo.png", null, "../images/icon_map/icon_condos.png");  	
	cicon['apartment'] = new GIcon(baseIcon, "../images/icon_map/icon_apartment.png", null, "../images/icon_map/icon_apartments.png");  	
	cicon['land'] = new GIcon(baseIcon, "../images/icon_map/icon_land.png", null, "../images/icon_map/icon_lands.png");  	
	cicon['space'] = new GIcon(baseIcon, "../images/icon_map/icon_space.png", null, "../images/icon_map/icon_spaces.png");  	
	cicon['building'] = new GIcon(baseIcon, "../images/icon_map/icon_building.png", null, "../images/icon_map/icon_buildings.png");  	
	cicon['hotel'] = new GIcon(baseIcon, "../images/icon_map/icon_hotel.png", null, "../images/icon_map/icon_hotels.png");  	
	cicon['other'] = new GIcon(baseIcon, "../images/icon_map/icon_other.png", null, "../images/icon_map/icon_others.png");  
}


function updateHiddenFields(marker) {   
    latlng = marker.getLatLng();   
        document.aspnetForm.ctl00$content_main$txt_lat.value= latlng.lat();   
        document.aspnetForm.ctl00$content_main$txt_lng.value=latlng.lng();   
}   
  
    function createTopicMarker(point,adsid,html,iconname)
    {
       var options = { icon: cicon[iconname], title:" "+adsid+" ", draggable: false };
       var marker = new GMarker(point, options);
		disMarkers[disMarkersNum]=marker;
		disMarkersNum++;

       GEvent.addListener(marker, "click", function(overlay, latlng) {
         marker.openInfoWindowHtml(html);
       });
       return marker;
    }

function removeMarker(){
	if (markers!=null)
	{
	map.removeOverlay(markers);
	present=null;
	}
}

function addMarker(latlng) {   
    if (!present){
		if (confirm("Are you sure you want to Add address to the Map !!") == true)  {
			present=true;
			var newMarker = new GMarker(latlng, { draggable: true, bouncy: false});    
			map.addOverlay(newMarker);         
			markers=newMarker;	// กำหนด maker

			GEvent.addListener(newMarker, "dragend", function() {   
				updateHiddenFields(newMarker);   
			});        
	  
			updateHiddenFields(newMarker);   
			map.panTo(latlng);   
		}
	}
}  

        function showAddress(address) {
            if (geocoder) {
                geocoder.getLatLng(address, function(point) {
					  if (!point) {
						  alert(address + " not found");
					  } else {
						  map.panTo(point);   
						  map.setCenter(point, zoomIn);
						  removeMarker();
					  }
					 }
				);
            }
        }

	function loadMap() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("getMap"),{ size: new GSize(550,300) });
		var center = new GLatLng(iniLat , iniLng);
        map.setCenter(center, zoomIn);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
//		map.addControl(new GOverviewMapControl());
        map.enableDoubleClickZoom();
        
    // Add click
            GEvent.addListener(map, "click", function(overlay, latlng) {   
					if (!overlay) {  
						addMarker(latlng);   

					}else{
						removeMarker();
					}
				});  
		geocoder = new GClientGeocoder();

      }
    }

    function loadMakerMap(lat,lng) {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("getMap"));
//		var center = new GLatLng(iniLat , iniLng);
  //      map.setCenter(center, zoomIn);
		if (lat==0) lat=iniLat;
		if (lng==0) lng=iniLng;

        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
//		map.addControl(new GOverviewMapControl());
        map.enableDoubleClickZoom();

		var point = new GLatLng (lat, lng); 	
		markers= new GMarker(point, {draggable: true , bouncy: false});           
		map.addOverlay(markers);	 
                 GEvent.addListener(markers, "dragend", function() {
					point = marker.getLatLng();
                   updateHiddenFields(markers);   
                 });
			map.panTo(point);   
			map.setCenter(point, zoomIn);
			updateHiddenFields(markers);   
    // Add click
            GEvent.addListener(map, "click", function(overlay, latlng) {   
					if (!overlay) {  
						addMarker(latlng);   

					}else{
						removeMarker();
					}
				});  
			geocoder = new GClientGeocoder();
      }

    }

	function viewMap(zoom){
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("getMap")); 
		var center = new GLatLng(iniLat , iniLng);
        map.setCenter(center, zoom);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
//		map.addControl(new GOverviewMapControl());
        map.enableDoubleClickZoom();
		
		initial_Icon();
		mapMk = new GMarkerManager(map, {maxZoom:17});
		AddTopicMarker(map,zoom); 
		geocoder = new GClientGeocoder();
      }
	}


function clearMarkers() { 
//	if (clickPan !=null) alert(clickPan.lat() + " , " + clickPan.lng());
/*
		var latlng = disMarkers[i].getLatLng();   
		if (clickPan !=null) {
			if (latlng.lat() !=clickPan.lat() && latlng.lng() !=clickPan.lng())
			{
				map.removeOverlay(disMarkers[i]);
			}
		}else{
			map.removeOverlay(disMarkers[i]);
		}
*/

	for (var i=0;i<disMarkersNum ; i++){
		map.removeOverlay(disMarkers[i]);
	}	

 } 

// abstract function
	function AddTopicMap_ajax(map,func){
       // clearMarkers();
        disMarkersNum=0;
		disMarkers.clear;
		func(map);
	}



      function showAddress_ajax(address,zoom,catid) {
           if (geocoder) {
                geocoder.getLatLng(address, function(point) {
					  if (!point) {
						  alert(address + " not found");
					  } else {
						  map.panTo(point);   
						  map.setCenter(point, zoom);
							ajax_updateMap(zoom,catid);	// Update Maker
					  }
					 }
				);
            }
        }

	function viewMap_ajax(zoom,catid){
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("getMap")); 
		var center = new GLatLng(iniLat , iniLng);
        map.setCenter(center, zoom);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
//		map.addControl(new GOverviewMapControl());
        map.enableDoubleClickZoom();
		
		initial_Icon();
		geocoder = new GClientGeocoder();
// แสดงส่วน Add Maker
		ajax_updateMap(zoom,catid);	// Update Maker

//		map.enableDoubleClickZoom();
		
		

		// ajax 
		/*
			GEvent.addListener(map, "moveend", function() {   
				ajax_updateMap(zoom,catid);	// Update Maker
			});        
			GEvent.addListener(map, "zoomend", function() {   
				ajax_updateMap(zoom,catid);	// Update Maker
			});       
		*/
      }
	}

	function ajax_updateMap(zoom,catid){
		//	map.clearOverlays();	// remove all maker
			var bounds = map.getBounds(); 
			var southWest = bounds.getSouthWest(); 
			var northEast = bounds.getNorthEast(); 
			var mapBoundary = '?zoom='+zoom+'&cat_id='+catid+'&ne=' + northEast.toUrlValue() + '&sw=' + southWest.toUrlValue()+"&"+new Date().getTime() + Math.random();
					// load map
						AjaxLoad("../java/AddTopicMap_ajax.aspx"+mapBoundary,"GET",null,function(objXML){
								if (objXML.readyState==4){ 
									if (objXML.status==200){ 
										eval(objXML.responseText);
									}else{
										alert(objXML.statusText);
									}
								}
						});
	}

//]]>
