var iconParking = new GIcon();
iconParking.image = "images/carparkicon1.jpg";
iconParking.shadow = "images/shadow-carparkicon1.png";
iconParking.iconSize = new GSize(16, 16);
iconParking.shadowSize = new GSize(23, 16);
iconParking.iconAnchor = new GPoint(6, 20);
iconParking.infoWindowAnchor = new GPoint(5, 1);

var customIcons = [];
customIcons["parking"] = iconParking;

var map;
var centerLatitude = 36.162762;
var centerLongitude = -86.779966;
var startZoom = 17;
var deselectCurrent = function() {};

function initializePoint(pointData) {
	// check the point data for validity...
	if (!(pointData.latitude && pointData.longitude)) return;
	
	var point = new GLatLng(pointData.latitude, pointData.longitude);
	var marker = new GMarker(point, customIcons[pointData.type]);
	// var html = "<b>" + name + "</b> <br/> <span class='address'>" + address + "</span> <br/> <br/><strong>Hours: </strong><br/><span class='hours'>" + hours + "</span><br/><br/><strong>Number of spaces:</strong> " + spaces + "<br/><br/><strong>Rates: </strong><br/><span class='rates'>daily: &#36;" + daily + "</span><br/><span class='rates'>hourly: &#36;" + hourly + "</span><br/><span class='special' style='width:50px;'>special: " + hourlyspecial + "</span>";
	var listItem = document.createElement('li');
	
	// ID the item so we can pass that on to the comparison pages if needed
	listItem.id = "parking-area-"+pointData.id;
	
	var listItemLink = listItem.appendChild(document.createElement('a'));
	var visible = false;

	listItemLink.href = "#";
	listItemLink.innerHTML = '<strong>' + pointData.name + ' </strong><span>' + pointData.address + ', daily rate: &#36;' + pointData.daily + ', hourly rate: &#36;' + pointData.hourly + '</span>';
	
	var focusPoint = function() {
		deselectCurrent();
		listItem.className = 'current';
		deselectCurrent = function() { listItem.className = ''; }
		
		var pointImage = "";
		if (pointData.image) {
			pointImage = '<div><img src="http://www.nashvilledowntown.com/images/lots/parking/'+pointData.image+'" class="parkingImg" /></div>';
		}
		
		var getDirections = "<p><a href='#' onclick=\"ParkItDirections('"+pointData.latitude+","+pointData.longitude+"'); return false;\">Get Directions</a></p>";
		var website = "";
		if (pointData.website) website = pointData.website
			else website = "&#35;";
		if (pointData.monthly == "0") {
			pointData.monthly = 'Not Available';
		}
		if (pointData.hourly == "0") {
			pointData.hourly = 'Not Available';
		}
		if (pointData.daily == "0") {
			pointData.daily = 'Not Available';
		}
		if (pointData.special == "0") {
			pointData.special = 'Not Available';
		}
		marker.openInfoWindowHtml('<div id="node">' + pointImage + '<br /><strong>' + pointData.name + '</strong><br /><span class="address">' + pointData.address + '</span><br/><br/><strong>Manager:</strong><br /><a href="'+website+'" target="_blank">'+pointData.owner+'</a></strong>' + '<br/><br/><strong>Rates: </strong><br/><span class="rates">daily: &#36;' + pointData.daily + '</span><br/><span class="rates">hourly: &#36;' + pointData.hourly + '</span><br/><span class="rates">monthly: &#36;' + pointData.monthly + '</span><br/><div class="special" style="max-width:250px;">misc rate(s): ' + pointData.special + '</div><br /><strong>Number of spaces:</strong> ' + pointData.spaces + '<br />'+getDirections+'</div>');
		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;
	}
	
	if (document.getElementById('filters')) {
		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 handleResize() {
	/*
	var height = windowHeight() - document.getElementById('toolbar').offsetHeight - 30;
	document.getElementById('map').style.height = height + 'px';
	document.getElementById('sidebar').style.height = height + 'px';
	*/
	
	/*
	var potentialHeight = windowHeight() - document.getElementById('toolbar').offsetHeight - 30;
	var maxHeight = 800;
	var height = 0;
	if (potentialHeight > maxHeight) {
		height = maxHeight;
	} else {
		height = potentialHeight;
		if (document.getElementById('comparison-container')) {
			document.getElementById('comparison-container').style.marginTop = (potentialHeight+10)+'px';
		}
	}
	*/
	
	var height = 700;
	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 init() {
	var type;
	var allTypes = { 'All':[] };
	
	
	//handleResize();
	
	map = new GMap2(document.getElementById("map-container"));
	map.addControl(new GSmallMapControl());
	// map.getPane(G_MAP_FLOAT_SHADOW_PANE).style.visibility="hidden"; 
	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');
	
	// lets us define an optional post-map initialization hook for the venue selection.
	postMapInitHook(map);
	addMapOverlays(map);
}

// window.onresize = handleResize;
window.onload = init;

// stub
var postMapInitHook = function (map) {};
function addMapOverlays(map) {	
	
	var pointSW = new GLatLng(36.151356,-86.79225);
	var pointNE = new GLatLng(36.175747,-86.76753);
	var groundOverlay = new GGroundOverlay(

	   "http://www.parkitdowntown.com/includes/8.gif", 
	   new GLatLngBounds(pointSW, pointNE));
	map.addOverlay(groundOverlay);
}
