jsG				= new Object();
jsG.loaded  	= false;
jsG.circle		= false;
jsG.marks		= new Array();
arr_lmarks	= new Array();
jsG.hotels 	= null;
jsG.map		= null;
jsG.hno		= 0;
jsG.distance= 1000;
jsG.key		= ""; // sawadee.com
hotel_blue 	= new Object();
hotel_red  	= new Object();
landmarker 	= new Object();
arr_lmarks 	= new Array();
alllmarks	= new Array();

jsG.PanoStatus   = false;
jsG.pano		 = false;

jsG.wikiStatus   = false;
jsG.wiki		 = false;


jsG.LandmarkStatus  = false;
jsG.hotelStatus  	= false;
jsG.landmark		= false;
jsG.arrlm 			= new Array();	
jsG.arrH 			= new Array();


function onloading_map(){
  startGoogleMap();   	
}

jsG.host = document.domain ;

function setGoogleKey (host) {
	switch (host) {
		case "192.168.77.99" : jsG.key = "ABQIAAAA38Al4LfMy5OTS-FNy0FHrBRZd5YyCMGkSMgK0m3Tq8d9CW5lRBQLCUvAQVM95YAMwily7-qWG-h_yA";break;
		case "192.168.2.11"  : jsG.key = "ABQIAAAAJNTPOVVdDWX3Ab1J-K5npxRaCOl_N0WlA2IcvoDFRX1zz5eA_hSwcUc5HsrNjj0DI7A8uIZOFJDT5Q"; break;
		case "209.197.98.254":
		case "hno.th66.com"	 : jsG.key = "ABQIAAAAJNTPOVVdDWX3Ab1J-K5npxQVmSLRI28xYNWPt3t2xXF8gZwjQRSJxMCw9F4z5DLXkuIoj4IKB21WkQ"; break;
		//------NEW CASE -----//
		case "bangkok.th66.com"	 : jsG.key = "ABQIAAAAJNTPOVVdDWX3Ab1J-K5npxQUQbOUUy8nvcPOQHGb2g-aE6VOFhT_Ie3xns2ZMmYOyU3bfDLJH-mISw"; break;
		case "bangkok-city.com"	 : jsG.key = "ABQIAAAAJNTPOVVdDWX3Ab1J-K5npxSgsZkprL91DrooxHQk9HAiggllahRuA7GO8LEt7ezWCkHfcKJrOvftaA"; break;
		case "samui.th66.com"	 : jsG.key = "ABQIAAAAJNTPOVVdDWX3Ab1J-K5npxT9JiItxbJip_UR-i7QMvUAO5DpKRQ-iHkiHq7dkQNXDMU8wy2Ue-kG1A"; break;
		case "www.chaweng.com"	 	 : jsG.key = "ABQIAAAAJNTPOVVdDWX3Ab1J-K5npxT3FB9L3Za24fpeFFUtcqQHipGdKhTsHrO552oEPi_u8qKts_qwlC3x2Q"; break;
		default				 : jsG.key = "ABQIAAAAJNTPOVVdDWX3Ab1J-K5npxQB-qoDsNPXZ32zJljSeRAb6WeV2xTL6FOjOpLrpHhz1s_7ul0d-WjCpw"; break; 
	}						   /*   *.sawadee.com   */	
	return jsG.key;
}

setGoogleKey( jsG.host ) ;

function loadGoogleMap () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://maps.google.com/maps?file=api&v=2.x&key=" + jsG.key + "&async=2&callback=initializeMap";
    document.body.appendChild(script);
   	jsG.loaded = true;     
	window.onunload=function(){ GUnload(); }	

}

