/**
 * Tell the flash to play the video
 */
function playVideo(contentId, fromPlaylist, fromTodaysTheme) {
    if (window.parent) {
        // if parent exists, this is call is coming from a frame
        if (window.parent.getFlashObject("mediaroom_player")) {
            window.parent.document.getElementById("mediaroom_player").tcmPlayVideo(contentId);

            // when video is selected, jump to top of page
            var href = window.parent.location.href.split("#");
            window.parent.location.href = href[0] + "#player_area";
        }

        if (fromTodaysTheme) {
            window.parent.playFromTodaysTheme = fromTodaysTheme;
            window.parent.currentTodaysThemeVideo = contentId;
            updateTodaysThemeNext();
        } else {
            window.parent.playFromTodaysTheme = false;
            window.parent.currentTodaysThemeVideo = "";
            updateTodaysThemeNext();
        }

        if (fromPlaylist) {
            window.parent.playFromPlaylist = fromPlaylist;
            window.parent.currentPlaylistVideo = contentId;
            if (window.parent.leftFrame.document.getElementById("playlist")) {
                window.parent.leftFrame.updatePlaylistNext();
            }
        } else {
            window.parent.playFromPlaylist = false;
            window.parent.currentPlaylistVideo = "";
            if (window.parent.leftFrame.document.getElementById("playlist")) {
                window.parent.leftFrame.updatePlaylistNext();
            }
        }
    }
}

/**
 * Get the flash object based on object name and browser type
 */
function getFlashObject(objectName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[objectName];
    } else {
           return document[objectName];
    }
}


/**
 * On video end event; called from flash.<b>
 * Play the next thing in the playlist.
 */
function onVideoEnd(contentId) {
    var nextVideoId;

    if (window.parent.playFromPlaylist) {
        var sPlaylist = readCookie('mediaroom_playlist');
        if (sPlaylist != null) {
            var playlistArray = sPlaylist.split(",");
            playlistArray.pop();

            // find the next video to play
            for (i in playlistArray) {
                if (playlistArray[i] == contentId) {
                    nextVideoId = new Number(i) + 1;
                    break;
                }
            }

            // if the video was not found in the playlist, start at the beginning
            if (nextVideoId == undefined) {
                playVideo(playlistArray[0], true);
                sendMediaroomEvent(this, 'playlist/autoplay/' + playlistArray[0] ,'http://www.tcm.com/mediaroom/index/?cid=' + playlistArray[0] + '&name=playlist_autoplay');
            } else if (nextVideoId < playlistArray.length) {
                playVideo(playlistArray[nextVideoId], true);
                sendMediaroomEvent(this, 'playlist/autoplay/' + playlistArray[nextVideoId] ,'http://www.tcm.com/mediaroom/index/?cid=' + playlistArray[nextVideoId] + '&name=playlist_autoplay');
            }
        }
    } else if (window.parent.playFromTodaysTheme) {
        for (i in window.parent.todaysThemeList) {
            var contentId = window.parent.todaysThemeList[i];

            // find the next video in today's theme and play
            if (window.parent.currentTodaysThemeVideo == contentId) {
                nextVideoId = window.parent.todaysThemeList[new Number(i) + 1];
                if (nextVideoId != undefined) {
                    playVideo(nextVideoId, false, true);
                    sendMediaroomEvent(this, 'todaystheme/autoplay/' + nextVideoId ,'http://www.tcm.com/mediaroom/index/?cid=' + nextVideoId + '&name=todaystheme_autoplay');
                    break;
                }
            }
        }
    }
}

/**
 * Update the next indicator in Today's Theme
 */
function updateTodaysThemeNext() {
    var nextVideoId;
    for (i in window.parent.todaysThemeList) {
        var contentId = window.parent.todaysThemeList[i];

        if (window.parent.currentTodaysThemeVideo == contentId) {
            nextVideoId = window.parent.todaysThemeList[new Number(i) + 1];
        }

        if (window.parent.leftFrame.document.getElementById("todays-table-" + contentId)) {
            if (window.parent.playFromTodaysTheme == true && nextVideoId == contentId) {
                window.parent.leftFrame.document.getElementById("todays-table-" + contentId).className = "playingNext";
                window.parent.leftFrame.document.getElementById("todays-next-" + contentId).style.display = "block";
                window.parent.leftFrame.document.getElementById("todays-hr-" + contentId).style.display = "none";
            } else {
                window.parent.leftFrame.document.getElementById("todays-table-" + contentId).className = "";
                window.parent.leftFrame.document.getElementById("todays-next-" + contentId).style.display = "none";
                window.parent.leftFrame.document.getElementById("todays-hr-" + contentId).style.display = "block";
            }
        }
    }
}