﻿var max_titles = 3; // Maximum number of titles to show in the box
var current_title = 0; // Starting title
var titles = null;
var title_height = null;
var title_top = 0;
var titlesplayed = 0;

var up_a = '&#x25B2;';
var up_i = '&nbsp;';
var up_h = '&#x25B2;';
var up_p = '&#x25B2;';
var dn_a = '&#x25BC;';
var dn_i = '&nbsp;';
var dn_h = '&#x25BC;';
var dn_p = '&#x25BC;';

/* Brightcove player config */
var config = new Array();
config["videoId"] = null; //the default video loaded into the player
config["videoRef"] = null; //the default video loaded into the player by ref id specified in console
config["lineupId"] = null; //the default lineup loaded into the player
config["playerTag"] = null; //player tag used for identifying this page in brightcove reporting
config["autoStart"] = false; //tells the player to start playing video on load
config["preloadBackColor"] = "#FFFFFF"; //background color while loading the player
config["wmode"] = "transparent"; //allow use of z-index property
config["width"] = 326;
config["height"] = 292;
/* do not edit these config items */
config["playerId"] = 1127798136;


/* Set up events */
$(document).ready(function(){
});

/* Player functions */
function onTemplateLoaded(errorMessage) {
  callFlash("addEventListener", "contentLoad", "onContentLoad");
	callFlash("addEventListener", "mediaComplete", "onMediaComplete");
}

function getTitleById_Result(titleDTO){
  titles.push(titleDTO);
}

function getLineupByReferenceId_Result(lineupDTO)
{
	for(i = 0; i < lineupDTO.videoIds.length; i++)
		callFlash('getTitleById', lineupDTO.videoIds[i]);
	onContentLoad();

  /* Append items to the scrolling list */
  $('#module-video-titles').append('<ul class="module-list"></ul>');
  for ( i=0; i<titles.length; i++ ){
    var mins = Math.floor(titles[i].length/60000);
    var secs = Math.round((titles[i].length%60000)/1000);
    if (secs < 10) secs = '0' + secs;

    $('#module-video-titles > ul').append(
      '<li id="' + i + '"><div class="title">' + titles[i].displayName +
      '</div><div class="time">(' + mins + ':' + secs + ')</div></li>'
    );
  }
  $('#module-video-titles > ul').fadeIn('slow');
}

function onContentLoad(){
  if(!titles) { // first load only
    $('#module-video-scrollbox').fadeOut('fast');
    titles = [];
		callFlash('getLineupByReferenceId', lineupRef );

    $('#module-video-scrollbox').prepend('<div id="up"><div id="up-alpha"><div id="up-content">'+ up_i +'</div></div></div>');
    $('#module-video-scrollbox').append('<div id="down"><div id="down-alpha"><div id="down-beta"><div id="down-content">'+ dn_i +'</div></div></div></div>');

    $('#module-video-scrollbox').fadeIn('slow');

    $('#down').hover(
      function(){
        if ( title_top > -(titles.length-max_titles)*title_height ){
          $('#down').addClass('hover');
          $('#down-content').html(dn_h);
        }
      }, update_arrows );
    $('#down').mousedown(
      function() {
        if ( title_top > -(titles.length-max_titles)*title_height ){
          //added this to enable scrolling for html arrow
          scroll_down();
          $('#down').addClass('hover');
          $('#down-content').html(dn_p);
        }
      }
    );
    //removed this to enable scrolling for html arrow
    //$('#down').click( scroll_down );


	 $('#module-video-titles > ul > li').hover(
      function(){
        $(this).addClass('hover');//.fadeIn('slow');
      }, function(){
      	$(this).removeClass('hover');//.fadeIn('slow');
      } ); 

    $('#up').hover(
      function(){
        if ( title_top < 0 ){
          $('#up').addClass('hover');
          $('#up-content').html(up_h);
        }
      }, update_arrows );
    $('#up').mousedown(
      function() {
        if ( title_top < 0 ) {
          //added this to enable scrolling for html arrow
          scroll_up();
          $('#up').addClass('press');
          $('#up-content').html(up_p);
        }
      }
    );
    //removed this to enable scrolling for html arrow
    //$('#up').click( scroll_up );

    $('#module-video-enlarge').append('<a href="#">enlarge</a>').fadeIn('slow');
    title_height = Number($('#module-video-titles > ul > li').height());

    $('#module-video-titles > ul > li').bind('click', function(){
      playtitle($(this).attr('id'));
    });
    scroll_to( current_title );
    set_active( current_title );
    update_arrows();
  }
  update_url();
}

function onMediaComplete() {
  titlesplayed++;
  current_title++;
  if ( current_title > titles.length )
    current_title = 0;
  if(titlesplayed < titles.length)
    playtitle(current_title);
}


function playtitle(id){
  current_title = id;
  set_active( current_title );
  scroll_to( current_title );
  update_url();
  callFlash("loadTitleById", titles[current_title].id);
}



/* Lineup scroll functions */
function scroll_down(){
  if ( -(titles.length-max_titles)*title_height >= title_top )
    return;
  title_top -= title_height;
  $('#module-video-titles > ul').animate({top: title_top + 'px'}, 'normal');
  update_arrows();
}

function scroll_up(){
  if ( title_top >= 0 )
    return;
  title_top += title_height;
  $('#module-video-titles > ul').animate({top: title_top + 'px'}, 'normal');
  update_arrows();
}

function update_arrows(){

  $('#down').removeClass('hover');
  $('#down').removeClass('press');
  $('#up').removeClass('hover');
  $('#up').removeClass('press');


  if ( title_top >= 0 ) {
    $('#up-content').html(up_i);
    $('#up').removeClass('inactive');
    $('#up').addClass('inactive');
  } else {
    $('#up-content').html(up_a);
    $('#up').removeClass('inactive');
    $('#up').addClass('active');
  }

  if ( title_top <= -(titles.length-max_titles)*title_height ) {
    $('#down-content').html(dn_i);
    $('#down').removeClass('active');
    $('#down').addClass('inactive');
  } else {
    $('#down-content').html(dn_a);
    $('#down').removeClass('inactive');
    $('#down').addClass('active');
  }
}

function scroll_to(titleno){
  if ( titleno == 0 )
    title_top = 0;
  else if ( titleno == titles.length-1 )
    title_top = -(titles.length-max_titles)*title_height
  else
    title_top = -(titleno-1)*title_height;
  $('#module-video-titles > ul').animate({top: title_top + 'px'}, 'normal');
  update_arrows();
}

function set_active(titleno){
  $('#module-video-titles > ul > li').removeClass('active');
  $('#module-video-titles > ul > li:eq('+titleno+')').addClass('active');
}

function update_url() {
  var url = titles[current_title].linkURL;
  if( !url ) {
    url = 'http://blog.inc.com/mt/mt-search.cgi?blog_id=9&search="' +
          titles[current_title].displayName + '"';
    $('#module-video-enlarge > a').attr('href', url);
  }
}
