/* Copyright (c) 2007 In1 Solutions Inc.
 *
 * Author: Ruiqing Liang
 * June 2007
 */
 	var loadingmessage = null;
 	var isIE = false;
	var isAll = true; // until region marker has been clicked
    var map = null; // map object
	//var mgr = null; // marker manager for Region/County level, used for control zoom level etc.
	var mgr1 = null; // marker manager for Town level (only for Ireland)
	var mgr2 = null; // marker manager for Hotels
	var mgrOption = {borderPadding: 50, maxZoom: 15, trackMarkers: false};
	var myXHR; // XMLHttpRequest for getting XML data of Hotels 
	//var rXHR; // XMLHttpRequest for getting XML data of Regions
	var tXHR; // XMLHttpRequest for getting XML data of Town
	//var defaultPath = "../"; //for local
	var defaultPath = "/default-ear/dublinhotelsin1-war/"; // for DublinHotelsin1
	var imagePath = "http://www.irelandin1.com/images/newirelandin1_images/images/hotels/img_"; //"BOF/1.jpg"
	var bookBtnLink = "http://www.bookin1.com/portal/in1.jsp?id=";
	var dispWin = null;
	var currCounty = "Dublin";
	
	var markersInMgr = []; // hold currenty markers in the mgr2
	var infoHtmls = []; // hold all information window html for currenty markers
	var selectedHotelID = []; // 0 length if displayed from MAP search, otherwise from SS
	var selectedHotels = []; // using to hold hotel booking object from SS search
	var hotels = []; // hotel objects holder
	var towns = []; // town objects holder

	function load() {
		if (navigator.appName == "Microsoft Internet Explorer") 
			isIE = true;
		
		resizeMap();
		setupMap();
	}
	
	function setupMap() {
      	if (GBrowserIsCompatible()) {
        	map = new GMap2(document.getElementById("map"));
			map.addControl(new GLargeMapControl()); 
			map.addControl(new GMapTypeControl());
			map.addControl(new GScaleControl());
			map.addControl(new GOverviewMapControl()); // the small map down the conorn
			map.setCenter(new GLatLng(53.41774904749087, -6.3005908203125), 10);
			map.enableDoubleClickZoom();
			var helpText = 
				"Please click a Blue Icon to<br> view the hotels in that region.";
			map.openInfoWindowHtml(new GLatLng(53.46448464557602, -6.710635986328125), helpText);

			GEvent.addListener(map, "zoomend", function(last, curr) {
				if(map.getZoom() >= 9 && map.getZoom() < 11) {
					if(currCounty == "all" && towns.length == 0 && isAll)
						goGetIt("all");
					
					// every time user return to the town view level, display model reset
					// to Map Search; could have problem when integrating with SS		
					if(selectedHotelID.length != 0)
						selectedHotelID.length = 0;
					hotels.length = 0;
					mgr2Clear();
					isAll = true;
				} else if(map.getZoom() >= 11) {
					if(last < curr && !loadingmessage && last < 11) {
						var html = "<table border=0 width=100% height=100%><tr><td align=center bgcolor=#CCCCCC width=100% height=100% style='font-size:20px'> Loading Property Data... </td></tr></table>"
						loadingmessage = new LoadingMessage(getLMBounds(), html, 2, "black"); 
						map.addOverlay(loadingmessage);	
						if(isAll && hotels.length == 0) {
							goGetIt("all");
						}
					} else {
 						map.removeOverlay(loadingmessage);
						loadingmessage = null;
					}
				} else if(map.getZoom() < 9) {
					// every time user return to the region view level, display model reset
					// to Map Search; could have problem when integrating with SS		
					if(selectedHotelID.length != 0)
						selectedHotelID.length = 0;
					hotels.length = 0;
					//mgr1Clear();
					currCounty = "Dublin";
				}
			});
			//window.setTimeout(setupRegionMarkerManager, 0);
			window.setTimeout(setupTownMarkerManager, 0);
    	} else {
			alert("Your browser is not able to display the map, please use others!");
		}
	}
