/**
 *  Script qui gère la partie projet du site.
 *  @author ZEZUKA Clément  <clemzezuk@gmail.com>
 */
$(function(){
    
    /**
     *  
     */
    var projectID;
    
    /**
     *  
     */
    var k = 0;
    
    /**
     *	La variable 'test' est un booléen, permet de contingenter l'utilisation des boutons suivant et précédent.
     *	@default	false
     */
    var test = false;
    
    //  
    $('#next_images').click(function() {
        var len = $('#view_project img').length;
        if (k < len-1){
            if ( !test ){
                test = !test;
                k++;
                navigateRight(k);
            }
        }
    });
    
    //  
    $('#prev_images').click(function() {
        var len = $('#view_project img').length;
        if (k > 0){
            if ( !test ){
                test = !test;
                k--;
                navigateLeft(k);
            }
        }
    });
    
    //  
    $(window).keydown(function(event) {
        var len = $('#view_project img').length;
        if (event.keyCode == '39') {								
            if (k < len-1) {
                if ( !test ) {
                    test = !test;
                    k++;	
                    navigateRight(k);
                }
            }
        }
        if (event.keyCode == '37') {								
            if (k > 0) {
                if ( !test ) {
                    test = !test;
                    k--;
                    navigateLeft(k);
                }
            }
        }
    });
    
    //  
    $('#close').click(function() {
        k = 0;
        $('#container_project').stop().animate({ "left": 260 }, 300, "easeOutExpo" );
        $('#prev_images').stop().css({ cursor: 'default' }).animate({ opacity: .1 }, 100, "easeOutExpo" );
        $('#next_images').stop().css({ cursor: 'pointer' }).animate({ opacity: 1 }, 100, "easeOutExpo" );
        $(this).delay(300).fadeOut(200);
        $('#next_images').delay(300).fadeOut(200);
        $('#prev_images').delay(300).fadeOut(200);
        $('#view_project img').delay(300).fadeOut(200);
        $('#text_project').delay(300).fadeOut(200);
        $('#view_project').stop().delay(150).animate({ height: "0%" }, 1500, "easeOutExpo" );
    });
    
    /**
     *
     *  @param  index
     */
    function navigateLeft(index) {
        var gap = ( $('#view_project img').width() ) + 'px';
        $('#container_project').stop().animate({ "left": "+=" + gap }, 800, "easeOutExpo" );
        if ( index == 0 ) $('#prev_images').stop().css({ cursor: 'default' }).animate({ opacity: .1 }, 1000, "easeOutExpo" );
        $('#next_images').stop().css({ cursor: 'pointer' }).animate({ opacity: 1 }, 1000, "easeOutExpo", function (){ test = !test} );	
    }
    
    /**
     *  
     *  @param  index
     */
    function navigateRight(index) {
        var len = $('#view_project img').length;
        var gap = ( $('#view_project img').width() ) + 'px';
        $('#container_project').stop().animate({ "left": "-=" + gap }, 800, "easeOutExpo" );
        var alpha;
        if ( index == len-1 )$('#next_images').stop().css({ cursor: 'default' }).animate({ opacity: .1 }, 1000, "easeOutExpo" );
        $('#prev_images').stop().css({ cursor: 'pointer' }).animate({ opacity: 1 }, 1000, "easeOutExpo", function (){ test = !test} );
    }
    
});
