/**
 * Yves Gellie
 * Viewer script
 * @author Hans Lemuet <hans@upian.com>
 */

var originPhoto    = currentPhoto;
var slider         = null;
var size           = 600;
var durationSlider = 6000;
var bar            = '#serie .progress .bar';
var pauseState     = false;

var slide = function(){
    
    if (window.console && window.console.log) {console.log('function slide() // currentPhoto = '+currentPhoto);}
    if (currentPhoto == null) {return false;}
    
    $('#serie .photo').fadeIn(1000);
    
    if (originPhoto != 0) {
    $(bar).animate({width:'100px'},(durationSlider-50),'linear',function(){
        if (currentPhoto+1 < numberPhotos) {$(this).css({width: '0px'});}
    });
    
    slider = setTimeout(function(){
        $('#serie #aside .description').fadeOut(1000);
        $('#serie .photo').fadeOut(1000,function(){
            
            currentPhoto++;
            document.location.hash = currentPhoto;
            
            updateLinks(currentPhoto);
            
            showPhoto();
            
            preloadPhoto(currentPhoto+1);
            
            if (currentPhoto == 1) {}
            else if (currentPhoto < numberPhotos) {slide();}
            else {
                $(bar).css({width:'100px'});
                //setTimeout(function(){$('#serie #subnav ul.series').fadeIn(1000);},2000);
            }
        });
    }, durationSlider);
    }
}

// Fonction GoTo
var goTo = function(where){
    if (window.console && window.console.log) {console.log('function goTo('+where+')');}
    
    switch (where) {
        case 'next':
        var move = 1;
        break;
        
        case 'prev':
        var move = -1;
        break;
        
        default:
        var move = false;
        break;
    }
    
    pauseSlider(false);
    
    $('#serie #aside .description').fadeOut(500);
    $('#serie .photo').fadeOut(500,function(){
        
        if (originPhoto == 0) {
            $('#serie .photo .splash,#serie .photo ul li.start').hide();
            $('#serie .photo ul li:not(.start)').show();
            $('#serie #aside .section:not(.description)').fadeIn(500);
            originPhoto = 1;
            pauseSlider(true);
        }
        
        if (move) {
            currentPhoto = currentPhoto + move;
            document.location.hash = currentPhoto;
        } else {
            currentPhoto = parseInt(where);
            $('#serie .photo').show();
        }
        
        updateLinks(currentPhoto);
        
        showPhoto();
        
        preloadPhoto(currentPhoto+1);
    });
}

// Fonction show photo
var showPhoto = function(){
    if (window.console && window.console.log) {console.log('function showPhoto()');}
    $('#serie #aside .description').load("photo.php", {serie: currentSerie, id: currentPhoto, action: 'caption'} );
    
    $('#serie .number span').html(currentPhoto);
    $('#serie .photo').css({
        backgroundImage: 'url(img/series/'+currentSerie+'/'+size+'/'+currentPhoto+'.jpg)'
    });
    
    $('#serie .photo,#serie #aside .description').fadeIn(500);
    
    if (currentPhoto == numberPhotos) {
        setTimeout(function(){$('#serie #subnav ul.series').fadeIn(1000);},2000);
    }
}

// Fonction update links
var updateLinks = function(currentPhoto) {
    if (currentPhoto > 0) {var prev = currentPhoto - 1;}
    else {var prev = 0;}
    var linkPrev = 'serie/'+currentSerie+'#'+prev;
    $('#serie .photo ul li.prev a,#serie .mininav a.prev').attr('href',linkPrev);
    
    if (currentPhoto < numberPhotos) {var next = currentPhoto + 1;}
    else {var next = false;}
    var linkNext = 'serie/'+currentSerie+'#'+next;
    next ? $('#serie .photo ul li.next a,#serie .mininav a.next').attr('href',linkNext).show() : $('#serie .photo ul li.next a').hide();
    $('#serie .photo ul li.start a').attr('href',linkNext);
    
    // Cacher les liens aux extrémités
    if (currentPhoto == 1) {
        $('#serie .photo ul li.prev a,#serie .mininav a.prev').hide();
        $('#serie .mininav a.pause').css('margin-left','9px');
    }
    else if ($('#serie .mininav a.prev').css('display') == 'none') {
        $('#serie .photo ul li.prev a,#serie .mininav a.prev').show();
        $('#serie .mininav a.pause').css('margin-left','0');
    }
    if (!next) $('#serie .mininav a.next').hide();
    else if ($('#serie .mininav a.next').css('display') == 'none') $('#serie .mininav a.next').show();
    
    var linkPause = 'serie/'+currentSerie+'#'+currentPhoto;
    next ? $('#serie .photo ul li.pause a').attr('href',linkPause).show() : $('#serie .photo ul li.pause a').hide();
}