/**************************************************
	Display markers methods below
**************************************************/		
	/******* Operations for towns ***************/
	function setupTownMarkerManager() {
		mgr1 = new GMarkerManager(map, mgrOption);
		goGetTown();
	}	
	function getTownMarkers() {
		var townMarkers = [];
		for(var i = 0; i < towns.length; i++) {
			townMarkers.push(getTownMarker(new GLatLng(towns[i].townLat, towns[i].townLng), i));
		}
		return townMarkers;
	}
	// get customized town marker
	function getTownMarker(point, index) {
		var townIcon = new GIcon(getTownBaseIcon());
		townIcon.image = defaultPath + "google/markers/marker_blue.png";
 		//GMarker CODE
		var markerOption = {icon: townIcon, title: "Click to show all properties in " + towns[index].townName};
		var marker = new GMarker(point, markerOption); 
/* 		// Labeled Marker CODE
		var markerOption = {icon: townIcon, "clickable": true, "labelText": towns[index].townName, "labelOffset": new GSize(-10, -50)};
		var marker = new LabeledMarker(point, markerOption); */
		
		GEvent.addListener(marker, "click", function() {
			isAll = false;
			map.setCenter(new GLatLng(towns[index].townLat, towns[index].townLng), 12);
			goGetIt(towns[index].townName);
			mgr1.refresh();
		});
		return marker;
	}
	// Create a base icon for all of our markers that specifies the shadow, icon dimensions, etc.
	function getTownBaseIcon() {
		var baseIcon = new GIcon();
		baseIcon.shadow = defaultPath + "google/markers/shadow.png";
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);		
		
		return baseIcon;
	}		
	
	/******* Operations for hotels ***************/
 	function mgr2Clear() {
		if(mgr2 != null) {
			mgr2.clearMarkers(); // remove all markers from last search
			markersInMgr.length = 0; // remove all markers maping to mgr
			infoHtmls.length = 0; // remove all html info associate with the marker
			mgr2 = null;
		}
	}
	function mgr1Clear() {
 		if(mgr1 != null) {
			mgr1.clearMarkers();
			towns.length = 0;
			mgr1 = null;
		}
	}
	function setupHotelMarkerManager() {
		mgr2 = new MarkerManager(map, mgrOption);			
		mgr2.addMarkers(getHotelMarkers(), 10);
		mgr2.refresh();	
		map.removeOverlay(loadingmessage);
		loadingmessage = null;
	}
	function getHotelMarkers() {
		var hotelMarkers = [];
		for(var i = 0; i < hotels.length; i++) {
			hotelMarkers.push(getHotelMarker(new GLatLng(hotels[i].hotelLat, hotels[i].hotelLng), i));
		}
		if(markersInMgr.length != 0) {
			markersInMgr.length = 0;
		}
		markersInMgr = hotelMarkers.slice(0);

		return hotelMarkers;
	}
	/**
	 * Get customized hotel marker; define the marker click event;
	 * contructing each single marker
	 */
	function getHotelMarker(point, index) {
		var i = index + 1;
		var hotelIcon = new GIcon(getHotelBaseIcon());
		if(isAll)
			hotelIcon.image = defaultPath + "google/markers/marker.png";
		else
			hotelIcon.image = defaultPath + "google/markers/marker" + i + ".png";
		var markerOption = {icon: hotelIcon, title: hotels[index].hotelName};
		var marker = new GMarker(point, markerOption); 
//		var marker = new GMarker(point, hotelIcon);
		if(selectedHotelID.length == 0)
			setInfoHtml(index);
		else
			setRoomInfoHtml(index);
		GEvent.addListener(marker, "click", function() {
			if(dispWin != null) {
				if(!dispWin.closed)
				dispWin.window.close(); // for display image window
				dispWin = null;
			}
			var latOfCenter = map.getCenter().lat();
			var latOfMarker = hotels[index].hotelLat;
			if(latOfMarker >= ((latOfMarker + latOfCenter) / 2))
				map.panTo(new GLatLng(hotels[index].hotelLat, hotels[index].hotelLng));
				
			marker.openInfoWindowHtml(infoHtmls[index]);
		});
/* Comment out for now.
		GEvent.addListener(marker, "mouseover", function() {
			marker.setImage(defaultPath + "google/markers/marker_blue.png");
		});
		GEvent.addListener(marker, "mouseout", function() {
			marker.setImage(marker.getIcon().image);
        }); */
		
		return marker;
	}
	/**
	 * This method added two weeks before the deadline, so that map book 
	 * could take booker to stage 3, which is designed to construct the html
	 * for information window displayed for each property, with room type, etc.
	 */
	function setRoomInfoHtml(index) {
		var link = hotels[index].hotelLink;
		var image = imagePath + hotels[index].hotelCode;
		var starImg = defaultPath + "google/markers/" + hotels[index].hotelStarRate + ".gif";
		var button = bookBtnLink + hotels[index].hotelCode + hotels[index].hotelSrcRef;
		var roomtype = selectedHotels[index].roomtype.split(",");
		var roomid = selectedHotels[index].roomid.split(",");
		var price = selectedHotels[index].price.split(",");
		var bookroomurls = [];
		for(var i = 0; i < roomid.length; i++) {
			bookroomurls.push(getUrl(index, roomid[i], price[i]));
		}
		var top = "<table border=0 style=font-family:Verdana>" + 
			//"<tr><td><img src='" + defaultPath + "google/markers/marker" + i + ".png'>" + "</td>" +
			// next statement is used for numbering marker dynamically
			"<tr>" + 
				"<td background='" + defaultPath + "google/markers/icon.png' width=33 height=33 align=center>" + 
					"<a title='This number references to the marker number in map.'>" + (index+1) + "</a></td>" +
				"<td>&nbsp;<a href='" + hotels[index].hotelLink + "' title='Click to open this link in a new window.' target='_blank' ><b>" + hotels[index].hotelName + "</b></a></td>" +
				"<td width=85>&nbsp;<a href='#' title='Star Rating'><img border=0 src='" + starImg + "'></a></td>" +
			"</tr></table><table style=font-family:Verdana><tr>" +
				"<td width=15></td><td colspan=2>" + hotels[index].hotelAL1 + "</td>" +
			"</tr><tr>" +
				"<td width=15></td><td colspan=2>" + hotels[index].hotelAL2 + "</td>" +
			"</tr><tr>" +
				"<td width=15></td><td width=120><a href='" + hotels[index].hotelLink + "' title='Click here to view more about this property' target='_blank' ><img border=0 src='" + defaultPath + "google/markers/btn-readmore.gif'/></a></td>" +
				"<td width=150></td>" + 
					//"<a href='" + button + "' title='Click to make an online booking in a new window.'target='_blank' ><img border=0 src='" + defaultPath + "google/markers/btn-booknow.gif'/></a></a></td>" +
			"</tr></table><table style=font-family:Verdana>" +
			"<tr><td width=15 height=50></td>" + 
				"<td><a href='#' title='Click image to enlarge.'><img border=0 src='" + image + "/1.jpg' onError=loadImageFailed(); width=85 height=50 onclick=displayImgs('" + image + "'); /></a></td>" +
				"<td><a href='#' title='Click image to enlarge.'><img border=0 src='" + image + "/2.jpg' onError=loadImageFailed(); width=85 height=50 onclick=displayImgs('" + image + "'); /></a></td>" + 
				"<td><a href='#' title='Click image to enlarge.'><img border=0 src='" + image + "/3.jpg' onError=loadImageFailed(); width=85 height=50 onclick=displayImgs('" + image + "'); /></a></td>" +
				"<td width=15></td></tr>";
		var bottom = "<tr style=font-style:italic height=15><td></td><td colspan=2 align=left style=font-size:12px>* Click images to enlarge.</td>" + 
			"</tr><tr style=font-style:italic height=15><td></td><td colspan=2 align=left style=font-size:12px>* Click X to close this window.</td><td></td></tr><tr><td>&nbsp;</td></tr></table>";
				
		var middle = "";
		for(var i = 0; i < roomtype.length; i++) {
			middle += "<tr style=font-size:12px><td width=15></td>" + 
				"<td>" + roomtype[i] + "</td>" +
				"<td>" + hotels[index].hotelCurrency + price[i] + "</td>" + 
				"<td><a href='" + bookroomurls[i] + "' target='_blank' >BOOK NOW</a></td></tr>"; 
		}

		infoHtmls.push(top + middle + bottom);
	}
	/*
	 * Auxaliary method of setRoomInfoHtml() above. 
	 * Return room booking url link to stage3 of HTML bookengine.
	 */
	function getUrl(index, roomid, price) {
		return "https://www.bookin1.com/html/new_stage3.jsp?" +
				"RoomID=" + roomid + 
				"&hotelCode=" + hotels[index].hotelCode +
				"&hotelId=" + selectedHotels[index].hotelid + 
				"&src=newzealandin1" +
				"&ref=newzealandin1" +
				"&mode=html" +
				"&checkinDate=" + selectedHotels[index].checkin +
				"&checkoutDate=" + selectedHotels[index].checkout +
				"&noAdults=" + selectedHotels[index].noadults +
				"&noChildren=" + selectedHotels[index].nochildren +
				"&noInfants=" + selectedHotels[index].noinfants +
				"&noRooms=" + selectedHotels[index].norooms +
				"&RoomBreak=" + selectedHotels[index].roombreak +
				"&lengthOfStay=" + selectedHotels[index].lengthofstay +
				"&totalStayRate=" + selectedHotels[index].lengthofstay*price +
				"&search=newzealandin1" +
				"&propertytype=" + selectedHotels[index].propertytype +
				"&colourScheme=newzealandin1" +
				"&generic_name=" + hotels[index].hotelName +
				"&ad=" + selectedHotels[index].ad +
				"&am=" + selectedHotels[index].am +
				"&lm=" + selectedHotels[index].lm +
				"&InLmRange=" + selectedHotels[index].inlmrange +
				"&submit=Continue";
	}
	/**
	 * This method used to be inside of getHotelMarker, which is designed to
	 * construct the html for information window displayed for each property.
	 */
	function setInfoHtml(index) {
		var link = hotels[index].hotelLink;
		var image = imagePath + hotels[index].hotelCode;
		var starImg = defaultPath + "google/markers/" + hotels[index].hotelStarRate + ".gif";
		var button = bookBtnLink + hotels[index].hotelCode + hotels[index].hotelSrcRef;

		var html = "<table border=0 style=font-family:Verdana>" + 
			//"<tr><td><img src='" + defaultPath + "google/markers/marker" + i + ".png'>" + "</td>" +
			// next statement is used for numbering marker dynamically
			"<tr>" + 
				"<td background='" + defaultPath + "google/markers/icon.png' width=33 height=33 align=center>" + 
					"<a title='This number references to the marker number in map.'>" + (index+1) + "</a></td>" +
				"<td>&nbsp;<a href='" + hotels[index].hotelLink + "' title='Click to open this link in a new window.' target='_blank' ><b>" + hotels[index].hotelName + "</b></a></td>" +
				"<td width=85>&nbsp;<a href='#' title='Star Rating'><img border=0 src='" + starImg + "'></a></td>" +
			"</tr></table><table style=font-family:Verdana><tr>" +
				"<td width=15></td><td colspan=2>" + hotels[index].hotelAL1 + "</td>" +
			"</tr><tr>" +
				"<td width=15></td><td colspan=2>" + hotels[index].hotelAL2 + "</td>" +
			"</tr><tr>" +
				"<td width=15></td><td width=100><a href='" + hotels[index].hotelLink + "' title='Click here to view more about this property' target='_blank' ><img border=0 src='" + defaultPath + "google/markers/btn-readmore.gif'/></a></td>" +
				"<td width=150>" + 
					"<a href='" + button + "' title='Click here to check availability and book online'target='_blank' ><img border=0 src='" + defaultPath + "google/markers/btn-booknow.gif'/></a></a></td>" +
			"</tr></table><table style=font-family:Verdana>" +
			"<tr><td width=15 height=81></td>" + 
				"<td><a href='#' title='Click image to enlarge.'><img border=0 src='" + image + "/1.jpg' onError=loadImageFailed(); width=120 height=81 onclick=displayImgs('" + image + "'); /></a></td>" +
				"<td><a href='#' title='Click image to enlarge.'><img border=0 src='" + image + "/2.jpg' onError=loadImageFailed(); width=120 height=81 onclick=displayImgs('" + image + "'); /></a></td>" + 
				"<td width=15></td>" + 
			"</tr><tr><td width=15 height=81></td>" + 
				"<td><a href='#' title='Click image to enlarge.'><img border=0 src='" + image + "/3.jpg' onError=loadImageFailed(); width=120 height=81 onclick=displayImgs('" + image + "'); /></a></td>" +
				"<td><a href='#' title='Click image to enlarge.'><img border=0 src='" + image + "/4.jpg' onError=loadImageFailed(); width=120 height=81 onclick=displayImgs('" + image + "'); /></a></td><td width=15></td>" +
			"</tr><tr style=font-style:italic height=15><td></td><td colspan=2 align=left style=font-size:12px>* Click images to enlarge.</td>" + 
			"</tr><tr style=font-style:italic height=15><td></td><td colspan=2 align=left style=font-size:12px>* Click X to close this window.</td><td></td></tr><tr><td>&nbsp;</td></tr></table>";
		
		infoHtmls.push(html);
	}

	/**
	 * Create a base icon for all of our markers that specifies the shadow, icon dimensions, etc.
	 */
	function getHotelBaseIcon() {
		var baseIcon = new GIcon();
		baseIcon.shadow = defaultPath + "google/markers/shadow.png";
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
		
		return baseIcon;
	}

