﻿pagerBottom = 8;
titleMarginBottom = 5;
topBlackHeight = 4;
maxTopVideo = 4;
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //                                                             Current callback state check                                                            //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function CanUpdate() { return (typeof (mainPanel) != "undefined") && (mainPanel != null) && !mainPanel.InCallback(); }
function CanUpdateCatalog() { return (typeof (pcCatalog) != "undefined") && (pcCatalog != null) && !pcCatalog.InCallback(); }
function CanUpdateTopVideos() { return (typeof (pTopVideos) != "undefined") && (pTopVideos != null) && !pTopVideos.InCallback(); }
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //                                                                 End callback function                                                               //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function ScrollTopVideos(position) {
    if (!CanUpdateTopVideos()) return;
    pTopVideos.PerformCallback(position + "|" + BottomTabedMenu.GetActiveTab().name);
}
function IncrementViewCount(videoId, completed) {
    if (!((typeof (cbIncrementViewCount) != "undefined") && (cbIncrementViewCount != null)) || cbIncrementViewCount.InCallback()) return;
    cbIncrementViewCount.PerformCallback(videoId + '|' + completed);
}
function MainPanel_EndCallback() {
    videoStarted = false;
    videoCompleteded = false;
    var hfTitle = GetHiddenField("hfTitle");
    if (hfTitle != null && hfTitle.value != '') document.title = hfTitle.value;
    
    tagErrorBlock = document.getElementById("TagErrorMessage");
    tagErrorMessage = hfTagErrorMessage.Get("TagErrorMessage")
    tagErrorBlock.innerHTML = tagErrorMessage;
    if (tagErrorMessage == "") tagErrorBlock.style.display = "none";
    else tagErrorBlock.style.display = "block";
    
    videoErrorBlock = document.getElementById("VideoErrorMessage");
    videoErrorMessage = hfVideoErrorMessage.Get("VideoErrorMessage");
    videoErrorBlock.innerHTML = videoErrorMessage;
    if (videoErrorMessage == "") videoErrorBlock.style.display = "none";
    else videoErrorBlock.style.display = "block";
    
    invalidUrlBlock = document.getElementById("InvalidUrlErrorMessage");
    if (tagErrorMessage == "" && videoErrorMessage == "") {
        invalidUrlBlock.innerHTML = "";
        invalidUrlBlock.style.display = "none";
    } else {
        invalidUrlBlock.innerHTML = "Invalid URL.";
        invalidUrlBlock.style.display = "block";
    } 
    SetPlayListHeight();
}
function TopVideo_EndCallback() {
    TopVideoTitleFormating();
}
function SetTitle() {    
    var hfTitle = GetHiddenField("hfTitle");
    if (hfTitle != null && hfTitle.value != '' && hfVideoErrorMessage.Get("VideoErrorMessage") == "") document.title = hfTitle.value;
} 
function PlayList_EndCallback() {
    /*if (GetHiddenField("hfPages").value == "1") pnPager.SetVisible(false);
    else pnPager.SetVisible(true);*/
    tagErrorBlock = document.getElementById("TagErrorMessage");
    tagErrorMessage = hfTagErrorMessage.Get("TagErrorMessage");    
    tagErrorBlock.innerHTML = tagErrorMessage;
    if (tagErrorMessage == "") tagErrorBlock.style.display = "none";
    else tagErrorBlock.style.display = "block";
    videoErrorMessage = hfVideoErrorMessage.Get("VideoErrorMessage");
    
    invalidUrlBlock = document.getElementById("InvalidUrlErrorMessage");
    if (tagErrorMessage == "" && videoErrorMessage == "") {
        invalidUrlBlock.innerHTML = "";
        invalidUrlBlock.style.display = "none";
    } else {
        invalidUrlBlock.innerHTML = "Invalid URL.";
        invalidUrlBlock.style.display = "block";
    } 
    SetPlayListHeight();
}
function VideoBox_EndCallback() {
    videoErrorBlock = document.getElementById("VideoErrorMessage");
    videoErrorMessage = hfVideoErrorMessage.Get("VideoErrorMessage");
    videoErrorBlock.innerHTML = videoErrorMessage;
    if (tagErrorMessage == "" && videoErrorMessage == "") videoErrorBlock.style.display = "none";
    else videoErrorBlock.style.display = "block";
    tagErrorMessage = hfTagErrorMessage.Get("TagErrorMessage");
    
    invalidUrlBlock = document.getElementById("InvalidUrlErrorMessage");
    if (videoErrorMessage == "") {
        invalidUrlBlock.innerHTML = "";
        invalidUrlBlock.style.display = "none";
    } else {
        invalidUrlBlock.innerHTML = "Invalid URL.";
        invalidUrlBlock.style.display = "block";
    } 
    SetPlayListHeight();
}
function CodeBox_EndCallback() {
    if (dvCode.cpCurrentIndex == 0) document.getElementById('hlPrevFrame').className = 'disabledAnchor';
    else document.getElementById('hlPrevFrame').className = 'enabledAnchor';
    if (dvCode.cpCurrentIndex == (dvCode.cpPageCount - 1)) document.getElementById('hlNextFrame').className = 'disabledAnchor';
    else document.getElementById('hlNextFrame').className = 'enabledAnchor';
    if (dvCode.cpCurrentIndex == dvCode.cpPageIndex) document.getElementById('hlSyncFrame').className = 'disabledAnchor';
    else document.getElementById('hlSyncFrame').className = 'enabledAnchor';
}
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //                                                                 Different functions                                                                 //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function TopVideoTitleFormating() {
    for (i = 0; i < maxTopVideo; i++) {
        var title = document.getElementById("TopVideoTitle" + i);
        if (title == null) continue;
        var maxTimes = 20;
        var j = 0;
        while(title.scrollHeight > 39) {
            var titleText = title.innerHTML;
            var spacePosition = titleText.lastIndexOf(" ");
            if (spacePosition == -1) break;
            title.innerHTML = titleText.substr(0, spacePosition) + "...";
            j++;
            if (j > maxTimes) break;
        }
        title.style.visibility = "visible";
    }
}
function GetHiddenField(inputId) {
    var inputs = document.getElementsByTagName("INPUT");
    for (var i = 0; i < inputs.length; i++) {
        var id = new String(inputs[i].id);
        if (id.lastIndexOf(inputId) == id.length - inputId.length) return inputs[i];
    }
    return null;
}
function playPause(videoId, completed) {
    if (!videoStarted) {
        IncrementViewCount(videoId, false);
        videoStarted = true;
    } else if (!videoCompleteded && completed) {
        IncrementViewCount(videoId, true);
        videoCompleteded = true;
    }
}
function bandWidthCheck(bitrate) {
    if ((typeof (hfBindwidth.Get("Number")) == 'undefined') || (hfBindwidth.Get("Number") == null) || (hfBindwidth.Get("Number") < 1)) {
        hfBindwidth.Set("Number", 1);
        hfBindwidth.Set("bandWidth", bitrate);
    } else {
        actualBandwidth = ((bitrate + hfBindwidth.Get("bandWidth")) / 2);
        /*if (actualBandwidth < 500)*/ Bandwidth.PerformCallback(actualBandwidth + "|" + GetMovie(ClearHash()));
    }
}
function CodeBlockPrevClick() {
    if (document.getElementById('hlPrevFrame').className == 'disabledAnchor') return false;
    if (dvCode.GetVisible() && (typeof (dvCode.cpCurrentIndex) != 'undefined') && (dvCode.cpCurrentIndex != null)) {
        dvCode.cpCurrentIndex = dvCode.cpCurrentIndex - 1;
        dvCode.DoPagerClick('PN' + dvCode.cpCurrentIndex);
    }
}
function CodeBlockSyncClick() {
    if (document.getElementById('hlSyncFrame').className == 'disabledAnchor') return false;
    if (dvCode.GetVisible() && (typeof (dvCode.cpPageIndex) != 'undefined') && (dvCode.cpPageIndex != null)) {
        dvCode.cpCurrentIndex = dvCode.cpPageIndex;
        dvCode.DoPagerClick('PN' + dvCode.cpPageIndex);
    }
}
function CodeBlockNextClick() {
    if (document.getElementById('hlNextFrame').className == 'disabledAnchor') return false;
    if (dvCode.GetVisible() && (typeof (dvCode.cpCurrentIndex) != 'undefined') && (dvCode.cpCurrentIndex != null)) {
        dvCode.cpCurrentIndex = dvCode.cpCurrentIndex + 1;
        dvCode.DoPagerClick('PN' + dvCode.cpCurrentIndex);
    }
}
function SetSortCookie(value) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + 30);
    document.cookie = "TVRelatedSortingParametr" + "=" + escape(value) + ";expires=" + exdate.toUTCString();
}
function Decode(str) { return decodeURIComponent(str.replaceAll("+", " ")); }
function Encode(str) { return (str.replaceAll(" ", "+")); } 
String.prototype.replaceAll = function(search, replace) { return this.split(search).join(replace); }
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //                                                                 Working with hash functions                                                         //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function CheckHash() {
    var hash = ClearHash();
    var oldHash = hfOldHash.Get("oldHash");
    if (!((typeof (oldHash) != "undefined") && (oldHash != null))) oldHash = "";
    if (hash != oldHash) { ReadingHash(); }
}
function ChangeVideo(video) {
    var hash = ClearHash();
    var tmp = hash.split(";");
    tmp[0] = video;
    hash = tmp.join(";");
    hash = Encode(hash);
    document.location.hash = hash;
}
function ChangeVideoAndTag(movieId, tagId) {
    var hash = ClearHash();
    var tmp = hash.split(";");
    tmp[0] = movieId;
    tmp[1] = tagId;
    tmp[2] = 1;
    hash = tmp.join(";");
    hash = Encode(hash);
    document.location.hash = hash;
}
function ChangeTag(tagId) {
    var hash = ClearHash();
    var tmp = hash.split(";");
    tmp[1] = tagId;
    tmp[2] = 1;
    hash = tmp.join(";");
    hash = Encode(hash);
    document.location.hash = hash;
}
function ChangePage(pageId) {
    var hash = ClearHash();
    var tmp = hash.split(";");
    tmp[2] = pageId;
    hash = tmp.join(";");
    hash = Encode(hash);
    document.location.hash = hash;
}
function FirstTime() {
    TopVideoTitleFormating();
    var hash = ClearHash();
    if ((typeof (hash) != "undefined") && (hash != null) && (hash != "")) {
        if (hash.indexOf(".movie") > -1 || hash.indexOf(".tags") > -1 || hash.indexOf(".sitemap") > -1) hash = OldHashType(hash);
        movieId = GetMovie(hash);
        tagsId = GetTags(hash);
        pageId = GetPage(hash)
    } else {
        movieId = "";
        tagsId = "";
        pageId = 1;
    }
    hfOldHash.Set("oldHash", hash);
    var movieIdSend = Decode(movieId);
    var tagsIdSend = Decode(tagsId);
    superCallbackPanel.PerformCallback(0 + "|" + movieIdSend + "|" + tagsIdSend + "|" + pageId + "|" + PerPage());
}
function ForcedPlayListReload() {
    ChangePage(1);
    var hash = ClearHash();
    if ((typeof (hash) != "undefined") && (hash != null) && (hash != "")) {
        if (hash.indexOf(".movie") > -1 || hash.indexOf(".tags") > -1 || hash.indexOf(".sitemap") > -1) hash = OldHashType(hash);
        movieId = GetMovie(hash);
        tagsId = GetTags(hash);
        pageId = GetPage(hash)
    } else {
        movieId = "";
        tagsId = "";
        pageId = 1;
    }
    hfOldHash.Set("oldHash", hash);
    var movieIdSend = Decode(movieId);
    var tagsIdSend = Decode(tagsId);
    pcCatalog.PerformCallback(movieIdSend + "|" + tagsIdSend + "|" + pageId + "|" + PerPage());
}
function ReadingHash() {
    var hash = ClearHash();
    var oldHash = hfOldHash.Get("oldHash");
    var movieId;
    var movieIdSend;
    var movieIdOld;
    var tagsId;
    var tagsIdSend;
    var tagsIdOld;
    var pageId;
    var pageIdOld;
    if ((typeof (hash) != "undefined") && (hash != null) && (hash != "")) {
        if (hash.indexOf(".movie") > -1 || hash.indexOf(".tags") > -1 || hash.indexOf(".sitemap") > -1) hash = OldHashType(hash);
        movieId = GetMovie(hash);
        tagsId = GetTags(hash);
        pageId = GetPage(hash)
    } else {
        movieId = "";
        tagsId = "";
        pageId = 1;
    }
    if ((typeof (oldHash) != "undefined") && (oldHash != null) && (oldHash != "")) {
        movieIdOld = GetMovie(oldHash);
        tagsIdOld = GetTags(oldHash);
        pageIdOld = GetPage(oldHash);
    } else {
        movieIdOld = "";
        tagsIdOld = "";
        pageIdOld = 1;
    }
    movieIdSend = Decode(movieId);
    tagsIdSend = Decode(tagsId);
    if (!CanUpdate() || !CanUpdateCatalog()) return;
    hfOldHash.Set("oldHash", hash);
    if ((movieId == movieIdOld && movieId != "") || (movieId == movieIdOld && tagsId == tagsIdOld))
        pcCatalog.PerformCallback(movieIdOld + "|" + tagsIdSend + "|" + pageId + "|" + PerPage());
    else {
        if (movieId == movieIdOld && tagsId == tagsIdOld && pageId == pageIdOld) return;
        else mainPanel.PerformCallback(0 + "|" + movieIdSend + "|" + tagsIdSend + "|" + pageId + "|" + PerPage());
    }
}
function ClearHash() {
    var hash = document.location.hash;
    if (!((typeof (hash) != "undefined") && (hash != null))) return "";
    hash = hash.replace("#", "");
    hash = Decode(hash);
    return hash;
}
function GetMovie(hash) {
    var tmp = hash.split(";");
    var movieID = tmp[0];
    if (!((typeof (movieID) != "undefined") && (movieID != null))) return "";
    return movieID;
}
function GetTags(hash) {
    var tmp = hash.split(";");
    var tagsID = tmp[1];
    if (!((typeof (tagsID) != "undefined") && (tagsID != null))) return "";
    return tagsID;
}
function GetPage(hash) {
    var tmp = hash.split(";");
    var pageID = tmp[2];
    if (!((typeof (pageID) != "undefined") && (pageID != null))) return 1;
    return pageID;
}
function OldHashType(hash) {
    if (hash.indexOf(".movie") > -1) hash = hash.substring(0, hash.indexOf(".movie"));
    if (hash.indexOf(".tags") > -1) hash = ";" + hash.substring(0, hash.indexOf(".tags")).replaceAll(",", ".tag,") + ".tag;1";
    if (hash.indexOf(".sitemap") > -1) hash = "";
    document.location.hash = hash;
    return hash;
}
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //                                                                 Menu click functions                                                                //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function BottomMenuTabChange(s, o) { pTopVideos.PerformCallback("0|" + o.tab.name); }
function TopMenuTabChange(s, o) {
    if (!((typeof (mnTopMenu) != "undefined") && (mnTopMenu != null)
        )) return;
    if (!((typeof (o.tab) != "undefined") && (o.tab != null))) { TopMenuVisiblity(1); return; }
    switch (o.tab.name) {
        case "Tutorial": TopMenuVisiblity(1); break;
        case "Conference": TopMenuVisiblity(2); break;
        case "Misc": TopMenuVisiblity(3); break;
    }
}
function TopMenuClick(s, o) {
    var tmp = o.item.name.split('|').reverse()[0];
    if (tmp != 'None') {
        hfExpandCollapse.Set('ExpandCollapse', false);
        ChangeSizeMin();
    }
}
function TopMenuVisiblity(Number) {
    var count = mnTopMenu.GetItemCount();
    var item;
    for (i = 0; i < count; i++) {
        item = mnTopMenu.GetItem(i);
        if (item.name.indexOf(Number) == 0) item.SetVisible(true);
        else item.SetVisible(false);
    }
}
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //                                                          Size for video box and playlist changing                                                   //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function ChangeSize() {
    if (hfExpandCollapse.Get('ExpandCollapse')) ChangeSizeMax();
    else ChangeSizeMin();
}
function ChangeSizeMin() {
    document.getElementById("dContainerBox").className = "ContainerBoxSmall";
    document.getElementById("dDescriptionBox").className = "DescriptionBoxSmall";
    document.getElementById("dvVideoBox").className = "VideoBoxSmall";
    document.getElementById("dvPlayList").className = "PlayListSmall"
    document.getElementById("CollapseExpandTd").className = "CollapseTd";
    var videoImage = document.getElementById("videoImage");
    if (typeof (videoImage) != "undefined" && videoImage != null) videoImage.className = "videoImageSmall";
    var videoContainer = document.getElementById("fContainer");
    if (typeof (videoContainer) != "undefined" && videoContainer != null) videoContainer.className = "videoImageSmall";
    var videoObject = document.getElementById("fContainer_api");
    if (typeof (videoObject) != "undefined" && videoObject != null) {
        videoObject.height = "304px";
        videoObject.widn = "540px";
    }
    SetPlayListHeight();
}
function ChangeSizeMax() {
    document.getElementById("dContainerBox").className = "ContainerBox";
    document.getElementById("dDescriptionBox").className = "DescriptionBox";
    document.getElementById("dvVideoBox").className = "VideoBox";
    document.getElementById("dvPlayList").className = "PlayList"
    document.getElementById("CollapseExpandTd").className = "ExpandTd";
    var videoImage = document.getElementById("videoImage");
    if (typeof (videoImage) != "undefined" && videoImage != null) videoImage.className = "videoImage";
    var videoContainer = document.getElementById("fContainer");
    if (typeof (videoContainer) != "undefined" && videoContainer != null) videoContainer.className = "videoImage";
    var videoObject = document.getElementById("fContainer_api");
    if (typeof (videoObject) != "undefined" && videoObject != null) {
        videoObject.height = "480px";
        videoObject.widn = "853px";
    }
}
function GetActualHeightOfPlayList() {
    var plHeight = PlayListCatalog.GetHeight();
    var headlerHeight = dvTitle.GetHeight();
    var pagerHeight = pnPager.GetHeight();
    var playListTitleHeight = dvPlayListTitle.GetHeight() + 1;
    return plHeight + headlerHeight + pagerHeight + pagerBottom + titleMarginBottom + playListTitleHeight + topBlackHeight;
}
function SetPlayListHeight() {
    var splitterHeight = pnVideoBox.GetHeight();
    var headlerHeight = dvTitle.GetHeight();
    var playListTitleHeight = dvPlayListTitle.GetHeight() + 1;
    if (!hfExpandCollapse.Get('ExpandCollapse')) {
        document.getElementById("dvPlayList").style.marginTop = (headlerHeight + titleMarginBottom) + "px";
        pnCatalogContent.SetHeight(splitterHeight - titleMarginBottom - playListTitleHeight - headlerHeight - topBlackHeight);
    }
}
function PerPage() { return 7; }
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //                                                                    Perform search                                                                   //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function PerformSearch() {
    if (cbSearchBox.GetText() == "") return;
    if (hfSearchText.Get("OldRequest") == cbSearchBox.GetText()) {
        ListDropDown();
        cbSearchBox.ShowDropDown();
        return;
    } else {
        document.getElementById("SearchErrorMessage").style.display = "none";
        document.getElementById("SearchErrorText").innerHTML = "";
        if (cbSearchBox.GetItemCount() == 0) cbSearchBox.AddItem("", "");
        cbSearchBox.ShowDropDown();
        hfSearchText.Set("SearchText", cbSearchBox.GetText());
        hfSearchText.Set("OldRequest", cbSearchBox.GetText());
        cbSearchBox.PerformCallback(cbSearchBox.GetText());
        cbSearchBox.SetText(hfSearchText.Get("SearchText"));
    }
}
function SelectItemChanged() {
    if ((typeof (cbSearchBox.GetSelectedItem()) != "undefined") && (cbSearchBox.GetSelectedItem() != null)) {
        if(cbSearchBox.GetSelectedItem().value != "ErrorMessage")
            ChangeVideoAndTag(cbSearchBox.GetSelectedItem().value.split('|')[0], cbSearchBox.GetSelectedItem().value.split('|')[1]);
    }
    cbSearchBox.SetText(hfSearchText.Get("SearchText"));
    cbSearchBox.Focus();
    cbSearchBox.SetSelection(0, 0, false);
}
function SelectItemChangedMobile() {
    if ((typeof (cbSearchBox.GetSelectedItem()) != "undefined") && (cbSearchBox.GetSelectedItem() != null)) {
        if (cbSearchBox.GetSelectedItem().value != "ErrorMessage")
            window.location = cbSearchBox.GetSelectedItem().value;
    }
    cbSearchBox.SetText(hfSearchText.Get("SearchText"));
    cbSearchBox.Focus();
    cbSearchBox.SetSelection(0, 0, false);
}
function TextChanged() { if (cbSearchBox.GetSelectedIndex() == -1) hfSearchText.Set("SearchText", cbSearchBox.GetText()); }
function ComboBoxEndCallback() { ListDropDown(); }
function ListDropDown() {
    if (cbSearchBox.GetItemCount() == 1 && cbSearchBox.GetItem(0).value == "ErrorMessage") {
        document.getElementById("SearchErrorMessage").style.display = "block";
        document.getElementById("SearchErrorText").innerHTML = "Your search did not match any videos. Please try another request or use menu.";
    }
}
function ListCloseUp() {
    document.getElementById("SearchErrorMessage").style.display = "none";
    document.getElementById("SearchErrorText").innerHTML = "";
}
