function ajax_submit(button) {
	var f = button.form;
	button.disabled = true;
	new Ajax.Request('form.send/format/json', {
		parameters: f.serialize(true),
		method:'post',
		onSuccess: function(transport){
			var response = transport.responseText || '';
			if (response.isJSON()) {
				var response_obj = response.evalJSON();
				var messages = response_obj.data.messages;
				var error_string = '';
				if (!Object.isArray(messages)) {
					var messages_hash = $H(messages);
					
					messages_hash.each(function(pair) {
						switch (pair.key) {
							case 'email':
								var errortype = $H(pair.value).keys()[0];
								if (errortype=='isEmpty') {
									error_string += 'El campo "' + pair.key + '" es obigatorio.\n';
								} else {
									error_string += 'La dirección de "' + pair.key + '" no es correcta.\n';
								}break;
							default:
								error_string += 'El campo "' + pair.key + '" es obigatorio.\n';
								break;
						}
					});					
				}
				if (error_string == '') {
					alert('Tu solicitud ha sido enviada con éxito. En breve, nos pondremos en contacto contigo.\nMuchas gracias.\n\nEl equipo de Pirineo en Ruta');
					f.reset();
				} else {				
					alert(error_string);
				}
			} else {
				alert('Ha habido un error al enviar el formulario, por favor inténtelo de nuevo.');
			}			
			button.disabled = false;
		},
		onFailure: function(){
			alert('Ha habido un error al enviar el formulario, por favor inténtelo de nuevo.');
			button.disabled = false;
		}
	});
}

//document.observe('dom:loaded', function () {
	activateBannerScroller.delay(6);
//});


document.observe("dom:loaded", function() {	
	//Detectar el slideshow de banners
	window.animating_close = false;
	window.animating_open = false;
	window.interval = generarSlideshowBanners();
});

function generarSlideshowBanners() {
	var bloque = $('bloque_casos_home');
	if (bloque) {
        var interval = setInterval(function(){			
            homeOfertasSlider(1);
        }, 3000);
		return interval;
	}	
	return false;
}

function restartSlideshowBanners() {
	clearInterval(window.interval);
	window.interval = generarSlideshowBanners();
	$('button_pausa').show();
	$('button_play').hide();
}

function pararSlideshowBanners() {	
	clearInterval(window.interval);
	$('button_pausa').hide();
	$('button_play').show();
}

/**
 * Mover las ofertas
 */
function homeOfertasSlider(next) {
	if (!window.animating_close && !window.animating_open) {
		window.animating_close = true;
		window.animating_open = true;

		var bloque = $('bloque_casos_home');
		var current = bloque.readAttribute('_current');
		var item = parseInt(current, 10);

		
		if (next == '-1' && item == 0) {
			window.animating_close = false;
		} else {
			if ($('bloque_galeria_enlace_' + item)) {
				$('bloque_galeria_enlace_' + item).hide();
			}
			if ($('bloque_galeria_enlace_2_' + item)) {
				$('bloque_galeria_enlace_2_' + item).hide();
			}
			$('bloque_galeria_' + item).fade({
				duration: 0.5,
				after: function(){
					window.animating_close = false;
				}
			});
		}
		
		
		if (next == '-1') {
			next_item = item - 1;			
		} else {
			next_item = item + 1;
		}
		if ($('bloque_galeria_' + next_item)) {
			if ($('bloque_galeria_enlace_' + next_item)) {
				$('bloque_galeria_enlace_' + next_item).show();
			}
			if ($('bloque_galeria_enlace_2_' + next_item)) {
				$('bloque_galeria_enlace_2_' + next_item).show();
			}
			$('bloque_galeria_' + next_item).appear({
				duration: 0.5,
				after: function(){
                    window.animating_open = false;
                }
			});
			bloque.writeAttribute('_current', next_item);
		} else {
			if ($('bloque_galeria_enlace_0')) {
				$('bloque_galeria_enlace_0').show();
			}
			if ($('bloque_galeria_enlace_2_0')) {
				$('bloque_galeria_enlace_2_0').show();
			}
			$('bloque_galeria_0').appear({
				duration: 0.5,
				after: function(){
                    window.animating_open = false;
                }
			});
			bloque.writeAttribute('_current', 0);
		}
	}
}

function activateBannerScroller() {
	var banner_scroller = $('banner_scroller');
	var banner_scroller_container = $('banner_scroller_container'); 
	
	if (banner_scroller) {
		var counter = 0;
		var total_height = 0;
		var banner_scroller_container_height = banner_scroller_container.getHeight();
		
		banner_scroller.select('img').each(function(img) {
			counter++;
			total_height = total_height + img.getHeight() + 15;
		});
		
		banner_scroller_container.setAttribute('_current','banner_scroller');
		
		if (total_height<banner_scroller_container_height) {
			return;
		}
		
		var duration =	counter * 10;
				
		banner_scroller.setStyle({'top': '0px'});
		
		banner_scroller.morph('top:-' + (total_height + banner_scroller_container_height + banner_scroller_container_height) + 'px;', {
			duration: duration,
			transition: 'linearCustom'
		});
		
		
		
		var i = 0;
		var interval = setInterval(function(){
			
			var current_id = $('banner_scroller_container').getAttribute('_current');
			var current_scroll = $(current_id);
							
			var current_margin = current_scroll.getStyle('top');
			current_margin = parseInt(current_margin.replace("px", ""), 10);
		
			if (current_margin < (0 - (total_height - banner_scroller_container_height))) {
				
				var div = new Element('div', {
					'id': 'banner_scroller' + i
				}).update(banner_scroller.innerHTML);
				div.addClassName('carrusel_bloque_nodes');
				div.setStyle({
					'top': (banner_scroller_container_height) + 'px'
				});
				banner_scroller_container.appendChild(div);
				
				banner_scroller_container.setAttribute('_current','banner_scroller' + i);					
				
				div.morph('top:-' + (total_height + banner_scroller_container_height) + 'px;', {
					duration: duration,
					transition: 'linearCustom'
				});
			}
			i++;
        }, 100);
	}
}


