function Rumor() {
     var map = null;
     var marker = null;
     var infowindow = new google.maps.InfoWindow({ size: new google.maps.Size(150,50) });
  	 var geocoder = new google.maps.Geocoder();
    
     this.createMap  = function () {
		 // create the map
		var myOptions = {  zoom: 8,
						   center: new google.maps.LatLng(41.387917, 2.1699187),
						   mapTypeControl: true,
						   mapTypeControlOptions: {style: google.maps.NavigationControlStyle.DROPDOWN_MENU},
						   navigationControl: true,
						   mapTypeId: google.maps.MapTypeId.ROADMAP
						}
		
		map = new google.maps.Map(document.getElementById("rumor_map_canvas"), myOptions);

		google.maps.event.addListener(map, 'click', function() {
		       infowindow.close();
		});
		
		google.maps.event.addListener(map, 'click', function(event) {
			//call function to create marker
			if (marker) {
	           marker.setMap(null);
	           marker = null;
	        }
			marker = rumor.createMarker(event.latLng);
		 });
		
     },
     
  // A function to create the marker and set up the event window function 
     this.createMarker = function(latlng) {
         var marker = new google.maps.Marker({
             position: latlng,
             map: map,
             zIndex: Math.round(latlng.lat()*-100000)<<5
             });

         google.maps.event.addListener(marker, 'click', function() {
             rumor.codeLatLng(latlng);
         });
         google.maps.event.trigger(marker, 'click');    
         return marker;
     },
     
     this.codeLatLng = function(latlng){
         if (geocoder) {
             geocoder.geocode({'latLng': latlng}, function(results, status) {
                 if (status == google.maps.GeocoderStatus.OK) {
                     var str = "";
                     $.each(results, function(){
                         str += "<h4>"+this.formatted_address+"</h4>";
                     });
                     infowindow.setContent(str); 
                     infowindow.open(map,marker);

                 } else {
                     //alert("No se encontraron resultados: " + status);
                	 $("#messageRumorForm").html("No se encontraron resultados");
                 }
             });
         }
     },
     
     this.codeAddress = function(address) {
    	 geocoder.geocode( { 'address': address}, function(results, status) {
	       if (status == google.maps.GeocoderStatus.OK) {
	         map.setCenter(results[0].geometry.location);
	         if (marker) {
		           marker.setMap(null);
		           marker = null;
		     }
	         marker = rumor.createMarker(results[0].geometry.location)
	       } else {
	         //alert("No se encontraron resultados: " + status);
	    	   $("#messageRumorForm").html("No se encontraron resultados");
	       }
	     });
     },  
     
     this.setPosition = function() {
    	 if(marker){
        	 $("#rumor_shop_latitude").val(marker.getPosition().lat());
        	 $("#rumor_shop_longitude").val(marker.getPosition().lng());
    	 }
     },
     
     this.validateForm = function() {
	    $("#submitRumorNew").click(function(event){
			rumor.setPosition();		
	        $("#form_rumor").validate({meta: "validate",
	  	  		onsubmit: false,
	  	  		onkeyup: false,
	  	  		onfocusout: false,
	  	  		showErrors: function(errorMap, errorList) {
	  	  			if (this.numberOfInvalids()) {
	  	            	$("#messageRumorForm").html("Por favor, ingrese todos los campos. <br>Si no especifica la direccion, debe seleccionarla en el mapa");
	  	            	setTimeout(function() { $("#messageRumorForm").empty() }, 15000);
	  	            }
	  			}
	    	});
	   		if ($("#form_rumor").valid() == true){
				return true;
	        }
	        return false;

	  	});
     }
}

var rumor = new Rumor();
