// Set initial page number
var pageNumber  = 1;
var startNumber = 1;
var lineupCalls = [];
var startVideo  = '';
var overRideLimit = 0;

function initCallLineup(lineupId, limit, containerId, playType) 
{
    limit = limit + overRideLimit;
    lineupCalls[lineupCalls.length] = new brightCoveLineup(lineupId, limit, containerId, pageNumber, startNumber+overRideLimit, 'lineupCalls['+lineupCalls.length+'].responseLineup', playType);
}

function brightCoveLineup(lineupId, limit, containerId, pageNumber, startNumber, callBack, playType) 
{
    this.initialize(lineupId, limit, containerId, pageNumber, startNumber, callBack, playType);
}

brightCoveLineup.prototype = {
    lineupId    : 0,
    limit       : 0,
    containerId : '',
    pageNumber  : '',
    startNumber : '',
    playType    : '',
    proxy       : "http://api.connectok.com/json/?method=BrightCoveProxy.GetProxy",
    reqLibrary  : "http://api.brightcove.com/services/library?",
    reqCommand  : "command=find_playlist_by_id",
    reqParams   : "&playlist_id=",
    reqCallBack : "&callback=",

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

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

        this.reqParams   = "&playlist_id="+lineupId;
        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) 
    {
        //document.getElementById(this.containerId).style.background = "url()";
        
        var items = jsonData["videos"];

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

        var i = this.startNumber - 1;
        
        if (items[i]) {

        while (i<this.limit) {
                
                var str = "";
                str += "<div class='title item"+[i]+"' ";
                if (i+1 == this.limit) {
                    str += "style='margin-bottom: 0;' ";
                }
                if (this.playType == "playerList") {
                str += "onClick='playTitleFromList("+items[i].id+")'>";
                startVideo = items[0].id;
                } else {
                str += "onClick='tb_show(\"\", \"/videoplayer?bctid="+items[i].id+"&KeepThis=true&TB_iframe=true&height=325&width=967\")'>";
                }
                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="smallPlay"></div>';
                str += '<div class="thumb"><img src="' + items[i].thumbnailURL + '"/></div>';
                str += '</div>';
                str += '<p class="displayName">';
                if (items[i].name.length > 25) {
                    str += items[i].name.substring(0, 25).replace(/\s+\w+$/, '... ')
                } else {
                    str += items[i].name
                }
                str += '</p>';
                str += '<p class="desc">';
				str += '<span id="BCduration">' + formatTime(items[i].length) + ' min |</span> ';
		        str += '<span id="BCpubDate"> Posted ' + formatBCDate(items[i].publishedDate, 3) + '</span><br />';
                if (items[i].shortDescription.length > 70) {
                    str += items[i].shortDescription.substring(0, 70).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++;
        }
        } else {
            tDiv.innerHTML = "<br /><br /><center><i>There are no videos past this point.</i></center>";
        }
    },
// 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;
    }
}

