$(document).ready(function() {
	$(".fixpng").each(function() {
		IEPNGFix.process(this, 1);
	});
	
    $('#mycarousel').jcarousel({
		vertical: true,
		scroll: 1,
        auto: 2,
        wrap: 'last',
        initCallback: mycarousel_initCallback,
		easing: 'easeInOutBack',
		animation: 1500		
    });
	
});

function mycarousel_initCallback(carousel){
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function lb_callback(){
	$('.lightbox').find('form').attr('name','contactoLB');
	$('.lightbox').find(".error").hide();
  	$('.lightbox').find(".boton_enviar").click(function() {	
    	return false;
	});
}

function validaFormulario() {
	var nombre = $('.lightbox').find("input#nombre").val();
	var email = $('.lightbox').find("input#email").val();
	var comentario = $('.lightbox').find("#comentario").val();
	
	$('.lightbox').find(".error").hide();
	
	// Validar que los campos nombre y email no se encuentren vacios
	if ((nombre == null || nombre.length == 0 || /^\s+$/.test(nombre)) && (email == null || email.length == 0 || /^\s+$/.test(email))) {
		$('.lightbox').find("label#nombre_error").show();
		$('.lightbox').find("label#email_error").show();
		return false;
	}else{
		if ((nombre == null || nombre.length == 0 || /^\s+$/.test(nombre))) {
			$('.lightbox').find("label#nombre_error").show();
			return false;
		}
		if ((email == null || email.length == 0 || /^\s+$/.test(email))) {
			$('.lightbox').find("label#email_error").show();
			return false;
		}
		else{
			// Validar el formato del email
			var formato = /^[A-Za-z.][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
			if( !(formato.test(email)) ) {
				$('.lightbox').find("label#email_invalido_error").show();
				return false;
			}
		}
		if ((comentario == null || nombre.length == 0 || /^\s+$/.test(comentario))) {
			$('.lightbox').find("label#comentario_error").show();
			return false;
		}		
	}
	
	return true;
}

function procesarFormulario() {
	var nombre = $('.lightbox').find("input#nombre").val();
	var email = $('.lightbox').find("input#email").val();
	var comentario = $('.lightbox').find("#comentario").val();
	var dataString = 'nombre='+nombre+'&email='+email+'&comentario='+comentario;
	
	if(validaFormulario()) {
		$.ajax({
			type: "POST",
			url: "send.php",
			data: dataString,
			success: function() {		
				$('.lightbox').find(".formulario_header").find(".texto").hide();
				$('.lightbox').find(".formulario_campos").hide();
				$('.lightbox').find(".formulario_footer").show();
			}
		});
	}
	else{
		return false;
	}
}

function enviarFormulario(){	
	if(procesarFormulario()){
		return false;
	}
}

function cerrarLB(){
	$('.lb_close').trigger('click');
}

// Funcion para iniciar la api de google maps
    function initialize() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
		var coordenadas = new GLatLng(-33.430313,-70.623808);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
        map.setCenter(coordenadas, 12);
		map.zoomIn(coordenadas, 12);
		map.zoomIn(coordenadas, 12);
		map.openInfoWindowHtml(coordenadas,
			html=("<table><tr><td><img src='img/icon_i2b_mapa.png' alt='i2b' /></td></tr><tr><td class='direccion_titulo'>Direcci&oacute;n: <img src='img/icon_star_mapa.png' alt='star' /></td></tr><tr><td class='direccion'><a href='http://maps.google.com/maps?f=q&source=s_q&hl=es&geocode=&q=av.+providencia+929,+providencia,+santiago,+chile&sll=37.0625,-95.677068&sspn=31.839416,56.601563&ie=UTF8&view=map'>Av. Providencia 929<br />Providencia, Santiago, Chile</a></td></tr><tr><br /><td></td></tr><tr><td class='direccion'>C&oacute;mo llegar: <a href='http://maps.google.com/maps?f=q&source=s_q&hl=es&geocode=&q=av.+providencia+929,+providencia,+santiago,+chile&sll=37.0625,-95.677068&sspn=31.839416,56.601563&ie=UTF8&view=map'>Hasta aqu&iacute;</a> - <a href='http://maps.google.com/maps?f=q&source=s_q&hl=es&geocode=&q=av.+providencia+929,+providencia,+santiago,+chile&sll=37.0625,-95.677068&sspn=31.839416,56.601563&ie=UTF8&view=map'>Desde aqu&iacute;<br />Buscar en alrededores</a></td></tr></table>"));;
				   
		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		var baseIcon = new GIcon();
		baseIcon.shadow = "img/icon_mapa.png";
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		
		// Creates a marker whose info window displays the letter corresponding
		// to the given index.
		function createMarker(point, index) {
		  // Create a lettered icon for this point using our icon class
		  var letter = String.fromCharCode("A".charCodeAt(0) + index);
		  var letteredIcon = new GIcon(baseIcon);
		  
		
		  // Set up our GMarkerOptions object
		  markerOptions = { icon:letteredIcon };
		  var marker = new GMarker(point, markerOptions);
		
		  GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml("Marker <b>" + letter + "</b>");
		  });
		  return marker;
		}
		// Add 10 markers to the map at random locations
		var bounds = map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		var lngSpan = northEast.lng() - southWest.lng();
		var latSpan = northEast.lat() - southWest.lat();
		for (var i = 0; i < 1; i++) {
		  var point = coordenadas;
		  map.addOverlay(createMarker(point, i));
		}		

      }
    }