var videoPath = "/inc/video/";
var listUrl = "/videolist.html";
var playlistUrl = "/playlist.html";
var recomUrl = "/video/versenden.html";
var loadVideoUrl = "/videoload.html";
var actVideo = "";
var autoStart = "false";
var actButton = {
  "type" : "",
  "tag"  : "",
  "cat"  : ""
};

function createPlayer(id, file, image) {
    var flashvars = {
        file: file,
        image: image,
        skin: videoPath+"skin/kopp.xml",
        autostart: autoStart,
        dock: "false",
        controlbar: "bottom",
        bufferlength: "5",
//        allownetworking: "all",
//        stretching: "uniform",
//        plugins: videoPath+'d-related.swf'
//        streamer: "pseudo",
//        type: "http",
        plugins: videoPath+'drelated-1.swf'
    };
//    flashvars['http.startparam'] = 'start';
    flashvars['drelated.dxmlpath'] = '/relatedclips.xml?id='+id;
    flashvars['drelated.dposition'] = 'center';
    flashvars['drelated.dskin'] = videoPath+'skin/grayskin.swf';
    flashvars['drelated.dtarget'] = '_self';
//    flashvars['drelated.dynamic'] = 'true';
//    flashvars['drelated.height_offset'] = '50';
    var params = {
        allowfullscreen:"true",
        allowscriptaccess:"always",
        wmode: "transparent"
    };
    var attributes = {
        id:"player1",
        name:"player1"
    };
    swfobject.embedSWF(videoPath+"player-licensed.swf", "videoPlayer", "548", "334", "9", false, flashvars, params, attributes);
}                             
var player = null;
function playerReady(thePlayer) {
    player = document.getElementById(thePlayer.id);
    autoStart = "true";
//    addListeners();
//    player.getPlugin('controlbar').addButton(videoPath+"skin/controlbar/devider.png", "Weiterempfehlung", alert("Weiterempfehlung"));
//    player.addControlbarButton(videoPath+"skin/controlbar/fullscreenButton.png", alert("ssf"));

}

function setFullscreen(){
    player.sendEvent("FULLSCREEN",true);
    return false;
}

function loadVideo(videoId) {
    $("#videoPlayerContent").load(loadVideoUrl, {videoid: videoId});
    toggleActBgColor(false, actVideo);
    actVideo = videoId;
    toggleActBgColor(true, actVideo);
    countView(videoId);
}

function loadVideoList(type) {
    var url = type=='playlist'?playlistUrl:listUrl;
    $("#videoList").load(url, {type: type}, videosLoaded);
    changeActiveButton("type", type);
    return false;
}

function loadTagVideoList(tag) {
    $("#catVideoList").load(listUrl, {tag: tag}, catVideosLoaded);
    changeActiveButton("cat", tag);
    changeRssFeed();
    return false;
}

function loadCatVideoList(category, shortName, name) {
    $("#catVideoList").load(listUrl, {category: category}, catVideosLoaded);
    changeActiveButton("cat", category);
    changeRssFeed(shortName, name);
    return false;
}

function addToPlayList(id, isAuthenticated) {
//    if(isAuthenticated!='true') {
//        window.location.href = playlistUrl + "?login=true&addid="+id;
//        return false;
//    } else {
        $("#videoList").load(playlistUrl, {addid: id}, videosLoaded);
        changeActiveButton("type", "playlist");
        return false;
//    }
}

function removeFromPlayList(id) {
    $("#videoList").load(playlistUrl, {removeid: id}, videosLoaded);
    changeActiveButton("type", "playlist");
    return false;
}

//function playVideo(id, video, image, head, source) {
//    try {
//        player.sendEvent('LOAD', {'file':video, 'image':'', 'title':'', 'duration':'', 'link':''});
//        player.sendEvent('PLAY');
//        document.getElementById("videoHead").innerHTML = head;
//        document.getElementById("videoSource").innerHTML = source;
//        toggleActBgColor(false, actVideo);
//        actVideo = id;
//        toggleActBgColor(true, actVideo);
//        countView(id);
//    } catch(e) {
//    }
//    return false;
//}

function countView(id) {
    $("#loadContainer").load(listUrl, {countid: id});
}

function changeRssFeed(shortName, name) {
    if(name==undefined) {
        document.getElementById("rssFeed").innerHTML = "";
    } else {
        var rssLink = "/data/rss/"+shortName+"_videos.xml";
        document.getElementById("rssFeed").innerHTML = '<a href="'+rssLink+'" target="_blank"><img src="/img/rss.png" alt="RSS-Feed" ' +
                          'title="RSS-Feed"></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="'+rssLink+'" target="_blank">RSS zu '+name+'</a>';
    }
}

function catVideosLoaded(responseText, textStatus, XMLHttpRequest) {
    videosLoaded(responseText, textStatus, XMLHttpRequest);
    $(".video-jcarousellite").jCarouselLite({
        vertical: true,
        visible: 2,
        btnNext: "#nextVideo",
        btnPrev: "#prevVideo",
        circular: true,
        speed:1000
    });

}

function videosLoaded(responseText, textStatus, XMLHttpRequest) {
    toggleActBgColor(true, actVideo);
    if(responseText.indexOf("<!--Message")>-1) {
        var message = responseText.substring(responseText.indexOf("<!--Message")+11, responseText.indexOf("-->")-1);
        alert(message);
    }
    if(textStatus=='error') {
//        ignore
//        this.innerHTML = responseText;
    }
}

function changeActiveButton(buttonType, newId) {
    try {
        if (actButton[buttonType] != "") {
            removeClass(document.getElementById(actButton[buttonType]), "act");
        }
        actButton[buttonType] = buttonType + "_" + newId;
        addClass(document.getElementById(actButton[buttonType]), "act");
    } catch(e) {
    }
}

function sendRecommendation() {
    alert(getRecomUrl());
    window.location.href = getRecomUrl();
    return false;
}

function getRecomUrl() {
    return recomUrl+"?videoid="+actVideo;
}

function toggleBgColor(elem, id){
    if(id!=actVideo)
        elem.style.backgroundColor = elem.style.backgroundColor? "":"#E3E2E2";
}

function toggleActBgColor(activate, id){
    var bgColor = activate? "#E3E2E2":"";
    var elem = document.getElementById("video_"+id);
    if(elem!=null) {
        elem.style.backgroundColor = bgColor;
    }
    elem = document.getElementById("catVideo_"+id);
    if(elem!=null) {
        elem.style.backgroundColor = bgColor;
    }
}

function saveComment(elem, id) {
    $("#loadContainer").load(listUrl, {plid: id, type: "playlist", comment: elem.value});
}

function showFunctionsLayer(elem1,elem2) {
    document.getElementById(elem1).style.display = "block";
    document.getElementById(elem2).style.width = "185px";
}

function hideFunctionsLayer(elem1,elem2) {
    document.getElementById(elem1).style.display = "none";
    document.getElementById(elem2).style.width = "20px";
}

function showURL(){
    document.getElementById("videoURLContainer").style.display = "block";
}

function hideURL(){
    document.getElementById("videoURLContainer").style.display = "none";
}

function hideURLDelayed(e){
    if (!e) var e = window.event;
    var tg = (window.event) ? e.srcElement : e.target;
    if (tg.id!='videoURLContainer') return;
    window.setTimeout('hideURL()', 500);
}


