if (typeof(_leftBarWidth) == "undefined") {
	var _leftBarWidth;
}
if (_leftBarWidth == null || _leftBarWidth == "") {
	_leftBarWidth = 219;
}
if (typeof(_headerHeight) == "undefined") {
	var _headerHeight;
}
if (_headerHeight == null || _headerHeight == "") {
	_headerHeight = 80;
}
if (typeof(_lang) == "undefined") {
	var _lang;
}
if (_lang == null || _lang == "") {
	_lang = "tc";
}
var _disableStreeView = "N";
var _disableControl = "N";
var _DefaultX = null;
var _DefaultY = null;

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(){
	var headerHeight = _headerHeight;

	if($("headerBarStatus").value=="block"){
		hideLayer($('hearderContent'));
		hideLayer($('btnHeaderMin'));
		showLayer($('btnHeaderMax'));
		$('StreetView').style.height = "100%";
		$('StreetView').style.top = "0px";	
		$('OpenLayers_Control_fullscreenDiv').style.top = "-95px";	
		$("headerBarStatus").value = "none";
		$('dvLeft').style.top = "3px";
	}else{
		showLayer($('hearderContent'));
		hideLayer($('btnHeaderMax'));
		showLayer($('btnHeaderMin'));
		$('StreetView').style.height = "90%";
		$('StreetView').style.top = "83px";	
		$('OpenLayers_Control_fullscreenDiv').style.top = "-60px";	
		
		$("headerBarStatus").value = "block";
		$('dvLeft').style.top = (headerHeight + 3) + "px";
	}
	setSearchPlaneHeight();
	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 (_disableControl != 'Y')
	{
		if($("barStatus").value=="none" && $("headerBarStatus").value=="none") {
			$("btnHideAll").style.display = "none";
			$("btnDisplayAll").style.display = "block";
		}
		else {
			$("btnHideAll").style.display = "block";
			$("btnDisplayAll").style.display = "none";
		}
	}

	if ($("map").style.width == "50%")
	{
		$("btnHideAll").style.right = $("map").offsetWidth;
		$("btnDisplayAll").style.right = $("map").offsetWidth+5;
		
		if (_disableControl != 'Y')
		{
			$("midlandMap").style.right = $("map").offsetWidth +130;
			$("midlandMapText").style.right = $("map").offsetWidth+3;
		}
	}
	else
	{
		$("btnHideAll").style.right = "0px";
		$("btnDisplayAll").style.right = "5px";

		if (_disableControl != 'Y')
		{
			$("midlandMap").style.right ="130px";
			$("midlandMapText").style.right ="3px";
		}
	}
}

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);
		}
	}
}

function goSearchByDistStr(DistStr) {
	if (DistStr != null && DistStr != "") {
		var districtValueArray = DistStr.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);
	}
}


function goSearchBycoord(srtId, coord_x, coord_y, show_popup) {
	setMapCoord(coord_x, coord_y, "Street", srtId, 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();
	setSearchPlaneHeight();
	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 && currentHeaderWidth >0)
		$('dvHeader').style.width = currentHeaderWidth + "px";
}

function setLeftHeight() {
	var currentLeftBarHeight;
	var currentHeaderHeight;
	if($("headerBarStatus").value=="block"){
		currentHeaderHeight = _headerHeight;
	}else{
		currentHeaderHeight = 0;
	}
	currentLeftBarHeight = map.getCurrentSize().h - currentHeaderHeight + 2;
	
	if ($('searchPlane') != null && currentLeftBarHeight > 0)
		$('searchPlane').style.height = currentLeftBarHeight + "px";

}

function setSearchPlaneHeight() {
	var dxSearchResultHeight = 340;
	var dxDivSearchPlaneContent = 150;	
	var currentLeftBarHeight;
	var currentHeaderHeight;

	if($("headerBarStatus").value=="block"){
		currentHeaderHeight = _headerHeight;
	}else{
		currentHeaderHeight = 0;
	}

	var searchResultHeight = map.getCurrentSize().h - currentHeaderHeight - dxSearchResultHeight;
	var divSearchPlaneContent = map.getCurrentSize().h - currentHeaderHeight - 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){};
}

var sv = new google.maps.StreetViewService();
var panorama;

function checkSVExist(x,y)
{
sv.getPanoramaByLocation(new google.maps.LatLng(y,x),50 ,processSVData);
}

