// JavaScript Document
$(document).ready(function()
{
 
	 $("#address").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
 
	jQuery('#address-submit-1').click(function(){
		jQuery('#map1').jmap('SearchAddress', {
				'query': jQuery('#address').val() + jQuery('#addressB').val(),
				'returnType': 'getLocations'
			}, function(result, options) {
				
				var valid = Mapifies.SearchCode(result.Status.code);
				if (valid.success) {
				

				var letteredIcon = new GIcon(G_DEFAULT_ICON);
				letteredIcon.image = "http://www.google.com/mapfiles/markerA.png"; 
				
				alert('puntos encontrados');
				
				jQuery.each(result.Placemark, function(i, point){
					jQuery('#map1').jmap('AddMarker',{
							'pointLatLng':[point.Point.coordinates[1], point.Point.coordinates[0]],
							'pointHTML': point.address  + '<br />' + [point.Point.coordinates[1], point.Point.coordinates[0]] ,
							'pointIsRemovable':true,
							'centerMap':true,
							'centerMoveMethod':'pan',
							'pointIcon':letteredIcon
						});
					});
				} else {
					
					alert(valid.message);
				}
			});
			return false;	
		});	


});


function georss_ciudad(idciudad,CityName,CityCountry,ProvinciaLat,ProvinciaLng,ProvinciaZoom,CiudadLat,CiudadLng){
	 if (CiudadLat==0){
		 $.ajax({
		   type: "GET",
		   dataType: "xml",
		   url: "/nucleo/booking/frontoffice/ajax/ciudad-georss-xml.asp?CityName="+CityName+"&CityCountry="+CityCountry,
		   error: function(objeto, quepaso, otroobj){
				//Si no anda el webservice pongo lo de la base de datos mia.
				//alert('error al conectar con el web service que me trae la lat long de la ciudad');
				CiudadLat = ProvinciaLat
				CiudadLng = ProvinciaLng	
				jQuery('#map1').jmap('init', {'mapType':'map','mapCenter':[CiudadLat, CiudadLng],'mapEnableType':'true','mapControl':'small'});
			},
		   success: function(xml){
				$(xml).find('geoname').each(function(){
						 var name 			= $(this).find('name').text();
						 var lat 			= $(this).find('lat').text();
						 var lng 			= $(this).find('lng').text();
						 var countryName 	= $(this).find('countryName').text();
						 	
						 $.ajax({
						   type: "GET",
						   dataType: "text",
						   url: "/nucleo/booking/frontoffice/ajax/ciudad-localizacion.asp?idciudad="+idciudad+"&lat="+lat+"&lng="+lng,
						   error: function(objeto, quepaso, otroobj){
									//alert('error al guardar los datos de la ciudad');
									},
						   success: function(){
									//alert('datos guardados');
									}
							 }); //cierro ajax que guarda los datos de la ciudad
							CiudadLat = lat
							CiudadLng = lng
							jQuery('#map1').jmap('init', {'mapType':'map','mapCenter':[CiudadLat, CiudadLng],'mapEnableType':'true','mapControl':'small'});
					 }); 
		   	}
		 }); //cierro ajax xml

	}//cierro if
	
	else{
		
		
		jQuery('#map1').jmap('init', {'mapType':'map','mapCenter':[CiudadLat, CiudadLng],'mapEnableType':'true','mapControl':'large'});	
	}
	
	jQuery('#map1').jmap('CreateAdsManager');

	jQuery('.add-streetview').click(function(){
					jQuery('#map1').jmap('CreateStreetviewPanorama', {
							'latlng':[CiudadLat, CiudadLng]
					}, function (view, options){
							jQuery('.remove-streetview').click(function(){
									jQuery('#map1').jmap('RemoveStreetviewPanorama', view);
									jQuery('#map1').jmap('init', {'mapType':G_HYBRID_MAP,'mapCenter': [CiudadLat, CiudadLng]});
									return false;
							});
					});
					return false;
			});
	






	
	
	
}