﻿// constants
var c_intMediaScrollFrames   = 10;
var c_intMediaScrollSpeed    = 22;
var c_intSlideAnimFrames     = 8;
var c_intSlideAnimSpeed      = 28;
var c_intSlideShowDuration   = 3;  //seconds

// variables
var aBoolScroll              = new Array(false, false);
var aFltScrollX              = new Array(0, 0);
var aIntScrollCount          = new Array(0, 0);
var aIntScrollTargetX        = new Array(0, 0);
var aIntTabOffset            = new Array(0, 0, 0, 0);
var aObjScroll               = new Array(null, null);
var fltAnimStep              = 0;
var fltAnimY                 = 0;
var fltScrollStep            = 0;
var intActiveTab             = 0;
var intTabHeight             = 0;
var intTabWidth              = 0;
var oMediaSelected           = null;
var oSlideArea               = null;
var tmrAnim                  = null;

// initialize
window.onload = function() {
    if (window.top == window) {
        window.open("../default.aspx" + document.location.search, "_top");
    }

    // configure tabs
    intTabHeight = document.documentElement.clientHeight - 36;
    intTabWidth = document.documentElement.clientWidth - 182;
    sizeTab("ContentArea");
    sizeTab("Content0");
    sizeTabBreak("Break1");
    sizeTab("Content1");
    sizeTabBreak("Break2");
    sizeTab("Content2");
    sizeTabBreak("Break3");
    sizeTab("Content3");

    // size content border
    var oBorder = document.getElementById("ContentBorder");
    oBorder.style.width = (intTabWidth + 2) + "px";
    oBorder.style.height = (intTabHeight + 0) + "px";

    // size side bar
    document.getElementById("SideBar").style.height = intTabHeight + "px";

    //center video player and media navigation
    document.getElementById("Player").style.marginLeft = Math.round((intTabWidth - 560) / 2) + "px";
    document.getElementById("MediaNav").style.marginLeft = Math.round((intTabWidth - 582) / 2) + "px";

    // setup animation
    oSlideArea = document.getElementById("SlideArea");
    fltAnimStep = (intTabHeight + 1) / c_intSlideAnimFrames;
    fltScrollStep = 90 / c_intMediaScrollFrames;

    // turn scrollbars on
    document.getElementById("Content0").style.overflow = "auto";

    // hide documents tab
    if (document.getElementById("pnlDocuments").innerHTML.indexOf("<") < 0) {
        document.getElementById("Content1").style.display = "none";
        document.getElementById("Break1").style.display = "none";
        document.getElementById("Tab1").style.display = "none";
        document.getElementById("Tab2").style.top = "260px";
        document.getElementById("Tab3").style.top = "300px";
        aIntTabOffset[2]++;
        aIntTabOffset[3]++;
    } else {
        document.getElementById("Tab1").style.display = "block";
    }


    // hide links tab
    if (document.getElementById("pnlLinks").innerHTML.indexOf("<") < 0) {
        document.getElementById("Content2").style.display = "none";
        document.getElementById("Break2").style.display = "none";
        document.getElementById("Tab2").style.display = "none";
        document.getElementById("Tab3").style.top = (300 - (aIntTabOffset[3] * 40)) + "px";
        aIntTabOffset[3]++;
    } else {
        document.getElementById("Tab2").style.display = "block";
    }

    // configure media navigation
    aObjScroll[0] = document.getElementById("Thumbs0");
    aObjScroll[1] = document.getElementById("Thumbs1");
    for (var i = 0; i < aObjScroll[0].childNodes.length; i++) if (aObjScroll[0].childNodes[i].getAttribute("programId") != null) aIntScrollCount[0]++;
    for (var i = 0; i < aObjScroll[1].childNodes.length; i++) if (aObjScroll[1].childNodes[i].getAttribute("programId") != null) aIntScrollCount[1]++;
    if (aIntScrollCount[0] == 0 && aIntScrollCount[1] == 0) {
        document.getElementById("Content3").style.display = "none";
        document.getElementById("Tab3").style.display = "none";
    } else if (aIntScrollCount[0] == 0) {
        document.getElementById("Photos").style.display = "none";
        document.getElementById("Tab3").style.display = "block";
    } else if (aIntScrollCount[1] == 0) {
        document.getElementById("Videos").style.display = "none";
        document.getElementById("Tab3").style.display = "block";
    } else {
        document.getElementById("Tab3").style.display = "block";
    }
    if (aIntScrollCount[0] > 6) document.getElementById("btnThumbsNext0").className = "btnNextOn";
    if (aIntScrollCount[1] > 6) document.getElementById("btnThumbsNext1").className = "btnNextOn";

    // load flash player
    if (aIntScrollCount[0] > 0 || aIntScrollCount[1] > 0)
        swfobject.embedSWF("../../Library/longtail/player-licensed.swf", "JWPlayer", "560", "315", "9.0.0", "expressInstall.swf", { "repeat": "always", "controlbar": "bottom" }, { "allowscriptaccess": "always", "allowfullscreen": "true" }, { id: "JWPlayer", name: "JWPlayer" });

}

