var map;

var centerLatitude = 33.826277;

var centerLongitude = -84.402695; 

var startZoom = 9;

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 listItemLink = listItem.appendChild(document.createElement('a'));

	var visible = false;


	var htmlText = '<table width="100%" cellpadding="10" cellspacing="0" border="0"><tr valign="top"><td width="100%"><h3>' + pointData.name + '</h3>';
	if(pointData.addtlinfo != '') {
		htmlText = htmlText + pointData.addtlinfo + '<br />\n';
	}

	htmlText = htmlText + pointData.address + '<br />\n';
	if(pointData.address2 != '') {
		htmlText = htmlText + pointData.address2 + '<br />\n';
	}

	htmlText = htmlText + pointData.city + ', ' + pointData.state + ' ' + pointData.zip + '<br />\n';

	if(pointData.phone != '') {
		htmlText = htmlText + '<strong>Phone:</strong> ' + pointData.phone + '<br />\n';
	}

	if(pointData.fax != '') {
		htmlText = htmlText + '<strong>Fax:</strong> ' + pointData.fax + '<br />\n';
	}

	if(pointData.tollfree != '') {
		htmlText = htmlText + '<strong>Toll Free:</strong> ' + pointData.tollfree + '<br />\n';
	}

	if(pointData.mailingaddress != '') {
		htmlText = htmlText + '<strong>Mailing Address:</strong> ' + pointData.mailingaddress + '<br />\n';
	}

	if(pointData.officehours != '') {
		htmlText = htmlText + '<strong>Office Hours:</strong> ' + pointData.officehours + '<br />\n';
	}

	htmlText = htmlText + '<br /><hr></td>\n</tr>\n</table>\n';

	listItemLink.href = "#";

	listItemLink.innerHTML = htmlText;

	

	var focusPoint = function() {

		deselectCurrent();

		listItem.className = 'current';

		deselectCurrent = function() { listItem.className = ''; }



		var	desc = '<span class="title">' + pointData.name + '</span>';

			desc = desc + '<br />' + pointData.address;

			desc = desc + '<br />' + pointData.city + ', ' + pointData.state + ' ' + pointData.zip;

			desc = desc + '<br />Phone: ' + pointData.phone;







		marker.openInfoWindowHtml(desc);

		map.panTo(point);

		return false;

	}



	GEvent.addListener(marker, 'click', focusPoint);	

	listItemLink.onclick = focusPoint;



	pointData.show = function() {

		if (!visible) {

			document.getElementById('sidebar-list').appendChild(listItem);

			map.addOverlay(marker);

			visible = true;

		}

	}

	pointData.hide = function() {

		if (visible) {

			document.getElementById('sidebar-list').removeChild(listItem);

			map.removeOverlay(marker);

			visible = false;

		}

	}



	pointData.show();

}



function initializeSortTab(type) {

	var listItem = document.createElement('li');

	var listItemLink = listItem.appendChild(document.createElement('a'));



	listItemLink.href = "#";

	listItemLink.innerHTML = type;

	listItemLink.onclick = function() {

		changeBodyClass('standby', 'loading');



		for(id in markers) {

			if (markers[id].type == type || 'All' == type)

				markers[id].show();

			else

				markers[id].hide();	

		}



		changeBodyClass('loading', 'standby');



		return false;

	}



	document.getElementById('filters').appendChild(listItem);

}



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 changeBodyClass(from, to) {

	document.body.className = document.body.className.replace(from, to);

	return false;

}



function init() {

	var type;

	var allTypes = { 'All':[] };

	

	

	map = new GMap(document.getElementById("map"));

	map.addControl(new GSmallMapControl());

	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);

	

		for(id in markers) {

			initializePoint(markers[id]);

			allTypes[markers[id].type] = true;

		}



	for(type in allTypes) {

		initializeSortTab(type);

	}



	changeBodyClass('loading', 'standby');

}



window.onload = init;