/**************************************************
	Get Data methods below
**************************************************/	
	// get hotels located in the selected region
	function goGetIt(selected){
		mgr2Clear();
		myXHR = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
		//var url = "google/mapDataFeeder?"; this line is using Servlet
		var url = "google/mapDataFeeder.jsp?";
		url = url + "town=" + selected;
		url = url + "&county=" + currCounty;
		url += "&country=IE"; // only modify here for changing country
		if(selected != "all") { isAll = false; }
		
		myXHR.onreadystatechange = function() {
			if(myXHR.readyState == 4){
				hotels.length = 0; // clear hotel object holder
				if(selectedHotelID.length != 0) // 0 means no SS search limits, display all properties
					parseSelectedHotels(myXHR.responseXML);
				else
					parseAllHotels(myXHR.responseXML);
				window.setTimeout(setupHotelMarkerManager, 0);
			}
		};
		myXHR.open("GET", url, true);
		myXHR.send(null);
	}
	/**
	 * Get all search result from servlet output XML.
	 * Independent method for map search.
	 */
	function parseAllHotels(responseXML) {
		if(responseXML) {
			try {
				var x = responseXML.documentElement.getElementsByTagName("Property");
				for(var i = 0; i < x.length; i++) {
					var hotel = new Object();
					hotel.hotelID = x[i].getElementsByTagName("HotelID")[0].firstChild.data;
					hotel.hotelName = x[i].getElementsByTagName("HotelName")[0].firstChild.data;
					hotel.hotelCode = x[i].getElementsByTagName("HotelCode")[0].firstChild.data;
					//hotel.hotelCurrency = x[i].getElementsByTagName("Currency")[0].firstChild.data;
					hotel.hotelSrcRef = x[i].getElementsByTagName("SrcRef")[0].firstChild.data;
					hotel.hotelStarRate = x[i].getElementsByTagName("StarRate")[0].firstChild.data;
					hotel.hotelLink = x[i].getElementsByTagName("HotelInformationLink")[0].firstChild.data;
					hotel.hotelAL1 = x[i].getElementsByTagName("AddressLine1")[0].firstChild.data;
					hotel.hotelAL2 = x[i].getElementsByTagName("AddressLine2")[0].firstChild.data;
					hotel.hotelLat = parseFloat(x[i].getElementsByTagName("Latitude")[0].firstChild.data);
					hotel.hotelLng = parseFloat(x[i].getElementsByTagName("Longitude")[0].firstChild.data);
					
					hotels.push(hotel);
				}
			} catch(err) {
				// Shouldn't do nothing, but that has been done in Servlet
				//alert(err); 
			}
		} else { 
			alert("Application Server Error(No Data To Display)! ");
		}
    }
	/**
	 * Get SS search result only. SS search resultset <= Map Search resultset.
	 * Using HotelID passed by SS.
	 * Dependent method for displaying SS result only in map.
	 */	
	function parseSelectedHotels(responseXML) {
	    if(responseXML) {
			try {
				var x = responseXML.documentElement.getElementsByTagName("Property");
				for(var j = 0; j < selectedHotelID.length; j++){
					for(var i = 0; i < x.length; i++) {
						if(selectedHotelID[j] == x[i].getElementsByTagName("HotelID")[0].firstChild.data) {
							var hotel = new Object();
							hotel.hotelID = x[i].getElementsByTagName("HotelID")[0].firstChild.data;
							hotel.hotelName = x[i].getElementsByTagName("HotelName")[0].firstChild.data;
							hotel.hotelCode = x[i].getElementsByTagName("HotelCode")[0].firstChild.data;
							//hotel.hotelCurrency = x[i].getElementsByTagName("Currency")[0].firstChild.data;
							hotel.hotelSrcRef = x[i].getElementsByTagName("SrcRef")[0].firstChild.data;
							hotel.hotelStarRate = x[i].getElementsByTagName("StarRate")[0].firstChild.data;
							hotel.hotelLink = x[i].getElementsByTagName("HotelInformationLink")[0].firstChild.data;
							hotel.hotelAL1 = x[i].getElementsByTagName("AddressLine1")[0].firstChild.data;
							hotel.hotelAL2 = x[i].getElementsByTagName("AddressLine2")[0].firstChild.data;
							hotel.hotelLat = parseFloat(x[i].getElementsByTagName("Latitude")[0].firstChild.data);
							hotel.hotelLng = parseFloat(x[i].getElementsByTagName("Longitude")[0].firstChild.data);
							
							hotels.push(hotel);
							break;
						}
					}
				}
			} catch(err) {
				// Shouldn't do nothing, but that has been done in Servlet
				//alert(err);
			}
		} else {
			alert("Application Server Error(No Data To Display)!");
		}
    }
    /** Operation for loading map with region/county displayed starts */
