// CMS Video Player
$(document).ready(function() {
    loadRuntimeObjects();
});

function loadRuntimeObjects() {
    $('.cms_video_player').each(function(ind,ele) {
        // Get existing video player settings
        $.post("/cms/framework/cms_videos.php", {
            "video_player_id": $(ele).attr("id"),
            "get_settings": 1
        }, function(result) {
            // Load the first video if not in edit mode...
            if (window.edit_authorized === undefined || !window.edit_authorized) {
                $.each(result.youtube_videos, function(ind,value) {
                    // Just load the first video in the list.
                    $(ele).children(".player").html('<iframe class="youtube-player" type="text/html" width="640" height="480" src="http://www.youtube.com/embed/'+value+'?rel=0&wmode=transparent" frameborder="0"></iframe>');
                    return false;
                });
            }
            $(ele).children(".selector").html("");
            $.each(result.youtube_videos, function(ind,value) {
                $(ele).children(".selector").append(
                    '<div class="element">'
                        +'<div class="data">'+value+'</div>'
                        +'<div class="name">'+result['youtube_video_titles'][ind]+'</div>'
                    +'</div>'
                );
            });
            $(ele).children(".selector").children(".element:first").addClass("selected");
            $(ele).children(".selector").children(".element").hover(function() {
                $(this).addClass("hover");
            }, function() {
                $(this).removeClass("hover");
            });
            // Load the first video if not in edit mode...
            if (window.edit_authorized === undefined || !window.edit_authorized) {
                $(ele).children(".selector").children(".element").click(function() {
                    if (!$(this).hasClass("selected")) {
                        var youtube_id = $(this).children(".data").text();
                        $(this).siblings(".selected").removeClass("selected");
                        $(this).addClass("selected");
                        $(ele).children(".player").html('<iframe class="youtube-player" type="text/html" width="640" height="480" src="http://www.youtube.com/embed/'+youtube_id+'?rel=0&wmode=transparent" frameborder="0"></iframe>');
                    }
                });
            }
        }, "json");
    });
}
