
var m_num = 1;
var m_type = 'Temperature';
var m_region = 'Northeast';

var m_num_us = 1;
var u_type = 'Temperature';

function crop_region(img){

	img.parentNode.style.height = img.offsetHeight - 48 + 'px';
	
}

function ch_map(inc){
			
	m_num += inc;
	
	if(m_num == 0) m_num = 1;
	if(m_num == 26) m_num = 25;	
	if(m_type == 'Snowfall'){
		if(m_num > 8) m_num = 8;
		document.getElementById("snowfall").src = 'http://www.weather.gov/forecasts/graphical/images/'+m_region.toLowerCase()+'/SnowAmt' + m_num + '_'+m_region.toLowerCase()+'.png';
	}
	if(m_type == 'Temperature')
		document.getElementById("snowfall").src = 'http://www.weather.gov/forecasts/graphical/images/'+m_region.toLowerCase()+'/T' + m_num + '_'+m_region.toLowerCase()+'.png';		
	if(m_type == 'Precipitation')
		document.getElementById("snowfall").src = 'http://www.weather.gov/forecasts/graphical/images/'+m_region.toLowerCase()+'/Wx' + m_num + '_'+m_region.toLowerCase()+'.png';
	if(m_type == 'WindGust'){
		if(m_num == 25) m_num = 24; 
		document.getElementById("snowfall").src = 'http://www.weather.gov/forecasts/graphical/images/'+m_region.toLowerCase()+'/WindGust' + m_num + '_'+m_region.toLowerCase()+'.png';
	}
}

function ch_maptype(type){
	m_type = type;
	ch_map(0);
}

function ch_region(reg){
	m_region = reg;
	ch_map(0);
}


function ch_map_us(inc){
			
	m_num_us += inc;
	
	if(m_num_us == 0) m_num_us = 1;
	if(m_num_us == 26) m_num_us = 25;	
	if(u_type == 'Snowfall'){
		if(m_num_us > 8) m_num_us = 8;
		document.getElementById("us_map").src = 'http://www.weather.gov/forecasts/graphical/images/conus/SnowAmt' + m_num_us + '_conus.png';
	}
	if(u_type == 'Temperature')
		document.getElementById("us_map").src = 'http://www.weather.gov/forecasts/graphical/images/conus/T' + m_num_us + '_conus.png';		
	if(u_type == 'Precipitation')
		document.getElementById("us_map").src = 'http://www.weather.gov/forecasts/graphical/images/conus/Wx' + m_num_us + '_conus.png';
	if(u_type == 'WindGust'){
		if(m_num_us == 25) m_num_us = 24;
		document.getElementById("us_map").src = 'http://www.weather.gov/forecasts/graphical/images/conus/WindGust' + m_num_us + '_conus.png';
	}
}

function ch_maptype_us(type){
	u_type = type;
	ch_map_us(0);
}


function region_zoom(number){
	
	document.getElementById("surface").src = 'http://images.intellicast.com/WxImages/SurfaceAnalysis/r'+number+'.gif';
	document.getElementById("mapzoomlink").innerHTML = '<a href="javascript:zoom_out()">Zoom out to US overview</a>';
}

function zoom_out(){
	
document.getElementById("surface").src = 'http://images.intellicast.com/WxImages/SurfaceAnalysis/usa.gif';
	document.getElementById("mapzoomlink").innerHTML = 'Click the map to zoom in on a region';

}


