
$(document).ready(function() {
	$('#map').mapIt();						   
});

(function($) {
		  
    $.fn.mapIt = function(options) {
		// plugin default options
        var defaults = {};
		// extends defaults with options provided
        if (options) {
			$.extend(defaults, options);
		}
		
//BIND ALL CONTROLS
$('#mapit').bind('submit', mapit);
$('#cmdSaveit').bind('click', saveit);


//DEFINE VARIABLES
var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 14,
      center: new google.maps.LatLng(43.67299, -79.310318),
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      mapTypeControl: true,
      mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
      navigationControl: true,
      navigationControlOptions: {style: google.maps.NavigationControlStyle.ZOOM_PAN}
    });

var flag = new google.maps.MarkerImage('/canadaday/fl.png', new google.maps.Size(34, 25), new google.maps.Point(0,0), new google.maps.Point(7, 5)); 

var selflag = new google.maps.MarkerImage('/canadaday/casel.png', new google.maps.Size(54, 41), new google.maps.Point(0,0), new google.maps.Point(10, 7)); 



var selMarker = new google.maps.Marker({   map: map, visible:false,
									        icon: selflag,
											draggable: true,
											position: new google.maps.LatLng(43.67929, -79.310818),
										 });

plotFlags();



function plotFlags() {   // Add markers to the map

$.ajax({
   type: "POST",
   url: "/canadaday/ajax.php",
  dataType: 'json', 
   data: {a:'getAll'},
   success: function(json){
	   $(json.coords).each(function() {
	
	   var myLatLng = new google.maps.LatLng(this['lat'],this['lng']);
	   var marker = new google.maps.Marker({
			  position: myLatLng,
			  map: map,
			  icon: flag
	   }); 
	});
	  
	 $('#flagcount').text(json.coords.length); 
	   
   }
 });
	

 
} 



function mapit(ev) {
	ev.preventDefault();
	if (!$('#adr1').val() && !$('#postcode').val()) { alert('Please enter an Address'); return false;}
	
	var geocoder = new google.maps.Geocoder();
	
	geocoder.geocode( { 'address':$('#adr1').val() + ', ' + $('#city').val() + ', ' + $('#prov').val() + ', ' + $('#postcode').val()},
							function(results, status) {
 								if (status == google.maps.GeocoderStatus.OK) {
									 map.setCenter(results[0].geometry.location);
									 selMarker.setPosition(results[0].geometry.location);
									 selMarker.setVisible(true);
								
									$('#cmdSaveit').attr('disabled','');
									$('#saveit').removeClass('disabled');
							} else {
								alert ('Couldn\'t find your address');	
							}
					});		
}




function saveit() {
	
	$.ajax({
	   type: "POST",
	   url: "/canadaday/ajax.php",
	  dataType: 'json', 
	   data: {a:'add',
			  lat:selMarker.position.lat(),
			  lng:selMarker.position.lng()},
	   success: function(msg){
		   if (msg == true){
			   
			 //Add new Marker
			var marker = new google.maps.Marker({
					  position: selMarker.position,
					  map: map,
					  icon: flag
			  });
				
			//Reset Temp Marker
			 alert('Saved! - thanks for taking part'); 
			selMarker.setVisible(false);
			$('#adr1').val('').focus();
			$('#postcode').val('');
			$('#cmdSaveit').attr('disabled','disabled');
			$('#saveit').addClass('disabled');
			$('#flagcount').text(parseInt($('#flagcount').text())+1);
			 
			 
		   } 
	   }
	 });
}

};

})(jQuery);










function cl(a){ console.log(a);}
