/* Added to in Feb by Brad to fix featured title autoplaying & James for omniture tracking.
   Added to again 2-20 by Brad to allow for some preroll advertising.
*/

// global variables
var isVideoLarge=0;
var currentVideoTitle="";
var currentVideoDescription="";
var currentLineupDTO="";
var currentLineupIndex=0;
var nextVideo="";
var recentlyPlayed="";
var adCycle=0;
var hasFeatureTitlePlayed=0;
var ad0=1422261274;
var ad1=1422258782;
var ad2=1422258785;
var currentRSSfeed="http://brightcove.tv/rss/?channel=713285227&newest=20";
var buildAddThisURL="http://wimgo.com/project"

// this function is called when the BC player finishes loading - used to add event listeners
function onTemplateLoaded(message) {
    // This event is my meat & potatoes for loading videos in the player when clicked or featured video on player load.
    callFlash("addEventListener","titleLoad","onTitleLoad");
    // I used this event to check & see what the size of the player window was at the end of a play & resize if necessary.
    callFlash("addEventListener","mediaComplete","onMediaComplete");
    // This event is used to display information about the currently playing video.
    callFlash("addEventListener", "streamStart", "onStreamStart");
    // This event is used to autoplay the featured title after the player loads.
    callFlash("addEventListener", "contentLoad", "onContentLoad");
}

/* This function runs when the player is fully loaded & gets the featured title & lineup for the player.
   It sets into motion autoplaying the featured title & continues then to autoplay the rest of the featured lineup.
   Called by the event listener for contentLoad.*/
function onContentLoad(evt) {
    /* myPlayAd(); I took this out so no ads would play - James */
    callFlash("getLineupById", 1416618297);
    var query=location.search.substring(1);
    if(query!="") { 
        if(query.slice(0,8)=="titleID=")  {
            nextVideo = query.slice(8,18);
        }
    }
}

/* This function returns a lineupDTO for the lineup ID # it is called with.
    I'm calling it from inside onContentLoad to get the lineup DTO that is in the player so I can cycle to the next video. */
function getLineupById_Result(myLineup) {
    currentLineupDTO = myLineup;
    nextVideo = currentLineupDTO.videoIds[currentLineupIndex];
}

/* Uses BC API to call getCurrentTitle_Result & display 'Now Playing' text. Runs everytime a new video starts.
   Called by the event listener for streamStart.*/
function onStreamStart (evt) {
    callFlash("getCurrentTitle");
}

/* This function displays info about what is current playing.
   Called by getCurrentTitle inside the function onStreamStart.*/
function getCurrentTitle_Result(titleDTO) {
    recentlyPlayed = titleDTO.id;
    currentVideoTitle = titleDTO.displayName;
    currentVideoDescription = titleDTO.shortDescription;    
    document.getElementById('description_text').innerHTML=currentVideoDescription;
    document.getElementById('title').innerHTML=currentVideoTitle;
    document.getElementById('nowPlaying').style.display="block";
    document.getElementById('addThis').style.display="block";
    s.pageName='Video Play: ' + titleDTO.displayName
    s.hier1="Brightcove"
    s.prop7=readCookie("nid")
    s.prop8='Video Play: ' + titleDTO.displayName
    var s_code=s.t();if(s_code)document.write(s_code)
}

/* This function loads the specified title into the player.
   Called inside the onMediaComplete */ 
function loadTitle(titleId) {
    buildAddThisURL="http://wimgo.com/project/?titleID="+titleId;
    callFlash("fetchTitleById",titleId);
}

// The toggleVideo functions use scriptaculous to resize the video player
function toggleVideoLarge() {
    new Effect.Morph('bc_video_resize', {style:"width:963px; height: 590px;", duration:0.5});
    new Effect.Morph('bc_video_holder', {style:"height: 590px;", duration:0.5});
    document.getElementById('enlarge_video').style.display = 'none';
    document.getElementById('shrink_video').style.display = 'block';
    isVideoLarge=1;
    document.getElementById('video_right_column').style.display = 'none';
}
function toggleVideoSmall() {
    new Effect.Morph('bc_video_resize', {style:"width:642px; height: 410px;", duration:0.5});
    new Effect.Morph('bc_video_holder', {style:"height: 410px;", duration:0.5});
    document.getElementById('shrink_video').style.display = 'none';
    document.getElementById('enlarge_video').style.display = 'block';
    isVideoLarge=0;
    document.getElementById('video_right_column').style.display = 'block';
}

/* Resize video player at end of video if necessary & autoplay next video.
   This is called by the event listener mediaComplete.*/
function onMediaComplete() {
    if (isVideoLarge==1) {
        toggleVideoSmall();
    }
    if (adCycle == 10) {
        adCycle=0;
        myPlayAd();
    } else { 
        adCycle++;
        if (recentlyPlayed.toString()==nextVideo.toString()) {
            currentLineupIndex++;
            nextVideo = currentLineupDTO.videoIds[currentLineupIndex];
        }
        loadTitle(nextVideo); 
        currentLineupIndex++;
        try {
            nextVideo = currentLineupDTO.videoIds[currentLineupIndex];
        } catch(errorMsg){
            currentLineupIndex = 0;
            nextVideo = currentLineupDTO.videoIds[currentLineupIndex];
        }
    }
}

/* Plays title after its loaded. Sets up variables for title & description of currently playing video.
   Called by the event listener titleLoad.*/
function onTitleLoad(titleDTO) {
    callFlash("loadTitleById", titleDTO.parameters.title.id);
    currentVideoTitle = titleDTO.parameters.title.displayName;
    currentVideoDescription = titleDTO.parameters.title.shortDescription;
    document.getElementById('description_text').innerHTML=currentVideoDescription;
    document.getElementById('title').innerHTML=currentVideoTitle;
}

