var primeau_request;
var primeau_currentId = '';
$(document).ready(function () { 
    initNavEvents();
    if($('#content-wrapper').attr('data-bg-url')) {
        $('.left-img-bar').css("background", "url("+$('#content-wrapper').attr('data-bg-url')+") no-repeat");
    }
    //ajustFooter();
});

/*$(window).load(function() {		
    $(window).wresize(ajustFooter);	
});*/

function ajustFooter() {
    $('#footer').css('height', (($('.main-content').css('height')).slice(0,($('.main-content').css('height')).length-2) - 959) + 'px');
    $('#footer').css('width', $('body').width());
}

function initNavEvents() {
    
    $(".nav-list-wrapper .nav-content").unbind('mouseover');
    $(".nav-list-wrapper .nav-content").unbind('mouseleave');
    $(".nav-list-wrapper .nav-content").unbind('click');
    $(".arrow-right").unbind('click');
    $(".arrow-left").unbind('click');
    
    $(".nav-list-wrapper .nav-content").mouseover(function(){
        $(this).parent('.nav-list-wrapper').siblings('.label').not(".current").stop().animate({
            backgroundColor: '#000'
        });
    })
    
    $(".nav-list-wrapper .nav-content").mouseleave(function(){
        $(this).parent('.nav-list-wrapper').siblings('.label').not(".current").stop().animate({
            backgroundColor: '#96959A'
        });
    })
    
    $(".nav-list-wrapper .nav-content").click(function(){
        
        initializeNav($(this).parent('.nav-list-wrapper').parent('li'));
        $(this).unbind('mouseover');
        $(this).unbind('mouseleave');
        
        $(".nav-list-wrapper").siblings('.label').removeClass("current");
        $(this).parent('.nav-list-wrapper').siblings('.label').addClass("current");
        
        $(this).parent('.nav-list-wrapper').siblings('.label').stop().animate({
            marginLeft: '-40px',
            backgroundColor: '#000'
        });
        
        $(this).parent('.nav-list-wrapper').parent('li').siblings('li')
        .children('.nav-list-wrapper').siblings('.label').stop().animate({
            marginLeft: '0px',
            backgroundColor: '#96959A'
        });
        
        /*$(this).parent('.nav-list-wrapper').parent('li').siblings('li')
        .children('.nav-list-wrapper').children('.nav-content').children('img').stop().animate({
            opacity: 0.5
        });*/
        
        $(this).parent('.nav-list-wrapper').parent('li')
        .children('.nav-list-wrapper').children('.nav-content').children('img').stop().animate({
            opacity: 1
        });
        
        $(this).parent('.nav-list-wrapper').siblings('.arrow-left').show();
        $(this).parent('.nav-list-wrapper').siblings('.arrow-left').stop().animate({
            left: '-26px',
            opacity: '1'
        });
        
        $(this).parent('.nav-list-wrapper').siblings('.arrow-right').show();
        $(this).parent('.nav-list-wrapper').siblings('.arrow-right').stop().animate({
            right: '-15px',
            opacity: '1'
        });
        
        updateProject($(this).attr('data-id'));        
        
        return false;
    })
    
    $(".arrow-right").click(function(event) {
        
        event.stopPropagation();        
        var target = $(this).siblings('.nav-list-wrapper').children('.nav-content.active');
        var currentId = '';
        
        if(target.next().length == 0) {
            if ($.browser.msie && $.browser.version < 9) {
                $(this).siblings('.nav-list-wrapper').children().first().show();
            }else{
                $(this).siblings('.nav-list-wrapper').children().first().fadeIn();
            }
            
            $(this).siblings('.nav-list-wrapper').children().first().addClass('active');
            currentId = $(this).siblings('.nav-list-wrapper').children().first().attr('data-id');
        }
        else {
            if ($.browser.msie && $.browser.version < 9) {
                target.next().show();
            }else{
                target.next().fadeIn();
            }
            
            target.next().addClass('active');           
            currentId = target.next().attr('data-id');
        }
        if(currentId != '') {
            updateProject(currentId);
        }
        
        if ($.browser.msie && $.browser.version < 9) {
            target.hide().removeClass('active');
        }else{
            target.fadeOut().removeClass('active');
        }
        
        return false;
        
    });
    
    $(".arrow-left").click(function(event) {
        
        event.stopPropagation();        
        var target = $(this).siblings('.nav-list-wrapper').children('.nav-content.active');
        var currentId = '';
        
        if(target.prev().length == 0) {
            if ($.browser.msie && $.browser.version < 9) {
                $(this).siblings('.nav-list-wrapper').children().last().show();
            }else{
                $(this).siblings('.nav-list-wrapper').children().last().fadeIn();
            }
            
            $(this).siblings('.nav-list-wrapper').children().last().addClass('active');
            currentId = $(this).siblings('.nav-list-wrapper').children().first().attr('data-id');
        }
        else {
            if ($.browser.msie && $.browser.version < 9) {
                target.prev().show();
            }else{
                target.prev().fadeIn();
            }
            target.prev().fadeIn();
            target.prev().addClass('active');
            currentId = target.prev().attr('data-id');
        }
        if(currentId != '') {
            updateProject(currentId);
        }
        if ($.browser.msie && $.browser.version < 9) {
            target.hide().removeClass('active');
        }else{
            target.fadeOut().removeClass('active');
        }
        return false;
        
    });
}

function initializeNav(currentNav) {
    initNavEvents();
    
    $(currentNav).siblings('li').children('.arrow-right').stop().animate({
        right: '12px',
        opacity: 0
    });
    
    $(currentNav).siblings('li').children('.arrow-left').stop().animate({
        left: '0',
        opacity: 0
    });
    
    $(currentNav).siblings('li').children('.nav-content').children('.label').stop().animate({
        marginLeft: '0',
        backgroundColor: '#96959A'
    });
}

function updateProject(id) {
    if(primeau_currentId != id) {
        if(primeau_request) {
            primeau_request.abort();
        }
        primeau_request = $.get(
            $('#project-nav').attr('data-url'),
            { id : id },
            function(result){            
                $('#content-wrapper').replaceWith(result);
                $('#content-wrapper').hide();
                if ($.browser.msie && $.browser.version < 9) {
                    $('#content-wrapper').show();
                }else{
                    $('#content-wrapper').fadeIn();
                }                
                
                $('.left-img-bar').css("background", "url("+$('#content-wrapper').attr('data-bg-url')+") no-repeat");
                $('.left-img-bar').hide();
                
                if ($.browser.msie && $.browser.version < 9) {
                    $('.left-img-bar').show();
                }else{
                    $('.left-img-bar').fadeIn();
                }
                
                ajustFooter();
            }
        );
        primeau_currentId = id;
    }
    
}


