if (typeof(_leftBarWidth) == "undefined") {
	var _leftBarWidth;
}
if (_leftBarWidth == null || _leftBarWidth == "") {
	_leftBarWidth = 245;
}
if (typeof(_headerHeight) == "undefined") {
	var _headerHeight;
}
if (_headerHeight == null || _headerHeight == "") {
	_headerHeight = 80;
}
if (typeof(_lang) == "undefined") {
	var _lang;
}
if (_lang == null || _lang == "") {
	_lang = "tc";
}

function $(obj){return document.getElementById(obj);}

function switchLeftBar(){
	var leftBarWidth = _leftBarWidth;	
	
	if($("barStatus").value=="block"){
		$('dvLeft').style.left = -1*leftBarWidth;
		$('dvHeader').style.left = 0;
		hideLayer($('btnSearchMin'));
		showLayer($('btnSearchMax'));
		$("barStatus").value = "none";
	}else{
		$('dvLeft').style.left = 0;
		$('dvHeader').style.left = leftBarWidth;
		hideLayer($('btnSearchMax'));
		showLayer($('btnSearchMin'));
		$("barStatus").value = "block";
	}

	setHeaderWidth();
	setMapStartPoint();
	setBtnHideAllDisplay();
}

function switchHeaderBar(){
	if($("headerBarStatus").value=="block"){
		hideLayer($('hearderContent'));
		hideLayer($('btnHeaderMin'));
		showLayer($('btnHeaderMax'));
		$("headerBarStatus").value = "none";
	}else{
		showLayer($('hearderContent'));
		hideLayer($('btnHeaderMax'));
		showLayer($('btnHeaderMin'));
		$("headerBarStatus").value = "block";
	}
	setMapStartPoint();
	setBtnHideAllDisplay();
}

function hideAllBar() {
	if($("barStatus").value=="block")
		switchLeftBar();
	
	if($("headerBarStatus").value=="block")
		switchHeaderBar();
}
function displayAllBar() {
	if($("barStatus").value=="none")
		switchLeftBar();
	
	if($("headerBarStatus").value=="none")
		switchHeaderBar();
}

function setBtnHideAllDisplay() {
	if($("barStatus").value=="none" && $("headerBarStatus").value=="none") {
		$("btnHideAll").style.display = "none";
		$("btnDisplayAll").style.display = "block";
	}
	else {
		$("btnHideAll").style.display = "block";
		$("btnDisplayAll").style.display = "none";
	}
}

function setMapStartPoint() {
	var startX = 0;
	var startY = 0;
	
	if($("barStatus").value=="block")
		startX = _leftBarWidth;

	if($("headerBarStatus").value=="block")
		startY = _headerHeight;

	if (map)
		map.startPoint =  new OpenLayers.Pixel(startX, startY);
}

function setMapCoord(coord_x, coord_y, type, ref_id, show_popup) {
	setDetailMarkerCenter(coord_x, coord_y, type, ref_id, show_popup, "Markers", icon);
}

function setLocatedMapCoord(coord_x, coord_y, type, ref_id, show_popup) {
	setDetailMarkerCenter(coord_x, coord_y, type, ref_id, show_popup, "LocateMarkers", locateIcon);
}

// private function
function setDetailMarkerCenter(coord_x, coord_y, type, ref_id, show_popup, markerType, iconType) {
	var zoomlevel = 6;
	if (coord_x > 0 && coord_y > 0) {
		setDetailMarker(new OpenLayers.LonLat(coord_x, coord_y), type, ref_id, show_popup, markerType, iconType);

		map.setCenter(new OpenLayers.LonLat(coord_x, coord_y), zoomlevel);
		map.events.triggerEvent("dragend");
	}
}

function getDetailByCoord(coord, show_popup) {
	setDetailMarker(coord, "coord", null, show_popup, "Markers", icon);
}

function getLocateDetailByCoord(coord, show_popup) {
	setDetailMarker(coord, "coord", null, show_popup, "LocateMarkers", locateIcon);
}

// private function
function setDetailMarker(coord, type, ref_id, show_popup, markerType, iconType) {
	var markers = map.getLayerByName(markerType);
	markers.clearMarkers();

	var marker = new OpenLayers.Marker(coord, iconType.clone(), type, ref_id);
	markers.addMarker(marker);
	//map.setCenter(coord);

	if (show_popup){
		marker.onclick();
	}
}

