function loadBgVideos(){
    var videos = jQuery('.video-background').videobackground({
        videoSource: bgVideoSources,
        controlPosition: '#video-control',
        poster: bgVideoScreenshot,
        preload: true,
        loop: false
    });
}

var customSectionVideo = false;
var muted = false;
var paused = false;

$(function(){
    $('body').prepend('<div class="video-background"></div>');
    loadBgVideos();

    $('.sections li a').click(function(){
        $('#sections-2 li .section-info').slideUp();
        $('#sections-1').fadeOut();
        $('#sections-2').show();
        $('#sections-2 li a').removeClass('active');
        var e = '#sections-2 li[id$=-'+$(this).parent().attr('id').replace('section-','').replace('section2-','')+']';
        $(e).children('a').addClass('active');
        $('#overlay-sections').fadeIn();
        $(e).children('.section-info').slideDown();

        if ($(e).attr('video-ogg')){
            $('.video-background').videobackground({
                videoSource: [[$(e).attr('video-ogg'), $(e).attr('video-mp4')]],
                controlPosition: '#video-control',
                poster: $(e).attr('video-screenshot'),
                preload: true,
                loop: false,
                list: false
            });
            customSectionVideo = true;
            $('.ui-video-background-prev, .ui-video-background-next').hide();
        }
        else if(customSectionVideo){
            loadBgVideos();
            customSectionVideo = false;
            $('.ui-video-background-prev, .ui-video-background-next').show();
        }

        return false;
    });
    $('#main').click(function(){
        $('#sections-2 li .section-info').slideUp(500, function(){
            $('#overlay-sections').fadeOut();
            $('#sections-1').fadeIn();
            $('#sections-2').fadeOut();
        });
        if (customSectionVideo)
            loadBgVideos();
    });


    $('#video-control, .section-info').click(function(event){
        event.stopPropagation();
    });
});

