/*!
* jQuery estudio crealia
*/

/* eliminar marco de enlaces */
jQuery(function($) {
	$('a').focus(function() {
		$(this).trigger('blur');
	});
	$('.display').focus(function() {
		$(this).trigger('blur');
	});
	
	$(window).resize(function() {
		// on hover
		$("#vintra").css(  
		{
			position: 'fixed',  
			left: ($(window).width() - 514) / 2 + 'px',  
			top: ($(window).height() -  254) / 2 + 'px'    
		});
		
		$("#fintra").css(  
		{
			position: 'fixed',
			width: $(window).width() + 'px',
			height: $(window).height() + 'px'    
		});
		$("#referenciacont").css(  
		{
			position: 'fixed',  
			left: ($(window).width() - 514) / 2 + 'px',  
			top: ($(window).height() -  254) / 2 + 'px'    
		});
		//$("#ventanaavisos").css(  
		//{
			//position: 'fixed',  
			//left: ($(window).width() - 504) / 2 + 'px',  
			//top: ($(window).height() -  244) / 2 + 'px'    
		//});
	});
	
	$(window).resize();
});

// efectos menu
jQuery(function($) {
	// nos aseguramos de no aplicar el efecto a versiones de ie que no soporten este código
	if ($.browser.msie && $.browser.version < 7) return;
		
	$('#nav')
		.removeClass('efecto')
		.find('a')
		.append('<span class="hover" />').each(function () {
			var $span = $('> span.hover', this).css('opacity', 0);
	});
		
	$('#nav li').hover(function () {
		// on hover
		$(this).find('span.hover:first').stop().fadeTo('fast', 1);
	}, function () {
		// off hover
		$(this).find('span.hover:first').stop().fadeTo('fast', 0);
	});

	$('#nav li ul').css('left', 0)
	.not('.selec').hide();
	$('#nav li').hover(function() {
		$(this).find('ul').not('.selec').stop().fadeTo('fast', 1);
	}, function () {
		
		//$(this).find('ul').not('.selec').stop().fadeTo('slow', 0, function() {
			// oculto elemento para que no aparezca hasta pasar sobre el botón
			//$(this).hide();
		//});
		var obj = $(this).find('ul').not('.selec');
		//setTimeout(function(){
			obj.stop();
			obj.fadeTo('slow', 0, function() {
				// oculto elemento para que no aparezca hasta pasar sobre el botón
				$(this).hide();
			});
		//}, 200);
	});
	
//	var e = $('.notice'); 
//e.fadeIn(); 
//e.queue(function(){ 
  //setTimeout(function(){ 
    //e.dequeue(); 
  //}, 2000 ); 
//}); 
	
});

// efectos menu sub
jQuery(function($) {
	// nos aseguramos de no aplicar el efecto a versiones de ie que no soporten este código
	if ($.browser.msie && $.browser.version < 7) return;
		
	$('#menusub')
		.removeClass('efecto')
		.find('a')
		.append('<span class="hover" />').each(function () {
			var $span = $('> span.hover', this).css('opacity', 0);
	});
		
	$('#menusub li').hover(function () {
		// on hover
		$(this).find('span.hover:first').stop().fadeTo('fast', 1);
	}, function () {
		// off hover
		$(this).find('span.hover:first').stop().fadeTo('fast', 0);
	});

});

// efectos menu lateral
jQuery(function($) {
	// nos aseguramos de no aplicar el efecto a versiones de ie que no soporten este código
	if ($.browser.msie && $.browser.version < 7) return;
		
	$('#menulat')
		.removeClass('efecto')
		.find('a')
		.append('<span class="hover" />').each(function () {
			var $span = $('> span.hover', this).css('opacity', 0);
	});
		
	$('#menulat li').hover(function () {
		// on hover
		$(this).find('span.hover:first').stop().fadeTo('fast', 1);
	}, function () {
		// off hover
		$(this).find('span.hover:first').stop().fadeTo('fast', 0);
	});

});

// efectos menu redes
jQuery(function($) {
	// nos aseguramos de no aplicar el efecto a versiones de ie que no soporten este código
	if ($.browser.msie && $.browser.version < 7) return;
		
	$('#red')
		.removeClass('efecto')
		.find('a')
		.append('<span class="hover" />').each(function () {
			var $span = $('> span.hover', this).css('opacity', 0);
	});
		
	$('#red li').hover(function () {
		// on hover
		$(this).find('span.hover:first').stop().fadeTo('fast', 1);
	}, function () {
		// off hover
		$(this).find('span.hover:first').stop().fadeTo('fast', 0);
	});
	
});
	
