var maxLon=0,maxLat=0;
var minLon=0,minLat=0;
var isGoogleRequest = false;
var isMapbarRequest = false;

function showMapBar() {
	$("list_ditu_mapbar_handler").style.backgroundImage = "url(/theme/ie/images/realestate/tag_ditu_on.gif)";
	$("list_ditu_google_handler").style.backgroundImage = "url(/theme/ie/images/realestate/tag_wxt_off.gif)";
	$("list_price_chart_handler").style.backgroundImage = "url(/theme/ie/images/realestate/priceChartOff.gif)";
		
	$("mapbar").style.display = "";
	$("map").style.display = "none";
	$("propertyPriceContainer").style.display = "none";
}
function initGoogleMap() {
	onMapLoad();
	document.getElementsByTagName("body")[0].onunload = function() {
		GUnload();
	}
}
var gmap;
var centerLat,centerLon;
function showGoogle() {
	$("list_ditu_mapbar_handler").style.backgroundImage = "url(/theme/ie/images/realestate/tag_ditu_off.gif)";
	$("list_ditu_google_handler").style.backgroundImage = "url(/theme/ie/images/realestate/tag_wxt_on.gif)";
	$("list_price_chart_handler").style.backgroundImage = "url(/theme/ie/images/realestate/priceChartOff.gif)";
		
	$("mapbar").style.display = "none";
	$("map").style.display = "";
	$("propertyPriceContainer").style.display = "none";
	if(!isGoogleRequest) {
		if (gmap == null) {
			var count = 0;
			var lonValueNode = null;
			var latValueNode = null;
			var lonValue = 0;
			var latValue = 0;
			var strll = null;
			var publishDays = null;
			var moyaIcon = null;
			gmap = new google.maps.Map(document.getElementById("gmap"));
			gmap.addControl(new GLargeMapControl());
			centerLat = (maxLat+minLat)/2;
			centerLon = (maxLon+minLon)/2;
			var centerPoint = new google.maps.LatLng(centerLat, centerLon);
			gmap.setCenter(centerPoint, 14);
			gmap.setMapType(G_SATELLITE_MAP);
			
			while(true) {    
				lonValueNode = $('housingLongitude'+count);
				if(lonValueNode == null) {
					break;
				}
				latValueNode = $('housingLatitude'+count);
				lonValue = lonValueNode.value - 0;
				latValue = latValueNode.value - 0;
									
				publishDays = $('publishDays'+count).value;
				if(publishDays < 1) {
					moyaIcon = getMoyaIcon('true');
				}else {
					moyaIcon = getMoyaIcon('false');
				}
				
				var point = new GLatLng(latValue,lonValue);
				var marker = createMarker(point,moyaIcon,count);
					
				gmap.addOverlay(marker);
					
				count ++;
			}
	    }
	}
}
function createMarker(point, moyaIcon, count) {
	var housingContent = "";
	if(count > -1) {
		housingContent = "<table width='200' border='0' cellspacing='3' cellpadding='0'><tr><td width='205' bgcolor='#FFFFCC' class='title' >"
						+$('postAddrs'+count).value+"</td></tr><tr><td class='price'>"+$('jg'+count).innerHTML+"</td></tr><tr><td class='zhengwen'>"
						+$('hx'+count).innerHTML+"，"+$('mj'+count).innerHTML+"</td></tr><tr><td><a href=\"/housing/viewHousing.htm?housingId="
						+$('housingId'+count).value+"\" target='_blank'>[ 详情 ]</a></td></tr></table>";
	}
	
	var marker = new GMarker(point,{icon: moyaIcon});
	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowHtml(housingContent);
	});
	return marker;
}
var maplet = null; 
function onMapLoad(){
	var isCentered = false;
	var count = 0;
	var lonValueNode = null;
	var latValueNode = null;
	var lonValue = 0;
	var latValue = 0;
	var strll = null;
	var publishDays = null;
	maplet = new Maplet("mapbar");
	
	while(true) {
		lonValueNode = $('housingLongitude'+count);
		if(lonValueNode == null) {
			break;
		}			
		latValueNode = $('housingLatitude'+count);
		lonValue = lonValueNode.value - 0;
		latValue = latValueNode.value - 0;
		
		publishDays = $('publishDays'+count).value;

		var iconImage = '';
		if(publishDays < 1) {
			iconImage=moyaIconStringNew;
		}else {
			iconImage=moyaIconString;
		}
		var tmpMarkerContent="<table width='200' border='0' cellspacing='3' cellpadding='0'><tr><td class='price'>"+$('jg'+count).innerHTML+"</td></tr><tr><td class='zhengwen'>"
			+$('hx'+count).innerHTML+"，"+$('mj'+count).innerHTML+"</td></tr><tr><td><a href='/housing/viewHousing.htm?housingId="
			+$('housingId'+count).value+"' target='_blank'>[ 详情 ]</a></td></tr></table>";
		var marker = new MMarker(  
	    	    new MPoint(lonValue,latValue),
	    	    new MIcon(iconImage,32,32),
	    	    new MInfoWindow($('postAddrs'+count).value,tmpMarkerContent)
	    	);  
	    	  
	    maplet.addOverlay(marker);
		
		if(count == 0) {
			maxLon = lonValue;
			maxLat = latValue;
			minLon = lonValue;
			minLat = latValue;
		}else {
			if(lonValue > maxLon) {
				maxLon = lonValue;
			}else {
				if(lonValue < minLon) {
					minLon = lonValue;
				}
			}
			if(latValue > maxLat) {
				maxLat = latValue;
			}else {
				if(latValue < minLat) {
					minLat = latValue;
				}
			}
		}
		count ++;
	}
	maplet.centerAndZoom(new MPoint((maxLon+minLon)/2,(maxLat+minLat)/2), 10);  
    maplet.addControl(new MStandardControl()); 
}