// JavaScript Document
var LocationsMap = {
	locations:null,
	markers:null,
	manager:null,
	init:function(map_id){

		//LocationsMap.objects = objects;
		//LocationsMap.locations = new Array();
		LocationsMap.markers = new Array();
		LocationsMap.map = new GMap2(document.getElementById(map_id));
		//change map type to hybrid
	
		
		//var keys = GKeyboardHandler(LocationsMap.map);
		
		LocationsMap.map.addControl(new GSmallMapControl());
		LocationsMap.map.addControl(new GMapTypeControl());
		LocationsMap.map.setCenter(new GLatLng(43.609549935614595,-72.7679443359375), 5);
		LocationsMap.map.setMapType(G_HYBRID_MAP);
		
		LocationsMap.manager = new GMarkerManager(LocationsMap.map);
		
		//LocationsMap.construct();
		
		//LocationsMap.manager.refresh();
		//shading
		
		GEvent.addListener(LocationsMap.map, 'click', function(overlay, point) {
		
		//  var ta = document.getElementById("drawit");
		  //	  ta.value += 'new GLatLng(' + point.y + ',' + point.x + '),' + "\n";
			  
			
		});
			
		      
		//Then you can add individual markers like this:
		//LocationsMap.manager = new GMarkerManager(LocationsMap.map);    
		LocationsMap.load_radar();
	},
	construct:function(){	
		LocationsMap.map.clearOverlays();
		LocationsMap.load_city_markers();		
		
		//LocationsMap.map.setZoom(LocationsMap.map.getBoundsZoomLevel(bounds));
		//LocationsMap.map.setCenter(bounds.getCenter());
		//LocationsMap.shade_outside_vt();
		
	},
	filter_location:function(by, test){
		var bounds = new GLatLngBounds();					
		for(var p = 0; p < LocationsMap.objects.length; p++){
			if(LocationsMap.objects[p].location[by] == test || test == ""){
				var pnt = new GLatLng(LocationsMap.objects[p].location.lat, LocationsMap.objects[p].location.lng);
				bounds.extend(pnt);	
			}
		}
		LocationsMap.map.setZoom(LocationsMap.map.getBoundsZoomLevel(bounds, new GSize(600,500)));
		LocationsMap.map.panTo(bounds.getCenter());
		
	},
	filter_object_prop:function(by, test){
		
		var bounds = new GLatLngBounds();					
		for(var p = 0; p < LocationsMap.markers.length; p++){
			if(LocationsMap.markers[p].object[by] == test || test == "null"){
				LocationsMap.markers[p].marker.show();
				var pnt = new GLatLng(LocationsMap.objects[p].location.lat, LocationsMap.objects[p].location.lng);
				bounds.extend(pnt);	
			}else{
				LocationsMap.markers[p].marker.hide();
			}
		}
		//LocationsMap.map.setZoom(LocationsMap.map.getBoundsZoomLevel(bounds, new GSize(600,500)));
		//LocationsMap.map.panTo(bounds.getCenter());
			
	},
	load_radar:function(){
		
		/*<marker tllat="" tllng="" brlat="" brlng="" site="ENX"/>
		<marker tllat="47.2796" tllng="-76.1867" brlat="41.73241" brlng="-70.13522" site="CXX"/>
		<marker tllat="46.6306" tllng="-73.2461" brlat="41.1413855" brlng="-67.257866" site="GYX"/>
		<marker tllat="44.611" tllng="-74.0348" brlat="39.291312" brlng="-68.231504" site="BOX"/>
		
		<marker tllat="46.4894" tllng="-78.6624" brlat="41.0125605" brlng="-72.687666" site="TYX"/>
		<marker tllat="44.8663" tllng="-78.893" brlat="39.5260585" brlng="-73.067282" site="BGM"/>
		

<marker tllat="43.4769" tllng="-75.7127" brlat="38.245608" brlng="-70.005836" site="OKX"/>
<marker tllat="42.5225" tllng="-77.2211" brlat="37.36207" brlng="-71.59154" site="DIX"/>

marker tllat="43.5361" tllng="-80.8551" brlat="38.3003035" brlng="-75.143322" site="CCX"/>

<marker tllat="41.5149" tllng="-80.2492" brlat="36.4259425" brlng="-74.69761" site="LWX"/>

<marker tllat="45.6464" tllng="-81.68" brlat="40.241814" brlng="-75.784088" site="BUF"/>
<marker tllat="43.1288" tllng="-83.0524" brlat="37.923721" brlng="-77.374132" site="PBZ"/>
<marker tllat="48.883" tllng="-70.91" brlat="43.184615" brlng="-64.69358" site="CBW"/>

		*/
		
		var en_boundaries = new GLatLngBounds(new GLatLng(39.894551,-76.9899), new GLatLng(45.2676,-71.128392));
		var cx_boundaries = new GLatLngBounds(new GLatLng(41.73241,-76.1867004255815), new GLatLng(47.2795563992587,-70.13522));
		var	gy_boundaries = new GLatLngBounds(new GLatLng(41.1413855,-73.2461), new GLatLng(46.6306,-67.257866));
		var bo_boundaries = new GLatLngBounds(new GLatLng(39.291312,-74.0348), new GLatLng(44.611,-68.231504));
		var ty_boundaries = new GLatLngBounds(new GLatLng(41.0125605,-78.6624), new GLatLng(46.4894,-72.687666));
		var bg_boundaries = new GLatLngBounds(new GLatLng(39.5260585,-78.893), new GLatLng(44.8663,-73.067282));
		var ok_boundaries = new GLatLngBounds(new GLatLng(38.245608,-75.7127), new GLatLng(43.4769,-70.005836));
		var di_boundaries = new GLatLngBounds(new GLatLng(37.36207,-77.2211), new GLatLng(42.5225,-71.59154));
		var cc_boundaries = new GLatLngBounds(new GLatLng(38.3003035,-80.8551), new GLatLng(43.5361,-75.143322));
		var lw_boundaries = new GLatLngBounds(new GLatLng(36.4259425,-80.2492), new GLatLng(41.5149,-74.69761));
		
		var cb_boundaries = new GLatLngBounds(new GLatLng(43.184615,-70.91), new GLatLng(48.883,-64.69358));
		
		LocationsMap.radar_one = new GGroundOverlay("http://www.srh.noaa.gov/ridge/RadarImg/NCR/CXX_NCR_0.gif", cx_boundaries);
		LocationsMap.radar_two = new GGroundOverlay("http://www.srh.noaa.gov/ridge/RadarImg/NCR/ENX_NCR_0.gif", en_boundaries);
		LocationsMap.radar_three = new GGroundOverlay("http://www.srh.noaa.gov/ridge/RadarImg/NCR/GYX_NCR_0.gif", gy_boundaries);
		LocationsMap.radar_four = new GGroundOverlay("http://www.srh.noaa.gov/ridge/RadarImg/NCR/BOX_NCR_0.gif", bo_boundaries);
		LocationsMap.radar_five = new GGroundOverlay("http://www.srh.noaa.gov/ridge/RadarImg/NCR/TYX_NCR_0.gif", ty_boundaries);
		LocationsMap.radar_six = new GGroundOverlay("http://www.srh.noaa.gov/ridge/RadarImg/NCR/BGM_NCR_0.gif", bg_boundaries);
		LocationsMap.radar_seven = new GGroundOverlay("http://www.srh.noaa.gov/ridge/RadarImg/NCR/OKX_NCR_0.gif", ok_boundaries);
		LocationsMap.radar_eight = new GGroundOverlay("http://www.srh.noaa.gov/ridge/RadarImg/NCR/DIX_NCR_0.gif", di_boundaries);
		LocationsMap.radar_nine = new GGroundOverlay("http://www.srh.noaa.gov/ridge/RadarImg/NCR/CCX_NCR_0.gif", cc_boundaries);
		LocationsMap.radar_ten = new GGroundOverlay("http://www.srh.noaa.gov/ridge/RadarImg/NCR/CCX_NCR_0.gif", lw_boundaries);
		LocationsMap.radar_eleven = new GGroundOverlay("http://www.srh.noaa.gov/ridge/RadarImg/NCR/CBW_NCR_0.gif", cb_boundaries);
		
		LocationsMap.map.addOverlay(LocationsMap.radar_one);
		LocationsMap.map.addOverlay(LocationsMap.radar_two);
		LocationsMap.map.addOverlay(LocationsMap.radar_three);
		LocationsMap.map.addOverlay(LocationsMap.radar_four);
		LocationsMap.map.addOverlay(LocationsMap.radar_five);
		LocationsMap.map.addOverlay(LocationsMap.radar_six);
		LocationsMap.map.addOverlay(LocationsMap.radar_seven);
		LocationsMap.map.addOverlay(LocationsMap.radar_eight);
		LocationsMap.map.addOverlay(LocationsMap.radar_nine);
		LocationsMap.map.addOverlay(LocationsMap.radar_ten);
		LocationsMap.map.addOverlay(LocationsMap.radar_eleven);
		
		//portland GYX
		
		 
	

		
	},
	zoom_to:function(latlng){
		if(latlng == 'null'){		
			LocationsMap.map.setCenter(new GLatLng(43.609549935614595,-72.7679443359375), 7);
			
		} else {
			var ll = latlng.split(",");
			LocationsMap.toggle_radar('off');
			LocationsMap.map.setCenter(new GLatLng(ll[0],ll[1]*-1), 10);
			
		}
		
		//LocationsMap.construct();
	},
	toggle_radar:function(onoff){
		var on  = document.getElementById("radaron");
		var off = document.getElementById("radaroff");
		if(onoff=='off'){
			LocationsMap.map.removeOverlay(LocationsMap.radar_one);
			LocationsMap.map.removeOverlay(LocationsMap.radar_two);
			on.checked = false;
			off.checked = true;		
		} else {
			LocationsMap.map.addOverlay(LocationsMap.radar_one);
			LocationsMap.map.addOverlay(LocationsMap.radar_two);					
			on.checked = true;
			off.checked = false;		
	
		}
	}
	
	
}

