﻿function ChangeBack(elem, backgroundUrl) {
    if (backgroundUrl)
        elem.style.backgroundImage = "url('" + backgroundUrl + "')";
    else
        elem.style.background = "none";

}

function playPauseVideo(playerId, elem, imgId) {
    var state = eval("jsVar_" + playerId + "['playerState']")

    if (!state) state = "play";

    if (state == "play") {
        //pause
        pauseVideo(playerId);
        eval("jsVar_" + playerId + "['playerState'] = 'paused';");
        document.getElementById(imgId).src = video_player_img_play_src;
    }
    else {
        //play
        playVideo(playerId);
        eval("jsVar_" + playerId + "['playerState'] = 'play';");
        document.getElementById(imgId).src = video_player_img_pause_src;
    }
}

function muteUnmuteVideo(playerId, elem, imgId) {
    var state = elem.attributes["state"].value;

    if (!state) state = "unmute";
    if (state == "mute") {
        //unmute
        unmuteVideo(playerId);
        elem.attributes["state"].value = "unmute";
        document.getElementById(imgId).src = video_player_img_unmute_src;
    }
    else {
        //mute
        muteVideo(playerId);
        elem.attributes["state"].value = "mute";
        document.getElementById(imgId).src = video_player_img_mute_src;
    }
}

var playlistDetails = new Array();

function videoPlCbFunc(jsondata, clientId, playListContent,playListContent_MORE, videoplayerId, titleId, descriptionId, AnzVideosinPlaylist) {

    if (AnzVideosinPlaylist == undefined) AnzVideosinPlaylist = 1000;
    //clear playlist
    $('#' + playListContent).empty();
    var totalItems = jsondata.data.items.length;
    var startIndex = jsondata.data.startIndex - 1;
    if (startIndex < 0) startIndex = 0;
    var playerId = videoplayerId + "_videoplayer";

    //show all videos
    for (i = 0; i < totalItems; i++) {
        try {
            var id = jsondata.data.items[i].video.id;
            var title = jsondata.data.items[i].video.title;
            var description = jsondata.data.items[i].video.description;
            var date = jsondata.data.items[i].video.uploaded.substr(0, 10);
            var thumbnail = jsondata.data.items[i].video.thumbnail.Default;
            if (!thumbnail) thumbnail = jsondata.data.items[i].video.thumbnail.sqDefault;
            if (!thumbnail) thumbnail = jsondata.data.items[i].video.thumbnail.hqDefault;


            var divClass = "DIV_VideoPlaylistentry";
            if (i == startIndex) divClass += " playlistActive";
            if (i < AnzVideosinPlaylist) {
                $('#' + playListContent).append("<div class='" + divClass + "'><a href='javascript:;' onclick=\"loadVideoFromPlaylist(this,'" + id + "','" + videoplayerId + "','" + i + "','" + clientId + "', '" + titleId + "', '" + descriptionId + "');\"><img height='45' width='60' src='" + thumbnail + "'></a><span class='SPAN_mediaDate'>" + dateFormat(date, "d mmm yyyy").toUpperCase() + "</span><a href='javascript:;' onclick=\"loadVideoFromPlaylist(this,'" + id + "','" + videoplayerId + "','" + i + "','" + clientId + "', '" + titleId + "', '" + descriptionId + "');\">" + title + "</a></div>");
            } else {
                $('#' + playListContent_MORE).append("<div class='" + divClass + "'><a href='javascript:;' onclick=\"loadVideoFromPlaylist(this,'" + id + "','" + videoplayerId + "','" + i + "','" + clientId + "', '" + titleId + "', '" + descriptionId + "');\"><img height='45' width='60' src='" + thumbnail + "'></a><span class='SPAN_mediaDate'>" + dateFormat(date, "d mmm yyyy").toUpperCase() + "</span><a href='javascript:;' onclick=\"loadVideoFromPlaylist(this,'" + id + "','" + videoplayerId + "','" + i + "','" + clientId + "', '" + titleId + "', '" + descriptionId + "');\">" + title + "</a></div>");
            }

            playlistDetails[clientId + i + ""] = new Array();
            playlistDetails[clientId + i + ""]['title'] = title;
            playlistDetails[clientId + i + ""]['description'] = filterDescription(description);

            if (i == startIndex) {
                //load video startIndex
                eval("jsVar_" + playerId + "['videoToPlay']= '" + id + "';");
                $('#' + titleId).html(title);
                $('#' + descriptionId).html(filterDescription(description));

                //set the variable
                //try to cue video if player ready
                try {
                    cueVideo(playerId, id, 0);
                } catch (exc) { }

            }
        } catch (exc) { }
    }
}

function filterDescription(description) {
    var D = "[DEUTSCH]";
    var E = "[ENGLISH]";
    var ret = description;
    if (description.indexOf(D) != -1 && description.indexOf(E) != -1) {
        //youtube videos von SF enthalten in der Beschreibung 2 Language Tags Deutsch und Englisch für die jeweilige Sprache
        var di = description.indexOf(D);
        var ei = description.indexOf(E);

        if (lang == undefined || lang.toUpperCase() == 'DE') {
            ret = description.substring(di, ei);
        } else {
            ret = description.substring(ei, description.length);
        }
    } else {
        ret = description;
    }
    return ret.replace(D, "").replace(E, "").replace(/\n/g, '<br />');
}

function loadVideoFromPlaylist(elem, id, playerControlId, index, clientId, titleId, descriptionId) {

    var playerId = playerControlId + "_videoplayer";
    loadVideo(playerId, id, 0);

    //$('#' + titleId).html(eval("playlistDetails_" + clientId + "[" + index + "]['title']"));
    //$('#' + descriptionId).html(eval("playlistDetails_" + clientId + "[" + index + "]['description']"));
    $('#' + titleId).html(playlistDetails[clientId + index + ""]['title']);
    $('#' + descriptionId).html(playlistDetails[clientId + index + ""]['description']);

    var parent = $(elem).parent();
    $(parent).siblings().removeClass("playlistActive");
    $(parent).addClass("playlistActive");

    var new_position = $('#' + playerId).offset();
    window.scrollTo(new_position.left, new_position.top - 80);
}