// Uses BCs RSS search feature.  Utilizes a search.php file created by Ethan Cairns.
function videoSearch(search, limit) {
    searchString = search;
    searchString.replace(/ /,"%20");
    currentRSSfeed="http://brightcove.tv/rss/?channel=713285227&search="+searchString;
    document.getElementById('lineup_column').style.display = 'none';
    document.getElementById('list_column').style.display = 'block';
    searchString = 'NewsOK.tv search results for: "'+search+'"';
    url = '/ajax/newsoktv/search.php?limit='+limit+'&title='+escape(searchString)+'&search='+escape(search);
    new Ajax.Updater($('list_column'), url);
    s.pageName=''
    s.hier1=''
    s.prop7=''
    s.prop8=''
    s.prop11='NewsOK.tv Search: ' + search
    var s_code=s.t();if(s_code)document.write(s_code)
}
// Pulls in RSS feed for lineups when selected.  Also utilizes search.php made by Ethan.    Edited by Brad 2/11/08 to allow for autoplaying through selected lineup.
function retrieveLineup(player, lineup, limit, lineupTitle) {
    // The following 2 lines were added by Brad to change the lineup that autoplays when a user chooses a lineup.
    currentLineupIndex = 0;
    callFlash("fetchLineupById", lineup);
    // I added the following line to build the RSS url for users to subscribe to specific lineups
    currentRSSfeed = "http://link.brightcove.com/services/link/bcpid"+player+"/bclid"+lineup+"?action=rss"; 
    
    document.getElementById('list_column').style.display = 'none';
    document.getElementById('lineup_column').style.display = 'block';
    url = '/ajax/newsoktv/search.php?limit='+limit+'&title='+escape(lineupTitle)+'&lineup='+escape(lineup)+'&playerid='+escape(player);
    new Ajax.Updater($('lineup_column'), url);
    lineupID = lineup;
    s.prop8=''
    s.prop11=''
    s.pageName='wimgo ' + lineupTitle
    s.hier1="NewsOK.tv Brightcove"
    s.prop7=readCookie("nid")
    var s_code=s.t();if(s_code)document.write(s_code)
}
// Pulls & displays RSS for the newest videos. Uses search.php made by Ethan.
function newestLineup(limit) {
    currentRSSfeed="http://brightcove.tv/rss/?channel=713285227&newest=20";
    document.getElementById('list_column').style.display = 'block';
    document.getElementById('lineup_column').style.display = 'none';
    url = '/ajax/newsoktv/search.php?newest='+limit+'&title='+escape("NewsOK.TV Most Recent Videos")+"&limit="+limit;
    new Ajax.Updater($('list_column'), url);
    s.prop8=''
    s.prop11=''
    s.pageName='newsoktv_newest_videos'
    s.hier1="NewsOK.tv Brightcove"
    s.prop7=readCookie("nid")
    var s_code=s.t();if(s_code)document.write(s_code)
}

// Pulls & displays RSS for the most popular videos.  Uses search.php made by Ethan.
function mostPopular(limit) {
    currentRSSfeed="http://brightcove.tv/rss/?channel=713285227&popular=20";
    document.getElementById('lineup_column').style.display = 'none';
    document.getElementById('list_column').style.display = 'block';
    url = '/ajax/newsoktv/search.php?popular='+limit+'&title='+escape("NewsOK.tv Most Popular Videos")+"&limit="+limit;
    new Ajax.Updater($('list_column'), url);
    s.prop8=''
    s.prop11=''
    s.pageName='newsoktv_most_popular'
    s.hier1="NewsOK.tv Brightcove"
    s.prop7=readCookie("nid")
    var s_code=s.t();if(s_code)document.write(s_code)
}

/* This function gets the lineup DTO when a user clicks a specific lineup in the dropdown menus.
   Called inside the retrieveLineup function.*/
function fetchLineupById_Result(theFetchedLineup) {
    currentLineupDTO = theFetchedLineup;
    nextVideo = currentLineupDTO.videoIds[currentLineupIndex];
}

searchText = "Search all videos";
function clearSearchText(srchBox) {
    if (srchBox.value == searchText) {
        srchBox.value = "";
    } else {
        srchBox.select();
    }
}

/* Commented out for Wimgo
// I added the following code 2-19 to use scriptaculous to animate our footer. BB.
var currentlyOpenElement = "";

Effect.OpenUp = function(element) {
    element = $(element);
    new Effect.BlindDown(element, arguments[1] || {});
    currentlyOpenElement = element;
}

Effect.CloseDown = function(element) {
    element = $(element);
    new Effect.BlindUp(element, arguments[1] || {});
}

Effect.Combo = function(element) {
    element = $(element);
    if(currentlyOpenElement != "") {
        new Effect.CloseDown(currentlyOpenElement, arguments[1] || {});
    }
    if(element.style.display == 'none' ) { 
        new Effect.OpenUp(element, arguments[1] || {queue: 'end'}); 
    } else { 
        new Effect.CloseDown(element, arguments[1] || {}); 
        currentlyOpenElement = "";
    }
}
*/

/* I added the following code 2-20 to play some Cox preroll. BB.
   This is called once from onContentLoad and then every few plays from onMediaComplete.*/
function myPlayAd() {
    callFlash("setControlsMode", "ad");
    var randomNum= Math.floor(Math.random()*3);
    switch(randomNum)
    {
        case 0:
            callFlash("fetchTitleById", ad0);
            break;
        case 1:
            callFlash("fetchTitleById", ad1);
            break;
        case 2:
            callFlash("fetchTitleById", ad2);
            break;
    }
}