function startGoogleMap() {
	if (jsG.loaded) { initializeMap (); }  else { loadGoogleMap(); }
}
function paneTo( latlng ){ 		
			 if ( latlng == '9.51658 , 99.99131' ){ // bkk
			 	startGoogleMap();
			 }else{
					coor = latlng.split(' , ' );	
				  	var point = new GLatLng( coor[0] , coor[1] );
					var mark = new GMarker( point );
					jsG.map.addOverlay(mark);
					 
				  window.setTimeout(function() {
					  jsG.map.setZoom( 13 );
					  jsG.map.panTo(new GLatLng( coor[0] , coor[1]));
				}, 50);
				//==================//
				var txt =getID('samuizone')[getID('samuizone').selectedIndex].text ;
				GEvent.addListener(mark , 'click', function() {mark.openInfoWindowHtml(txt);});
				//==================//			 
			 }
}
function initializeMap () {
	if (GBrowserIsCompatible()) {
        jsG.map = new GMap2(document.getElementById("gmap"));
		jsG.map.addControl(new GOverviewMapControl());    
		jsG.map.disableScrollWheelZoom() ;
		hotel_blue=	new GIcon();
		hotel_blue.image 			=	"images/gmap/hotel_blue.png";
		hotel_blue.iconSize			=	new GSize(17,22);
		hotel_blue.iconAnchor		=	new GPoint(11,27);
		hotel_blue.infoWindowAnchor	=	new GPoint(11,3);

		hotel_red=	new GIcon();
		hotel_red.image 		=	"images/gmap/hotel_red.png";
		hotel_red.iconSize		=	new GSize(20,25);
		hotel_red.iconAnchor	=	new GPoint(14, 34);
		hotel_red.infoWindowAnchor=new GPoint(14,3);
		
	  

alllmarks = new Array('BTS', 'MRT','TRP','HIS','REL','MON','MUS','PRK','SHO','MAR','HOS','STA','SIT','NIT','FAM','THR','EXH','POI') ;
for( var i = 0 ; i < alllmarks.length  ;  i++ ){
		arr_lmarks[alllmarks[i]] = 'icon_' + alllmarks[i] + '.gif';
		landmarker[alllmarks[i]]=	new GIcon();
		landmarker[alllmarks[i]].image 	=	"images/gmap/landmark/" +  arr_lmarks[alllmarks[i]] ;
		landmarker[alllmarks[i]].iconSize	=	new GSize(20, 20);
		landmarker[alllmarks[i]].iconAnchor	=	new GPoint(14, 34);
}

			var pointer =  new GLatLng(9.51658 , 99.99131) ;
			if (pointer) jsG.map.setCenter(pointer, 12);
			jsG.map.addControl( new GMenuMapTypeControl() );
			jsG.map.addControl( new GLargeMapControl());
		 	Maping();
		 	
	    }
}
   
function createMarker(pointer,st,hname,iconType,imgHotel,imgHotels,hno_center ){
	if (pointer == null) return false;	
	var mark = new GMarker(pointer, { title : hname ,icon : iconType});	
	jsG.map.addOverlay(mark);	
	GEvent.addListener(mark , "click", function() {	 
			mark.setImage("images/gmap/hotel_red.png");
			mark.openInfoWindow(st);	 
	}); 	 
	return mark;
}
     