/*	
	function goGetRegion(){
		rXHR = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
		var url = defaultPath + "google/county.xml";
		rXHR.onreadystatechange = function() {
			if(rXHR.readyState == 4){
				parseRegion(rXHR.responseXML);
				mgr.addMarkers(getRegionMarkers(), 0, 8);
				mgr.refresh();
			}
		};
		rXHR.open("GET", url, true);
		rXHR.send(null);
	}
	function parseRegion(responseXML) {
		if(responseXML) {
			var x = responseXML.documentElement.getElementsByTagName("County");
			regions.length = 0;
			for(var i = 0; i < x.length; i++) {
				var region = new Object();
				region.regionName = x[i].getElementsByTagName("CountyName")[0].firstChild.data;
				region.regionLat = parseFloat(x[i].getElementsByTagName("Latitude")[0].firstChild.data);
				region.regionLng = parseFloat(x[i].getElementsByTagName("Longitude")[0].firstChild.data);
				regions.push(region);
			}
		} else {
			alert("Region Location file is missing or interupted");
		}
   }
 */  
    /** Operation for loading map with town displayed starts */
	function goGetTown(){
		//mgr1Clear();
		tXHR = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
		var url = defaultPath + "google/town.xml";
		tXHR.onreadystatechange = function() {
			if(tXHR.readyState == 4){
				parseTown(tXHR.responseXML);
				//window.setTimeout(setupTownMarkerManager, 0);
				mgr1 = new MarkerManager(map, mgrOption);
				//mgr1.addMarkers(getTownMarkers(), 9);
				mgr1.addMarkers(getTownMarkers(), 4, 10);
				mgr1.refresh();
			}
			
		};
		tXHR.open("GET", url, true);
		tXHR.send(null);
	}
	function parseTown(responseXML) {
		if(responseXML) {
			try {
				var x = responseXML.documentElement.getElementsByTagName(currCounty);
				towns.length = 0;
				for(var i = 0; i < x.length; i++) {
					var town = new Object();
					town.townName = x[i].getElementsByTagName("Town")[0].firstChild.data;
					town.townLat = parseFloat(x[i].getElementsByTagName("Latitude")[0].firstChild.data);
					town.townLng = parseFloat(x[i].getElementsByTagName("Longitude")[0].firstChild.data);
 					towns.push(town);
				}
			} catch(err) {
				alert(err + " in parsing Towns in " + currCounty);
			}
		} else {
			alert("Town Location file is missing or interupted");
		}
   }   
   /** Operation for loading map with region/town displayed ends */
   
