
setTimeout(function(){
    // Hide the address bar!
    window.scrollTo(0, 1);
}, 0);


$(document).ready(function() {
    
    if(screen.width>480){
        $('#container').hide();
        if($('#fullscreen').length > 0) {
            $('#nav').hide();
        }

        FO = {};
        FO.nb_pages = 1;
        FO.els_per_page = 0;
        FO.pager = 0;
        FO.wn = 0;
        FO.hn = 0;
        $('#gallery-nav').hide();
        $('#bio').find('.navi.prev').hide().click(function(){
            $('#bio').find('#container').hide();
            $('#bio').find('#text-content').show();
            $(this).hide();
            $('#bio').find('.navi.next').show();
        });
        $('#bio').find('.navi.next').click(function(){
            $('#bio').find('#container').show();
            $('#bio').find('#text-content').hide();
            $(this).hide();
            $('#bio').find('.navi.prev').show();
            centerElements();
            animateInPage();
        });
        init();
        resize();
        $(window).resize(function() {
            resize();
        });

        if($('#container-front').length == 1){
            $('#nav').fadeTo(0, 0).delay(2000).fadeTo(2000, 1);
            $('#container-front').find('p.image').fadeTo(0, 0).delay(2000).fadeTo(2000, 1);
            $('#container-front').find('p.logo').delay(2000).fadeOut(2000);
        }

        if(parseInt($('#nav li.pdf span').text()) == 0){
            $('#nav li.pdf a').hide();
        }

        $('#selection-box').hide();
        $('a.open-link').click(function(){
            $('#selection-box').show();
        });
        $('#selection-box .close a').click(function(){
            $('#selection-box').hide();
        });

        $('.pdf-selection a').click(function () {
            //console.log($(this));
            var id = $(this).parent().parent().attr('id').replace('pic-', ''),

            isAdded = $(this).hasClass('added'),
            $link = $(this);

            if(isAdded){
                $link.removeClass('added').text('add to pdf');
                changePDFNumber(-1);
            }else{
                $link.addClass('added').text('added').fadeOut(400).fadeIn(200, function(){
                    $(this).text('remove');
                })
                changePDFNumber(1);
            }
            if($(this).hasClass('pdf-overview')){
                $(this).parent().parent().remove();
                centerElements();
            }
            $.post(global.root + '/selection/add/' + id, function (rsp) {

                if (rsp == '00') {
                    $('#pdf-selection').hide();
                } else {
                    $('#pdf-selection').show();
                }

                $('#pdf-selection span').text(rsp);
            });
        });
    } else { // mobile
        
        if (navigator.userAgent.match(/(iPad|iPhone); CPU OS 5_\d/i)){
            $('html').addClass('ios5');
        }
        
        if($.cookie('logo')==null){
            $('#container-front').find('p.logo').css('display', 'block');
            $('#container-front').click(hideLogo);
            $('#nav').hide();
            $('p.image').hide();
            $.cookie('logo',1);
            if($('#container-front').length == 1){
                //$('#container-front').find('p.logo').delay(10000).fadeOut(0);
                $('#nav').fadeTo(0, 0);
                $('p.image').fadeTo(0, 0);
                setTimeout(hideLogo, 10000);
            }
            function hideLogo(){
                $('#container-front').find('p.logo').fadeOut(0);
                $('#nav').fadeTo(0, 1);
                $('p.image').fadeTo(0, 1);
            };
        }

        function setImageHeight(){
            var ratio=0;
            var maxWidth=0;
            
            if(window.orientation==0){
                maxWidth = 292;
            } else {
                maxWidth = 370;
            }

            $('#container-inner img').each(function(){
                ratio = $(this).height()/$(this).width();

                if($(this).width() > maxWidth) {
                    $(this).css('width', maxWidth);
                    $(this).css('height', Math.round(maxWidth * ratio));
                }
            });
           
            
            $('body').css({'height':screen.height});
            $('#container').css({'height': screen.height});
        }
        
        setImageHeight();
        
        // change orientation
        var supportsOrientationChange = "onorientationchange" in window,
        orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";
            
            
        window.addEventListener(orientationEvent, function() {
            if(window.orientation==90 || window.orientation== -90){
                $('body').addClass('resize-browser');
               
            } else {
                $('body').removeClass('resize-browser');
            }
            setImageHeight();
        }, false);
        
        
        // navigation fixed
        var galleryScroll;
        function loaded() {
            galleryScroll = new iScroll('container-inner');
        }
        
        document.addEventListener('touchmove', function (e) {
            e.preventDefault();
        }, false);

        document.addEventListener('DOMContentLoaded', setTimeout(function () {
            loaded();
        }, 200), false);
        document.addEventListener('DOMContentLoaded', loaded, false);
        window.addEventListener('load', setTimeout(function () {
            loaded();
        }, 200), false);
        
        
        $('#bio #bio-pictures').cycle({
            fx: 'fade'
        });
        
    }
});

