// 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(\"\", \"http://newsok.com/multimedia/videoplayer/"+items[i].id+"?KeepThis=true&custom_click=video&TB_iframe=true&height=500&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="smallPlay"></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">';
		        str += '<span id="BCpubDate">' + formatBCDate(items[i].publishedDate) + '</span> ';
                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++;
        }
        } 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;
    }
}
function loadingDivSwap() {
	if (document.getElementById("newsok_BCloading")) {
		document.getElementById("newsok_BCloading").style.display = "none";
		document.getElementById("titleListHome").style.display = "block";
	}
}
try {
    jQuery(document).ready(function(){
        loadingDivSwap();
    }); 
} catch(err) {
//Handle errors here
}

var player;
var content;
var video;
var experience;
var layout;
  
// called when template loads, this function stores a reference to the player and modules.
// Then event listeners will be added for when the template is ready and when a user 
// clicks on a video.
function onTemplateLoaded(pEvent) {
    player = bcPlayer.getExperience("myExperience");
    video = player.getModule(APIModules.VIDEO_PLAYER);
    content = player.getModule(APIModules.CONTENT);
    experience = player.getModule(APIModules.EXPERIENCE);
    content.addEventListener(BCContentEvent.VIDEO_LOAD, onVideoLoad);
    experience.addEventListener(BCExperienceEvent.TEMPLATE_READY, onTemplateReady);
    video.addEventListener(BCVideoEvent.STREAM_START, onStreamStart);
}   
// handles click event from list items 
function playTitleFromList(id) {
    video.loadVideo(id);
}

function onStreamStart() {
    var currentVideo = video.getCurrentVideo();
    s.pageName='Video Play: ' + currentVideo.displayName
    var s_code=s.t();
    if(s_code)document.write(s_code)
    var YWATracker = YWA.getTracker("1000942893616");
    YWATracker.submit();

}

function onTemplateReady(event) {
    video.cueVideo(startVideo);
}

function onVideoLoad(event) {
}

function nextPage(num, containerId) {
    overRideLimit += num;
    theID = this.containerId;
    document.getElementById(containerId).style.background = "url(http://static.newsok.biz/sites/newsok/images/loadingAnimation.gif) no-repeat center center";
}