// flash player ready event
var player = null;
function playerReady(obj) {
    // set pointer to player object
    player = document.getElementById(obj["id"]);

    // parse path (required for videos to play correctly)
    var strPath = location.pathname;
    strPath = strPath.substr(0, strPath.lastIndexOf("/"));
    strPath = strPath.substr(0, strPath.lastIndexOf("/") + 1);

    // load first media item
    if (aIntScrollCount[0] > 0) {
        oMediaSelected = aObjScroll[0].childNodes[0];
        player.sendEvent("LOAD", { file: strPath + "media.aspx?ID=" + oMediaSelected.getAttribute("programId") + "&File=" + escape(oMediaSelected.getAttribute("filename")) });
        player.sendEvent("PLAY", true);
    } else if (aIntScrollCount[1] > 0) {
        oMediaSelected = aObjScroll[1].childNodes[0];
        player.sendEvent("LOAD", { file: strPath + "media.aspx?ID=" + oMediaSelected.getAttribute("programId") + "&File=" + escape(oMediaSelected.getAttribute("filename")) });
    }
    if (oMediaSelected != null)
        oMediaSelected.innerHTML = "<div class=\"thumbSelect\"></div>";
}

// add program
function addProgram() {
    parent.myLytebox.resizeContainer_norefresh(633, 290);
    return false;
}

// email page
function emailPage() {
    var htmContent = "";
    htmContent += "<div class=\"descriptorrightheader\">" + document.getElementById("ProgramTitle").innerHTML + "<br />&nbsp;</div>";
    htmContent += document.getElementById("PrintArea").innerHTML;
    document.getElementById("EmailContent").value = htmContent;
        
    parent.myLytebox.resizeContainer_norefresh(632, 480);
    return true;
}

// media navigation scroll stop
function mediaNavOff(n) {
    aBoolScroll[n] = false;
}

// media navigation scroll start
function mediaNavOn(btn, n, i) {
    if (btn.className.indexOf("Off") < 0 && !aBoolScroll[n]) {
        aBoolScroll[n] = true;
        mediaScroll(n, i);
    }
}

// show photo or video
function mediaPlay(o) {
    if (player) {
        //highlight thumbnail
        if (oMediaSelected != null) oMediaSelected.innerHTML = "";
        oMediaSelected = o;
        o.innerHTML = "<div class=\"thumbSelect\"></div>";

        setTimeout(function() {
            // stop playing
            player.sendEvent("STOP");

            // parse path (required for videos to play correctly)
            var strPath = location.pathname;
            strPath = strPath.substr(0, strPath.lastIndexOf("/"));
            strPath = strPath.substr(0, strPath.lastIndexOf("/") + 1);

            // show photo or video
            player.sendEvent("LOAD", { file: strPath + "media.aspx?ID=" + o.getAttribute("programId") + "&File=" + escape(o.getAttribute("filename")) });
            player.sendEvent("PLAY", true);
        }, 10);
    }
}