function enterKey(evt) {
	var evt = window.event||evt; 
	if(evt.keyCode==13){
		evt.returnValue=false;
   		evt.cancel = true;
		return true;
	}
	return false;
}

function listCond(posId){
	var selectValue = $(posId).options[$(posId).selectedIndex].value;
 	var selectValues = selectValue.split("-");
 	if(posId == "totalPriceList") {
 		searchUrl = "housingPriceFrom=" + selectValues[0] + "&housingPriceTo=" + selectValues[1];
 	}
 	if(posId == "unitPriceList") {
 		searchUrl = "housingUnitPriceFrom=" + selectValues[0] + "&housingUnitPriceTo=" + selectValues[1];
 	}
 	if(posId == "bedRoomsList") {
 		searchUrl = "housingBedRooms=" + selectValues[0];
 	}
 	if(posId == "areaSizeList") {
 		searchUrl = "housingAreaFrom=" + selectValues[0] + "&housingAreaTo=" + selectValues[1];
 	}
 	if(posId == "totalPriceOrder") {
 		searchUrl = "totalPriceOrder=" + selectValues[0];
 	}
 	loadHousing(null,searchUrl);
}

function changePrice(clickNode, ival) {
	var selectValues = ival.split("-");
	var searchUrl = "housingPriceFrom=" + selectValues[0] + "&housingPriceTo=" + selectValues[1];
	var spans = $('changePriceTable').getElementsByTagName('span');
	for (var i = 0 ; i < spans.length ; i ++) {
		if (clickNode == spans[i]) {
			spans[i].style.fontWeight = 'bold';
			spans[i].style.color = '#FF6600';
		} else {
			spans[i].style.fontWeight = 'normal';
			spans[i].style.color = '';
		}
	}
	loadHousing(null,searchUrl);
}

