var map;
var xmlhttp;
var markers;
var deselectCurrent = function() {};
var bounds = null;
bounds = new GLatLngBounds();

var deselectCurrent = function() {};

function initializePoint(pointData) {
	var point = new GLatLng(pointData.latitude, pointData.longitude);
	var marker = new GMarker(point);
	var listItem = document.createElement('li');
	var listItemPlace = listItem.appendChild(document.createElement('div'));
	var listItemMore = listItem.appendChild(document.createElement('a'));
	var listItemLink = listItem.appendChild(document.createElement('a'));
	listItemLink.href = "#";
	listItemMore.href = 'sales_detail.php?id=' + pointData.id;
	if(pointData.price==0) { liprice = "NA";} else { liprice = pointData.price;}
	listItemPlace.innerHTML = '<b>' +  pointData.address + '</b>, ' + pointData.city + '<br><b>Date:</b> ' + pointData.date + ' <b>Price:</b> ' + liprice;
	listItemMore.innerHTML = 'More information';
	if(pointData.precision == "address" || pointData.precision == "street" || pointData.precision == 'intersection' || pointData.precision == 'premise') {
		listItemLink.innerHTML = 'Map it';
	}
	
	var focusPoint = function() {
		deselectCurrent();
		listItem.className = 'current';
		deselectCurrent = function() { listItem.className = ''; }
		if(pointData.price==0) { price = "";} else { price = pointData.price;}
		html = '<span><b>' + pointData.address + '</b><br>' + pointData.city + '<br>' + price + ' ' + pointData.propclass + '<br><a href=\"sales_detail.php?id=' + pointData.id + '\">More information</a></span>';
		marker.openInfoWindowHtml(html);
		map.panTo(point);
		map.setZoom(15);
		return false;	
	}

	GEvent.addListener(marker, 'click', focusPoint);	
	listItemLink.onclick = focusPoint;

	document.getElementById('sales-list').appendChild(listItem);
	if(pointData.precision == "address" || pointData.precision == "street" || pointData.precision == 'intersection' || pointData.precision == 'premise') {	
		map.addOverlay(marker);
		bounds.extend(point); 
	}
}
	

function windowHeight() {
	// Standard browsers (Mozilla, Safari, etc.)
	if (self.innerHeight)
		return self.innerHeight;
	// IE 6
	if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	// IE 5
	if (document.body)
		return document.body.clientHeight;
	// Just in case.
	return 0;
}

function handleResize() {
	var height = windowHeight() - document.getElementById('toolbar').offsetHeight - 30;
	document.getElementById('map').style.height = height + 'px';
	document.getElementById('sidebar').style.height = height + 'px';
}

function changeBodyClass(from, to) {
     document.body.className = document.body.className.replace(from, to);
     return false;
}

function setAlertText(str) {
	document.getElementById('alert').innerHTML = '<p>' + str + '</p>';
}

function initData() {
	map = new GMap(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
	
	for(id in markers) {
		initializePoint(markers[id]);
	}
	map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); 
	changeBodyClass('loading', 'standby');
}
function init() {
	//document.getElementById('button-sidebar-hide').onclick = function() { return changeBodyClass('sidebar-right', 'nosidebar'); };
	//document.getElementById('button-sidebar-show').onclick = function() { return changeBodyClass('nosidebar', 'sidebar-right'); };
	//handleResize();

	var searchString = 'js/map_data_sales.php?'+mapSearch;
	
	xmlhttp = GXmlHttp.create();
    xmlhttp.open('GET', searchString, true);
    xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
       		if (xmlhttp.status != 200) 
       			setAlertText('Could not access map data.');
       		else
       		{
				var responseText = xmlhttp.responseText;
				markers = eval(responseText);
				if (!markers)
					setAlertText('Map data error.');
				else
					initData();
			}
       }
    }
    xmlhttp.send(null);
}

//window.onresize = handleResize;
window.onload = init;