// Fonction toggle play/pause
var pauseSlider = function(toggle){
    if (window.console && window.console.log) {console.log('function pauseSlider('+toggle+') // pauseState = '+pauseState);}
    if (!pauseState) {
        clearTimeout(slider);
        $(bar).stop().animate({width:'0px'},100,'linear');
        $('#serie .photo ul li.pause').addClass('play');
        $('#serie .mininav .pause img').attr({
            src: 'img/viewer/btn-mini-play.png',
            alt: 'play'
        });
        $('#serie .mininav .pause img').attr('title','play slideshow');
    } else if (toggle) {
        slide();
        $('#serie .photo ul li.pause').removeClass('play');
        $('#serie .mininav .pause img').attr({
            src: 'img/viewer/btn-mini-pause.png',
            alt: 'pause'
        });
        $('#serie .mininav .pause img').attr('title','pause slideshow');
    }
    if (toggle) pauseState = !pauseState;
    else pauseState = true;
}

// Fonction toggle mininav
var activateMininav = function(){
    $('#serie .number').toggle();
    $('#serie .mininav').toggle();
}

// Fonction preload image
var preloadPhoto = function(photonum){
    if (window.console && window.console.log) {console.log('function preloadPhoto('+photonum+')');}
    if (currentPhoto < numberPhotos && photonum != '') {
        $('#photoloader').css({
            backgroundImage: 'url(img/series/' + currentSerie + '/' + size + '/' + photonum + '.jpg)'
        });
    }
}

$(document).ready(function() {

   $(window).bind('load', function() {
        // Détection de la photo courante
        if (document.location.hash != '') {
            var currentHash = document.location.hash.replace('#', '');
            originPhoto = currentHash;
            $('#serie .photo .splash,#serie .photo ul li.start').hide();
            $('#serie .photo ul li:not(.start)').show();
            $('#serie #aside .section:not(.description)').fadeIn(1000);
            goTo(currentHash);
        } else {
            slide();
        }
        
        updateLinks(currentPhoto);
        
        // Détection de la taille d'écran
        //if ($(window).height() < 500) {$('#serie').css;}
        
        // Création du preloader
        $('body').append('<div id="photoloader"></div>');
        preloadPhoto(currentPhoto+1);
        
        // Détection du navigateur
        if (jQuery.browser['mozilla'] && navigator.platform.indexOf('Mac') == -1) {
            $('#serie .photo ul').addClass('cursors');
        }
    });
    
    // Mise en place des events
    $('#serie .photo ul li.pause a,#serie .mininav a.pause').bind('click',function(){
        pauseSlider(true);
        return false;
    });
    
    $('#serie #aside').bind('mouseover',activateMininav);
    $('#serie #aside').bind('mouseout',activateMininav);
    
    $('#serie #subnav a#nav-series').bind('click',function(){
        $('#serie #subnav ul.series').toggle();
        return false;
    });
    
    $('#serie .photo ul li.prev a,#serie .mininav a.prev').bind('click',function(){goTo('prev');});
    $('#serie .photo ul li.next a,#serie .mininav a.next').bind('click',function(){goTo('next');});
    $('#serie .photo ul li.start a,#serie .photo .splash').bind('click',function(){goTo('next');});
    
});