// efectos menu pie
jQuery(function($) {
	// nos aseguramos de no aplicar el efecto a versiones de ie que no soporten este código
	if ($.browser.msie && $.browser.version < 7) return;
		
	$('#navpie')
		.removeClass('efecto')
		.find('a')
		.append('<span class="hover" />').each(function () {
			var $span = $('> span.hover', this).css('opacity', 0);
	});
		
	$('#navpie li').hover(function () {
		// on hover
		$(this).find('span.hover:first').stop().fadeTo('fast', 1);
	}, function () {
		// off hover
		$(this).find('span.hover:first').stop().fadeTo('fast', 0);
	});

	$('#navpie li ul').css('left', 0)
	.not('.selec').hide();
	$('#navpie li').hover(function() {
		$(this).find('ul').not('.selec').stop().fadeTo('fast', 1);
	}, function () {
		
		//$(this).find('ul').not('.selec').stop().fadeTo('slow', 0, function() {
			// oculto elemento para que no aparezca hasta pasar sobre el botón
			//$(this).hide();
		//});
		var obj = $(this).find('ul').not('.selec');
		//setTimeout(function(){
			obj.stop();
			obj.fadeTo('slow', 0, function() {
				// oculto elemento para que no aparezca hasta pasar sobre el botón
				$(this).hide();
			});
		//}, 200);
	});
	
//	var e = $('.notice'); 
//e.fadeIn(); 
//e.queue(function(){ 
  //setTimeout(function(){ 
    //e.dequeue(); 
  //}, 2000 ); 
//}); 
	
});

/* formulario newsletter */
jQuery(function($) {
				
	$('<div></div>')
		.attr({
			'id': 'formnewsload'
		})
			.hide()
			.insertAfter('#formnewsdiv');
			
			
			
	var $buscador = $('#formnews li').addClass('sublabel');
	var $buscadorLabel = $buscador.find('label');
	$buscadorLabel.show();

	$('#mailnews').focus(function() {
		if ($(this).val() == $(this).attr('defaultValue')) {
			//$(this).val('');
			//$(this).addClass('imputactivo');
			$buscadorLabel.fadeTo('fast', 0.3);
		}
	});
	
	$('#mailnews').keyup(function() {
		if ($(this).val() != '') {
			//$(this).val('');
			//$(this).addClass('imputactivo');
			$buscadorLabel.fadeTo('fast', 0);
		} else {
			$buscadorLabel.fadeTo('fast', 0.3);
		}
	});
	
	$('#mailnews').blur(function() {
		if ($(this).val() == '') {
			//$(this).removeClass('imputactivo');
			//$def = $(this).attr('defaultValue');
			//$(this).val($def);
			$buscadorLabel.fadeTo('fast', 1);
		}
	});
	
	$('#formnews').submit(function () {
		//if ($('#usuario').val() == $('#usuario').attr('defaultValue') || $('#password').val() == $('#password').attr('defaultValue')) {
		//if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($('#mailnews').val())) {
		if (/.+@.+\.[a-zA-Z]{2,4}$/.test($('#mailnews').val())) {
			$('#formnewsdiv').hide();
			$('#formnewsload').show();
			
			
			datosenviar = $(this).serialize();
			$.ajax({
				url: "/alta.php",
				data: datosenviar,//"mailSend=antonio@e-crealia.com",
				async:true,
				beforeSend: function(objeto){

				},
				complete: function(objeto, exito){

				},
				contentType: "application/x-www-form-urlencoded",
				dataType: "html",
				error: function(objeto, quepaso, otroobj){

				},
				success: function(datos){
					if (datos == 2) {
						var mensaje = 'No incluyó un correo electrónico válido';
					} else if (datos == 1) {
						var mensaje = 'Hemos enviado un correo de confirmación';
					} else {
						var mensaje = 'Hubo un problema al incluir su correo en nuestra lista';
					}
					$('#newsinfo').text(mensaje);
					// mostrar formulario
					$('#mailnews').val('');
					$buscadorLabel.fadeTo('fast', 1);
					$('#formnewsdiv').show();
					$('#formnewsload').hide();
					
				},
				type: "POST"
			});
		} else {
			var mensaje = "Debe incluir un correo electrónico válido";
			$('#newsinfo').text(mensaje);
		}

		return false;
	});
});

