// Set initial page number
var pageNumber  = 1;
var startNumber = 1;
var lineupCalls = [];

function pauseVideo() {
	try {
		video.pause();
	} catch(error) {}
}

function initCallVideos(videoIDs, limit, containerId) 
{
    lineupCalls[lineupCalls.length] = new brightCoveVideos(videoIDs, limit, containerId, pageNumber, startNumber, 'lineupCalls['+lineupCalls.length+'].responseLineup');
}
function initCallVideosSingle(videoIDs, limit, containerId) 
{
    lineupCalls[lineupCalls.length] = new brightCoveVideos(videoIDs, limit, containerId, pageNumber, startNumber, 'lineupCalls['+lineupCalls.length+'].responseLineupSingle');
}
function initCallFeatured(videoIDs, limit, containerId) 
{
    lineupCalls[lineupCalls.length] = new brightCoveVideos(videoIDs, limit, containerId, pageNumber, startNumber, 'lineupCalls['+lineupCalls.length+'].responseFeatured');
}
function initCallFeaturedInline(videoIDs, limit, containerId) 
{
    lineupCalls[lineupCalls.length] = new brightCoveVideos(videoIDs, limit, containerId, pageNumber, startNumber, 'lineupCalls['+lineupCalls.length+'].responseFeaturedInline');
}

function brightCoveVideos(videoIDs, limit, containerId, pageNumber, startNumber, callBack) 
{
    this.initialize(videoIDs, limit, containerId, pageNumber, startNumber, callBack);
}

