
	$(document).ready(function() {
	   $(window).unload( function() {
			GUnload();
	   })
	   
	   $(window).load(function() {	
			initialize();
			var url_name = $.query.get("name");
			var college_name = $.query.get("college");
			var checkbox_index = $.query.get("checkbox");
			var building_code = $.query.get("buildingcode");
			var facility_id = $.query.get("facilityid");
			var arrObjTitle = [];
			
			if ($("h3#title-" + url_name).length > 0) {
				var $objTitle = $("h3#title-" + url_name);
				
				arrObjTitle.push($objTitle);
			
				//Trace all the <h3> tag of $objTitle and push into an array.
				while ($objTitle.closest("div").attr("id") != "accordion") {
					$objTitle = $objTitle.closest("div").parent().closest("div").prev("h3");
					arrObjTitle.push($objTitle);
				}
				
				//By popping the array, simulate clicking the <h3> tag from top level to bottom level
				while (arrObjTitle.length > 0 ) {
					$objTitle = arrObjTitle.pop();
					$objTitle.click();	
				}
				
				//If there is only one checkbox within the region, check it.
				if ($objTitle.next().find(">div input:checkbox").length == 1) {
					$objTitle.next().find(">div input:checkbox:eq(0)").attr("checked", "checked");
					$objTitle.next().find(">div input:checkbox:eq(0)").triggerHandler("click");
					//$objTitle.next().find(">div >input:checkbox:eq(0)").attr("checked", "checked");
				} else if (checkbox_index != "" && /\d+/.test(checkbox_index)) {
					//alert($objTitle.next().find(">div input:checkbox").length);
					var $target_checkbox = $objTitle.next().find(">div input:checkbox:eq(" + (checkbox_index*1-1) + ")");
					
					if ($target_checkbox != null) {
						$target_checkbox.attr("checked", "checked");
						$target_checkbox.triggerHandler("click");
						//$target_checkbox.attr("checked", "checked");
					}
				}
			} else if (college_name != "") {
				for (objItem in CUHK_MAP_DATA.colleges) {
					if (CUHK_MAP_DATA.colleges[objItem].shortname == college_name) {
						var lat_lng = CUHK_MAP_DATA.colleges[objItem].lat_lng.replace(/^\((.+)\)$/,"$1").split(",");
						map.setZoom(17);
						map.panTo(new GLatLng(lat_lng[0], lat_lng[1]));
						break;
					}
				}
			} else if (building_code != "") {
				for (objItem in CUHK_MAP_DATA.buildings) {
					if (CUHK_MAP_DATA.buildings[objItem].bldg_code.toLowerCase() == building_code.toLowerCase()) {
						var lat_lng = CUHK_MAP_DATA.buildings[objItem].lat_lng.replace(/^\((.+)\)$/,"$1").split(",");
						map.setZoom(17);
						map.panTo(new GLatLng(lat_lng[0], lat_lng[1]));

						marker.setLatLng(new GLatLng(lat_lng[0], lat_lng[1]));
						marker.show();

						GEvent.addDomListener(marker,'click',function() {
							var objInfoTabsHTMLArray = showInfoWindow(CUHK_MAP_DATA.buildings[objItem]);

							if (objInfoTabsHTMLArray.length == 1) {
								marker.openInfoWindowHtml(objInfoTabsHTMLArray[0]);
							} else {
								infoWindowTabsArray[0] = new GInfoWindowTab('Information', objInfoTabsHTMLArray[0]);
								infoWindowTabsArray[1] = new GInfoWindowTab('Description', objInfoTabsHTMLArray[1]);
								marker.openInfoWindowTabsHtml(infoWindowTabsArray)
							}
						});

						marker.show();
						GEvent.trigger(marker, "click");

						break;
					}
				}
			} else if (facility_id != "") {
				for (objItem in CUHK_MAP_DATA.facilities) {
					if (CUHK_MAP_DATA.facilities[objItem].facilities_id == facility_id) {
					
						if (CUHK_MAP_DATA.facilities[objItem].lat_lng != "") {
					
							var lat_lng = CUHK_MAP_DATA.facilities[objItem].lat_lng.replace(/^\((.+)\)$/,"$1").split(",");
							map.setZoom(16);
							//map.panTo(new GLatLng(lat_lng[0], lat_lng[1]));

							var allowedBounds = CUHK_MAP_SETTING.internal_boundary;
		
							// check if the dedicated location is on CUHK Main Campus, if not, remove the Bounds restriction.
							if (allowedBounds.contains(new GLatLng(lat_lng[0], lat_lng[1])) == false) {
								GEvent.removeListener(checkBoundListener);
								map.panTo(new GLatLng(lat_lng[0], lat_lng[1]));
							}
							
							marker.setLatLng(new GLatLng(lat_lng[0], lat_lng[1]));
							marker.show();
							

							GEvent.addDomListener(marker,'click',function() {
								var objInfoTabsHTMLArray = showInfoWindow(CUHK_MAP_DATA.facilities[objItem]);

								if (objInfoTabsHTMLArray.length == 1) {
									marker.openInfoWindowHtml(objInfoTabsHTMLArray[0]);
								} else {
									infoWindowTabsArray[0] = new GInfoWindowTab('Information', objInfoTabsHTMLArray[0]);
									infoWindowTabsArray[1] = new GInfoWindowTab('Description', objInfoTabsHTMLArray[1]);
									marker.openInfoWindowTabsHtml(infoWindowTabsArray)
								}
							});

							marker.show();
							GEvent.trigger(marker, "click");
						} else {
							if ($("div#fullscreen-msgbox").length == 0)
								$("<div id='fullscreen-msgbox'></div>").appendTo("body");
								
							var objInfoTabsHTMLArray = showInfoWindow(CUHK_MAP_DATA.facilities[objItem]);
							$("div#fullscreen-msgbox").html(objInfoTabsHTMLArray[0]).dialog({title:"Note:", modal:"true", width:400});
							$(".ui-dialog-title").css("font-size", "12px");

						}

						break;
					}
				}
			}
	   });

		//Shorten the height of social-bookmarking
		$("div.tools").css("margin-top","15px");
	   
	var infoWindowTabsArray = [];
		
		//Coming to CUHK
		$("#comingCUHK-accordion").accordion({fillSpace: true, icons: { 'header': 'ui-icon-plus', 'headerSelected': 'ui-icon-minus' }});

		$("#goto-carparks").click( function(event) {
			$("#accordion").accordion( 'activate' , 4 );
			setTimeout("$('#shuttle-accordion').accordion( 'activate' , 3 )", 500);
			event.preventDefault();
		})
		$("#acc-coming-content input:checkbox").click( function() {
		
			// Note that Coming to CUHK is hardcoded.
			var tmpValue = $(this).val();
			var tmpValueArray = tmpValue.split(":");
			
			//value should in the pattern of x:y where x is the position of checkbox and y is the related id
			if (tmpValueArray.length == 2) {
				var option = tmpValueArray[0];
				var selectedFieldArray = tmpValueArray[1].split(",");
				var tmpchecked = $(this).is(":checked");

				switch (option*1) {
					case 1 : //Entrance
						for (i in selectedFieldArray) {
							showCategory(tmpchecked, "facilities", "facilities_id", selectedFieldArray[i], 'entrance.gif');
						}
						break;
					case 2 : //By MTR Station
						for (i in selectedFieldArray) {
							showCategory(tmpchecked, "facilities", "type_id", selectedFieldArray[i], 'mtr.gif');
						}
						break;
					case 3 : //Bus Stop near Main Entrance
					case 4 : //Bus Stop near CC College
					case 5 : //University Railway Station Bus Terminus
					case 6 : //Eastern Campus
						
						//Get Bus-stop information
						
						var lastMarkerLatLng = null;
						
						for (i in CUHK_MAP_DATA.facilities) {
							var objCurrentFacility = CUHK_MAP_DATA.facilities[i];
							if (objCurrentFacility.type_id == 23 || objCurrentFacility.type_id == 24 ) {
							
								if (objCurrentFacility.Marker == null) {
									if (objCurrentFacility.type_id == 23) {
										objCurrentFacility.Marker = createMarker('/english/images/campus/icons/kmb_stop.gif', new GSize(20, 20), objCurrentFacility, true);
									} else {
										objCurrentFacility.Marker = createMarker('/english/images/campus/icons/mini_b_stop.gif', new GSize(20, 20), objCurrentFacility, true);
									}
									objCurrentFacility.Marker.Owner = objCurrentFacility;
								}

								//Eastern Campus
								var bLogic1 = (/Water Sports Centre|Hong Kong Institute of Biotechnology/i.test(objCurrentFacility.facilities_name_en) && option == 6);

								//Bus Stop at Terminus
								var bLogic2 = (/Railway Station/i.test(objCurrentFacility.facilities_name_en) && option == 5);

								//Bus Stop at Chung Chi College
								var bLogic3 = (/Chung Chi College/i.test(objCurrentFacility.facilities_name_en) && option == 4);
								
								//Bus Stop at Terminus
								var bLogic4 = (/Chinese University|Chek Nai Ping/i.test(objCurrentFacility.facilities_name_en) && option == 3);
								
								if (bLogic1 || bLogic2 || bLogic3 || bLogic4 ) {
									if (tmpchecked == true) { 
										objCurrentFacility.Marker.show();
										var tmpLatLng = objCurrentFacility.lat_lng.replace(/^\((.+)\)$/,"$1").split(",");
										if (tmpLatLng.length == 2) {
											lastMarkerLatLng = new GLatLng(tmpLatLng[0], tmpLatLng[1]);
										}
									} else {
										objCurrentFacility.Marker.hide();
									}
								}
							}
						}
						
						if (lastMarkerLatLng != null) {
							map.panTo(lastMarkerLatLng);
						}
						
						break;
				}
			} else {
				return;
			}
		})
		$("#acc-coming-content input:checkbox:checked").attr("checked", "");
		
		
		//Landmarks
		for(objTypeItem in CUHK_MAP_DATA.campus) {
			var objCurrentType = CUHK_MAP_DATA.campus[objTypeItem];
			if (objCurrentType.contains_landmarks == "True") {
				var landmarkchkboxpattern = "<span class=\"landmark_item\"><input type=checkbox /><span/> <img/><br/></span>";
				var $objLandmarkChkbox = $(landmarkchkboxpattern).clone(true);
				$objLandmarkChkbox.find("input:checkbox").val(objCurrentType.id)
					.attr('focus_latlng', objCurrentType.lat_lng).end()
					.find("span").text(objCurrentType.campus_en).end()
					.find("img").attr('src', '/english/images/campus/icons/landmark.gif').end();
				$objLandmarkChkbox.appendTo("#acc-landmarks-content");
					
			}
		}

		$("span.landmark_item input:checkbox").click(function() {
			var tmpval = $(this).val();
			var tmpchecked = $(this).is(":checked");
			showCategory(tmpchecked, "landmarks", "campus_id", tmpval, 'landmark.gif');
			
			if (tmpchecked) {
				var tmpLatLng = $(this).attr('focus_latlng').replace(/^\((.+)\)$/,"$1").split(",");
				if (tmpLatLng.length == 2) {
					map.panTo(new GLatLng(tmpLatLng[0], tmpLatLng[1]));
				}
			}
		} );
		$("input:checkbox.landmark_item:checked").attr("checked", "");

		$("a#goto-culturaltrail").parent().clone().appendTo("#acc-landmarks-content").end().css("margin", "30px 0 0 -10px").css("font-size", "11px").end().remove();
		
		//Buildings
		CUHK_MAP_DATA.buildings.sort(function(a, b) {
			var compA = a.bldg_name_en.toUpperCase();
		   	var compB = b.bldg_name_en.toUpperCase();
		   return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
		});
		
		for(objItem in CUHK_MAP_DATA.buildings) {
			var rowHTMLpattern = "<tr><td><a href=\"javascript:void(0);\" class=\"bldg_listitem\" latlng=\"\" style='text-decoration:none; display:block'></a></td></tr>";
			var $objBuildingItem = $(rowHTMLpattern).clone(true);
			
			var bldgname = CUHK_MAP_DATA.buildings[objItem].bldg_name_en;
			bldgname += (CUHK_MAP_DATA.buildings[objItem].shortname == "") ? "" : (" - " + CUHK_MAP_DATA.buildings[objItem].shortname );
			bldgname += (CUHK_MAP_DATA.buildings[objItem].bldg_code == "") ? "" : (" (" + CUHK_MAP_DATA.buildings[objItem].bldg_code + ")" );
			
			$objBuildingItem.find("a").html(bldgname);
			$objBuildingItem.find("a").attr("latlng", CUHK_MAP_DATA.buildings[objItem].lat_lng);
			$objBuildingItem.find("a").attr("building_id", CUHK_MAP_DATA.buildings[objItem].building_id);
			$objBuildingItem.appendTo("#building_list");
		}
		
		$(".bldg_listitem").click(function() {
			var tmpLatLng = $(this).attr("latlng").replace(/^\((.+)\)$/,"$1").split(","); 
			map.panTo(new GLatLng(tmpLatLng[0], tmpLatLng[1]));
			marker.setLatLng(new GLatLng(tmpLatLng[0], tmpLatLng[1]));
			marker.show();
			
			var strHTML = "";
			for(objItem in CUHK_MAP_DATA.buildings) {
				if (CUHK_MAP_DATA.buildings[objItem].building_id == $(this).attr("building_id")) {

					var objInfoTabsHTMLArray = showInfoWindow(CUHK_MAP_DATA.buildings[objItem]);
				
					if (objInfoTabsHTMLArray.length == 1) {
						marker.openInfoWindowHtml(objInfoTabsHTMLArray[0]);
					} else {
						infoWindowTabsArray[0] = new GInfoWindowTab('Information', objInfoTabsHTMLArray[0]);
						infoWindowTabsArray[1] = new GInfoWindowTab('Description', objInfoTabsHTMLArray[1]);
						marker.openInfoWindowTabsHtml(infoWindowTabsArray)
					}

					break;
				}
			}
		});
		
		//Facilities
		for(objItem in CUHK_MAP_DATA.facilities_categories) {
			
			if (objItem >= CUHK_MAP_DATA.facilities_categories.length-2) {
				//the last one "Transport" and "Car Parks" need not to be displayed under "Facilities".
				continue;
			}
			var fac_cat_head_pattern = "<h3><a href=\"#\"/></h3>";
			var fac_cat_content_pattern = "<div style='padding:5px;'/>";
			var $objFacCatHead = $(fac_cat_head_pattern).clone(true);
			var $objFacCatContent = $(fac_cat_content_pattern).clone(true);
			
			var category_url_name = CUHK_MAP_DATA.facilities_categories[objItem].facilities_cate_url_name;
			
			$objFacCatHead.attr("id", "title-" + category_url_name);
			$objFacCatHead.find("a").text(CUHK_MAP_DATA.facilities_categories[objItem].facilities_cate_en);
			$objFacCatHead.appendTo("#fac-accordion");
			$objFacCatContent.appendTo("#fac-accordion");
			
			for(objTypeItem in CUHK_MAP_DATA.facilities_types) {
				var objCurrentType = CUHK_MAP_DATA.facilities_types[objTypeItem];
				if (objCurrentType.fac_cate_id == CUHK_MAP_DATA.facilities_categories[objItem].fac_cate_id) {
					var facchkboxpattern = "<div class=\"fac_list\"><input type=checkbox /><span/> <img/><br/></div>";
					var $objFacChkbox = $(facchkboxpattern).clone(true);
					$objFacChkbox.find("input:checkbox").val(objCurrentType.fac_type_id).end()
						.find("span").text(objCurrentType.fac_type_en).end()
						.find("img").attr('src', '/english/images/campus/icons/' + objCurrentType.icon).
						attr('alt', objCurrentType.fac_type_en).end();
					$objFacChkbox.appendTo($objFacCatContent);
					
				}
				
			}
		}
		$("#fac-accordion").accordion({fillSpace: true, icons: { 'header': 'ui-icon-plus', 'headerSelected': 'ui-icon-minus' }});
		$("#fac-accordion div.ui-accordion-content").height(60);
		
		$("div.fac_list input:checkbox").click(function() {
			var tmpval = $(this).val();
			var tmpchecked = $(this).is(":checked");
			var tmpIcon = $(this).parent().find("img").attr("src").replace(/[^\/]+\//g, "");
			showCategory(tmpchecked, "facilities", "type_id", tmpval, tmpIcon);
		} );
		
		//Facilities - Accommodation
		for(objTypeItem in CUHK_MAP_DATA.hostel_type) {
			var objCurrentType = CUHK_MAP_DATA.hostel_type[objTypeItem];
			var hostelchkboxpattern = "<div class=\"hostel_list\"><input type=checkbox /><span/> <img/><br/></div>";
			var $objHostelChkbox = $(hostelchkboxpattern).clone(true);
			$objHostelChkbox.find("input:checkbox").val(objCurrentType.hostel_type_id).end()
				.find("span").text(objCurrentType.hostel_type_name_en).end()
				.find("img").attr('src', '/english/images/campus/icons/' + objCurrentType.icon).end();
			$objHostelChkbox.appendTo("#fac-accordion >div:eq(3)");
		}
		
		$("div.hostel_list input:checkbox").click(function() {
			var tmpval = $(this).val();
			var tmpchecked = $(this).is(":checked");
			var tmpIcon = $(this).parent().find("img").attr("src").replace(/[^\/]+\//g, "");
			showCategory(tmpchecked, "buildings", "hostel_type", tmpval, tmpIcon);
		} );
		
		//Bus Transportation
			
		$("#shuttle-accordion").accordion({fillSpace: true, icons: { 'header': 'ui-icon-plus', 'headerSelected': 'ui-icon-minus' }});

		for(objTypeItem in CUHK_MAP_DATA.shuttle_bus_route_type) {
			var objCurrentType = CUHK_MAP_DATA.shuttle_bus_route_type[objTypeItem];
			var shuttleheaderpattern = "<h4 />";
			var $objShuttleHeader = $(shuttleheaderpattern).clone(true);
			$objShuttleHeader.text(objCurrentType.shuttle_bus_route_type_name_en);
			$objShuttleHeader.appendTo("#shuttle-accordion >div:eq(0)");
			
			for(objServiceTimeItem in CUHK_MAP_DATA.shuttle_bus_route_service_time) {

				var objCurrentServiceTime = CUHK_MAP_DATA.shuttle_bus_route_service_time[objServiceTimeItem];
				
				if (objCurrentServiceTime.shuttle_bus_route_service_type_id == objCurrentType.shuttle_bus_route_type_id ) {
				
					var shuttleservicetimepattern = "<div class='service-time'/>";
					var $objServiceTime = $(shuttleservicetimepattern).clone(true);
					$objServiceTime.text(objCurrentServiceTime.shuttle_bus_route_service_time_name_en);
					$objServiceTime.appendTo("#shuttle-accordion >div:eq(0)");
					
					generateShuttleCheckbox(objCurrentServiceTime.shuttle_bus_route_service_type_id, objCurrentServiceTime.shuttle_bus_route_service_time_id, "#shuttle-accordion >div:eq(0)");
				}
			}
			
			//If Service time is not defined in the bus route.
			generateShuttleCheckbox(objCurrentType.shuttle_bus_route_type_id, "", "#shuttle-accordion >div:eq(0)");
			
		}
		
		$("div.busroute_item input:checkbox").click(function() {
			var tmpval = $(this).val();
			var tmpchecked = $(this).is(":checked");
			showBusRoute(tmpchecked, tmpval);
			showBusStop(tmpchecked, tmpval);
		} );
		$("div.busroute_item input:checkbox").attr("checked", "");
		
		$("div.light_bus_list input:checkbox").click(function() {
			var tmpval = $(this).val();
			var tmpchecked = $(this).is(":checked");
			var tmpIcon = $(this).parent().find("img").attr("src").replace(/[^\/]+\//g, "");
			showCategory(tmpchecked, "shuttle_bus_stops", "bus_stop_type_id", "2", tmpIcon);
		} );
		$("div.light_bus_list input:checkbox").attr("checked", "");

			
		//Walking Route
		for(objItem in CUHK_MAP_DATA.walking_route) {
			var objCurrentRoute = CUHK_MAP_DATA.walking_route[objItem];
			var walkingroutechkboxpattern = "<span class=\"walkingroute_item\"><input type=checkbox /><span/> <span/><br/></span>";
			var $objWalkingRouteChkbox = $(walkingroutechkboxpattern).clone(true);
			$objWalkingRouteChkbox.find("input:checkbox").val(objCurrentRoute.walking_route_id).end()
				.find("span:eq(0)").text(objCurrentRoute.walking_route_name_en).end()
				.find("span:eq(1)").css({'display':'inline-block', 'width':'15px', 'height':'10px', 'background':objCurrentRoute.route_color}).end();
				$objWalkingRouteChkbox.appendTo("#shuttle-accordion >div:eq(2)");
		}

		$("span.walkingroute_item input:checkbox").click(function() {
			var tmpval = $(this).val();
			var tmpchecked = $(this).is(":checked");
			showWalkingRoute(tmpchecked, tmpval);
		} );
		$("span.walkingroute_item input:checkbox:checked").attr("checked", "");
		
		
		//Car Parks - Add Learn More in "Car Parks" Section
		//$("<div style='padding-top:20px;'>Enquiries: <br/>
		$("<div style='padding-top:0px;'>Contact: <br/>(852) 3943 7999 | <a class=\"infowindow-row-email\" href=\"mailto:security_unit@cuhk.edu.hk\"><img border=\"0\" title=\"\" alt=\"Email\" src=\"/english/images/campus/email.png\"></a><br/><br/><a href='http://www.cuhk.edu.hk/security_unit/eLabel.html' target='_blank' style='font-size:10px;'>More &gt;</a></div>").appendTo($("#shuttle-accordion >div:eq(3)"));
		
		$("#shuttle-accordion >div:eq(3) input:checkbox").click( function() {
			var tmpval = $(this).val();
			var tmpchecked = $(this).is(":checked");
			if ($(this).val() == 8) {
				showCategory(tmpchecked, "facilities", "type_id", tmpval, 'car_park.gif');
			} else {
				showCategory(tmpchecked, "facilities", "type_id", tmpval, 'car_park_guest.gif');
			}
			
		} );
		$("#shuttle-accordion >div:eq(3) input:checkbox").attr("checked", "");
	
		//Search Box
		$('input#positionsearchbox').jsonSuggest(searchResult, {onSelect:callback, ajaxResults:true});
		$.browser.mozilla ? $('input#positionsearchbox').keypress(disableEnterSubmission) : $('input#positionsearchbox').keydown(disableEnterSubmission);
		
		function searchResult(searchText,bWildCard,bCaseSensitive,bNotCharacter) {
		
			var tmplocationSearchArray = [];
			var re = new RegExp(searchText, "gi");
		
			//Buildings
			for(i in CUHK_MAP_DATA.buildings) {
				var strTextForSearch = CUHK_MAP_DATA.buildings[i].bldg_name_en;
				strTextForSearch += (CUHK_MAP_DATA.buildings[i].shortname == "") ? "" : (" - " + CUHK_MAP_DATA.buildings[i].shortname );

				if (re.test(strTextForSearch)) {
					tmplocationSearchArray.push({
						type:'buildings',
						id: CUHK_MAP_DATA.buildings[i].building_id,
						text: strTextForSearch,
						latlng: CUHK_MAP_DATA.buildings[i].lat_lng,
						objData : CUHK_MAP_DATA.buildings[i]
					})
				}
			}
			
			//Facilities 
			for(i in CUHK_MAP_DATA.facilities) {
				var fac_type_id = CUHK_MAP_DATA.facilities[i].type_id;
				var buildingid = CUHK_MAP_DATA.facilities[i].building_id;
				if (buildingid == "" && (fac_type_id != "8" && fac_type_id != "9")) {
					var strTextForSearch = CUHK_MAP_DATA.facilities[i].facilities_name_en;
					strTextForSearch += (CUHK_MAP_DATA.facilities[i].shortname == "") ? "" : (" - " + CUHK_MAP_DATA.facilities[i].shortname );
					if (re.test(strTextForSearch)) {
						//Not include facilities of Car Parks (fac_type_id = 8 or 9)
						tmplocationSearchArray.push({
							type:'facilities',
							id: CUHK_MAP_DATA.facilities[i].facilities_id,
							text: strTextForSearch,
							latlng:CUHK_MAP_DATA.facilities[i].lat_lng,
							objData : CUHK_MAP_DATA.facilities[i]
						})
					}
				}
			}
		
			//Landmarks
			for(i in CUHK_MAP_DATA.landmarks) {
				var strTextForSearch = CUHK_MAP_DATA.landmarks[i].landmark_name_en;
				if (re.test(strTextForSearch)) {
					tmplocationSearchArray.push({
						type:'landmarks',
						id: CUHK_MAP_DATA.landmarks[i].landmark_id,
						text: strTextForSearch,
						latlng: CUHK_MAP_DATA.landmarks[i].lat_lng,
						objData: CUHK_MAP_DATA.landmarks[i]
					})
				}
			}
		
			//Shuttle Bus Stop
			for(i in CUHK_MAP_DATA.shuttle_bus_stops) {
				var strTextForSearch = (CUHK_MAP_DATA.shuttle_bus_stops[i].bus_stop_type_id == 1 ? "Shuttle Bus Stop : " : "Shuttle Light Bus Stop : ") + CUHK_MAP_DATA.shuttle_bus_stops[i].bus_stop_name_en;
				if (re.test(strTextForSearch)) {
					tmplocationSearchArray.push({
						type:'shuttle_bus_stops',
						id: CUHK_MAP_DATA.shuttle_bus_stops[i].bus_stop_id,
						text: strTextForSearch,
						latlng:CUHK_MAP_DATA.shuttle_bus_stops[i].lat_lng,
						objData : CUHK_MAP_DATA.shuttle_bus_stops[i]
					})
				}
			}

			//sorting
			tmplocationSearchArray.sort(function(a, b) {
				var compA = a.text.toUpperCase();
				var compB = b.text.toUpperCase();
				return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
			});

		return tmplocationSearchArray;
		}
		
		//Set Accordion
		$("#accordion").accordion({fillSpace: true});
		
		//Clear All Selection
		$("a#clear_all").click(function(event) {
			$("#accordion input:checkbox:checked").each(
				function() {
					//Note that method "trigger" has a strange behavior that it calls the trigger method before change the state of the target form object,
					//rather than changing the status of the form object before calling the trigger method 
					//Therefore, please change the status and then call triggerHandler method.
					$(this).attr('checked', false);
					$(this).triggerHandler('click');
				}
			)
			
			event.preventDefault();
		});
		
		//show other campus dialog box 
		$("a#towncentres-link, a#beijing-office-link").click(function(event) {
			event.preventDefault();
			var campusid, targetCampus = null;
	
			if ($(this).attr("id") == "towncentres-link") {
				campusid = 1;
			} else {
				campusid = 2;
			}
		
			for (i in CUHK_MAP_DATA.other_campuses) {
				if (CUHK_MAP_DATA.other_campuses[i].campus_id == campusid) {
					targetCampus = CUHK_MAP_DATA.other_campuses[i];
				}
			}
		
			if (targetCampus != null) {
				$("#extra-map-dialog").dialog('option', 'title', $(this).text());
				$("#extra-map-dialog").dialog("open");
				other_map.checkResize();

				var tmpLatLng = targetCampus.lat_lng.replace(/^\((.+)\)$/,"$1").split(",");
				//alert(tmpLatLng[0]*1 + " : " + tmpLatLng[1]*1);
				other_map.setCenter(new GLatLng(tmpLatLng[0]*1, tmpLatLng[1]*1), targetCampus.defaultzoom*1);
				other_map.setZoom(targetCampus.defaultzoom);
				other_map_marker = new GMarker(other_map.getCenter());
				other_map.addOverlay(other_map_marker);
			
				var objInfoTabsHTMLArray = showInfoWindow(targetCampus);
				
				if (objInfoTabsHTMLArray.length == 1) {
					var tmpWrapper = $("<div />");					
					$(objInfoTabsHTMLArray[0]).attr("width", "250").appendTo(tmpWrapper);
					other_map_marker.bindInfoWindowHtml(tmpWrapper.html());
				} else {
					infoWindowTabsArray[0] = new GInfoWindowTab('Information', objInfoTabsHTMLArray[0]);
					infoWindowTabsArray[1] = new GInfoWindowTab('Description', objInfoTabsHTMLArray[1]);
					other_map_marker.bindInfoWindowTabsHtml(infoWindowTabsArray)
				}
				GEvent.trigger(other_map_marker, 'click');
			
			} 
		});

		
		function generateShuttleCheckbox(busRouteTypeId, busServiceTimeId, strAppendToElement) {

			for(objItem in CUHK_MAP_DATA.shuttle_bus_route) {
				var objCurrentRoute = CUHK_MAP_DATA.shuttle_bus_route[objItem];
				
				if (objCurrentRoute.route_service_type_id == busRouteTypeId && objCurrentRoute.rotue_service_time_id == busServiceTimeId) {
				
					var buschkboxpattern = "<div class=\"busroute_item\"><input type=checkbox /><span/> <span/><br/></div>";
					var $objBusChkbox = $(buschkboxpattern).clone(true);
					$objBusChkbox.find("input:checkbox").val(objCurrentRoute.route_id).end()
						.find("span:eq(0)").text(objCurrentRoute.route_name_en).end()
						.find("span:eq(1)").css({'display':'inline-block', 'width':'20px', 'height':'5px', 'background':objCurrentRoute.route_color}).end();
					$objBusChkbox.appendTo(strAppendToElement);
				}
			}
		}
		

	});
	
	function callback(item) {
		var tmpLatLng = item.latlng.replace(/^\((.+)\)$/,"$1").split(",");
		map.panTo(new GLatLng(tmpLatLng[0], tmpLatLng[1]));
		marker.setLatLng(new GLatLng(tmpLatLng[0], tmpLatLng[1]));
		
		marker.show();
		
		GEvent.addDomListener(marker,'click',function() {
			var objInfoTabsHTMLArray = showInfoWindow(item.objData);

			if (objInfoTabsHTMLArray.length == 1) {
				marker.openInfoWindowHtml(objInfoTabsHTMLArray[0]);
			} else {
				infoWindowTabsArray[0] = new GInfoWindowTab('Information', objInfoTabsHTMLArray[0]);
				infoWindowTabsArray[1] = new GInfoWindowTab('Description', objInfoTabsHTMLArray[1]);
				marker.openInfoWindowTabsHtml(infoWindowTabsArray)
			}
		});

		marker.show();
		GEvent.trigger(marker, "click");
		
	}

	//this event can be detected by Internet Explorer
	window.onbeforeprint = function() {
		//IE 6  cannot print the tile layer promptly. Give alert warning to IE6 users
		if($.browser.msie && $.browser.version * 1 < 7) {
			alert("CUHK Campus Map does not support print function in Internet Explore 6 or below. \r\nPlease use Internet Explorer with higher version or download the print version for printing.");
		}
	}

	function showImage(obj) {
		var strSrc = obj.src.replace(/\/photos\//,'/photos/large/');
		var title = $(obj).closest("table").find("td.infowindow-title").text();
		$.slimbox(strSrc, title);
	}

	