function changePDFNumber(index){
    var nb = parseInt($('#nav li.pdf span').text());
    nb = nb + index;
    $('#nav li.pdf span').text(nb);
    if(nb == 0){
        $('#nav li.pdf a').hide();
    }else{
        $('#nav li.pdf a').show();
    }
}

function init(){
    $('#container-inner p').hover(
        function () {
            $(this).find('span').css('visibility', 'visible');
        }, 
        function () {
            $(this).find('span').css('visibility', 'hidden');
        }
        );
            
    $('#gallery-nav p.next').click(function(event){
        event.preventDefault();
        setPager(+1);
    });
    $('#gallery-nav p.prev').click(function(event){
        event.preventDefault();
        setPager(-1);
    });
    
    $('#fullscreen').mousemove(function(e){
        mousePosition(e.pageX);
    //$('#status').html( +', '+ e.pageY);
    });
    $('#container').show();
    $('#bio #container').hide();
}

function mousePosition(pageX){
    var half = $(window).width() / 2;
    var side = '';
    var old = '';
    (half < pageX) ? side = "left" : side = "right";
    if(old != side){
        if(side == "left"){
            $('#fullscreen .prev').fadeOut();
            $('#fullscreen .next').fadeIn();
        }else{
            $('#fullscreen .prev').fadeIn();
            $('#fullscreen .next').fadeOut();
        }
    }
    old = side;
}

function animateInPage(){
    var images = $('#container-inner p:visible a');
    
    for(k=0;k<images.length;k++){
        $(images[k]).hide().delay(k*100).fadeIn();
    }
}

function setPager(i){
    FO.pager = FO.pager + i;
    if(FO.pager < 0){
        FO.pager = 0;
    }
    if(FO.pager >= FO.nb_pages){
        FO.pager = FO.nb_pages-1;
    }
    reorder(FO.nb_pages);
}

function getElsPerPage(){
    //calculer le nombre d'element en largeur et hauteut
    FO.wn = Math.floor($(window).width() / 210);
    
    FO.hn = Math.floor(($(window).height() - 70) / 210);
    
    //calculer le total par page et le nombre de pages
    var tot = FO.wn * FO.hn;
    //FO.els_per_page = tot;
    var imgs = $('#container-inner p').length;
    //hider ceux en trop et store number
    FO.nb_pages = Math.ceil(imgs / tot);
    return tot;
}

function showThumbs(){
    $('#container-inner p').hide();
    start = FO.pager * FO.els_per_page;
    
    stop = ((FO.pager+1) * FO.els_per_page);
    
    $('#container-inner p').each(function(index){
        if(index >= start && index < stop) $(this).show();
    });
    centerElements();
    animateInPage();
}

function reorder(){
    
    if(FO.nb_pages <= 1){
        $('#container-inner p').show();
        $('#gallery-nav').hide();
        $('#gallery-nav p.pager span.current').text('0'+(FO.pager+1));
        $('#gallery-nav p.pager span.total').text('0'+FO.nb_pages);
        FO.nb_pages = FO.nb_pages;
        animateInPage();
    }else{
        showThumbs();
        $('#gallery-nav').show();
        $('#gallery-nav p.pager span.current').text('0'+(FO.pager+1));
        $('#gallery-nav p.pager span.total').text('0'+FO.nb_pages);
        $('#gallery-nav p.navi.prev').show();
        $('#gallery-nav p.navi.next').show();
        if(FO.nb_pages == FO.pager+1){
            $('#gallery-nav p.navi.next').hide();
        }
        if(FO.pager == 0){
            $('#gallery-nav p.navi.prev').hide();
        }
    }
}

