$(document).ready(function() {
    var mediaList = new Array();
    var mediaListCount;
    $.getJSON(
        'json.php?type=banner',
        function(response){
            if(response.status == 'ok' && response.banners) {
                $.each(response.banners, function(key, val){
                    mediaList[key] = {m4v: val.m4v, poster: val.poster};
                });
                mediaListCount = mediaList.length;
                loadVideoPlayer();
            }
        }
        );
    
    var currentMedia = 0;
    
    function playVideo(instance) {
        instance.jPlayer("setMedia", mediaList[currentMedia]).jPlayer("play");
        if(currentMedia >= mediaListCount - 1) {
            currentMedia = 0;
        } else {
            currentMedia++;
        }
    }
    
    function loadVideoPlayer() {
        $("#jquery_jplayer_1").jPlayer({
            ready: function () {
                playVideo($(this));
            },
            ended: function () {
                playVideo($(this));
            },
            swfPath: "flash",
            solution: "html, flash",
            supplied: "m4v",
            size: {
                width: "440px",
                height: "280px",
                cssClass: "jp-video-270p"
            }
        });
    }
});