function MapMarker(location_obj, handler, icon){
	var This = this;
	this.object = location_obj;
	if(location_obj.location.lng){
		this.geopoint = new GLatLng(location_obj.location.lat, location_obj.location.lng);
		this.handler = handler;
		this.icon = icon ? icon : false;
		this.marker = new GMarker(this.geopoint, this.icon);			
		GEvent.addListener(this.marker, "click", function() { This.handler(This) } );

	} else {
	
		this.geopoint = false;
	}
	return this;							
}


function CityInfo(mm){
	
	//find posts within 25 miles of the city using distanceFrom
	var close = 0;
	for(var m = 0; m < LocationsMap.markers.length; m++){		
		if(LocationsMap.markers[m].geopoint.distanceFrom(mm.geopoint) < 45000){
			close++;
		}		
	}

	var html = "<div class='markerwin' style='width:300px;height:165px'><h2 style='width:100%;'>";
		html += '<h1 style="margin:0;">' + mm.object.city + '</h1>';
		html += '<a href="javascript:LocationsMap.zoom_to(\''+mm.object.location.lat+','+(mm.object.location.lng*-1)+'\');">' + close + " witnesses within 45km</a>";		
		html += '<div id="weatherdata">Loading weather data...</div><br />';
		html += '<br />';
		html += "</div>";
		mm.marker.openInfoWindowHtml(html);

	//lookup weather for mm.object.zip
	
	RemoteCall.handlers = {
		onData:function(data){
			
			var tp = document.getElementById("weatherdata");
				tp.innerHTML = '<p>'+data.weather.current.conditions+'</p><h1>' + data.weather.current.temperature + '<sup>&deg;</sup>F</h1>';
			    tp.innerHTML += '<p>TOMORROW: ' + data.weather.tomorrow.conditions + '<br />' + data.weather.tomorrow.high + '<sup>&deg;</sup>F (High) ' + data.weather.tomorrow.low + '<sup>&deg;</sup>F (Low)</p>';
		}
	}
	RemoteCall.make("mms_data_service","json_weather",'{"zip":"'+mm.object.zip+'"}');
		
		
}