function centerElements(){
    //console.log('center elements');
    // gerer l'affichage de pages
    var vis = $('#container-inner p:visible').length;
    $('#container-inner').css('width', FO.wn * 210);
    //console.log(vis +'  '+ FO.wn);
    if(vis < FO.wn){
        $('#container-inner').css('width', vis * 210);
    }
    var wd = $(document).width() - $('#container-inner').width();
    $('#container-inner').css({
        marginTop : '0px'
    });
    $('#container-inner').css({
        marginLeft: wd / 2,
        marginTop : ($(document).height() - $('#nav').height() - $('#container-inner').height() - 30) / 2
    });
    $('#text-content').css({
        marginTop : ($(document).height() - $('#nav').height() - $('#text-content').height()) / 2
    });
}

function resize(){
    $('#nav ul').css('width', $(document).width() + 10);
    $('#nav li').css('width', Math.floor($(document).width() / 5));
    $('#nav li a').css('width', $('#nav li').width() - 50);
    
    var tot = getElsPerPage();
    
    if(FO.els_per_page != tot){
        FO.els_per_page = tot;
        if(FO.pager == FO.nb_pages){
            FO.pager = FO.nb_pages-1;
        }
        reorder();
    }
    centerElements();
    
    if($('#container-front').length == 1){
        $('#container-front').css('height', $(window).height() - 52); 
       
        if($('#container-front p.image img').attr('width') <= $('#container-front').width()){
            $('#container-front p.image img').width('100%');
            $('#container-front p.image img').height('auto');
        }else{
            $('#container-front p.image img').width('100%');
            $('#container-front p.image img').height('auto');
        //$('#container-front p.image img').width($('#container-front p.image img').attr('width'));
        //$('#container-front p.image img').height($('#container-front p.image img').attr('height'));
           
        //var w = ($('#container-front').width() - $('#container-front p.image img').attr('width'))/2;
        //$('#container-front p.image img').css('margin-left', w);
        }
       
    }
    
    

    if($('#fullscreen').length == 1){
        mousePosition($(window).width() / 3 * 2);
        var ww = $(window).width();
        var wh = $(window).height();
        var fs = ('#fullscreen');

        if($(fs).find(".image").length > 0){
            $(fs).find(".image").css("height", wh-70+"px");
            if($(fs).find('#f-infos').length > 0){
                $(fs).find(".image").css("height", wh-120+"px");
            }
            
            //image container should always have the maximum possbible width
            $(fs).find(".image").css("width", ww-40+"px");
            
            //aligning the image in center, height 100%
            //but not bigger than the maximum height
            $(fs).find(".image img").css("margin-top","");
            if($(fs).find(".image img").attr('height') <= $(fs).find(".image").height()){
                $(fs).find(".image img").css("height", $(fs).find(".image img").attr('height'));
                $(fs).find(".image img").css("width", $(fs).find(".image img").attr('width'));
                $(fs).find(".image").css("height", ($(fs).find(".image img").height() + 30)+'px');
                //ici aligner les images au milieu
                var mT = (wh - 40 - $(fs).find(".image").height())/2;
                $(fs).find(".image").css("margin-top", mT+"px");
            }else{
                $(fs).find(".image").css("margin-top", "0px");
                $(fs).find(".image img").css("height", ($(fs).find(".image").height() - 30)+'px');
                $(fs).find(".image img").css("width", "auto");
                
            }
            
            var imgW = $(fs).find(".image img").width();	
            var imgH = $(fs).find(".image img").height();
            if(imgW == 0){
                setTimeout(function(){
                    resize();
                }, 100);
            }
            
            if(imgW >= (ww-40)){	
                if($(fs).find(".image img").attr('width') <= $(fs).find(".image").width()){
                    $(fs).find(".image img").css("height", $(fs).find(".image img").attr('height'));
                    $(fs).find(".image img").css("width", $(fs).find(".image img").attr('width'));
                }else{
                    $(fs).find(".image img").css("width", "100%");
                    $(fs).find(".image img").css("height", "auto");
                    var mT = ($(fs).find(".image").height()-$(fs).find(".image img").height())/2;
                    $(fs).find(".image img").css("margin-top", mT+"px");
                }
                
            }
            
            t = ( ww - 40 - $(fs).find(".image img").width()) / 2;
            $(fs).find(".pdf-selection").css({
                marginLeft: t,
                visibility: 'visible'
            });
            
            
            $(fs).find('#f-infos').css('width', $(fs).find(".image img").width());
        }
        
        if($(fs).find(".video").length > 0){

            $(fs).find(".video p.box").css("margin-top",(wh-40-$(fs).find(".video p.box").height())/2);
            $(fs).find(".video p.box").css("margin-left", (ww-$(fs).find(".video p.box").width())/2);
            
        }
    }
	
}
