$(function(){
	
	if (document.getElementById('mapa-google')){
		load();
		$('body').unload(GUnload);
	}
	
	//escondemos el formulario 
	$('#form_testimonio').hide();
	
	//mostramos el formulario para intruducir nuevos testimonios
	$('#nuevo_testimonio').click(function(){
		
		$('#form_testimonio:visible').slideUp('slow');
		$('#form_testimonio:hidden').slideDown('slow');
		
		return false;
	});
	
	//formulario testimonios
	$('#form_testimonio').submit(function(){
		$('input[@type="submit"]',this).attr('disabled','disabled');
		$('.cargando',this).show();
			
		//cuidado con el textarea, aqui me pueden hacer sqlinjection
		$.ajax({ 
		  type: "POST", 
		  url: "/inc/form-testimonios.php", 
		  data: "name=" + $('#name').attr('value') + "&testimonio=" + $('#testimonio').attr('value') + "&lang=" + $('#lang').attr('value') + "&ajax=1", 
		  success: function(msg){ 
			//lo pongo todo en su estado inicial
			$('input[@type="submit"]',this).removeAttr("disabled");
			$('.cargando',this).fadeOut('slow');
			$('input[@type="text"],textarea',this).attr('value','');
			
			$('#form_testimonio:visible').slideUp('slow',function(){
				$('#nuevo_testimonio').parent().after('<p class="form-alert">' + msg + '</p>');
				$('p.form-alert').fadeIn('slow',function(){
					setTimeout(function(){$('p.form-alert').fadeOut('slow', function(){ $('p.form-alert').remove() });},5000);
				});
			});
		  } 
		});
		
		return false;
	});
	
});


function load() {

    function crearMarcador(point, text) {
        var marker = new GMarker(point);
        
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(text);
        });
        return marker;
    } 
        
    if (GBrowserIsCompatible()) {
	
        var point = new GLatLng(37.69575,-0.96960);

        var html_oxoplas = '<p style="margin:0; color:#6560A4">DISTRIBUIDORA DE AGROQUÍMICOS S.L.<br />Ctra Del Albujón-Cabo de Palos, Km-7.5<br />La Palma-Cartagena (Murcia)  C.P:30593<br />Teléfono: 968554437 / 968554686<br />Fax: 968554044</p>';
              
        var map = new GMap2(document.getElementById("mapa-google"));
        
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());
        map.setCenter( point, 15);
        map.addOverlay(crearMarcador(point, html_oxoplas));
        map.openInfoWindowHtml(point, html_oxoplas);

        /*var icon = new GIcon();
        icon.iconSize = new GSize(37,50);
        icon.iconAnchor = new GPoint(13, 50);
        icon.infoWindowAnchor = new GPoint(13, 50);        
        icon.image = "http://www.fundamentosweb.org/2006/img/bocadillo-logo.png";
        icon.shadow = "http://www.fundamentosweb.org/2006/img/bocadillo_sombra.png";
        
        map.addOverlay(crearMarcador(point, icon, html_oxoplas));*/
        
        var geocoder = new GClientGeocoder();

		function showAddress(address) {
		  geocoder.getLatLng(
		    address,
		    function(point) {
		      if (!point) {
		        alert(address + " not found");
		      } else {
				map.addControl(new GSmallMapControl());
		        map.addControl(new GMapTypeControl());
		        map.addControl(new GScaleControl());
		        map.setCenter(point, 16);
		        map.openInfoWindowHtml(point, html_oxoplas);
		      }
		    }
		  );
		}
		
		//showAddress("Avenida de la Estación, 30594, Cartagena, Murcia, Región de Murcia, España");
    }
}