function initialize(x,y) {
var divpano = document.getElementById('pano');
divpano.style.display = '';
cafe = new google.maps.LatLng(y,x);
var panoramaOptions = {
  position: cafe,
  pov: {
	heading: 0,
	pitch: 0,
	zoom: 1
  },
  visible: true
};
sv.getPanoramaByLocation(cafe,50 ,processSVData);
//document.getElementById("googleframe").src= "http://maps.google.com/maps?hl=zh-HK&q=&ie=UTF8&z=18&layer=c&cbll="+ y + "," + x + "&cbp=13,324.0,0,0,-10&source=embed&output=svembed";
}

function processSVData(data, status) {  
  var divNoData = document.getElementById('NoData');
  var divpano = document.getElementById('pano');
  var imgSv = document.getElementById('OpenLayers_Control_StreetViewDiv_innerImage');
	  
	  if (status == google.maps.StreetViewStatus.OK) {
			
			imgSv.src = "img/icon_sv.png";
			
			var latlngStr = data.location.latLng.toString().split(",",3);
			
			var lat = parseFloat(latlngStr[0].replace('(',''));
			var lng = parseFloat(latlngStr[1]);

			if (document.getElementById("map").style.width == "50%")
		    {
				
				document.getElementById("googleframe").src= "http://maps.google.com/maps?hl=zh-HK&q=&ie=UTF8&z=18&layer=c&cbll="+ lat + "," + lng + "&panoid=" + data.location.pano + "&cbp=13,324.0,0,0,-10&source=embed&output=svembed";
		    }


			if (document.getElementById("map").style.width == "50%" )
			{
				divpano.style.display = '';
				divNoData.style.display = 'none';
				imgSv.alt = "\u95dc\u9589\u8857\u666f\u5716";
				imgSv.title = "\u95dc\u9589\u8857\u666f\u5716";
			}
			else
			{
				imgSv.title = "\u958b\u555f\u8857\u666f\u5716";
				imgSv.alt = "\u958b\u555f\u8857\u666f\u5716";
			}
	  }
	  else
	  {
		    
			imgSv.src = "img/icon_sv_off.png";
			if (document.getElementById("map").style.width == "50%" )
			{
				divpano.style.display = 'none';
				divNoData.style.display = '';
				imgSv.alt = "\u95dc\u9589\u8857\u666f\u5716";
				imgSv.title = "\u95dc\u9589\u8857\u666f\u5716";
			}
			else
			{
				imgSv.title = "";
				imgSv.alt = "";
			}
	  }
}
var svimage;
function sv_mouse_over()
{
	
	if (document.getElementById("map").style.width == "50%" || 
		document.getElementById("OpenLayers_Control_fullscreenDiv").style.display == '')
	{
		var imgSv = document.getElementById('OpenLayers_Control_StreetViewDiv_innerImage');
		svimage = imgSv.src;
		imgSv.src= "img/icon_sv_close.png";

	}
}

function sv_mouse_out()
{
	if (document.getElementById("map").style.width == "50%" || 
		document.getElementById("OpenLayers_Control_fullscreenDiv").style.display == '')
	{
		var imgSv = document.getElementById('OpenLayers_Control_StreetViewDiv_innerImage');
		imgSv.src = svimage;
	}
}

if (navigator.userAgent.indexOf("Chrome") != -1)
{
	window.addEventListener ("resize", resizemap, false);
}
else
{
	window.onresize = resizemap;
}
function resizemap()
{
	if (_disableControl == 'Y')
	{
		document.getElementById('midlandMapText').style.display = 'none';
		document.getElementById('midlandMap').style.right = '3px';
		return;
	}
	var winW, winH;
	if (document.documentElement && document.documentElement.clientWidth) {
        winW = document.documentElement.clientWidth;
        winH = document.documentElement.clientHeight;
		
    } else if (document.body) {
        winW = document.body.clientWidth;
        winH = document.body.clientHeight;
		
    }
	else if (self.innerWidth) {
        winW = self.innerWidth;
        winH = self.innerHeight;

    }

	if (winW <350 || winH < 300 )
	{
		document.getElementById('midlandMap').style.display = 'none';
		document.getElementById('midlandMapText').style.display = 'none';
	}
	else
	{
		document.getElementById('midlandMap').style.display = '';
		document.getElementById('midlandMapText').style.display = '';
	}
	
}

function disableControl()
{
	_disableControl = 'Y';
	$("btnHideAll").style.display = "none";
	$("btnDisplayAll").style.display = "none";
}
