$(document).ready(function(){
	
	mapTool();
	
	// Fancybox
	$("a.single_image").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'speedIn'			: 300, 		
		'speedOut'			: 200, 
		'overlayColor'		: '#000',
		'overlayOpacity'	: .7,
		'overlayShow'		: true,
		'hideOnContentClick': true
	});
		
});

// Limita caracteres en el formulario de colaboradores
$('#form_comentarios').keyup(function() {
	var maximum = 300;
  	var len = this.value.length;
  	if (len >= maximum) {
    	this.value = this.value.substring(0, maximum);
  	}
  	var charVal = maximum - len;
  	$('#form_caracteres_restantes').html('&nbsp;<small>(' + charVal + ' caracteres restantes)</small>');
});

// Formulario de contacto
function formTool(){
	
	var form_error	= false;
	$('#form_errors').fadeOut();

	var color_error = '2px solid red';
	var color_ok	= '2px solid #EFEFEF';

	$(':input[value=""]').css('border' , color_error );
	$(':input[value!=""]').css('border' , color_ok );
	$(':input[type=button]').css('border' , '0' );
	
	if ($('#form_nombre').val() == 0) 		{ form_error = true }
	if ($('#form_telefono').val() == 0) 	{ form_error = true }
	if ($('#form_email').val() == 0) 		{ form_error = true }
	if ($('#form_comentarios').val() == 0) 	{ form_error = true }

	if (form_error == false) {
		
		// Todo ok
	
		$('#form_errors').html('');
	
		$('#contact_form').fadeOut(600, function(){
		
			$('#contact_form_sent').fadeIn().html('Su <strong>mensaje</strong> ha sido <strong>enviado correctamente</strong> y ser&aacute; contestado tan pronto como nos sea posible. <strong>Muchas gracias por confiar</strong> en nosotros.');
			
			// Enviar formulario
			
			$('#formulario_comentarios').submit();
		
		});
	      
	} else {
	
		// Mensaje de error	
		$('#form_errors').html('<strong>Por favor, rellene el formulario correctamente.</strong>').fadeIn();
		
		// Efecto de parpadeo en campos erroneos
		var blink_speed = 200;
		
		setTimeout(function() {
			$(':input[value=""]').css('border', color_ok);
		}, blink_speed);
		setTimeout(function() {
			$(':input[value=""]').css('border', color_error);
		}, blink_speed*2);
		setTimeout(function() {
			$(':input[value=""]').css('border', color_ok);
		}, blink_speed*3);
		setTimeout(function() {
			$(':input[value=""]').css('border', color_error);
		}, blink_speed*4);
		setTimeout(function() {
			$('#form_errors').fadeOut();
		}, blink_speed*8);		
	}

}

// Page Scroll
function scroll_up(){
	$('html, body').animate({scrollTop:0}, 'slow');
	return false;
}

// Snow Effect
function snow_effect(){
	$().jSnow({
	    vSize			:'200',
	    fadeAway		: true,
	    flakes 			: 30,
	    fallingSpeedMax : 3,  
	    fallingSpeedMin : 0,  
	    interval 		: 30
	});
}

// Mapa de localización - Google Maps API v3
function mapTool(){
	
	var myCenter = new google.maps.LatLng(gMapCoordLat , gMapCoordLon);
	var myOptions = {
		center: myCenter,
		mapTypeControl: false,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		navigationControl: true,
		navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL },
		zoom: 16
	};
	
	var mapDiv = document.getElementById("mapDiv");
	var map = new google.maps.Map(mapDiv, myOptions);
	
	var myOptionsSmall = {
		center: myCenter,
		draggable: false,
		mapTypeControl: false,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		navigationControl: false,
		streetViewControl: false,
		zoom: 11
	};
	
	var mapDivSmall = document.getElementById("mapDivSmall");
	var mapSmall = new google.maps.Map(mapDivSmall, myOptionsSmall);
	
	var marker = new google.maps.Marker({
		map: map,
		position: myCenter,
		title: gMapTitle
	});
	
	var markerSmall = new google.maps.Marker({
		map: mapSmall,
		position: myCenter,
		title: gMapTitle
	});

    var contentString =     
    '<div id="gmContent" style="width:400px;">' +
    '<h3 id="firstHeading" class="firstHeading">' + gMapTitle + '</h3>' +
    '<p>' + gMapDescription + '</p>' 
    '</div>';
     
    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });

    google.maps.event.addListener(marker, 'click', function() {
    	infowindow.open(map,marker);
    });

	setTimeout(function() {
		google.maps.event.trigger(map, 'resize');
		google.maps.event.trigger(mapSmall, 'resize');
		map.setCenter(myCenter);
		mapSmall.setCenter(myCenter);
	}, 500);

}