function OpenInfo(markerobj){
	var loc = markerobj.object.location;		
	var addr = loc.address == "" ? "" : loc.address + ", ";
	//var point = new GLatLng(loc.lat, loc.lng);
	//LocationsMap.map.setCenter(point);
	//LocationsMap.map.setZoom(8);
	
	var cat = markerobj.object.submission_category.name.toLowerCase();

		cat = cat.replace(/ /,"");
		cat = cat.replace(/\//,"-");
		cat = cat.replace(/\//,"-");
		
	
	
	var html = "<div class='markerwin' style='width:375px;height:260px'><h2 style='width:100%;'>";
		html += '<img class="icon" src="http://sites.legitify.com/vermontclimatewitness.org/images/icons/sml/'+cat+'.gif" />';		
		html += '<h1>'+markerobj.object.title+' </h1>';
		html += '<p>' + markerobj.object.date + '</p>'; 
		html += '<p><a href="'+WEB_HOME+'submissions/location/city/' + loc.city + '">'+loc.city +'</a></p>';
		var poster = markerobj.object.registration.alias == "" ? markerobj.object.registration.name : markerobj.object.registration.alias;
		html += '<p>Posted By: <a href="'+WEB_HOME+'register/profile/' + markerobj.object.registration.id + '">'+poster +'</a></p>'; 				
		html += '<p><a href="'+WEB_HOME+'submissions/view/'+markerobj.object.id+'">Read Details</a>';
		//html += ' | <a href="javascript:LocationsMap.construct();">Zoom Back Out</a></p><br />';
		
		//html += '<p>' + addr + loc.city + ", " + loc.state + '</p>'; 	
		if(markerobj.object.photos && markerobj.object.photos.length > 0){
			var photos  =  markerobj.object.photos;
			var lbl = photos.length > 1 ? photos.length + ' Photos' : '1 Photo';
			
			html += '<div id="photos_'+	markerobj.object.id + '" class="markerphotos">';
			html += '<p><a href="'+WEB_HOME+'submissions/view_photos/'+markerobj.object.id+'">' + lbl + ':</a></p>';
			for(var p = 0; p < photos.length; p++){
				if(p > 5) continue;
				var name = photos[p].file_name.substr(0, photos[p].file_name.lastIndexOf("."));
				var ext = photos[p].file_name.substr(photos[p].file_name.lastIndexOf("."), photos[p].file_name.length);
				html += '<img src="'+WEB_HOME+'user_media/'+ markerobj.object.registration.id + '/' + name + "_square" +  ext + '" />';				
			}
			html += '</div>';
		}
		
		if(markerobj.object.videos && markerobj.object.videos.length > 0){
			
			var videos  =  markerobj.object.videos;
			var lbl = videos.length > 1 ? videos.length + ' Videos' : '1 Video';
			
			html += '<div id="video_'+	markerobj.object.id + '" class="markerphotos">';
			html += '<p><a href="'+WEB_HOME+'submissions/view_videos/'+markerobj.object.id+'">' + lbl + ':</a></p>';
			
			for(var p = 0; p < videos.length; p++){
				if(p > 5) continue;
				html += '<img src="'+WEB_HOME+'user_media/'+ markerobj.object.registration.id + '/' + videos[p].file_name + '.jpg" />';
				
			}
			html += '</div>';
		}
		//Videos..</strong>';
				
		html += '<br />';
		html += "</div>";
		markerobj.marker.openInfoWindowHtml(html);			
				
}
