$(document).ready(function(){
    
    if($('#contenido').height()<$('.contenido').height()){
        $('.scroll').css('display','none');
        $('.control_scroll').css('display','none');
    }
    
    var cursorHome = 0;
    var cantidadHome = $('.galeria .imagen').length;
    var homeTimeout = 0;
    var allowScroll = false;
    var sc_delta = 0;
    var sc_threshold = 0;
    var sc_postop = 0;
    var allowMove = false;
    var posGaleria = $('.galeria').offset();
    
    $('.numeracion a').click(function(){
        allowMove = false;
        cursorHome = parseInt($(this).html());
        slideHome();
    });
    
    $('.numeracion img').click(function(){
        controlHome($(this).attr('id'));
    });
    
    $('.numeracion img').mouseover(function(){
        if($(this).attr('id') == 'prev') $(this).attr('src','/imgs/graphics/prev_black.png');
        else $(this).attr('src','/imgs/graphics/next_black.png');
    });
    
    $('.numeracion img').mouseout(function(){
        if($(this).attr('id') == 'prev') $(this).attr('src','/imgs/graphics/prev.png');
        else $(this).attr('src','/imgs/graphics/next.png');
    });
    
    $('.scroll img').mousedown(function(e){
        e.preventDefault();
        sc_threshold = $('.scroll').height() - $('.scroll img').height();
        sc_delta = ($('#contenido').height() - $('.contenido').height()) / sc_threshold;
        var pos = $('.contenido').offset();
        sc_postop = pos.top;
        allowScroll = true;
    });
    
    $('.joel').click(function(){
        location.href = '/'; 
    });
    
    $('.biography').click(function(){
        location.href = '/biography/about'; 
    });
    
    $('.contact').click(function(){
        location.href = '/contact'; 
    });
    
    $('body').mousemove(function(e){
        e.preventDefault();
        if(allowScroll){
            var pos = e.pageY - sc_postop - $('.scroll img').height()/2;
            $('.scroll img').css( { 'margin-top':((pos<=sc_threshold && pos>=0)?pos + 'px':$('.scroll img').css('margin-top')) } );
            $('#contenido').css( { 'margin-top':(parseInt($('.scroll img').css('margin-top'))*sc_delta*(-1)) + 'px' } );
        }
    });
    
    $('body').mouseup(function(e){
        allowScroll = false;
    });
    
    $('.galeria .detalle').click(function(){
        url = $('#img_' + cursorHome + ' img').attr('id').split('~');
        location.href = '/gallery/' + url[0] + '/' + url[1];
    });
    
    $('.scroll_up').click(function(){
        if(parseInt($('#contenido').css('margin-top'))<0){
            var delta_texto = 50;
            var delta_img = Math.round((480 / $('#contenido').height()) * delta_texto);
            $('.scroll img').css( { 'margin-top':parseInt($('.scroll img').css('margin-top'))-delta_img } );
            $('#contenido').css( { 'margin-top':parseInt($('#contenido').css('margin-top'))+delta_texto } );
        }
    });
    
    $('.scroll_down').click(function(){
        if(Math.abs(parseInt($('#contenido').css('margin-top')))<Math.abs(parseInt($('#contenido').height()))-480){
            var delta_texto = 50;
            var delta_img = Math.round((480 / $('#contenido').height()) * delta_texto);
            $('.scroll img').css( { 'margin-top':parseInt($('.scroll img').css('margin-top'))+delta_img } );
            $('#contenido').css( { 'margin-top':parseInt($('#contenido').css('margin-top'))-delta_texto } );
        }
    });
    
    $('.volver_galeria').click(function(){
        history.back();
    });
    
    /*
    
    $('.lupa a').click(function(){
        clearTimeout(homeTimeout);
        if($('#img_'+cursorHome+' img').height()>415){
            $('#img_'+cursorHome+' img').width($('#img_'+cursorHome+' img').width()/2);
            $('#img_'+cursorHome+' img').height($('#img_'+cursorHome+' img').height()/2);
            $('#img_'+cursorHome+' img').css({ 'margin-left':'0px','margin-top':'0px' });
            allowMove = false;
            homeTimeout = setTimeout(function(){ controlHome('next'); },8000);
        } else {
            $('#img_'+cursorHome+' img').width($('#img_'+cursorHome+' img').width()*2);
            $('#img_'+cursorHome+' img').height($('#img_'+cursorHome+' img').height()*2);
            allowMove = true;
        }
    });
    
    $('.imagen').mousemove(function(e){
        if(allowMove){
            $('#img_'+cursorHome+' img').css({ 'margin-left':(posGaleria.left - e.pageX)+'px','margin-top':(posGaleria.top - e.pageY)+'px' });
        }
    });
    
    */
    
    var slideHome = function(){
        clearTimeout(homeTimeout);
        $('.galeria .texto').css({ 'display':'none' });
        $('.galeria .imagen').fadeOut(500);
        $('#img_'+cursorHome).fadeIn(500);
        setTimeout(function(){ 
            if($('#img_'+cursorHome).height()<20) alto = 500; else alto = $('#img_'+cursorHome).height();
            $('#txt_'+cursorHome).css({ 'margin-top': (alto+20) });
            $('#txt_'+cursorHome).css({ 'display':'block' }); 
        },500);
        $('.numeracion a').removeClass('selected');
        $('#i_'+cursorHome).addClass('selected');
        /*homeTimeout = setTimeout(function(){ controlHome('next'); },8000);*/
    };
    
    var controlHome = function(acc){
        cursorHome = (acc == 'prev')?((cursorHome <= 1)?cantidadHome:cursorHome-1):((cursorHome >= cantidadHome)?1:cursorHome+1);
        slideHome();
    };
    
    controlHome('next');
    
});
