var map, manager;
var centerLatitude = 40.736462, centerLongitude = -96.98777, startZoom = 4;


function createMarkerClickHandler(marker, aname, astreet, acity, astate, azip, acountry, aphone, alat, along, link) {
        return function() {
                marker.openInfoWindowHtml(
                        '<h3>' + aname  + '</h3>' + 
						'<p>' + astreet + '<br />' + 
						acity + ", " + astate + ' ' + azip  + '<br />' + 
						'Phone: ' + aphone + '</p>' + 
                        '<p><a href="' + link + '">Website</a></p>' + 
						'<p>Get Directions <a href="http://maps.google.com/maps?daddr=' + alat + ',' + along + '&hl=en">to here</a> or <a href="http://maps.google.com/maps?saddr=' + alat + ',' + along + '&hl=en">from here</a></p>'
                );
                return false;
        };
}




function createMarker(pointData) {
        var latlng = new GLatLng(pointData.latitude, pointData.longitude);
        var icon = new GIcon();
        icon.image = 'http://niosh-erc.org/images/bluecircle.gif';
        icon.iconSize = new GSize(32, 32);
        icon.iconAnchor = new GPoint(16, 16);
        icon.infoWindowAnchor = new GPoint(25, 7);

        opts = {
                "icon": icon,
                "clickable": true,
                "labelText": pointData.state,
                "labelOffset": new GSize(-16, -16)
        };
        var marker = new LabeledMarker(latlng, opts);
        var handler = createMarkerClickHandler(marker, pointData.name, pointData.street, pointData.city, pointData.state, pointData.zip, pointData.country, pointData.phone, pointData.latitude, pointData.longitude, pointData.site);
        
        GEvent.addListener(marker, "click", handler);

        var listItem = document.createElement('li');
        listItem.innerHTML = '<div class="label">'+pointData.state+'</div><a href="' + pointData.site + '">' + pointData.name + '</a>';
        listItem.getElementsByTagName('a')[0].onclick = handler;

        document.getElementById('sidebar-list').appendChild(listItem);

        return marker;
}

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('map').style.height = height + 'px';
        document.getElementById('sidebar').style.height = height + 'px';
}

function init() {
        handleResize();
        
        map = new GMap(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
        map.addControl(new GMapTypeControl());

        manager = new GMarkerManager(map);
        
        // This is a sorting trick, don't worry too much about it.
        markers.sort(function(a, b) { return (a.name > b.name) ? +1 : -1; }); 
        
        batch = [];
        for(id in markers) {
                batch.push(createMarker(markers[id]));
        }
        manager.addMarkers(batch, 1);
        manager.refresh();
}

window.onresize = handleResize;
window.onload = init;
window.onunload = GUnload;