/* formulario referencias */
jQuery(function($) {
	//$('#referenciacont').hide();
	//$('#referenciacont').css('opacity', 0);
	
	$('<div></div>')
		.attr({
			'id': 'formrefload'
		})
			.hide()
			.insertAfter('#formref');
	
	$vcmostrada = 0;
	
	var $nombre = $('#formref li#nombreli').addClass('sublabelnombre');
	var $nombreLabel = $nombre.find('label');
	
	var $correo = $('#formref li#emailli').addClass('sublabel');
	var $correoLabel = $correo.find('label');
	
	$('.esobre').click(function() {
		if ($vcmostrada == 0) {
			$vcmostrada = 1;
			$('#ref').attr('value', $(this).attr('title'));
			//$('<span></span>')
					//.attr({
						//'id': 'avisoref'
					//})
					//.append($(this).attr('title'))
					//.hide()
					//.insertAfter('#ulsobre')
					//.fadeIn('fast');
			$('#referenciacont').fadeIn('fast');
		} else {
			$vcmostrada = 0;
			$('#referenciacont').fadeOut('fast', function() {
				// inicializar aspecto de formulario
				$(this).find('input').val('');
				$nombreLabel.fadeTo('fast', 1);
				$correoLabel.fadeTo('fast', 1);
				$('#avisoref').remove();
			});	
		}
		return false;
	});
	
	//$('#eevent').toggle(function() {
		//$('#referenciacont').fadeIn('fast');
		//$('#referenciacont').animate({height: 'toggle'}, 'fast').animate({opacity: 1}, 'fast');
		//return false;
	//}, function () {
		
		//$('#intranet').fadeOut('fast');
		//$('#referenciacont').animate({opacity: 0}, 'fast').animate({height: 'toggle'}, 'fast');
		//$('#referenciacont').fadeOut('fast', function() {
			// inicializar aspecto de formulario
			//$(this).find('input:text').val('');
			//$correoLabel.fadeTo('fast', 1);
			//$nombreLabel.fadeTo('fast', 1);
			//$('#avisoref').remove();
		//});

		//return false;
	//});
	
	$('#emailref').focus(function() {
		if ($(this).val() == $(this).attr('defaultValue')) {
			//$(this).val('');
			//$(this).addClass('imputactivo');
			$correoLabel.fadeTo('fast', 0.3);
		}
	});
	
	$('#emailref').keyup(function() {
		if ($(this).val() != '') {
			//$(this).val('');
			//$(this).addClass('imputactivo');
			$correoLabel.fadeTo('fast', 0);
		} else {
			$correoLabel.fadeTo('fast', 0.3);
		}
	});
	
	$('#emailref').blur(function() {
		if ($(this).val() == '') {
			//$(this).removeClass('imputactivo');
			//$def = $(this).attr('defaultValue');
			//$(this).val($def);
			$correoLabel.fadeTo('fast', 1);
		}
	});
	
	$('#nombreref').focus(function() {
		if ($(this).val() == $(this).attr('defaultValue')) {
			//$(this).val('');
			//$(this).addClass('imputactivo');
			$nombreLabel.fadeTo('fast', 0.3);
		}
	});
	
	$('#nombreref').keyup(function() {
		if ($(this).val() != '') {
			//$(this).val('');
			//$(this).addClass('imputactivo');
			$nombreLabel.fadeTo('fast', 0);
		} else {
			$nombreLabel.fadeTo('fast', 0.3);
		}
	});
	
	$('#nombreref').blur(function() {
		if ($(this).val() == '') {
			//$(this).removeClass('imputactivo');
			//$def = $(this).attr('defaultValue');
			//$(this).val($def);
			$nombreLabel.fadeTo('fast', 1);
		}
	});

	$('#formref').submit(function () {
		//if ($('#email').val() == '') {
		$('#avisoref').remove();
		contenido = $('#emailref').val();
		nombre = $('#nombreref').val();
		if (!/.+@.+\.[a-zA-Z]{2,4}$/.test(contenido) || nombre == '') {
			var mensajeAviso = 'Por favor, incluye tu <strong>nombre</strong> y un <strong>correo electr&oacute;nico</strong> de destino.';
			$('<span></span>')
				.attr({
					'id': 'avisoref'
				})
				.append(mensajeAviso)
				.hide()
				.insertAfter('#ulsobre')
				.fadeIn('fast');				   
		//} else {
				
		} else {
			$('#formrefload').show();
			$('#formref').hide();
			$('#avisoref').remove();
			
			
			datosenviar = $(this).serialize();
			$.ajax({
				url: "/enviarreferencia.php",
				data: datosenviar,//"mailSend=antonio@e-crealia.com",
				async:true,
				beforeSend: function(objeto){
					//calen.fadeTo('fast', 0.1);
					//calen.addClass('objload');
					//$("#contcalend").remove();
					//calen.find('#contcalend').remove();
					//alert("Adiós, me voy a ejecutar");
					
				},
				complete: function(objeto, exito){
					//alert("Me acabo de completar")
					//if(exito=="success"){
						// alert("Y con éxito");
					//}
					//$("#calendario").append("<p>Recibido: " + objeto + "</p>");
					
				},
				contentType: "application/x-www-form-urlencoded",
				dataType: "html",
				error: function(objeto, quepaso, otroobj){
					//alert("Estas viendo esto por que fallé");
					//alert("Pasó lo siguiente: "+quepaso);
					//$("#calendario").append("<p>Error: " + quepaso + "</p>");
					
				},
				success: function(datos){
					
					$('#formref').find('input').val('');
					$nombreLabel.fadeTo('fast', 1);
					$correoLabel.fadeTo('fast', 1);
					
					$('#formref').show();
					$('#formrefload').hide();
					$('#avisoref').remove();
					if (datos == 1) {
						var mensajeAviso = 'La información ha sido compartida.';	
					} else if (datos == 2) {
						var mensajeAviso = 'El sistema no incluyó una referencia de la información correcta.' . datos;
					} else {
						var mensajeAviso = 'Hubo un problema al compartir la información.' . datos;
					}
					$('<span></span>')
					.attr({
						'id': 'avisoref'
					})
					.append(mensajeAviso)
					.hide()
					.insertAfter('#ulsobre')
					.fadeIn('fast');
				},
				type: "POST"
			});
		}
		
		return false;
	});
});

