var allMarkers  = [];
	var map;
	var default_lat='13.759394168442002';
	var default_lng='100.48233032226562';
	var default_zoom=11;
	var infowindowOpen=true;
	
	function initMap() {
	 // if (GBrowserIsCompatible()) {
	    map = new GMap2(document.getElementById("map"));
	    map.setCenter(new GLatLng(default_lat,default_lng),default_zoom);
		map.addControl(new GMapTypeControl());
		map.addControl(new GLargeMapControl3D());
		map.enableScrollWheelZoom();
	
		GEvent.addListener(map,'moveend',moveEnd);
		GEvent.addListener(map, "infowindowclose", function() {infowindowOpen=true;});
		GEvent.addListener(map, "infowindowopen", function() {infowindowOpen=false;});
		ShowMarkers();
	}
	



function removeAllMarker(){

		map.clearOverlays();
	}
	
	function ShowMarkers() {
		removeAllMarker();
		
		var bounds = map.getBounds();
			var southWest = bounds.getSouthWest();
			var northEast = bounds.getNorthEast();
			var nlng=northEast.lng();
			var nlat=northEast.lat();
			var slng=southWest.lng();
			var slat=southWest.lat();
			var mzoom=map.getZoom();
			
		var url ='_xml.php?nlng='+nlng+'&nlat='+nlat+'&slng='+slng+'&slat='+slat+'&zoom='+mzoom;
		GDownloadUrl(url, function(data) {
       		var xml = GXml.parse(data);
			var property = xml.documentElement.getElementsByTagName('locations');
			  for (var i = 0; i < property.length; i++) 
			  {
				 var id 	= property[i].getAttribute('id');
				 var category 		= property[i].getAttribute('category');
				 var title 			= property[i].getAttribute('title');
				 var lat 			= property[i].getAttribute('lat');
				 var lng 			= property[i].getAttribute('lng');
				 var zoom 			= property[i].getAttribute('zoom');
				 var url 			= property[i].getAttribute('url');
				 var img 			= property[i].getAttribute('img');
				 
				 var html		 =  '<div>'+img+'<h3>'+title+'</h3>'+
				 					'<a href="'+url+'" target="_blank" >detail</a>'
									'</div>';

							 
				 var icon = new GIcon();
				 	   icon.image = "mods/map/images/category"+category +".png";
					   icon.shadow = null;
					   icon.iconSize = new GSize(32, 32);
					   icon.iconAnchor = new GPoint(10, 30);
					   icon.infoWindowAnchor = new GPoint(5, 1);
				 placeMarker(id,new GLatLng(lat,lng),html,icon,title);  
			   }
			});
	}
	
	
	/**
	 * Place Marker
	 * @param {Latlngt} latlng
	 * @param {HTML} html
	 */
	function  placeMarker(id,latlng,html,icon,name) {
		  var marker = new GMarker(latlng,{icon:icon,title:name});
		  allMarkers[id]=marker;
		  map.addOverlay(marker);
  		  GEvent.addListener(marker, "click", function() {	
  		 		marker.openInfoWindowHtml(html);
		  });
 		return marker;
	}
	
	
	function moveEnd(){
		//var ozoom = document.getElementById("zoom");
		//ozoom.value=map.getZoom();
		if(infowindowOpen){
		ShowMarkers();
		}
	}
	
	function getAddress(overlay, latlng) {
	  if (latlng != null) {
		address = latlng;
		geocoder.getLocations(latlng, showAddress);
	  }
	}
	function showAddress(response) {
	  map.clearOverlays();
		  if (!response || response.Status.code != 200) {
			alert("Status Code:" + response.Status.code);
		  }
		  else {
			place = response.Placemark[0];
			point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
			marker = new GMarker(point);
			map.addOverlay(marker);
			var mapAddress = document.getElementById("address");
			mapAddress=place.address;
		  }
	}
	
	
	
	 function searchLocation() {
      if (geocoder) {
		  
		  var university= document.getElementById('university');
		  var address= document.getElementById('address');
		  var soi= document.getElementById('soi');
		  var street= document.getElementById('street');
		  var province= document.getElementById('province');
		  var tambon= document.getElementById('tambon');
		  var amphoe= document.getElementById('amphoe');
		  
		  var textSeart=province.options[province.selectedIndex].text;
		  
		  /*
		  if(university.options[university.selectedIndex].text==check){
			textSeart=soi.value+' '+street.value+' '+tambon.value+' '+amphoe.value+' '+province.options[province.selectedIndex].text;
		  }else{
		  	textSeart=university.options[university.selectedIndex].text;
		  }
		  */
		 geocoder.getLatLng(textSeart,
          function(point) {
            if (!point) {
             // alert(Country + " not found");
            } else {
              map.setCenter(point,map.getZoom());
			  marker.setLatLng(map.getCenter());
            }
          }
		);
	}
  } 