brightCoveVideos.prototype = {
    videoIDs    : 0,
    limit       : 0,
    containerId : '',
    pageNumber  : '',
    startNumber : 1,
    proxy       : "http://api.connectok.com/json/?method=BrightCoveProxy.GetProxy",
    reqLibrary  : "http://api.brightcove.com/services/library?",
    reqCommand  : "command=find_videos_by_ids",
    reqParams   : "&video_ids=",
    reqCallBack : "&callback=",

    initialize :  function(videoIDs, limit, containerId, pageNumber, startNumber, callBack)
    {
        this.videoIDs       = videoIDs;
        this.limit          = limit;
        this.containerId    = containerId;
        this.pageNumber     = pageNumber;
        this.startNumber    = startNumber;
        this.reqCallBack    += callBack;

        if (limit > 0) this.proxy   += "&limit="+limit;

        this.reqParams   = "&video_ids="+videoIDs;
        var req = this.proxy + "&url=" + escape(this.reqLibrary + this.reqCommand + this.reqParams + this.reqCallBack);
        document.getElementById(containerId).innerHTML = '';
        bObj = new JSONscriptRequest(req, pageNumber); 
        bObj.buildScriptTag(); 
        bObj.addScriptTag();
    },
// Define the callback function, which
// writes out the HTML for each title item in the list
    responseLineup  : function(jsonData) 
    {
        var items = jsonData["items"];

        var tDiv = document.getElementById(this.containerId);
        if (!tDiv) return;

        var i = this.startNumber - 1;
                
        while (i<this.limit) {
                
                var str = "";
                str += "<div class='title item"+i+"' onClick='tb_show(\"\", \"http://newsok.com/multimedia/videoplayer/"+items[i].id+"?KeepThis=true&TB_iframe=true&height=507&width=820\")'>";
                str += '<div class="titleHolder">';
                str += '<div class="imageHolder">';
                str += '<div class="playThumb"><img src="http://static.newsok.biz/sites/newsok5/images/rollover_thumb.png"/></div>';
                str += '<div class="thumb"><img src="' + items[i].thumbnailURL + '"/></div>';
                str += '</div>';
                str += '<p class="displayName">';
                if (items[i].name.length > 50) {
                    str += items[i].name.substring(0, 50).replace(/\s+\w+$/, '...')
                } else {
                    str += items[i].name
                }
                str += '</p>';
                str += '<p class="desc">';
                if (items[i].shortDescription.length > 65) {
                    str += items[i].shortDescription.substring(0, 65).replace(/\s+\w+$/, '...')
                } else {
                    str += items[i].shortDescription
                }
                str += '</p>';
                str += '<div class="clear"></div>';
                str += '</div>';
                str += '<div class="clear"></div>';
                str += '</div>';
                tDiv.innerHTML += str;
                i++;
        }
    },
    responseLineupSingle  : function(jsonData) 
    {
        var items = jsonData["items"];

        var tDiv = document.getElementById(this.containerId);
        if (!tDiv) return;

        var i = this.startNumber - 1;
                
        while (i<this.limit) {
                
                var str = "";
                str += "<div class='title item"+i+"' onClick='tb_show(\"\", \"http://feeds.newsok.tv/services/player/bcpid4659235001?bctid="+items[i].id+"&KeepThis=true&TB_iframe=true&height=398&width=592\")'>";
                str += '<div class="titleHolder">';
                str += '<div class="imageHolder">';
                str += '<div class="playThumb"><img src="http://static.newsok.biz/sites/newsok5/images/rollover_thumb.png"/></div>';
                str += '<div class="thumb"><img src="' + items[i].thumbnailURL + '"/></div>';
                str += '</div>';
                str += '<p class="displayName">';
                if (items[i].name.length > 50) {
                    str += items[i].name.substring(0, 50).replace(/\s+\w+$/, '...')
                } else {
                    str += items[i].name
                }
                str += '</p>';
                str += '<p class="desc">';
                if (items[i].shortDescription.length > 65) {
                    str += items[i].shortDescription.substring(0, 65).replace(/\s+\w+$/, '...')
                } else {
                    str += items[i].shortDescription
                }
                str += '</p>';
                str += '<div class="clear"></div>';
                str += '</div>';
                str += '<div class="clear"></div>';
                str += '</div>';
                tDiv.innerHTML += str;
                i++;
        }
    },
    responseFeatured  : function(jsonData)
    {
        var items = jsonData["items"];

        var tDiv = document.getElementById(this.containerId);
        if (!tDiv) return;

        var i = this.startNumber - 1;

        while (i<this.limit) {

                var str = "";
                str += "<div class='title item"+[i]+"' onClick='pauseVideo();tb_show(\"\", \"http://newsok.com/multimedia/videoplayer/"+items[i].id+"?KeepThis=true&TB_iframe=true&height=507&width=820\")'>";
                str += '<div class="titleHolder">';
                str += '<h2>';
                if (items[i].name.length > 50) {
                    str += items[i].name.substring(0, 50).replace(/\s+\w+$/, '...')
                } else {
                    str += items[i].name
                }
                str += '</h2>';
                str += '<div class="imageHolder">';
                str += "<div id='theButton'></div>";
                str += '<div class="thumb"><img width="280" src="' + items[i].videoStillURL + '"/></div>';
                str += '</div>';
                str += '<p class="desc">';
		str += '<span id="BCpubDate">' + formatBCDate(items[i].publishedDate) + '</span> ';
                if (items[i].shortDescription.length > 150) {
                    str += items[i].shortDescription.substring(0, 150).replace(/\s+\w+$/, '...')
                } else {
                    str += items[i].shortDescription
                }
                str += '</p>';
                str += '<div class="clear"></div>';
                str += '</div>';
                str += '<div class="clear"></div>';
                str += '</div>';
                tDiv.innerHTML += str;
                i++;
        }
    },
    responseFeaturedInline  : function(jsonData)
    {
        var items = jsonData["items"];

        var tDiv = document.getElementById(this.containerId);
        if (!tDiv) return;

        var i = this.startNumber - 1;

        while (i<this.limit) {

                var str = "";
                str += "<div class='title item"+[i]+"' onClick='pauseVideo();tb_show(\"\", \"http://feeds.newsok.tv/services/player/bcpid4659235001?bctid="+items[i].id+"&KeepThis=true&TB_iframe=true&height=398&width=592\")'>";
                str += '<div class="titleHolder">';
                str += '<h2>';
                if (items[i].name.length > 50) {
                    str += items[i].name.substring(0, 50).replace(/\s+\w+$/, '...')
                } else {
                    str += items[i].name
                }
                str += '</h2>';
                str += '<div class="imageHolder">';
                str += "<div id='theButton'></div>";
                str += '<div class="thumb"><img width="280" src="' + items[i].videoStillURL + '"/></div>';
                str += '</div>';
                str += '<p class="desc">';
		str += '<span id="BCpubDate">' + formatBCDate(items[i].publishedDate) + '</span> ';
                if (items[i].shortDescription.length > 150) {
                    str += items[i].shortDescription.substring(0, 150).replace(/\s+\w+$/, '...')
                } else {
                    str += items[i].shortDescription
                }
                str += '</p>';
                str += '<div class="clear"></div>';
                str += '</div>';
                str += '<div class="clear"></div>';
                str += '</div>';
                tDiv.innerHTML += str;
                i++;
        }
    },
// time is stored in milliseconds; we need to convert to a mm:ss display format
    formatTime: function(time) 
    {
        var t_secs = Math.round(time/1000);
        var mins = Math.floor(t_secs/60);
        var secs = t_secs - (mins*60);
        if (secs < 10) {
                secs = "0" + secs;
        }
        return mins + ":" + secs;
    },
// date is stored in milliseconds; we need to convert to a mm:dd:yy display format
    formatBCDate  : function(myDate) 
    {

        var weekday=new Array(7);
        weekday[0]="Sun";
        weekday[1]="Mon";
        weekday[2]="Tues";
        weekday[3]="Wed";
        weekday[4]="Thur";
        weekday[5]="Fri";
        weekday[6]="Sat";

        var month=new Array(12);
        month[0]="Jan";
        month[1]="Feb";
        month[2]="Mar";
        month[3]="Apr";
        month[4]="May";
        month[5]="Jun";
        month[6]="Jul";
        month[7]="Aug";
        month[8]="Sep";
        month[9]="Oct";
        month[10]="Nov";
        month[11]="Dec";

        myDate = Math.round(myDate / 1000);
        var pubDate = new Date(myDate * 1000);
        var theDate = pubDate.toLocaleString();
        var pubDay = pubDate.getDay();
        var pubMonth = pubDate.getMonth();
        var pubDays = pubDate.getDate();
        var pubYear = pubDate.getFullYear();
        var pubHour = pubDate.getHours();
        var pubMin = pubDate.getMinutes();
        return month[pubMonth] + " " + pubDays;
    }
}
