google.load("maps", "2");

// #define optimisation false;
// #define dirtyhacks true;

// var my.$mapOK = false;
var map = false;
var species = {};
var speciesCurrent = false;
var markerOptions;

//  species > frogs > frog > latlng
// Call this function when the page has been loaded
function initialize() {
	map = new google.maps.Map2(document.getElementById("map"));

	var mapTypeControl = new GMapTypeControl();
	var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
	var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));
	map.addControl(mapTypeControl, topRight);
	map.addControl(new GSmallMapControl());


	map.setCenter(new google.maps.LatLng(-32.0289,135.001999),5);


	var frogIcon = new GIcon(G_DEFAULT_ICON);
	frogIcon.image = "/ribbit/m.png";
	markerOptions = { icon:frogIcon };
  
	//   my.$mapOK = true;

	//   var polyline = new GPolyline([
	// 				new GLatLng(-33.836865,151.220767),
	// 				new GLatLng(-33.826865,151.220767)
	// 				], "#ff0000", 2);
	//   map.addOverlay(polyline);



}

$(  function () {

		$('#pick input[type=checkbox]').change( function ( e ) {

				var species_id = $(e.target).val();

				$('#pick ul li').removeClass ( 'active' );

				if ( speciesCurrent ) {
					hideSpecies ( speciesCurrent );
				}


// 				if ( ! $(this).attr('checked') ) {
// 					$(e.target).parent().removeClass ( 'active' );
// 					// hideSpecies ( species_id );
// 					return false;
// 				}

	
				var request = {
					year_species_id: species_id
				}

				$.get( '/frogs', request, function( response ) { 

						if ( '-1' == response ) {
							return false;
						}
						//	    log ('Url: ' + response);
						$('#chart').attr('src', response );
					});

				request = {
					loc_species_id: species_id
				}


				$.get( '/frogs', request, function( response ) { 

						if ( '-1' == response ) {
							return false;
						}

						$(e.target).parent().addClass ( 'active' );

						var r = response.split('|');
						var frogs = [];

						var minlat = false;
						var minlng = false;
						var maxlat = false;
						var maxlng = false;

						for ( i in r ) {

							if ( '' == r ) {
								continue;
							}

							var f = r[i].split(',');
							if ( f[0] == '' ) {
								continue;
							}
							

							var frog = {
								LatLng :new GLatLng( f[1], f[2]),
								frog_id: f[0],
								lat: f[1],
								long: f[2],
								species_id: f[3],
								sighting: f[4]
							}
							frogs.push(frog);

							if ( ! minlat || f[1] < minlat ) {
								minlat = f[1];
							}

							if ( ! maxlat || f[1] > maxlat ) {
								maxlat = f[1];
							}

							if ( ! minlng || f[2] < minlng ) {
								minlng = f[2];
							}

							if ( ! maxlng || f[2] > maxlng ) {
								maxlng = f[2];
							}

						}

						species[species_id] = frogs;
						showSpecies  ( species_id );

						var sw = new GLatLng( minlat, minlng );  
						var ne = new GLatLng( maxlat, maxlng );  
						var bounds = new GLatLngBounds( sw, ne );  
						map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));

					});
     
			});
	});

google.setOnLoadCallback(initialize);

function createMarker( frog ) { 
	var marker = new GMarker ( frog.LatLng, markerOptions );  
	GEvent.addListener ( marker, "click", function() { 
			var html = '' + '<img src="/ribbit/' + frog.species_id + '.jpg" style="float: right;" />' + frog.sighting;
			marker.openInfoWindowHtml( html );
		});  
	return marker;
}; 

function showSpecies ( species_id ) {

	speciesCurrent = species_id;

	for ( var i in species[species_id] ) {
		var frog = species[species_id][i];
		frog.species_id = species_id;
		var marker = createMarker( frog );
		map.addOverlay( marker );
		species[species_id][i].marker = marker;
	}

}

function hideSpecies ( species_id ) {
	$('#chart').attr('src', '/images/clear.gif' );

	for ( var i in species[species_id] ) {
		var frog =  species[species_id][i];
		map.removeOverlay(frog.marker);
	}

}


function log ( s ) {
	if ( $('#log').length == 0 ) {
		$('#background').prepend('<div id="log" style="background: #fc0;">-</div>');
	}
	$('#log').html(s);
}