/**************************************************
	ResizeMap, Display images
**************************************************/	
	// JavaScript Document
	function getElement(id){
		return document.getElementById(id);
	}
	function resizeMap(){
		var width = 905;
		var height = 720;
		if(height >= 0){
			getElement("map").style.height = height + "px";
			getElement("map").style.width = width + "px";
		}
		if(map) map.checkResize();
	}
	function getWindowHeight(){
		if(window.self && self.innerHeight){
			return self.innerHeight;
		}
		if(document.documentElement && document.documentElement.clientHeight){
			return document.documentElement.clientHeight;
		}
		return 0;
	}	
	function getWindowWidth(){
		if(window.self && self.innerWidth){
			return self.innerWidth;
		}
		if(document.documentElement && document.documentElement.clientWidth){
			return document.documentElement.clientWidth;
		}
		return 0;
	}
	/**
	 * Construct the images popup window.
	 */
	function displayImgs(path) {
		if(dispWin == null || dispWin.closed) {
			dispWin = window.open('', '', 'height=360,width=490,directories=no,resizable=no,status=no,titlebar=no,toolbar=no,menubar=no,location=no,left=0,top=50');
			dispWin.document.writeln("<html><head>");
			dispWin.document.writeln("<script type='text/javascript'>");
			dispWin.document.writeln("function loadImageFailed(index){");
			dispWin.document.writeln("if(navigator.appName=='Microsoft Internet Explorer') window.event.srcElement.style.display='None'; else { this.document.getElementById(index).innerHTML='';}}");
			dispWin.document.writeln("</script>");
			dispWin.document.writeln("</head><body>");
			var message = "<table><tr><td><div id='1'><img src='" + path + "/1.jpg' onError=loadImageFailed(1); /></div></td>";
			message += "<td><div id='2'><img src='" + path + "/2.jpg' onError=loadImageFailed(2); /></div></td></tr>";
			message += "<tr><td><div id='3'><img src='" + path + "/3.jpg' onError=loadImageFailed(3); /></div></td>";
			message += "<td><div id='4'><img src='" + path + "/4.jpg' onError=loadImageFailed(4); /></div></td></tr>";
			message += "<tr><td></td><td align=right><input type=button value='Close Window' onclick=window.close(); ></td></tr></table></body></html>";
			dispWin.document.write(message);
		} 
	}
	function loadImageFailed() {
		if(isIE)
			window.event.srcElement.style.display = "None";
	}
	