/* formulario buscador */
jQuery(function($) {
	
	var $buscador = $('#formbuscador li').addClass('sublabel');
	var $buscadorLabel = $buscador.find('label');
	$buscadorLabel.show();

	$('#buscar').focus(function() {
		if ($(this).val() == $(this).attr('defaultValue')) {
			//$(this).val('');
			//$(this).addClass('imputactivo');
			$buscadorLabel.fadeTo('fast', 0.3);
		}
	});
	
	$('#buscar').keyup(function() {
		if ($(this).val() != '') {
			//$(this).val('');
			//$(this).addClass('imputactivo');
			$buscadorLabel.fadeTo('fast', 0);
		} else {
			$buscadorLabel.fadeTo('fast', 0.3);
		}
	});
	
	$('#buscar').blur(function() {
		if ($(this).val() == '') {
			//$(this).removeClass('imputactivo');
			//$def = $(this).attr('defaultValue');
			//$(this).val($def);
			$buscadorLabel.fadeTo('fast', 1);
		}
	});
	
	$('#formbuscador').submit(function () {
		//if ($('#usuario').val() == $('#usuario').attr('defaultValue') || $('#password').val() == $('#password').attr('defaultValue')) {
		if ($('#buscar').val() == '') {
			return false;				   
		} else {
				
		}
	});
});

/* efectos galería fotografías */
jQuery(function($) {
	$('.gal div').not('.vacio').hover(function(){
		$($(this).find('img')).stop().fadeTo('slow', 1);
		//$('.gal img').not(this).not('.vacio').stop().fadeTo('slow', 0.5);
		$(this).parents('.gal').find('img').not($(this).find('img')).not('.vacio').stop().fadeTo('slow', 0.2);
		//$('<span></span>')
				//.attr({
					//'id': 'imgcontpieid',
					//'class': 'imgcontpie'
				//})
				//.append($(this).find('a').title)
				//.hide()
				//.appendTo(this)
				//.fadeIn('slow');
	},function(){
		//$(this).find('span').fadeOut('fast');
		//$(this).fadeTo('slow', 1);
	});
	$(".gal").hover(function(){
		//$(this).find('img').fadeTo("slow", 0.3); // This should set the opacity to 100% on hover
	},function(){
		$(this).find('.contimg img').not('.vacio').stop().fadeTo("slow", 1);
		$(this).find('.contimgnopub img').not('.vacio').stop().fadeTo("slow", 0.2); // This should set the opacity back to 30% on mouseout
	});
	
	$(".gal img.vacio").hover(function(){
		$(this).parents('.gal').find('.contimg img').not('.vacio').stop().fadeTo("slow", 1);
		$(this).parents('.gal').find('.contimgnopub img').not('.vacio').stop().fadeTo("slow", 0.2);
	},function(){
		// nada
	});
});