function changeRoom(clickNode, ival) {
	var selectValues = ival.split("-");
	var searchUrl = "housingBedRooms=" + selectValues[0];
	var spans = $('changeRoomTable').getElementsByTagName('span');
	for (var i = 0 ; i < spans.length ; i ++) {
		if (clickNode == spans[i]) {
			spans[i].style.fontWeight = 'bold';
			spans[i].style.color = '#FF6600';
		} else {
			spans[i].style.fontWeight = 'normal';
			spans[i].style.color = '';
		}
	}
	loadHousing(null,searchUrl);
}

function changeSize(clickNode, ival) {
	var selectValues = ival.split("-");
	var searchUrl = "housingAreaFrom=" + selectValues[0] + "&housingAreaTo=" + selectValues[1];
	var spans = $('changeSizeTable').getElementsByTagName('span');
	for (var i = 0 ; i < spans.length ; i ++) {
		if (clickNode == spans[i]) {
			spans[i].style.fontWeight = 'bold';
			spans[i].style.color = '#FF6600';
		} else {
			spans[i].style.fontWeight = 'normal';
			spans[i].style.color = '';
		}
	}
	loadHousing(null,searchUrl);
}

function changeProperty(propertyIds) {
	var housingType = $('housingType').value;
	var url = '/housing/listMonHousing.htm?housingType=' + housingType + '&propertyIds=' + propertyIds;
	window.location.href = url;
	return false;
}

function loadHousing(pageNumber, searchUrl) {
	if ($('listType') != null) {
		if (searchUrl == null) {
			searchUrl = "listType=" + $('listType').value;
		} else {
	 		searchUrl += "&listType=" + $('listType').value;
	 	}
 	}
	getHousingsList(pageNumber, searchUrl);
	if (pageNumber != null) {
 		window.location.href = "#ad_list";
 	}
}

function showLoadingDiv() {
	$("list_housings_loading").style.display = "";
	$("list_housings_loading_text").style.display = "";
	$("housingsList").style.display = "none";
}
function hideLoadingDiv() {
	$("list_housings_loading").style.display = "none";
	$("list_housings_loading_text").style.display = "none";
	$("housingsList").style.display = "";
}

var isRequesting = false;
function getHousingsList(pageNumber, searchUrl) {
	if(!isRequesting) {
		var housingType = $('housingType').value;
		var url = '';
		if (housingType != null && housingType == 'rent') {
			url = '/rent/housing/getHousingsList.htm';
		} else {
			url = '/housing/getHousingsList.htm';
		}
		var param = '';
		if (pageNumber != null) {
			param += "housingPageNumber=" + pageNumber;
		} else {
			param += searchUrl;
		}
		param += "&type=mon&housingPageSize=15&propertyIds=" + $("propertyIds").value
			+ "&housingType=" + housingType + "&random=" + Math.random();
		showLoadingDiv();
		isGoogleRequest = false;
		isMapbarRequest = false;
		isRequesting = true;
		var homeCenter = $('housingsList');
		var moyaUpdater = new Ajax.Updater(homeCenter,url, 
			{method: 'post',parameters: param,
				onComplete:function(){
					isRequesting = false;
					hideLoadingDiv();
					/*if ($("mapbar").style.display != "none") {
						showMapBar();
					} else {
						if ($("map").style.display != "none") {
							showGoogle();
						}
					}*/
					$("housingCountSpan").innerHTML = $("housingCount").value;
				}
			});
	}
}
function initListMonHousingPage() {
	var housingType = $('housingType').value;
	if (housingType == null || housingType=='' || housingType == 'buy') {
		//getPropertyPriceImage();
	}
	//initGoogleMap();
}

function showPriceChart() {
	$("list_ditu_mapbar_handler").style.backgroundImage = "url(/theme/ie/images/realestate/tag_ditu_off.gif)";
	$("list_ditu_google_handler").style.backgroundImage = "url(/theme/ie/images/realestate/tag_wxt_off.gif)";
	$("list_price_chart_handler").style.backgroundImage = "url(/theme/ie/images/realestate/priceChartOn.gif)";
		
	$("mapbar").style.display = "none";
	$("map").style.display = "none";
	$("propertyPriceContainer").style.display = "";
}


