
$(document).ready(function() {
    
    $('#izq').fadeTo("fast", 0);
    $('#der').fadeTo("fast", 0);
    
    $('#der').hover(
        function(e) {
            
            if(indice < cantidad)
            $('#der').fadeTo("fast", 1);
        },
        function(e) {
           $('#der').fadeTo("fast", 0); 
        }
    );
    
    $('#der').click(
        function () {
            
            if(indice < cantidad)
            {
                indice++;
                $('#thumb-'+indice).click();
            }
            
            if(indice > 1)
                $('#izq').css({'display':'block'});
            
            if(indice == cantidad)
                $('#der').css({'display':'none'});
                
        }
    );
    
    $('#izq').hover(
        function(e) {
            
            if(indice > 1)
            $('#izq').fadeTo("fast", 1);
        },
        function(e) {
           $('#izq').fadeTo("fast", 0); 
        }
    );
    
    $('#izq').click(
        function () {
            
            if(indice > 1)
            {
                indice--;
                $('#thumb-'+indice).click();
                
                if(indice < cantidad)
                    $('#der').css({'display':'block'});
                
                if(indice == 1)
                    $('#izq').css({'display':'none'});
            }
        }
    );
    
    var links = $('#thumbs li a');
    var cantidad = 0;
    var indice = 1;
    var actual;
    
    $.each(links, function () {
        cantidad++;
        var titulo = $(this).attr('rel');
        $('#thumbs-container').html($('#thumbs-container').html() + '<img alt="'+titulo+'" id="thumb-'+cantidad+'" class="thumb-img" border="0" src="'+$(this).attr('href')+'" width="34px" style="margin: 5px" />');
    }); 
    $('.thumb-img').fadeTo("fast", 0.33); 
    
    $('.thumb-img').hover(
        function(e) {
            
            if($(this).attr('id') != actual)
            $(this).fadeTo("fast", 1);
        },
        function(e) {
           
           if($(this).attr('id') != actual) 
           $(this).fadeTo("fast", 0.33); 
        }
    );
    
    $('.thumb-img').click(
        function () {
            var target = $(this).attr('src');
            var vector = target.split('.');
            var longitud = vector.length;
            var nombrehr = '';
            var titulo = $(this).attr('alt');
            
            nombrehr += vector[0]+'-HR.'+vector[1];
            $('#link-download').attr('href',nombrehr);
            
            $('div.nom-foto').html(titulo);
            
            $('#'+actual).fadeTo("fast", 0.33);
            
            actual = $(this).attr('id');
            $('#'+actual).fadeTo("fast", 1);
            
            var control = actual.split('-');
            indice = control[1];
                  
            $('#grande').fadeTo("fast",0,function () {
                $('#grande').css({background : 'url('+target+') no-repeat'});    
            });
            
            $('#grande').fadeTo("slow",1); 
            
            if(indice > 1)
                $('#izq').css({'display':'block'});
            
            if(indice == cantidad)
                $('#der').css({'display':'none'});
                
            if(indice < cantidad)
                $('#der').css({'display':'block'});
            
            if(indice == 1)
                $('#izq').css({'display':'none'});                       
    });
    
    $('#thumb-1').click();
    
});