function Maping (){

	var mark,pointer,center,dist,st;
	var imgHotel ="images/gmap/hotel_green.png";
	var imgHotels="images/gmap/hotel_blue.png";

var wikiDiv 	= document.createElement("div");  
var panoDiv 	= document.createElement("div");  
var hotelDiv 	= document.createElement("div");  
var landmarkDiv = document.createElement("div");  
// =================BEGIN WIKIPEDIA===============	
function WikiControl() {}
WikiControl.prototype = new GControl();
WikiControl.prototype.initialize = function(map) {  
	this.setButtonStyle_(wikiDiv);  
	wikiDiv.appendChild(document.createTextNode("Wikipedia"));  
	GEvent.addDomListener( wikiDiv, "click", function() {  
			if( jsG.wikiStatus  == false  ) {
				wikiDiv.style.fontWeight = 'bold' ; 
				jsG.wikiStatus  =true;  
				jsG.wiki = new  GLayer("org.wikipedia.en") ;  
				jsG.map.addOverlay( jsG.wiki  ) ;
			}else{
				jsG.wikiStatus  =false  ;  
				jsG.map.removeOverlay(jsG.wiki );
				wikiDiv.style.fontWeight = 'normal' ; 
			}	
	 } );  
	jsG.map.getContainer().appendChild(wikiDiv );  return wikiDiv ;
}
WikiControl.prototype.getDefaultPosition = function() {  
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
}
WikiControl.prototype.setButtonStyle_ = function(button) {  
		button.style.textDecoration = "none";  
		button.style.color = "#000000";  
		button.style.backgroundColor = "white";  
		button.style.font = "12px Arial";  
		button.style.border = "2px solid black";  
		button.style.padding = "0px";  
		button.style.marginBottom = "3px";  
		button.style.marginLeft = "360px";  
		button.style.textAlign = "center";  
		button.style.width = "6em";   
 		button.style.cursor = "pointer"; 
}
// =================END WIKIPEDIA===============

// =================BEGIN PANORAMIO===============		
function PanoramioControl() {}
PanoramioControl.prototype = new GControl();
PanoramioControl.prototype.initialize = function(map) {  
	this.setButtonStyle_(panoDiv );  
	panoDiv.appendChild(document.createTextNode("Photos"));  
	GEvent.addDomListener(panoDiv , "click", function() {  
			if( jsG.PanoStatus  == false  ) {
				panoDiv.style.fontWeight = 'bold' ; 
				jsG.PanoStatus  =true;  
				jsG.pano = new  GLayer("com.panoramio.all") ; //org.wikipedia.en
				jsG.map.addOverlay( jsG.pano  ) ;
				
			}else{
				jsG.PanoStatus  =false  ;  
				jsG.map.removeOverlay(jsG.pano );
				panoDiv.style.fontWeight = 'normal' ; 

			}	
	 } );  
	jsG.map.getContainer().appendChild(panoDiv);  return panoDiv;
}
PanoramioControl.prototype.getDefaultPosition = function() {  
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
}
PanoramioControl.prototype.setButtonStyle_ = function(button) {  
		button.style.textDecoration = "none";  
		button.style.color = "#000000";  
		button.style.backgroundColor = "white";  
		button.style.font = "12px Arial";  
		button.style.border = "2px solid black";  
		button.style.padding = "0px";  
		button.style.marginBottom = "3px";  
		button.style.marginLeft = "600px";  
		button.style.textAlign = "center";  
		button.style.width = "5em";   
 		button.style.cursor = "pointer"; 
}
// =================END PANORAMIO===============
function hotelDisplay(){


			if( jsG.hotelStatus  == false  ) {
							jsG.hotelStatus =true;  
							hotelDiv.style.fontWeight = 'bold' ; 
					//*************[BEGIN ALL HOTEL IN KOH SAMUI ]************// 
					var ajax = new ajaxObject('gmap/aj_hotelUsm.php');		
					var param = 	"p=sawadee.com";	
					ajax.callback = function(responseText,responseXML,responseStatus) { 
							jsG.hotels = responseXML.getElementsByTagName("hotel"); 
							for (var i = 0; i < jsG.hotels.length; i++) {  
								jsG.marks[i] = new Array();
								var hotel = jsG.hotels[i];
								jsG.marks[i][0] = hotel.getElementsByTagName("hno")[0].firstChild.data;
								jsG.marks[i][1] = hotel.getElementsByTagName("region")[0].firstChild.data;
								jsG.marks[i][2] = hotel.getElementsByTagName("userId")[0].firstChild.data;
								jsG.marks[i][3] = hotel.getElementsByTagName("hotelname")[0].firstChild.data;
								jsG.marks[i][4] = hotel.getElementsByTagName("hoteladr")[0].firstChild.data;
								jsG.marks[i][5] = hotel.getElementsByTagName("hotelcity")[0].firstChild.data;
								jsG.marks[i][6] = hotel.getElementsByTagName("geo_lat")[0].firstChild.data;
								jsG.marks[i][7] = hotel.getElementsByTagName("geo_long")[0].firstChild.data;
								jsG.marks[i][8] = "http://TH.R24.org/R24thumb.php?h=" + jsG.marks[i][0] + "&x=120&y=100"; // HOTEL'S LOGO
								jsG.marks[i][9] = "http://www.sawadee.com/hotel/"+jsG.marks[i][1]+"/"+jsG.marks[i][2] ;
						var pointer =( jsG.marks[i][6] )? new GLatLng(jsG.marks[i][6], jsG.marks[i][7]) : null;
						
						var st = "<a target='_blank' href='" + jsG.marks[i][9] + "'><img alt='" + jsG.marks[i][0] + "' title='" + jsG.marks[i][3] + "' src=" + jsG.marks[i][8] + " style='float:left;margin-right:10px'></img></a><b>" + jsG.marks[i][3] + "</b><br/><br/>" + jsG.marks[i][4] + ", " + jsG.marks[i][5] + "<br/><br/><br/>&nbsp;<br/>&nbsp;";
						
						var mark = createMarker( pointer,st,jsG.marks[i][3],hotel_red,"","",jsG.marks[i][0]);
						
						jsG.arrH.push( mark ) ;
			      		}
				   	};
				   	ajax.update(param,"GET");
				  	//*************[END ALL HOTEL IN KOH SAMUI ]************// 
			}else{
						 	jsG.hotelStatus =false  ;  
							hotelDiv.style.fontWeight = 'normal' ; 
							for( var ii = 0 ; ii < jsG.arrH.length ; ii++ )
			 					 jsG.map.removeOverlay( jsG.arrH[ii] ) ;
			}	
							  	
}	

function hotelControl() {}
hotelControl.prototype = new GControl();
hotelControl.prototype.initialize = function(map) {  
	//var container = document.createElement("div");  
	this.setButtonStyle_(hotelDiv );  
	//container.appendChild(hotelDiv );  
	hotelDiv.appendChild(document.createTextNode("Hotels"));  
	GEvent.addDomListener(hotelDiv, "click", function() {hotelDisplay()} );  
	jsG.map.getContainer().appendChild(hotelDiv);  return hotelDiv;
}
hotelControl.prototype.getDefaultPosition = function() {  
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
}
hotelControl.prototype.setButtonStyle_ = function(button) {  
		button.style.textDecoration = "none";  
		button.style.color = "#000000";  
		button.style.backgroundColor = "white";  
		button.style.font = "12px Arial";  
		button.style.border = "2px solid black";  
		button.style.padding = "0px";  
		button.style.marginBottom = "3px";  
		button.style.marginLeft = "440px";  
		button.style.textAlign = "center";  
		button.style.width = "6em";   
 		button.style.cursor = "pointer"; 
}


// ================BEGIN OF LANDMARK======================		


function LandmarkControl() {}
LandmarkControl.prototype = new GControl();
LandmarkControl.prototype.initialize = function(map) {  
	this.setButtonStyle_(landmarkDiv );  
	landmarkDiv.appendChild(document.createTextNode("Landmarks"));  
	GEvent.addDomListener(landmarkDiv , "click", function() {LandmarkControl.prototype.landmarkDisplay()} );  
	jsG.map.getContainer().appendChild(landmarkDiv);  return landmarkDiv;
}
LandmarkControl.prototype.getDefaultPosition = function() {  
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
}
LandmarkControl.prototype.setButtonStyle_ = function(button) {  
		button.style.textDecoration = "none";  
		button.style.color = "#000000";  
		button.style.backgroundColor = "white";  
		button.style.font = "12px Arial";  
		button.style.border = "2px solid black";  
		button.style.padding = "0px";  
		button.style.marginBottom = "3px";  
		button.style.marginLeft = "520px";  
		button.style.textAlign = "center";  
		button.style.width = "6em";   
 		button.style.cursor = "pointer"; 
}
LandmarkControl.prototype.landmarkDisplay = function(){
			if( jsG.LandmarkStatus  == false  ) {
				jsG.LandmarkStatus=true;  
				landmarkDiv.style.fontWeight = 'bold' ; 
				var params = "province=63a"  ;			 
			 	var ajax = new ajaxObject('gmap/aj_landmark.php'); if (!ajax) return false;
				ajax.callback = function(responseText,responseXML,responseStatus) {
				var	xmlLandmark= responseXML.getElementsByTagName('landmark');
					for( i = 0 ; i < xmlLandmark.length ; i++ ) {
						var coord 	= xmlLandmark[i].attributes.getNamedItem('area').value;
						var msg 	= xmlLandmark[i].firstChild.nodeValue;
						if( coord ){
								var arr_coord  = coord.split('|') ;
								var point =arr_coord[0]? new GLatLng(arr_coord[0],arr_coord[1]) : null;		
								if (point == null) return false;
								
								var mark = new GMarker( point , { title : msg , icon : eval('landmarker.'+arr_coord[2]) });
							 
								jsG.map.addOverlay(mark);
								jsG.arrlm.push( mark ) ;
						}
					}
				}; 
				ajax.update(params,"GET"); // fire ajax when ready...			 
			}else{
			 	jsG.LandmarkStatus  =false  ;  
				landmarkDiv.style.fontWeight = 'normal' ; 
				for( var ii = 0 ; ii < jsG.arrlm.length ; ii++ )
 					 jsG.map.removeOverlay( jsG.arrlm[ii] ) ;
			}	
}	


// ================END OF LANDMARK================
	
// =================BEGIN KOH SAMUI ZONE============
var data = "<select id='samuizone' name='samuizone' style='margin-bottom:5px;font-size:12px;width:130px;' onchange='paneTo(this.value);'><option value='9.51658 , 99.99131' >Koh Samui zone</option> <option value='9.5277 , 100.0616'>Chaweng Beach</option> <option value='9.4628 , 100.0450'>Lamai Beach</option> <option value='9.5636 , 100.0226'>Bophut Beach</option> <option value='9.5764 , 99.9909'>Maenam Beach</option> <option value='9.5585 , 100.0561'>Big Buddha Beach</option> <option value='9.5723 , 100.0816'>Choengmon Beach</option> <option value='9.5769 , 99.9489'>North Coast</option> <option value='9.4980 , 100.0603'>East Coast</option><option value='9.4928 , 99.9291'>West Coast</option> <option value='9.4200 , 99.9765'>South Coast</option></select>" ;function ZoneControl() {}
ZoneControl.prototype = new GControl();
ZoneControl.prototype.initialize = function(map) {  
	var container = document.createElement("div");  
	this.setButtonStyle_(container );  
	container.innerHTML = data ;
	jsG.map.getContainer().appendChild(container);  return container;
} 
ZoneControl.prototype.getDefaultPosition = function() {  
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
}
ZoneControl.prototype.setButtonStyle_ = function(button) {  
		button.style.textDecoration = "none";  
		button.style.color = "#000000";  
		button.style.font = "12px Arial";  
		button.style.padding = "0px";  
		button.style.marginBottom = "0px";  
		button.style.marginLeft = "70px";  
		button.style.textAlign = "center";  
 		button.style.cursor = "pointer"; 
}
// =================END KOH SAMUI ZONE============
		// add control  //
		jsG.map.addControl(  new PanoramioControl() );
		jsG.map.addControl(  new WikiControl() );
		jsG.map.addControl(  new LandmarkControl() );
		jsG.map.addControl(  new ZoneControl() );
		jsG.map.addControl(  new hotelControl() );
		LandmarkControl.prototype.landmarkDisplay() ; //
		return;
} // END MAPPING