﻿// web service call
var content, player, video;
var htmMore = "", reqNo = 0;
var token = "-XUzwf6ts_mLJn6DbJa14ZmHfHvxCz_ecj3u1AFMXoM.";
var req = [
        { "playlistId": 6703642001, "caption": "More Videos" },
        { "playlistId": 6710302001, "caption": "More Videos" },
        { "playlistId": 6703638001, "caption": "More Videos" },
        { "playlistId": 6138596001, "caption": "More Videos" }
    ];

/*
removed 8373808001

*/

// scroll animation
var animationSpeed = 10;
var animationStep = 10;
var animationTimer = null;
var animationPosition = new Array(req.length);
var animationTarget = new Array(req.length);
var animationStatus = new Array(req.length);
var videoCount = new Array(req.length);

// initiallization
window.onload = function() {
    SetupForm();
    nextReq(0);
}

// process next request (step 1 - playlist)
function nextReq(n) {
    reqNo += n;

    var bObj = new JSONscriptRequest("http://api.brightcove.com/services/library?command=find_playlist_by_id&playlist_id=" + escape(req[reqNo].playlistId) + "&fields=name,videoIds&token=" + encodeURIComponent(token) + "&callback=nextReqEx");
    bObj.buildScriptTag();
    bObj.addScriptTag();
}

// process next request (step 2 - videos)
function nextReqEx(jsonData) {
    req[reqNo].caption = jsonData.name
	try{
		var bObj = new JSONscriptRequest("http://api.brightcove.com/services/library?command=find_videos_by_ids&video_ids=" + escape(jsonData.videoIds) + "&token=" + encodeURIComponent(token) + "&fields=id,name,shortDescription,thumbnailURL,length&callback=response");
		bObj.buildScriptTag();
		bObj.addScriptTag();
	} catch(e){
		//alert(e);
	}
}

// Brightcove player loaded
function onTemplateLoaded(o) {
    var bctid = qs("bctid");
    
    player = brightcove.getExperience(o);
    video = player.getModule(APIModules.VIDEO_PLAYER);
    

    if (!isNaN(bctid)) {
        setTimeout("playTitleFromList(" + bctid + ")",50);
    }

    content = player.getModule(APIModules.CONTENT);
    content.addEventListener(BCContentEvent.VIDEO_LOAD, onVideoLoad);
}

// web service response from Brightcove (more videos)
function response(jsonData) {
    var items = jsonData["items"];
    var cnt = items.length;

    animationPosition[reqNo] = 0;
    animationTarget[reqNo] = 0;
    videoCount[reqNo] = cnt;
    htmMore += "<table class=\"tight\"><tr><td class=\"morePrevious\" rowspan=\"2\"><img id=\"MorePrevious" + reqNo + "\" alt=\"Previous\" height=\"30\" src=\"images/more_previous_off.gif\" width=\"18\" onmousedown=\"previous(" + reqNo + ");\" onmouseup=\"halt(" + reqNo + ");\" onmouseout=\"halt(" + reqNo + ");\" /></td><td class=\"moreHeader\"><div class=\"moreCaption\">" + req[reqNo].caption + "</div></td><td class=\"moreNext\" rowspan=\"2\"><img id=\"MoreNext" + reqNo + "\" alt=\"Next\" height=\"30\" src=\"images/more_next_" + (cnt > 5 ? "on" : "off") + ".gif\" width=\"18\" onmousedown=\"next(" + reqNo + ");\" onmouseup=\"halt(" + reqNo + ");\" onmouseout=\"halt(" + reqNo + ");\" /></td></tr><tr><td class=\"tightCell\"><div class=\"moreScrollBox\"><table id=\"MoreContent" + reqNo + "\" class=\"tight\"><tr>";

    for (var i = 0; i < cnt; i++) {
        var o = items[i];
		var oid = o.id;
        htmMore += "<td class=\"moreVideo\" onClick=\"playTitleFromList(" + oid + ")\">";
        htmMore += "<table class=\"morePreview\"><tr><td><img src=\"" + o.thumbnailURL + "\" /></td></tr></table>";
        htmMore += "<div class=\"moreName\">" + o.name + "</div><div class=\"moreLength\">(" + formatTime(o.length) + ")</div>";
        htmMore += "</td>";
    }

    htmMore += "</tr></table></div></td></tr></table>";

    if (reqNo < req.length - 1) {
        nextReq(1);
    } else {
        document.getElementById("MoreVidoes").innerHTML = htmMore;
    }
}

// time is stored in milliseconds; we need to convert to a mm:ss display format
function formatTime(time) {
    var t_secs = Math.round(time / 1000);
    var mins = Math.floor(t_secs / 60);
    var secs = t_secs - (mins * 60);
    return mins + ":" + (secs < 10 ? "0" : "") + secs;
}

// handles click event from list items
function playTitleFromList(id) {
    content.getVideoAsynch(id);
}

// load video into player
function onVideoLoad(event) {
    video.loadVideo(event.video.id);
    
    // make sure video player is visible
    var y = 235 - (document.documentElement.clientHeight - 546) / 2;
    if (document.documentElement.scrollTop) {
        document.documentElement.scrollTop = y < 0 ? 0 : y;
    } else if (document.body.scrollTop) {
     document.body.scrollTop = y < 0 ? 0 : y;
    }
}

// next video
function next(i) {
    var m = animationTarget[i] - 130;

    if (videoCount[i] * 130 + m > 520) {
        animationTarget[i] = m;
        if (videoCount[i] * 130 + m <= 650) {
            document.getElementById("MoreNext" + i).src = "images/more_next_off.gif";
        }
        document.getElementById("MorePrevious" + i).src = "images/more_previous_on.gif";
        animationStatus[i] = "NEXT";
        AnimateScroll(i);
    }
}

// previous video
function previous(i) {
    var m = animationTarget[i] + 130;

    if (m <= 0) {
        animationTarget[i] = m;
        if (m == 0) {
            document.getElementById("MorePrevious" + i).src = "images/more_previous_off.gif";
        }
        document.getElementById("MoreNext" + i).src = "images/more_next_on.gif";
        animationStatus[i] = "PREV";
        AnimateScroll(i);
    }
}

// stop animation
function halt(i) {
    animationStatus[i] = null;
}

// do scroll animation
function AnimateScroll(i) {
    if (animationTimer == null) AnimateScrollEx(i);
}
function AnimateScrollEx(i) {
    var o = document.getElementById("MoreContent" + i);
    var d = animationPosition[i] - animationTarget[i];

    if (d == 0) {
        if (animationStatus[i] == "PREV") {
            previous(i);
        } else if (animationStatus[i] == "NEXT") {
            next(i);
        } else {
            if (animationTimer != null) clearTimeout(animationTimer);
            animationTimer = null;
        }
    } else {
        animationPosition[i] += (d > 0 ? -animationStep : animationStep);
        o.style.marginLeft = animationPosition[i] + "px";
        setTimeout("AnimateScroll(" + i + ")", animationSpeed);
    }
}