/**************************************************
	Custom Overlay Loading Message Box
**************************************************/	
	function getLMBounds() {
		var bounds = map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		var latDelta = (northEast.lat() - southWest.lat()) / 2.2;
		var lngDelta = (northEast.lng() - southWest.lng()) / 2.8;
		var rectBounds = new GLatLngBounds(
								new GLatLng(southWest.lat() + latDelta, southWest.lng() + lngDelta),
								new GLatLng(northEast.lat() - latDelta, northEast.lng() - lngDelta));
		return rectBounds;
	}

	function LoadingMessage(bounds, html, opt_weight, opt_color) {
		this.bounds_ = bounds;
		this.html_ = html
		this.weight_ = opt_weight;
		this.color_ = opt_color;
	}
	LoadingMessage.prototype = new GOverlay();
	
	LoadingMessage.prototype.initialize = function(map) {
		// Create the DIV representing our LM
		var div = document.createElement("div");
		div.style.border = this.weight_ + "px solid " + this.color_;
		div.style.position = "absolute";
		div.innerHTML = this.html_;
	
		// Our rectangle is flat against the map, so we add our selves to the
	    // MAP_PANE pane, which is at the same z-index as the map itself (i.e.,
	    // front of everything else on the map)
	    map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
	
	    this.map_ = map;
	  	this.div_ = div;
	}
	
	// Remove the main DIV from the map pane
	LoadingMessage.prototype.remove = function() {
	  this.div_.parentNode.removeChild(this.div_);
	}
	// Copy our data to a new LoadingMessage
	LoadingMessage.prototype.copy = function() {
	  return new LoadingMessage(this.bounds_, this.weight_, this.color_,
						   this.backgroundColor_, this.opacity_);
	}
	// Redraw the rectangle based on the current projection and zoom level
	LoadingMessage.prototype.redraw = function(force) {
	  // We only need to redraw if the coordinate system has changed
	  if (!force) return;
	
	  // Calculate the DIV coordinates of two opposite corners of our bounds to
	  // get the size and position of our rectangle
	  var c1 = this.map_.fromLatLngToDivPixel(this.bounds_.getSouthWest());
	  var c2 = this.map_.fromLatLngToDivPixel(this.bounds_.getNorthEast());
	
	  // Now position our DIV based on the DIV coordinates of our bounds
	  this.div_.style.width = Math.abs(c2.x - c1.x) + "px";
	  this.div_.style.height = Math.abs(c2.y - c1.y) + "px";
	  this.div_.style.left = (Math.min(c2.x, c1.x) - this.weight_) + "px";
	  this.div_.style.top = (Math.min(c2.y, c1.y) - this.weight_) + "px";
	}
