$(document).ready(function() {
	var tiempo1=300;
	var tiempo2=1000;
	var nom;
	
	var temp;
	var tiempoEspera=4000;
	var auto=false;
	
	$('#botgaleria').mouseover(function(){
		$('#menugal').stop(true,true).slideDown();
    }).mouseout(function(){
		/*var abierto=false;
		$('#menugal ul li').each(function(){
			if ($(this).is('.activo'))
				abierto=true;
		});
		if (! abierto)*/
			$('#menugal').stop(true,true).slideUp();
    });

	$('#menugal').mouseover(function(){
		$(this).stop().slideDown();
    }).mouseout(function(){
		/*var abierto=false;
		$('#menugal ul li').each(function(){
			if ($(this).is('.activo'))
				abierto=true;
		});
		if (! abierto)*/
			$(this).stop().slideUp();
    });

	$('#menugal ul').mouseover(function(){
		$(this).parent().stop().slideDown();
    }).mouseout(function(){
		//$(this).parent().slideUp();
    });

	$('#menugal ul li a').mouseover(function(){
		$(this).parent().parent().parent().stop().slideDown();
    }).mouseout(function(){
		//$(this).parent().parent().parent().slideUp();
    });

	$('#menugal ul li a').hover(function(){
		if ($(this).parent().is('.no'))
			$(this).stop().animate({color:"#f6b412"},300);
    },function(){
		if ($(this).parent().is('.no'))
		{
			$(this).stop().animate({color:"#f6b412"},100);
			$(this).animate({color:"#ffffff"},600);
		}
    });

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	$('.contgaleria, .contgaleriapri').mouseover(function(){
		var cont=0;
		if ($(this).parent().find('.contvisor .visor .visor2 img').attr('src').length>=33)
			cont=1;
		$(this).parent().find('.contvisor .visor ul li a').each(function(){
			cont++;
		});
		
		if (cont>0)
			$(this).stop().animate({backgroundColor:"#f6b412"},tiempo1);
		else
			$(this).css({'cursor':'default'});
    }).mouseout(function(){
		$(this).stop().animate({backgroundColor:"#ccc7c0"},tiempo2);
    });

	$('.contvideopri').mouseover(function(){
		$(this).stop().animate({backgroundColor:"#f6b412"},tiempo1);
    }).mouseout(function(){
		$(this).stop().animate({backgroundColor:"#ccc7c0"},tiempo2);
    });

	$('.contgaleria').click(function(){
		var cont=0;
		if ($(this).parent().find('.contvisor .visor .visor2 img').attr('src').length>=33)
			cont=1;
		$(this).parent().find('.contvisor .visor ul li a').each(function(){
			cont++;
		});
		
		if (cont>0)
		{
			$('#fondo').slideDown(600);
		
			nom=$(this).parent();
			$(nom).find('.contvisor').css({"top":($('html,body').scrollTop()+125)+"px"});
			setTimeout(function(){
				$(nom).find('.contvisor').fadeIn(600);
				$(nom).find('.contvisor .visor .visor3').animate({opacity:0},1000);
				$(nom).find('.contvisor .visor .visor2 img').css({"left":((940-$(nom).find('.contvisor .visor .visor2 img').width())/2)+"px"});
			},600);
		}
    });

	$('.contgaleriapri, .contvideopri').click(function(){
		$(this).find('.error').stop().slideUp();
		$(this).find('.usupass').stop().slideDown();
		//$(this).find('.usupass form .usuario').focus();
    });

	$('.salir').click(function(){
		clearInterval(temp);
		auto=false;
		$(this).parent().find('.visor3 .automatico').text("PASE AUTOMÁTICO");
		
		$(this).parent().parent().fadeOut(600);
		
		setTimeout(function(){
			$('#fondo').slideUp(600);
		},600);
    });

	$('.visor2').mouseover(function(){
		$(this).parent().find('.visor3').stop().animate({opacity:0.6},300);
    }).mouseout(function(){
		$(this).parent().find('.visor3').stop().animate({opacity:0},300);
    });

	$('.visor3').mouseover(function(){
		$(this).stop().animate({opacity:0.6},300);
    }).mouseout(function(){
		$(this).stop().animate({opacity:0},300);
    });

	$('.visor3 img').mouseover(function(){
		$(this).parent().stop().animate({opacity:0.6},300);
    });

	$('.visor3 span').mouseover(function(){
		$(this).parent().stop().animate({opacity:0.6},300);
    });

	$('.visor3 div').mouseover(function(){
		$(this).parent().stop().animate({opacity:0.6},300);
    });

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	$('.visor ul').each(function(){
		var k=0;
		var i=0;
		var imagenes=new Array();
		
		var num=$(this).attr('id').substr(7,$(this).attr('id').length);
		
		imagenes[i]=$('.galimagen'+num).attr('src');
		
		$('#galimgs'+num+' li a').each(function(){
			i++;
			imagenes[i]=$(this).attr('href');
		});
		
		$('#galsiguiente'+num).click(function(){
			if (imagenes.length>1)
			{
				clearInterval(temp);
				auto=false;
				$(this).parent().find('.automatico').text("PASE AUTOMÁTICO");
			
				k++;
				if (k>=imagenes.length)
					k=0;

				$('div#num'+num+' span.num').replaceWith('<span class="num">'+(k+1)+'</span>');
				FLIR.replace( 'span.num' , new FLIRStyle({ cFont:'rockwellextbold' }) );

				$('#galimagenes'+num+' img').each(function() { $(this).fadeOut(900, function() { $(this).remove(); }) });
				$('#galimagenes'+num).append('<img src="'+imagenes[k]+'" alt="" class="galimagen'+num+'" style="display:none;" onload="$(this).fadeIn(900);$(this).css({\'left\':((940-$(this).width())/2)+\'px\'});" />');
			}
		});
		$('#galanterior'+num).click(function(){
			if (imagenes.length>1)
			{
				clearInterval(temp);
				auto=false;
				$(this).parent().find('.automatico').text("PASE AUTOMÁTICO");
			
				k--;
				if (k<0)
					k=imagenes.length-1;
				
				$('div#num'+num+' span.num').replaceWith('<span class="num">'+(k+1)+'</span>');
				FLIR.replace( 'span.num' , new FLIRStyle({ cFont:'rockwellextbold' }) );
			
				$('#galimagenes'+num+' img').each(function() { $(this).fadeOut(900, function() { $(this).remove(); }) });
				$('#galimagenes'+num).append('<img src="'+imagenes[k]+'" alt="" class="galimagen'+num+'" style="display:none;" onload="$(this).fadeIn(900);$(this).css({\'left\':((940-$(this).width())/2)+\'px\'});" />');
		}
		});
		
		$(this).parent().find('.visor3 .automatico').click(function(){
			if (imagenes.length>1)
			{
				if (!auto)
				{
					temp=setInterval(function(){
						k++;
						if (k>=imagenes.length)
							k=0;

						$('div#num'+num+' span.num').replaceWith('<span class="num">'+(k+1)+'</span>');
						FLIR.replace( 'span.num' , new FLIRStyle({ cFont:'rockwellextbold' }) );

						$('#galimagenes'+num+' img').each(function() { $(this).fadeOut(900, function() { $(this).remove(); }) });
						$('#galimagenes'+num).append('<img src="'+imagenes[k]+'" alt="" class="galimagen'+num+'" style="display:none;" onload="$(this).fadeIn(900);$(this).css({\'left\':((940-$(this).width())/2)+\'px\'});" />');
					},tiempoEspera);
				
					auto=true;
					$(this).text("DETENER");
				}
				else
				{
					clearInterval(temp);
					auto=false;
					$(this).text("PASE AUTOMÁTICO");
				}
			}
	    });
	});
});