// media scroll animation
function mediaScroll(n, i) {
    // calculate scroll limit
    var intScrollLimitX = 90 * (aIntScrollCount[n] - 6);
    var fltStep = fltScrollStep * i;
    
    // set target and turn off scroll buttons
    if ((i > 0 && aIntScrollTargetX[n] < aFltScrollX[n]) || (i < 0 && aIntScrollTargetX[n] > aFltScrollX[n])) {
        aIntScrollTargetX[n] += i * 90;
        if (aIntScrollTargetX[n] <= 0) document.getElementById("btnThumbsPrevious" + n).className = "btnPreviousOff";
        if (aIntScrollTargetX[n] >= intScrollLimitX) document.getElementById("btnThumbsNext" + n).className = "btnNextOff";
    }

    // move to next position
    aFltScrollX[n] += fltStep;
    if ((i > 0 && aFltScrollX[n] > intScrollLimitX) || (i < 0 && aFltScrollX[n] < 0)) aFltScrollX[n] = aIntScrollTargetX[n];
    aObjScroll[n].style.marginLeft = -Math.round(aFltScrollX[n]) + "px";
    
    // set scroll buttons on
    if ((i > 0 && aIntScrollTargetX[n] < aFltScrollX[n]) || (i < 0 && aIntScrollTargetX[n] > aFltScrollX[n])) {
        if (aIntScrollTargetX[n] > 0) document.getElementById("btnThumbsPrevious" + n).className = "btnPreviousOn";
        if (aIntScrollTargetX[n] < intScrollLimitX) document.getElementById("btnThumbsNext" + n).className = "btnNextOn";
    }
    
    // repeat scroll
    if ((i > 0 && aIntScrollTargetX[n] > aFltScrollX[n]) || (i < 0 && aIntScrollTargetX[n] < aFltScrollX[n]) || (aBoolScroll[n] && (aFltScrollX[n] + fltStep) < intScrollLimitX && (aFltScrollX[n] + fltStep) > 0)) {
        setTimeout("mediaScroll(" + n + ", " + i + ")", c_intMediaScrollSpeed);
    }
}

// media slide show
function mediaSlideShow() {
    if (player) {
        // unhighlight media item
        if (oMediaSelected != null) oMediaSelected.innerHTML = "";

        // stop playing
        player.sendEvent("STOP");
        
        // parse path (required for videos to play correctly)
        var strPath = location.pathname;
        strPath = strPath.substr(0, strPath.lastIndexOf("/"));
        strPath = strPath.substr(0, strPath.lastIndexOf("/") + 1);

        //create playlist
        var oThumbs = document.getElementById("Thumbs0");
        var lst = new Array();
        for (var i = 0; i < oThumbs.childNodes.length; i++) {
            var o = oThumbs.childNodes[i];
            if (o.getAttribute("programId") != null)
                lst[i] = { file: strPath + "media.aspx?ID=" + o.getAttribute("programId") + "&File=" + escape(o.getAttribute("filename")), "duration": c_intSlideShowDuration };
        }

        // show playlist
        player.sendEvent("LOAD", lst);
        player.sendEvent("PLAY", true);
    }
}

// set size of tab to fill light box 
function sizeTab(id) {
    var o = document.getElementById(id);
    o.style.width = intTabWidth + "px";
    o.style.height = intTabHeight + "px";
    o.style.display = "block";
}

// set size of tab to fill light box 
function sizeTabBreak(id) {
    var o = document.getElementById(id);
    o.style.width = intTabWidth + "px";
    o.style.height = "1px";
    o.style.display = "block";
}

// tab click
function tabClick(n) {
    if (n != intActiveTab) {
        // turn off previous tab
        document.getElementById("Tab" + intActiveTab).className = "tabOff";
        document.getElementById("Content" + intActiveTab).style.overflow = "hidden";

        // start slide animation
        var jw=document.getElementById("JWPlayer");
        if (jw) jw.style.visibility = "hidden";
        intActiveTab = n;
        tabSlide();
        
        // turn on active tab
        document.getElementById("Tab" + intActiveTab).className = "tabOn";
    }
}

// slide tab
function tabSlide() {
    // clear animation timer
    clearTimeout(tmrAnim);
    
    // move to new position
    var intTargetY = (intActiveTab - aIntTabOffset[intActiveTab]) * intTabHeight + (intActiveTab - aIntTabOffset[intActiveTab]);

    if (intTargetY > fltAnimY) {
        fltAnimY += fltAnimStep;
        if (fltAnimY > intTargetY) fltAnimY = intTargetY;
    } else if (intTargetY < fltAnimY) {
        fltAnimY -= fltAnimStep;
        if (fltAnimY < intTargetY) fltAnimY = intTargetY;
    }
    oSlideArea.style.marginTop = -Math.round(fltAnimY) + "px";
    
    // next frame or finish
    if (intTargetY == fltAnimY) {
        var jw = document.getElementById("JWPlayer");
        if (jw) jw.style.visibility = "visible";
        document.getElementById("Content" + intActiveTab).style.overflow = "auto";
    } else {
        tmrAnim = setTimeout("tabSlide()", c_intSlideAnimSpeed);
    }
}