/**
 * onDistrictChange in search plane
 */
function goSearchByDist() {
	if ($("district").value != null && $("district").value != "") {
		var districtValueArray = $("district").value.split("|");
		if (districtValueArray.length >= 3) {
			var districtId = districtValueArray[0];
			var districtX = districtValueArray[1];
			var districtY = districtValueArray[2];

			map.setCenter(new OpenLayers.LonLat(districtX, districtY), 3);
		}
	}
}

/**
 * onLoad street ID request
 */
function goSearchBySrt(srtId, coord_x, coord_y, show_popup) {
	if (srtId != null && srtId != "") {
		setMapCoord(coord_x, coord_y, "Street", srtId, show_popup);
	}
}

/**
 * onLoad estate ID request
 */
function goSearchByEst(estId, coord_x, coord_y, show_popup) {
	if (estId != null && estId != "") {
		setMapCoord(coord_x, coord_y, "PISEstate", estId, show_popup);
	}
}

/**
 * onLoad building ID request
 */
function goSearchByBldg(bldgId, coord_x, coord_y, show_popup) {
	if (bldgId != null && bldgId != "") {
		setMapCoord(coord_x, coord_y, "PISBuilding", bldgId, show_popup);
	}
}

/**
 * @param {Obj} objPlane
 * @param {String} contentHTML
 */
function setContentHTML(objPlane, contentHTML) {
	if (contentHTML != null) {
		objPlane.innerHTML = contentHTML;
	}
}

/**
 * @param {Obj} objPlane
 */
function showLayer(objPlane) {
	objPlane.style.display = "block";
};

/**
 * @param {Obj} objPlane
 */
function hideLayer(objPlane) {
	objPlane.style.display = "none";
};

/**
 * @param {String} content
 */
function getDetailFrameHTML (content) {
	var showtxt = "<table cellspacing=0 cellpadding=0 width=100%>" + 
				"<tr><td class='detailTopLeft'></td>" + 
					"<td class='detailTop' align=right></td>" + 
					"<td class='detailTopRight'></td></tr>" + 
				"<tr><td class='detailLeft'></td><td class='detailContent'>" + 
					content + 
				"</td><td class='detailRight'></td></tr>" + 
				"<tr><td class='detailBottomLeft'></td><td class='detailBottom'></td><td class='detailBottomRight'></td></tr>" +
				"</table>";
	return showtxt;
};
/**
 * @param {String} content
 */
function getDetailFrameLoadingHTML () {
	return "<br><br><br><center><img src='./img/loading.gif'><center><br><br><br>";
};
/**
 * @param {String} response
 */
function UrlReturnSuccessSearchResult (response) {
	showtxt = response.responseText;
	setContentHTML($("searchResult"), showtxt);
	hideLayer($("searchLoading"));
	$("searchResult").scrollTop = 0;
};

/**
 * @param {String} response
 */
function UrlReturnFailSearchResult (response) {
	showtxt = "<font color='black'>Connection fail. </font>";
	setContentHTML($("searchResult"), showtxt);
	hideLayer($("searchLoading"));
};

function submitSearchRequest(qryType, qryName) {
	if (qryName != null) {
		$("qryName").value = qryName;
		$("qryName").text = qryName;
	}

	if (qryType != null)
		$("qryType").value = qryType;
	else
		$("qryType").value = $("ddlQryType").options[$("ddlQryType").selectedIndex].value;

	searchRequest();
}

function searchRequest(page, pageSize) {
	if ($("qryName").value == "")
	{
		alert('Please input your query ½Ð¿é¤JÃöÁä¦r');
		return;
	}

	showLayer($("searchLoading"));
	//setContentHTML($("searchResult"), "<center><img src='./img/ajax-loader.gif'><center>");
	var uri = "/map_hk/search.jsp?lang=" + map.lang + "&qryName=" + encodeURI($("qryName").value);
	if ($("qryType").value != null)
		uri += "&qryType=" + $("qryType").value
	if (page != null)
		uri += "&page=" + page
	if (pageSize != null)
		uri += "&pageSize=" + pageSize

	var results = OpenLayers.loadURL(uri, '', this, UrlReturnSuccessSearchResult, UrlReturnFailSearchResult);
}

