$(document).ready(function(){

/*
 * 		Navegación Superior (Menú desplegable)
 */


	(function($) {
		$("#nav li.sub").hover(
			function() {
				$(this).find('ul:not(:animated)').fadeIn("fast");
				$('ul:first',this).css('visibility', 'visible');
			},
			function() {
				$(this).find('ul:not(:animated)').fadeOut("fast");
				$('ul:first',this).css('visibility', 'hidden');
			}
		)
	})(jQuery);

           
/*
 * 		Funciones del ciclo (jquery.cycle.js)
 */

	// Agregar la funcionalidad de 'scrollVert' para las cajas de desplazamiento
	(function($) {

		$.fn.cycle.transitions.scrollVert = function($cont, $slides, opts) {
		    $cont.css('overflow','hidden');
		    opts.before.push(function(curr, next, opts, fwd) {
		        $(this).show();
		        var currH = curr.offsetHeight, nextH = next.offsetHeight;
		        opts.cssBefore = fwd ? { top: -nextH } : { top: nextH };
		        opts.animIn.top = 0;
		        opts.animOut.top = fwd ? currH : -currH;
		        $slides.not(curr).css(opts.cssBefore);
		    });
		    opts.cssFirst = { top: 0 };
		    opts.cssAfter = { display: 'none' }
		};

	})(jQuery);


/*
 * 		Últimas Noticias Scroller
 */

	$('#scrollNoticias ul').cycle({ 
	    fx: 'scrollVert',
		speed: 650,
		rev: true,
		timeout: 10000,
		next:   '#scrollNoticias ol li.siguiente', 
	    prev:   '#scrollNoticias ol li.anterior'
	});


/*
 * 		twitter Scroller
 */

	$('#twitter ul').cycle({ 
	    fx: 'scrollVert',
		speed: 850,
		rev: true,
		timeout: 10000,
		next:   '#twitter ol li.siguiente', 
	    prev:   '#twitter ol li.anterior'
	});


/*
 * 		Validación del Formulario de Contacto
 */


	$('#formulario-de-contacto').submit(function() {
	
		// Desactivar el botón de enviar
		$('#formulario-de-contacto input[class=enviar]')
			.attr('value', 'Enviando mensaje')
			.attr('disabled', 'disabled');
	
		// Petición AJAX POST
		$.post(
			$(this).attr('action'),
			{
				name:$('#name').val(),
				email:$('#email').val(),
				message:$('#message').val()
			},
			function(errors) {
				// No hay errores
				if (errors == null) {
					$('#formulario-de-contacto')
						.hide()
						.html('<h3>Gracias.</h3><p>Su mensaje ha sido enviado correctamente.</p>')
						.show();
				}
	
				// Errores
				else {
					// Volver a activar el botón de enviar
					$('#formulario-de-contacto input[type=submit]')
						.removeAttr('disabled')
						.attr('value', 'Por favor. Env\u00EDenos su inquietud.');
	
					// Problemas con el servidor técnico, el correo electrónico no se pudo enviar
					if (errors.server != null) {
						alert(errors.server);
						return false;
					}
	
					// Vacíe el errorbox y restablecer las alertas de error
					$('#formulario-de-contacto .errorbox').html('<ul></ul>').show();
					$('#formulario-de-contacto li').removeClass('alert');
	
					// Bucle sobre los errores, marcar los campos de entrada correspondiente,
					// y agregar los mensajes de error a la errorbox.
					for (field in errors) {
						if (errors[field] != null) {
							$('#' + field).parent('li').addClass('alert');
							$('#formulario-de-contacto .errorbox ul').append('<li>' + errors[field] + '</li>');
						}
					}
				}
			},
			'json'
		);
	
		// Impedir  el no-AJAX envío del formulario
		return false;
	});

});
