/*
* These functions are used with map_style.css and the
* LabeledMarker Class to create custome labeled markers
* on Google Maps. They were written by  Mike Purvis (http://uwmike.com)
* with some minor chages on my part
*/ 

var map, 
	manager;

var centerLatitude  = 40.5000, /* center somewhere near Sandy Hook, NJ */
	centerLongitude = -74.0089, 
	startZoom = 9;

function createMarkerClickHandler(marker, location, FC_temp, rel_humidity, pressure, wind, last_update)
{
        return function() 
		{		
			   var html = '<h1>' + location + '</h1>'  +
						'<p>' + 'Temperature: ' + FC_temp + '</p>' +
                        '<p>' + 'Pressure: ' + pressure + '</p>' +
						'<p>' + 'Relative Humidity: ' + rel_humidity + '%' + '</p>' +
						'<p>' + 'Wind: ' + wind + '</p>' +
						'<p>' + last_update + '</p>';	
						
               marker.openInfoWindowHtml( html );				
               return false;
        };
}

/*******************************************************************************************************/
function createMarker(pointData) {
        var latlng = new GLatLng(pointData.latitude, pointData.longitude);
        var icon = new GIcon();
		icon.image = 'Images/red-marker.png';
        // icon.image = 'http://uwmike.com/maps/manhattan/img/red-marker.png';
        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.temp,
                "labelOffset": new GSize(-16, -16)
        };
        var marker = new LabeledMarker(latlng, opts);
        var handler = createMarkerClickHandler(marker, pointData.location, pointData.FC_temp, pointData.rel_humidity, 
											   pointData.pressure, pointData.wind, pointData.last_update );
        
        GEvent.addListener(marker, "click", handler);

        var listItem = document.createElement('li');
        listItem.innerHTML = '<div class="label">'+pointData.temp+'</div><a href="' + pointData.wp + '">' + pointData.location + '</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('toolbar').offsetHeight - 30;
        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());
		map.addControl( new GMapTypeControl());
    	map.disableDoubleClickZoom(map);
         
        manager = new GMarkerManager(map);
        
        // This is a sorting trick, don't worry too much about it.
        markers.sort(function(a, b) { return (a.abbr > b.abbr) ? +1 : -1; }); 
        
        batch = [];
        for(id in markers) {
                batch.push(createMarker(markers[id]));
        }
       
	    manager.addMarkers(batch, 9);
        manager.refresh();
}

window.onresize = handleResize;
window.onload = init;
window.onunload = GUnload;




