/*

  America.gov Site JavaScript: Video Page Functions
  
  America.gov Web Site
  United States Department of State
  Last Edited: Jan 2008 by Darren W Krape (krapedw@state.gov)

*/

//Swaps out brightcove media elements (such as a video or photo gallery component)
$.fn.brightcoveToggleVideo = function() {

  var loc = location.href;
  var videoId = new Array();
  videoId = loc.split("=");
  videoId = videoId[1];

  if ( !(videoId) && ($(".player").attr("class")) ) {
    var videoId = ($(".collection-box li a:eq(0)").attr("href")).substring(1);
    $.fn.brightcoveToggleVideo.swapVideo(videoId);
  }

  this.each(function() {

    $(this).find(".collection-box li").click(function () {
      var videoId = ($(this).find("a:eq(0)").attr("href")).substring(1);
      $.fn.brightcoveToggleVideo.swapVideo(videoId);
      return false;
    });

  });
};

$.fn.brightcoveToggleVideo.swapVideo = function(videoId) {

  var loc = location.href;
  if (loc.indexOf('?videoId=') !=-1) {
    var redirectURL = new Array();
    redirectURL = loc.split("?");
    redirectURL = redirectURL[0];
  } else {
    redirectURL = loc;
  }

  window.location = redirectURL + "?videoId=" + videoId;

};

$.fn.activeHighlight = function() {
  var loc = location.href;
  var itemId = new Array();
  itemId = loc.split("=");
  itemId = itemId[1];

  if(itemId) {
    $(".collection-box").find("li").removeClass("active");
    $("a[href=#" + itemId + "]:eq(0)").parent().addClass("active");

    var contentID = "#" + $("a[href=#" + itemId + "]:eq(0)").parent().parent().attr("id");

    $(".collection-nav").find("li").removeClass();
    $(".collection-nav").find("a[href=" + contentID + "]").parent().addClass("active");

    $(".collection-box").find(".elements").hide();
    $(contentID).show();
  }
};

$(function() {
  $("#video").find(".tabbed-collection").brightcoveToggleVideo();
  $(".tabbed-collection").activeHighlight();  
});
