/*
Written and Copyright (c) 2008
MicroChip Technical Services LLC
Leon Salisbury
All Rights Reserved
Licenced to Jim Pieniozek
for exclusive use on
nmmlsx.com
*/

function initialize( _elementName ) {
// load data
GDownloadUrl( '../cgi-bin/snowfall_data.pl?element='+_elementName, processData );
};

function processData( _data, _responseCode ) {
// parse data
var data = eval( '(' + _data + ')' );
var limits = data.limits;
var snowfall = data.snowfall;

// calculate display area
var mi = new Object;
mi.sw = new GLatLng( 41, -87 );
mi.ne = new GLatLng( 46, -82 );
mi.bounds = new GLatLngBounds( mi.sw, mi.ne );
mi.center = mi.bounds.getCenter();
var geo = new Object;
geo.sw = new GLatLng( limits.min.lat, limits.min.lng );
geo.ne = new GLatLng( limits.max.lat, limits.max.lng );
geo.bounds = new GLatLngBounds( geo.sw, geo.ne );
geo.center = geo.bounds.getCenter();

// create map
var defaultZoom = 5;
map = new google.maps.Map2( document.getElementById( "map_canvas" ) );
map.addControl( new google.maps.SmallMapControl() );
map.enableScrollWheelZoom();
map.setCenter(geo.center, defaultZoom);
map.setZoom(map.getBoundsZoomLevel( geo.bounds ) );

// create markers
for ( var i=0; i<snowfall.length; i++ ) {
makeMarker( snowfall[i] );
};
};

function makeMarker( _site ) {
var point = new GLatLng( _site.lat, _site.lng );
var marker = new GMarker( point, makeIcon( _site ) );
marker.site = _site;
GEvent.addListener( marker, "click", function() { eventClick( marker ) });
map.addOverlay( marker );
};

function makeIcon( _site ) {
var size = 30; // icon pixel size
var hsize = size/2;
var icon = new GIcon();
//icon.image = "http://www.michiganidx.com/idx/nmmls/residential/thumbnails/nmmls" + _site.mls + ".jpg"
icon.image = "http://www.michiganidx.com/Snowflake.png"
icon.shadow = "";
icon.iconSize = new GSize( size, size );
icon.shadowSize = new GSize( 0, 0 );
icon.iconAnchor = new GPoint( hsize, hsize );
icon.infoWindowAnchor = new GPoint( hsize, hsize );
return icon;
};

function eventClick( _marker ) {
var size = 200; // image pixel width
map.closeInfoWindow();
_marker.openInfoWindowHtml(
"<div style=\"margin:0px; border:0px; padding:0px; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:14px;\">"+
"<strong>Location: "+_marker.site.name+"</strong><br>"+_marker.site.amount+" "+
"inches"+" new snow reported in the last "+_marker.site.duration+"<br> Hours - Last Report "+_marker.site.date_time_report+"<br>"+
"<a href=\"http://www.nmmlsx.com/idx/paulfairbairn/residential/propview.php?view="+_marker.site.zipcode+"\" target=\"_blank\">"+

"</div>"
);
};
