if (typeof(_leftBarWidth) == "undefined") {
	var _leftBarWidth;
}
if (_leftBarWidth == null || _leftBarWidth == "") {
	_leftBarWidth = 245;
}
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();
}

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";
	}
}

function setMapCoord(coord_x, coord_y, type, ref_id, show_popup) {
	var markers = map.getLayerByName("Markers");
	markers.clearMarkers();

	var marker = new OpenLayers.Marker(new OpenLayers.LonLat(coord_x, coord_y), icon.clone(), type, ref_id);
	markers.addMarker(marker);
	map.setCenter(new OpenLayers.LonLat(coord_x, coord_y), 6);
	map.events.triggerEvent("dragend");

	if (show_popup){
		marker.onclick();
	}
}

function getDetailByCoord(coord) {
	var markers = map.getLayerByName("Markers");
	markers.clearMarkers();
	var marker = new OpenLayers.Marker(coord, icon.clone(), "coord", null);
	markers.addMarker(marker);
	//map.setCenter(coord);
	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');
		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);
	var uri = "/map_sz/search.jsp?lang=" + map.lang + "&qryName=" + encodeURI($("qryName").value);
	uri =encodeURI(uri);
	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();
}

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 = 390;
	var dxDivSearchPlaneContent = 140;	
	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("/mapsz/gmap.jsp?poi_indicator="+poi_indicator+"&level="+map_zoom_level+"&x="+clicked_geox+"&y="+clicked_geoy,"GoogleMapWindow",popup_option);

	if (window.focus) {GoogleMapWindow.focus()}
}