function setFrontEndComponent() {
	setSearchPlaneHeight();
	setHeaderWidth();
	setMapStartPoint();
}

function setFrontEndComponentWithLayers() {
	setBtnHideAllDisplay();
}

function setHeaderWidth() {
	var currentLeftBarWidth;
	var currentHeaderWidth;
	if($("barStatus").value=="block"){
		currentLeftBarWidth = _leftBarWidth;
	}else{
		currentLeftBarWidth = 0;
	}
	currentHeaderWidth = map.getCurrentSize().w - currentLeftBarWidth + 2;
	
	if ($('dvHeader') != null)
		$('dvHeader').style.width = currentHeaderWidth + "px";
}

function setSearchPlaneHeight() {
	var dxSearchResultHeight = 400;
	var dxDivSearchPlaneContent = 150;	
	var searchResultHeight = map.getCurrentSize().h - dxSearchResultHeight;
	var divSearchPlaneContent = map.getCurrentSize().h - dxDivSearchPlaneContent;

	if (searchResultHeight < 0)
		searchResultHeight = 1;
	if (divSearchPlaneContent < 0)
		divSearchPlaneContent = 1;

	if ($('searchResult') != null)
		$('searchResult').style.height = searchResultHeight + "px";
	if ($('divSearchPlaneContent') != null)
		$('divSearchPlaneContent').style.height = divSearchPlaneContent + "px";
}

function show_google_map() {
	var popup_x = 100;
	var popup_y = 100;
	var popup_option = "width=800,height=600,top="+popup_y+",screenY="+popup_y+",left="+popup_x+",screenX="+popup_x+",scrollbars=no,resizable=no";
	
	var map_zoom_level = map.getZoom();
	var map_center_x = map.getCenter().lon;
	var map_center_y = map.getCenter().lat;

	//if(poi_indicator == 0)
		GoogleMapWindow = window.open("/map/gmap.jsp?sys=longlat&level="+map_zoom_level+"&x="+map_center_x+"&y="+map_center_y,"GoogleMapWindow",popup_option);
	//else
	//	GoogleMapWindow = window.open("/map/gmap.jsp?poi_indicator="+poi_indicator+"&level="+map_zoom_level+"&x="+clicked_geox+"&y="+clicked_geoy,"GoogleMapWindow",popup_option);

	if (window.focus) {GoogleMapWindow.focus()}
}
/*
function resizeDisplayImage(nameRS, name, count) {
	var imgRS = document.getElementById(nameRS);
	var img = document.getElementById(name);

	if (imgRS && img) {
		var count = eval(count) + 1;
		var w = eval(imgRS.width);
		var h = eval(imgRS.height);
		var maxw = eval(img.width);
		var maxh = eval(img.height);
		var neww, newh;

		if (w <= 50 && count <= 10) {
			setTimeout("resizeDisplayImage('" + nameRS + "', '" + name + "', " + count + ")", 1000);
		} else {

			if (w > maxw || h > maxh) {
				if ((w / maxw) > (h / maxh)) {
					neww = maxw;
					newh = Math.floor(h * (maxw / w));

					if (newh > maxh) {
						neww = Math.floor(neww * (maxh / newh));
						newh = maxh;
					}
				} else {
					newh = maxh;
					neww = Math.floor(w * (maxh / h));
					if (neww > maxw) {
						newh =Math.floor(neww * (maxw / neww));
						neww = maxw;
					}
				}
				img.width = neww;
				img.height = newh;
			} else {
				img.width = w;
				img.height = h;
			}
			img.style.visibility = "";
		}
	}
}

function resize4in1Image() {
	resizeDisplayImage('imgOutlookRealSize', 'imgOutlook', 1);
	resizeDisplayImage('imgFloorplanRealSize', 'imgFloorplan', 1);
}
*/
function getLocatedLonLat() {
	var lonlat = null;
	var result = null;
	var locateMarkers = map.getLayerByName("LocateMarkers");
	if (locateMarkers)
		if (locateMarkers.markers)
			if (locateMarkers.markers[0])
				lonlat = locateMarkers.markers[0].lonlat;
	
	if (lonlat)
		result = lonlat.lon + "||" + lonlat.lat;

	return result;
}

function disablePopup() {
	var Marker = OpenLayers.Marker;
	if (Marker)
		OpenLayers.Marker.prototype.onclick = function(evt){};
}