/**************************************************
	Ends Custom Overlay Loading Message Box
**************************************************/

/**************************************************
	Public Interface for SS Controlling Starts
**************************************************/
 	/**
	 * Public method for SS displaying property in map. 
	 *
	 * This method is designed to communicate with SS part,
	 * it shows the popup information window for each property.
	 * @Param {int} index index number of hotelmarker array, which is 
	 *					  exactly same as markers controlled by mgr2.
	 */
	function popupInfoWindow(index) {
		var marker = null;
		if(mgr2 != null ) {
 			marker = markersInMgr[index];
			marker.openInfoWindowHtml(infoHtmls[index]);
		}
	}
	/*
	 * Set new marker image when mouseover fires
	 */
	function setImage(index) {
		var marker = null;
		if(mgr2 != null ) {
 			marker = markersInMgr[index];
			marker.setImage(defaultPath + "google/markers/marker_blue.png");
		}		
	}
	/*
	 * Restore marker image when mouseout fires
	 */	
	function restoreImage(index) {
		var marker = null;
		if(mgr2 != null ) {
 			marker = markersInMgr[index];
			marker.setImage(marker.getIcon().image);
		}	
	}
 	/**
	 * Public method for SS displaying property in map. 
	 *
	 * This method is designed to communicate with SS part,
	 * it binds displayed map region with user selected region in SS.
	 * @param {String} selectedID Selected HotelID, comes from SS search 
	 *							  resultset. The string must follow the format
	 *							  "hotelid&roomtype&roomid&price&checkindate&
	 *							  checkoutdate&noadults&nochildren&noinfants&
	 *							  norooms&roombreak&lengthofstay&propertytype&
	 *							  ad&am&lm&inlmrange".
	 *							  EXAMPLE:
	 * 							  "...;2387&Double Room,Triple Room
	 *								&22,33&200,300&17/05/2007
	 *								&19/05/2007&2&1&1&2&Room&2&null&16
	 *								&05:2007&false&false;..."
	 * @param {String} regionName Current region, name of the region wanna
	 * 							  be filted, which should be exactly  
	 * 							  same as its name in "city.xml" file.
	 */
	function setSelectedHotelID(regionName, selectedHotelsStr) {
		if(selectedHotelID.length != 0) {
			selectedHotelID.length = 0;
			selectedHotels.length = 0;
		}
		var selectedHotelsArr = selectedHotelsStr.split(";");
		for(var i = 0; i < selectedHotelsArr.length; i++) {
			var temp = selectedHotelsArr[i].split("&");
			var bookurl = new Object();
			bookurl.hotelid = temp[0];
			bookurl.roomtype = temp[1];
			bookurl.roomid = temp[2];
			bookurl.price = temp[3]
			bookurl.checkin = temp[4];
			bookurl.checkout = temp[5];
			bookurl.noadults = temp[6];
			bookurl.nochildren = temp[7];
			bookurl.noinfants = temp[8];
			bookurl.norooms = temp[9];
			bookurl.roombreak = temp[10];
			bookurl.lengthofstay = temp[11];
			bookurl.propertytype = temp[12];
			bookurl.ad = temp[13];
			bookurl.am = temp[14];
			bookurl.lm = temp[15];
			bookurl.inlmrange = temp[16];
			
			selectedHotelID.push(temp[0]); // put hotelid into array
			selectedHotels.push(bookurl); // add obj to array
		}
		
		goGetIt(regionName);
		var region = null;
		for(var i = 0; i < regions.length; i++) {
			if(regions[i].regionName == regionName) {
				region = regions[i];
				break;
			}
		}
		if(region) {
			map.setCenter(new GLatLng(region.regionLat, region.regionLng), 9);
			map.setZoom(9);
			mgr2Clear();
		} else {
			alert("This region is not one of the region markers in the map!");
		}
	}
 	/**
	 * Public method for SS displaying property in map. 
	 *
	 * This method is designed to communicate with SS part,
	 * it binds displayed map region with user selected region in SS.
	 * @Param {String} regionName region name, which should be exactly  
	 * 							  same as its name in "city.xml" file.
	 */
	function setRegion(regionName) {
		var region = null;
		goGetIt(regionName);
		for(var i = 0; i < regions.length; i++) {
			if(regions[i].regionName == regionName) {
				region = regions[i];
				break;
			}
		}
		if(region) {
			map.setCenter(new GLatLng(region.regionLat, region.regionLng), 9);
			map.setZoom(9);
			mgr2Clear();
		} else {
			alert("This region is not one of the region markers in the map!");
		}
	}
/**************************************************
	Public Interface for SS Controlling Ends
**************************************************/
