// JavaScript Document

	var map, other_map, marker, other_map_marker;
	var opacity = 1;
	var infoWindowTabsArray = [];
	var locationSearchArray = [];
	var checkBoundListener;

    function initialize() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
		map.removeMapType(G_SATELLITE_MAP);
		map.removeMapType(G_HYBRID_MAP);
        map.setCenter(CUHK_MAP_SETTING.defaultcenterLatLng, CUHK_MAP_SETTING.defaultzoom);

		var mapControl = new GMapTypeControl();
		map.addControl(mapControl);
        //map.setUIToDefault();

        // bind a search control to the map, suppress result list
        //map.addControl(new google.maps.LocalSearch(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,20)));
		marker = new GMarker(map.getCenter(), {draggable: true});
		map.setZoom(16);
		map.addOverlay(marker);
		marker.hide();
		
		//Check boundary - not allow to drag outside of the boundary
		checkBoundListener = GEvent.addListener(map, "move", function() { checkBounds();});
		
		// Close the InfoWindow when Drag
		GEvent.addListener(marker, "dragstart", function() {marker.closeInfoWindow(); GEvent.clearListeners(marker,'click')});
		
		map.addControl(new GLargeMapControl3D());
		
		var mapTypes = map.getMapTypes();
		// overwrite the getMinimumResolution() and getMaximumResolution() methods for each map type
		for (i=0; i<mapTypes.length; i++) {
			mapTypes[i].getMinimumResolution = function() {return CUHK_MAP_SETTING.minMapZoomLevel;}
			mapTypes[i].getMaximumResolution = function() {return CUHK_MAP_SETTING.maxMapZoomLevel;}
		}
		
		
		// For Other Map
		if (document.getElementById("other_map_canvas")) {

			other_map = new GMap2(document.getElementById("other_map_canvas"));
			other_map.removeMapType(G_SATELLITE_MAP);
			other_map.removeMapType(G_HYBRID_MAP);
			other_map.setCenter(CUHK_MAP_SETTING.defaultcenterLatLng, CUHK_MAP_SETTING.defaultzoom);
		
			//other_map.addControl(new GMapTypeControl());
			other_map.addControl(new GSmallZoomControl3D());
			$("#extra-map-dialog").dialog({title: '', resizable:false, autoOpen:false, width:410});
		}
      }
	  
	  	//Tile Map
        var tilelayer = new GTileLayer(GCopyrightCollection(''), CUHK_MAP_SETTING.minMapZoomLevel, CUHK_MAP_SETTING.maxMapZoomLevel);
        var mercator = new GMercatorProjection(CUHK_MAP_SETTING.maxMapZoomLevel+1);
        tilelayer.getTileUrl = function(tile,zoom) {
              if ((zoom < CUHK_MAP_SETTING.minMapZoomLevel) || (zoom > CUHK_MAP_SETTING.maxMapZoomLevel)) {
                  return "/english/images/campus/tile-map/blank.png";
              } 
              var ymax = 1 << zoom;
              var y = ymax - tile.y -1;
              var tileBounds = new GLatLngBounds(
                  mercator.fromPixelToLatLng( new GPoint( (tile.x)*256, (tile.y+1)*256 ) , zoom ),
                  mercator.fromPixelToLatLng( new GPoint( (tile.x+1)*256, (tile.y)*256 ) , zoom )
              );
              if (CUHK_MAP_SETTING.external_boundary.intersects(tileBounds)) {
                  return "/english/images/campus/tile-map/" + zoom+"/"+tile.x+"/"+y+".png";
              } else {
                  return "/english/images/campus/tile-map/blank.png";
              }
          }
          // IE 7-: support for PNG alpha channel
          // Unfortunately, the opacity for whole overlay is then not changeable, either or...
          tilelayer.isPng = function() { return true;};
          tilelayer.getOpacity = function() { return CUHK_MAP_SETTING.defaultOpacity; }

          overlay = new GTileLayerOverlay( tilelayer );
          map.addOverlay(overlay);
	  
		//Building Relationship between Building and Facilities
		
		for (i in CUHK_MAP_DATA.facilities) {
			var objCurFac = CUHK_MAP_DATA.facilities[i];
			if (objCurFac.building_id != "") {
				for (j in CUHK_MAP_DATA.buildings) {
					var objCurBldg = CUHK_MAP_DATA.buildings[j];
					if (objCurFac.building_id == objCurBldg.building_id) {
						objCurFac.building = objCurBldg;
						objCurBldg.facility = objCurFac;
						break;
					}
				}
			}
		}
    }
	
	//GSearch.setOnLoadCallback(initialize);
	function showCategory(bShow, categoryName, subname, id, icon) {
		var objItemArray = null;
		objItemArray = CUHK_MAP_DATA[categoryName];
		var iFound = 0;
		var MarkerBuildArray = [];
		var objLastFoundMarker = null;
		//The following variables are used to find the boundary of the items found.
		var iBoundaryLatMin = null, iBoundaryLatMax = null, iBoundaryLngMin = null, iBoundaryLngMax = null; 
		
		for(i in objItemArray) {			
			var objCurrent = objItemArray[i];
			if (objCurrent[subname] == id) {				
				iFound++;
				if (bShow) {
					if (objCurrent.Marker == null) {
						//create Marker
						//Don't show InfoWindow for car parks (Public/Private) [id=8,9]
						
						if (categoryName == "facilities" && subname == "type_id" && (id == 8 || id == 9)) {
							objCurrent.Marker = createMarker('/english/images/campus/icons/' + icon, new GSize(20, 20), objCurrent, false);
						} else {
							objCurrent.Marker = createMarker('/english/images/campus/icons/' + icon, new GSize(20, 20), objCurrent, true);
						}
						objCurrent.Marker.Owner = objCurrent;
					}

					if (objCurrent.Marker != null) {
						var lat_lng = objCurrent.lat_lng.replace(/[\(\)\s]/g, "").split(",");
						//find out the boundary
						//If this is the first one, set iBoundaryLatMin, iBoundaryLatMax, iBoundaryLngMin, iBoundaryLngMax to the lattitude and longtitude of the first item.
						if (iBoundaryLatMin == null) {
							iBoundaryLatMin = lat_lng[0];
							iBoundaryLatMax = lat_lng[0];
							iBoundaryLngMin = lat_lng[1];
							iBoundaryLngMax = lat_lng[1];
						} else {
							if (lat_lng[0] < iBoundaryLatMin) {
								iBoundaryLatMin = lat_lng[0];
							} else if (lat_lng[0] > iBoundaryLatMax) {
								iBoundaryLatMax = lat_lng[0];
							}

							if (lat_lng[1] < iBoundaryLngMin) {
								iBoundaryLngMin = lat_lng[1];
							} else if (lat_lng[1] > iBoundaryLngMax) {
								iBoundaryLngMax = lat_lng[1];
							}
							
						}
					
					
						//Record the last Marker Found
						objLastFoundMarker = objCurrent.Marker;
						
						objCurrent.Marker.show();
					}
				} else {
					if (objCurrent.Marker != null) {
						objCurrent.Marker.hide();
					}
				}
			}
		}
		
		//If there are more than one item found, locate to the average point.
		if (iFound > 1 && bShow) {
			
			var iBoundaryLatAvg = (iBoundaryLatMin * 1 + iBoundaryLatMax * 1) / 2;
			var iBoundaryLngAvg = (iBoundaryLngMin * 1 + iBoundaryLngMax * 1) / 2;
			
			map.setZoom(CUHK_MAP_SETTING.defaultzoom);
			map.panTo(new GLatLng(iBoundaryLatAvg, iBoundaryLngAvg));
			
		}
		
		//If there is only one item found, show its InfoWindow.
		if (iFound == 1 && bShow) {
			GEvent.trigger(objLastFoundMarker, "click");
		}
	}

	function showAccessingCUHKRoute(bShow, id) {
		var objItemArray = null;
		objItemArray = CUHK_MAP_DATA.transport_seg;		
		
		for(i in objItemArray) {
			var objCurrent = objItemArray[i];

			if (objCurrent.transport_seg_id == id) {
				if (bShow) {
					if (objCurrent.Route == null) {
						var encodedPolyline = new BDCCArrowedEncodedPolyline(objCurrent.encoded_line, objCurrent.encoded_levels, objCurrent.color == "" ? '#ff0000' : objCurrent.color, 3, 0.5, null, 100, 6, objCurrent.color == "" ? '#ff0000' : objCurrent.color, 2, 1.0);
						//create Route
						objCurrent.Route = encodedPolyline;
						map.addOverlay(objCurrent.Route); 
						objCurrent.Route.hide();
					}

					if (objCurrent.Route != null) {
						objCurrent.Route.show();
						objCurrent.selected = true;
						//map.setZoom(16);
						//map.panTo(new GLatLng(22.41934990697308, 114.207022190094));
					}
				} else {
					if (objCurrent.Route != null) {
						objCurrent.Route.hide();
						objCurrent.selected = false;
					}
				}
				break;
			}
		}
	}

	function showBusRoute(bShow, id) {
		var objItemArray = null;
		objItemArray = CUHK_MAP_DATA.shuttle_bus_route;		
		
		for(i in objItemArray) {
			var objCurrent = objItemArray[i];

			if (objCurrent.route_id == id) {
				if (bShow) {
					if (objCurrent.Route == null) {
						//create Route
						objCurrent.Route = createBusRoute(objCurrent);
					}

					if (objCurrent.Route != null) {
						objCurrent.Route.show();
						objCurrent.selected = true;
						//map.setZoom(16);
						//map.panTo(new GLatLng(22.41934990697308, 114.207022190094));
					}
				} else {
					if (objCurrent.Route != null) {
						objCurrent.Route.hide();
						objCurrent.selected = false;
					}
				}
				break;
			}
		}
	}

	
	function showBusStop(bShow, routeid) {
		var objItemArray = null;
		objItemArray = CUHK_MAP_DATA.shuttle_bus_route;		
		
		for(i in objItemArray) {
			var objCurrentBusRoute = objItemArray[i];

			if (objCurrentBusRoute.route_id == routeid) {
				if (bShow) {
					if (objCurrentBusRoute.BusStopArray == null) {
						//create Bus Stop Array
						objCurrentBusRoute.BusStopArray = [];
						objBusRouteSegArray = CUHK_MAP_DATA.shuttle_bus_route_seg;
						for (j in objBusRouteSegArray) {
							var objCurrentBusRouteSeg = objBusRouteSegArray[j];
							if (objCurrentBusRouteSeg.route_id == objCurrentBusRoute.route_id) {
								var objBusSegArray = CUHK_MAP_DATA.shuttle_bus_seg;
								
								for (k in objBusSegArray) {
									var objCurrentBusSeg = objBusSegArray[k];
									if (objCurrentBusSeg.bus_route_seg_id == objCurrentBusRouteSeg.seg_id) {
										
										if (objCurrentBusRoute.BusStopArray.length == 0) { //If it is the first bus stop, both start and end bus stop should be recorded.
											var objBusStop = getBusStop(objCurrentBusSeg.start_bus_stop_id);
											if (objBusStop != null) {
												objCurrentBusRoute.BusStopArray.push(objBusStop);
											}
										}

										var objBusStop = getBusStop(objCurrentBusSeg.end_bus_stop_id);
										if (objBusStop != null) {
											objCurrentBusRoute.BusStopArray.push(objBusStop);
										}
										break;
									}
								}
							}
						}
						
					}

					if (objCurrentBusRoute.BusStopArray != null) {
						for (j in objCurrentBusRoute.BusStopArray) {
							objCurrentBusRoute.BusStopArray[j].Marker.show();
						}
					}
				} else {
					if (objCurrentBusRoute.BusStopArray != null) {
						for (j in objCurrentBusRoute.BusStopArray) {

							//Check if any selected route has the same bus stop - If so - don't remove this bus stop.
							var bDuplicate = false;
							for (k in CUHK_MAP_DATA.shuttle_bus_route) {
								if (CUHK_MAP_DATA.shuttle_bus_route[k] != objCurrentBusRoute && (CUHK_MAP_DATA.shuttle_bus_route[k].selected != null && CUHK_MAP_DATA.shuttle_bus_route[k].selected)) {
									for (m in CUHK_MAP_DATA.shuttle_bus_route[k].BusStopArray) {
										if (CUHK_MAP_DATA.shuttle_bus_route[k].BusStopArray[m].Marker == objCurrentBusRoute.BusStopArray[j].Marker) {
											bDuplicate = true;
											break;
										}
									}
									
									if (bDuplicate) break;
								}
							}
							
							if (!bDuplicate) {
								objCurrentBusRoute.BusStopArray[j].Marker.hide();
							}
						}
					}
				}
				break;
			}
		}
	}
	
	function getBusStop(busStopId) {
		var objBusStopsArray = CUHK_MAP_DATA.shuttle_bus_stops;
		for (m in objBusStopsArray) {
			var objCurrentBusStop = objBusStopsArray[m];
			if (objCurrentBusStop.bus_stop_id == busStopId) {
				if (objCurrentBusStop.Marker == null) {
					//add Marker
					objCurrentBusStop.Marker = createMarker('/english/images/campus/icons/shuttle_stop.gif', new GSize(20, 20), objCurrentBusStop, true);
					objCurrentBusStop.Marker.Owner = objCurrentBusStop;
				}
				return objCurrentBusStop;
			}
		}
		return null;
	}
	
	function showWalkingRoute(bShow, id) {
		var objItemArray = null;
		objItemArray = CUHK_MAP_DATA.walking_route;		
		
		for(i in objItemArray) {
			var objCurrent = objItemArray[i];

			if (objCurrent.walking_route_id == id) {
				if (bShow) {
				
					if (objCurrent.Route == null) {
						objCurrent.Route = new BDCCArrowedEncodedPolyline(objCurrent.ecoded_line, objCurrent.ecoded_level, objCurrent.route_color == "" ? '#ff0000' : objCurrent.route_color, 5, 0.75, null, 250, 8, objCurrent.route_color == "" ? '#ff0000' : objCurrent.route_color, 3, 1.0);
						
						map.addOverlay(objCurrent.Route); 
						objCurrent.Route.hide();
					}

					if (objCurrent.Route != null) {
						objCurrent.Route.show();
						//map.setZoom(16);
						//map.panTo(new GLatLng(22.41934990697308, 114.207022190094));
					}
				} else {
					if (objCurrent.Route != null) {
						objCurrent.Route.hide();
					}
				}
				break;
			}
		}
	}
	

	function createMarker(imageurl, objSize, objItem, bShowInfo) {
	
		var objIcon = new GIcon(G_DEFAULT_ICON, imageurl);
		objIcon.iconSize = objSize;
		objIcon.shadowSize = new GSize(0, 0);
		objIcon.iconAnchor  = new GPoint(objSize.width / 2, objSize.height / 2);
		//alert(objIcon.iconAnchor);

		var lat_lng = objItem.lat_lng.replace(/[\(\)\s]/g, "").split(",");
		
		if (lat_lng.length == 2) {
			var objMarker = new GMarker(new GLatLng(lat_lng[0], lat_lng[1]), {icon:objIcon});
			
			if (bShowInfo) {
				GEvent.addDomListener(objMarker,'click',function() {
					var objInfoTabsHTMLArray = showInfoWindow(objMarker.Owner);
				
					if (objInfoTabsHTMLArray.length == 1) {	
						objMarker.openInfoWindowHtml(objInfoTabsHTMLArray[0]);
					} else {
						infoWindowTabsArray[0] = new GInfoWindowTab('Information', objInfoTabsHTMLArray[0]);
						infoWindowTabsArray[1] = new GInfoWindowTab('Description', objInfoTabsHTMLArray[1]);
						objMarker.openInfoWindowTabsHtml(infoWindowTabsArray)
					}
				});
				
				
				//Close the InfoWindow if the marker is invisible
				GEvent.addDomListener(objMarker, 'visibilitychanged', function(isVisible) {if (isVisible == false) this.closeInfoWindow();});
			}
			map.addOverlay(objMarker);
			objMarker.hide();
			return objMarker;
		} else {
			return null;
		}
	}

	function createBusRoute(objItem) {
	
		var routepoints = "";
		var routelevels = "";
		
		for (i in CUHK_MAP_DATA.shuttle_bus_route_seg) {
			
			if (CUHK_MAP_DATA.shuttle_bus_route_seg[i].route_id == objItem.route_id) {
				var seg_id = CUHK_MAP_DATA.shuttle_bus_route_seg[i].seg_id;
				
				for (j in CUHK_MAP_DATA.shuttle_bus_seg) {
					if (CUHK_MAP_DATA.shuttle_bus_seg[j].bus_route_seg_id == seg_id) {
						if (routepoints == "") {
							routepoints = CUHK_MAP_DATA.shuttle_bus_seg[j].encoded_start_pt;
							routelevels = "B";
						}
						routepoints += CUHK_MAP_DATA.shuttle_bus_seg[j].encoded_line + CUHK_MAP_DATA.shuttle_bus_seg[j].encoded_end_pt;
						routelevels += CUHK_MAP_DATA.shuttle_bus_seg[j].encoded_levels.substr(1);
						break;
					}
				}
			}
		}

		
		var encodedPolyline = new BDCCArrowedEncodedPolyline(routepoints, routelevels, objItem.route_color == "" ? '#ff0000' : objItem.route_color, 3, 0.5, null, 250, 6, objItem.route_color == "" ? '#ff0000' : objItem.route_color, 2, 1.0);
		
		map.addOverlay(encodedPolyline); 
		encodedPolyline.hide();
		return encodedPolyline;
	
	}
	
	function showInfoWindow(objElement) {
	
		if(objElement) {
			
			var target_fields_array;
			var target_fields_name_array;
			var infoWindowTabsArray = [];
			var photofolder = "";
			var photourl = "";
			var strCurrentIdentityField = "";
			//Check InfoWindow_Display Criteria:
			
			if (objElement.facility) objElement = objElement.facility;
			
			for (i in CUHK_MAP_DATA.InfoWindow_Display) {
				if (objElement[CUHK_MAP_DATA.InfoWindow_Display[i].identify_fields]) {
					strCurrentIdentityField = CUHK_MAP_DATA.InfoWindow_Display[i].identify_fields;
					if (strCurrentIdentityField == "facilities_id") {
						target_fields_array = CUHK_MAP_DATA.InfoWindow_Display[i].field_rank_en;
						target_fields_name_array = CUHK_MAP_DATA.InfoWindow_Display[i].field_name_en;
						if (objElement.photo != null && objElement.photo != "") {
							photourl = "/english/documents/campus/photos/facilities/" + objElement.photo + "." + objElement.photo_format;
						} else if (objElement.building) {
							if (objElement.building.photo != null && objElement.building.photo != "") {
								photourl = "/english/documents/campus/photos/buildings/" + objElement.building.photo + "." + objElement.building.photo_format;
							}
						} else {
						}
						break;
					} else {
						target_fields_array = CUHK_MAP_DATA.InfoWindow_Display[i].field_rank_en;
						target_fields_name_array = CUHK_MAP_DATA.InfoWindow_Display[i].field_name_en;
						if (objElement.photo != null && objElement.photo != "") {
							if (strCurrentIdentityField == "landmark_id") {
								photourl = "/english/documents/campus/photos/landmarks/" + objElement.photo + "." + objElement.photo_format;
							} else if (strCurrentIdentityField == "building_id" || strCurrentIdentityField == "hostel_type") {
								photourl = "/english/documents/campus/photos/buildings/" + objElement.photo + "." + objElement.photo_format;
							}
						}
						break;
					}
				}
			}
			

			var strTempHTML = "";
			var infoTab = $('#infowindow-information-template').clone(true).removeAttr("id");
			var descTab = $('#infowindow-description-template').clone(true).removeAttr("id");
			var contactRow = $('#infowindow-contact-row-template').clone(true).removeAttr("id");
			var elementTitle, infoRow;
			var bHasContact = false;
			var bHasExternalLink = false;
			var bHasDescription = false;
			var infoWindowTabsArray = [];
			
			//Photo image
			
			if (photourl != "") {
				infoTab.find("img.infowindow-img").attr("src",photourl);
				if (/cuhk-campus-map-fullscreen.html/i.test(document.location.href)) {
					infoTab.find("img.infowindow-img").removeAttr("onclick").css("cursor", "default");
				} else {
					infoTab.parent().attr("href", photourl.replace(/^(photos)/,'$1/large')).attr("rel","lightbox");
				}
				descTab.find("img.infowindow-img").attr("src",photourl);
				if (/cuhk-campus-map-fullscreen.html/i.test(document.location.href)) {
					descTab.find("img.infowindow-img").removeAttr("onclick").css("cursor", "default");
				} else {
					descTab.parent().attr("href", photourl.replace(/^(photos)/,'$1/large')).attr("rel","lightbox");
				}
			} else {
				//Restructure the table
				//Please do not remove rowspan using "removeAttr" - bug of IE6
				//Information Template
				infoTab.find("tr:first-child > td:first-child").attr("colSpan",1).end(); //set the colspan attribute of the first cell of the first row to 1 -- Please do not remove rowspan using "removeAttr" - bug of IE6
				infoTab.find("tr:eq(1) > td:eq(1)").remove().end(); //Remove the 2nd cell of the 2nd row
				infoTab.find("tr:eq(1) > td:first-child").attr("rowSpan", 1).end(); //set the rowspan attribute of thethe 2nd cell of the 2nd row to 1 -- Please do not remove rowspan using "removeAttr" - bug of IE6
				
				//Description Template
				descTab.find("tr:first-child > td:first-child").attr("colSpan",1).end(); //Remove the colspan attribute of the first cell of the first row
				descTab.find("tr:eq(1) > td:eq(1)").remove().end(); //Remove the 2nd cell of the 2nd row

			}

			for (i in target_fields_array) {
			
				var strCurrentField = target_fields_name_array[i];
				var strCurrentValue = objElement[target_fields_array[i]];
				
				if (target_fields_array[i].indexOf(".") >= 0) {
				} else {
					//alert(target_fields_name_array[i]);
					if (typeof(strCurrentValue) == "string") {
						var strCurrentField = target_fields_name_array[i];
						var strCurrentValue = objElement[target_fields_array[i]];
						
						//If strCurrentValue is empty, check if it is either a building or facilities 
						if (strCurrentValue == null || strCurrentValue == "") {
							if (typeof(objElement.building) == "object") {
								
							} else if (typeof(objElement.facility) == "object") {
								
							}
						}						
						
						if (strCurrentField == "Name") {
							if (strCurrentIdentityField == "bus_stop_id") {
								var strPrefix = (objElement.bus_stop_type_id == "1" ? "Shuttle Bus Stop : " : "Shuttle Light Bus Stop : ");
								infoTab.find("td.infowindow-title").text(strPrefix + strCurrentValue);
								descTab.find("td.infowindow-title").text(strPrefix + strCurrentValue);
								
							} else if (strCurrentIdentityField == "building_id" || strCurrentIdentityField == "facilities_id") {
							
								var strTempValue = strCurrentValue + (objElement.shortname == "" ? "" : " - " + objElement.shortname);
							
								infoTab.find("td.infowindow-title").text(strTempValue);
								descTab.find("td.infowindow-title").text(strTempValue);
							} else {
								infoTab.find("td.infowindow-title").text(strCurrentValue);
								descTab.find("td.infowindow-title").text(strCurrentValue);
							}
						} else if (strCurrentField == "Website" || strCurrentField == "Tel" || strCurrentField == "Email") {
							if (strCurrentField == "Website") {
								if (strCurrentValue != "") {
								
									//Check if it is an external link (Criteria : non cuhk.edu.hk link)
									//First get the hostname;
									var objMatches = strCurrentValue.match(/^https?:\/\/[^\/]+/);

									if (objMatches.length > 0 && /cuhk.edu.hk/.test(objMatches[0]) == false) {
										//External Link -> Don't put it here and put it to External Link
										infoTab.find("a.infowindow-learnmore").attr("href", strCurrentValue).end();
										bHasExternalLink = true;
										
										contactRow.find("a.infowindow-row-website").next("span.infowindow-row-separator").remove();
										contactRow.find("a.infowindow-row-website").remove();
									} else {
										contactRow.find("a.infowindow-row-website").attr("href", strCurrentValue);
										//Add link on the title
										var objA = $("<a></a>").attr("href", strCurrentValue).attr("target", "_blank").text(infoTab.find("td.infowindow-title").text());
										infoTab.find("td.infowindow-title").html("").append(objA.clone());
										descTab.find("td.infowindow-title").html("").append(objA.clone());
										
										if (bHasContact == false) {
											bHasContact = true;
											contactRow.appendTo(infoTab.find("td.infowindow-content"));
										}
									}
									
								} else {
									contactRow.find("a.infowindow-row-website").next("span.infowindow-row-separator").remove();
									contactRow.find("a.infowindow-row-website").remove();
								}
							} else if (strCurrentField == "Tel") {
								if (strCurrentValue != "") {
									contactRow.find("span.infowindow-row-tel").text(strCurrentValue);
									if (bHasContact == false) {
										bHasContact = true;
										contactRow.appendTo(infoTab.find("td.infowindow-content"));
									}
								} else {
									contactRow.find("span.infowindow-row-tel").next("span.infowindow-row-separator").remove();
									contactRow.find("span.infowindow-row-tel").remove();
								}
							} else if (strCurrentField == "Email") {
								if (strCurrentValue != "") {
									contactRow.find("a.infowindow-row-email").attr("href", "mailto:"+strCurrentValue);
									if (bHasContact == false) {
										bHasContact = true;
										contactRow.appendTo(infoTab.find("td.infowindow-content"));
									}
								} else {
									contactRow.find("a.infowindow-row-email").remove();
								}
							}
							
						} else if (strCurrentField == "Description") {
							if (strCurrentValue != "") {
								descTab.find("td.infowindow-description").html(strCurrentValue.replace(/\r\n/g,"<br />"));
								bHasDescription = true;
							}
							//infoWindowTabsArray.push(descTab);
						} else if (strCurrentField == "External Links") {
							if (strCurrentValue != "") {
								bHasExternalLink = true;
								infoTab.find("td.infowindow-description").html(strCurrentValue.replace(/\r\n/g,"<br />"));
							}
						} else {
							if (strCurrentField == "Bus route(s)") {
								//Change the text of strCurrentField if this is a minibus stop
								if (objElement.type_id * 1 == 24) {
									strCurrentField = "Green minibus route(s)";
								}
							}
							if (strCurrentValue != "") {
								infoRow = $('#infowindow-row-template').clone(true).removeAttr("id")
									.find("span.infowindow-row-title").text(strCurrentField + ":").end()
									.find("span.infowindow-row-content").html(strCurrentValue.replace(/\r\n/g,"<br/>").replace(/^(https?):\/\/(\S+)/,"<a href='$1://$2' target='_blank'>Details</a>")).end();
								infoRow.appendTo(infoTab.find("td.infowindow-content"));
							}
						}
					}
				}
			}

			
			//Removing the separator in case the front or the end is not included.
			while (contactRow.find(":first-child").is(".infowindow-row-separator")) {
				contactRow.find("span.infowindow-row-separator:first-child").remove().end();
			}
			while (contactRow.find(":last-child").is(".infowindow-row-separator")) {
				contactRow.find("span.infowindow-row-separator:last-child").remove().end();
			}

			if (!bHasExternalLink) {
				//Please do not remove rowspan using "removeAttr" - bug of IE6
				infoTab.find("tr:eq(2)").remove().end()
					.find("tr:eq(1) > td:eq(0)").attr("rowSpan",1).end();
			}
			
			//If there is not any single information except the Title, remove the ".infowindow-content" and restructure the table and narrow the width of table
			if (infoTab.find(".infowindow-content").text() == "") {
				//Please do not remove colspan using "removeAttr" - bug of IE6
				infoTab.find(".infowindow-content").remove().end()
					.find("td[colSpan]").attr("colSpan", 1).end()
					.attr("width","250");
				descTab.attr("width","250");
			}
			
			infoWindowTabsArray.push(infoTab.wrap(document.createElement("div")).parent().html());
			if (bHasDescription) {
				infoWindowTabsArray.push(descTab.wrap(document.createElement("div")).parent().html());
			}

			return infoWindowTabsArray;
			//objMarker.openInfoWindowHtml(strTempHTML);
		} else {
			return null;
		}
	}
	
	function disableEnterSubmission(event) {
		if (event.keyCode == 13) {
			event.preventDefault();
			return false;
		}
	}
	
	// If the map position is out of range, move it back
	function checkBounds() {
		var allowedBounds = CUHK_MAP_SETTING.internal_boundary;
		
		// Perform the check and return if OK
		if (allowedBounds.contains(map.getCenter())) {
			//alert("xxxx");
			return;
		}
		// It`s not OK, so find the nearest allowed point and move there
		var C = map.getCenter();
		var X = C.lng();
		var Y = C.lat();

		var AmaxX = allowedBounds.getNorthEast().lng();
		var AmaxY = allowedBounds.getNorthEast().lat();
		var AminX = allowedBounds.getSouthWest().lng();
		var AminY = allowedBounds.getSouthWest().lat();

		if (X < AminX) {X = AminX;}
		if (X > AmaxX) {X = AmaxX;}
		if (Y < AminY) {Y = AminY;}
		if (Y > AmaxY) {Y = AmaxY;}
		//alert ("Restricting "+Y+" "+X);
		map.setCenter(new GLatLng(Y,X));
	}
	
