
var centerLatitude = 39.50;
var centerLongitude = -98.35;
var startZoom = 4;

var map;
var bounds = new GLatLngBounds();


var baseIcon = new GIcon(G_DEFAULT_ICON);
baseIcon.shadow = "http://www.hootsandhellmouth.com/images/iconshadow.png";
baseIcon.iconSize = new GSize(32, 32);
baseIcon.shadowSize = new GSize(59, 32);
baseIcon.iconAnchor = new GPoint(16, 32);
baseIcon.infoWindowAnchor = new GPoint(16, 2);


function initializePoint(pointData) {
	var point = new GLatLng(pointData.lat, pointData.lng);
	var classIcon = new GIcon(baseIcon);
  	classIcon.image = "http://www.hootsandhellmouth.com/images/icon" + pointData.monthclass + ".png";

	var marker = new GMarker(point, {icon:classIcon});
	var listItem = document.createElement('li');
	listItem.className = "showClass" + pointData.monthclass;
	var listItemLink = listItem.appendChild(document.createElement('a'));
	listItemLink.href = "#";
	listItemLink.innerHTML = '<strong>' + pointData.venuename + '</strong>' + pointData.showdate + ' - ' + pointData.city + ', ' + pointData.state;
	
	var windowInfo = '<div id="window"><strong>' + pointData.venuename + '</strong><br /><hr />' + pointData.showdate + '<br />' + pointData.address + '<br />' + pointData.city + ', ' + pointData.state + '<br />' + pointData.showcopy + '<br /><a href="' + pointData.venuelink + '">' + pointData.venuelink + '</a><br /><a href="' + pointData.facebook + '" target="_blank">RSVP on faceboook</a>';
	var focusPoint = function() {
		marker.openInfoWindowHtml(windowInfo, {maxWidth:300});
   		map.setCenter(new GLatLng(pointData.lat, pointData.lng));
		return false;
	}
	
	GEvent.addListener(marker, 'click', focusPoint);
	listItemLink.onclick = focusPoint;
	
	document.getElementById('sidebar-list').appendChild(listItem);
	
	map.addOverlay(marker)
    bounds.extend(point);
}

function init() {
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
    document.getElementById("scrollWheelZoomToggle").checked = false;
    toggleZoom(false);

    
    for(id in markers) {
    	initializePoint(markers[id]);
    	}
    	
    map.setZoom(map.getBoundsZoomLevel(bounds));
    map.setCenter(bounds.getCenter());
}

function resetmap() {
    map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
    marker.infoWindowHtml.hide();
}

function toggleZoom(isChecked) {
    if (isChecked) {
      map.enableScrollWheelZoom();
    } else {
       map.disableScrollWheelZoom();
    }
 }


window.onload = init;
window.onunload = GUnload;

