function showTab(tabId, tabNo) {
	var tabCollection=document.getElementById(tabId);
	tabCollection.className=tabId+tabNo+'Visible';
}

var CVPCounter = 0;
var cnnCVP;
var cnnVideoContexts = [];
cnnVideoContexts.t1 = {width:662, height:423, autostart:1};
cnnVideoContexts.live_4_1_t1 = {width:662, height:423, autostart:1};
cnnVideoContexts.t1Bullet = {width:278, height:152, autostart:0};
cnnVideoContexts.hpBox1 = {width:320, height:180, autostart:0};
cnnVideoContexts.teamCarousel = {width:320, height:180, autostart:0};
cnnVideoContexts.storyEmbed = {width:400, height:225, autostart:0};
cnnVideoContexts.storyEmbed_4_1 = {width:400, height:225, autostart:0};
cnnVideoContexts.story_4_1 = {width:640, height:360, autostart:1};
cnnVideoContexts.videosection_4_1 = {width:640, height:360, autostart:1};
cnnVideoContexts.GotuitStream_4_1 = {width:640, height:360, autostart:1};
cnnVideoContexts.GotuitStreamPreview_4_1 = {width:640, height:360, autostart:1};
cnnVideoContexts.main_4_1 = {width:640, height:360, autostart:1};
cnnVideoContexts.plain = {width:640, height:360, autostart:1};
cnnVideoContexts.swim11 = {width:672, height:378, autostart:1};
cnnVideoContexts.NCAAvideo = {width:640, height:360, autostart:1};
cnnVideoContexts.behindthemic = {width:576, height:397, autostart:1};
cnnVideoContexts.swim11model = {width:320, height:180, autostart:1};
cnnVideoContexts.swim11modeltest = {width:320, height:180, autostart:1};
cnnVideoContexts.swim11home = {width:704, height:396, autostart:1};
cnnVideoContexts.swim11hometest = {width:704, height:396, autostart:1};
cnnVideoContexts.swim11 = {width:672, height:378, autostart:1};

/* create cvpPlayers array for legacy code */
var cvpPlayers = new Array();

/* 
instantiate cvp
	videoId - video to play
	vArea - div id to embed player
	vContext - player context name
	vReturn - return the name of the player if mulitple players on the page (0,1)
 */
function cnnVideoNewPlayer(videoId, vArea, vContext, vReturn, width, height) {
  CVPCounter++;
  if (typeof vArea == 'undefined') {
    vArea = "player"+videoId;
  }
  if (typeof vContext == 'undefined') {
    vContext = "main";
  }
  if (typeof vReturn == 'undefined') {
    vReturn = 0;
  }
  if (typeof width == 'undefined') {
    width = 0;
  }
  if (typeof height == 'undefined') {
    height = 0;
  }
  var cvpTmp;
  var vWidth;
  var vHeight;
  var autoStart = 1;
  var noMediaFile = false;
  
  if(typeof cnnVideoContexts[vContext] != 'undefined') {
    vWidth = cnnVideoContexts[vContext].width;
    vHeight = cnnVideoContexts[vContext].height;
    autoStart = cnnVideoContexts[vContext].autostart;
  } else if (width != 0 && height != 0) {
    vWidth = width;
    vHeight = height;
  } else { // main, GotuitStream
    vWidth = 662; vHeight = 373;
  }

  /* SETUP HTML5 VARS FOR USE WITH SWIMSUIT11 OR SWIMSUIT VAULT */
  if(window.location.pathname.indexOf('vault/swimsuit') != -1) {
    var html5Url = cvpHTML5Vault(videoId);
    if(isAppleMobile() === true) {
      if(html5Url == 'false') {
        html5Url = '';
        noMediaFile = true;
      }
    }
  } else if((window.location.pathname.indexOf('/swimsuit/') != -1) || (window.location.pathname.indexOf('/2011_swimsuit/') != -1)) {
    var html5Url = cvpHTML5Swimsuit(videoId);
    if(isAppleMobile() === true) {
      if(html5Url == 'false') {
        html5Url = '';
        noMediaFile = true;
      }
    }
  }

  var timestamp = new Date().getTime();
  cvpTmp = new CVP({ 
    id : 'cvp_'+CVPCounter,
    width : vWidth, 
    height : vHeight,
    perVideoFallbacks : [{
      criteria : '${video.category}',
      evaluate : function(criteria) {
        return (criteria.indexOf('swimsuit') != -1) ? true : false;
      },
      filter : html5Url
    }],
    flashVars : {
      context : vContext, 
      site : 'si', 
      profile : 'si_prod',  
      contentId : videoId,
      baseSwfUrl : 'http://i.cdn.turner.com/xslo/cvp/core/base/0/2.1.5.1/CVPBase.swf'
    },
    embed : {
      containerSwf : '/.element/swf/4.2/global/cvp/cvp_main_container_2.1.6.0.swf',
      expressInstallSwf : 'http://i.cdn.turner.com/xslo/cvp/assets/flash/expressInstall.swf',
      options : {
        quality : 'high',
        bgcolor : '#000000',
        allowFullScreen : 'true', 
        allowScriptAccess : 'always',
        wmode : 'transparent'		            
      }
    }, 
    onCVPReady: function() {
      siOnVideoPlayerReady();
    },
    onContentBegin : function(playerId, contentId) {
      siOnVideoPlayerBegin(contentId);
    },
    onAdPlay : function(playerId, token, mode, id, duration, segmentId, adType) {
      siOnAdStarted(token, videoId);
    },
    onAdEnd : function(playerId, token, mode, id, segmentId, adType) {
      siOnAdFinished(token, videoId);
    },
    onContentPlay : function(playerId, contentId) {
      this.switchTrackingContext(vContext);
      var vdata = this.getContentEntry(this.getContentId());
      sendVideoEvent(vdata,"video-start",playerId);
      siOnVideoPlayerPlay(contentId);
      document.getElementById('adCountdown').innerHTML = '';
    },
    onContentPlayhead : function(playerId, contentId, playhead, duration) {
      siOnVideoPlayerPlayHead(contentId, playhead, duration);
    },
    onContentPause : function(playerId, contentId, paused) {
      var vdata = this.getContentEntry(this.getContentId());
      sendVideoEvent(vdata,"video-pause",playerId);
      siOnVideoPlayerPause(contentId, paused)
    },
    onContentBuffering : function(playerId, contentId, buffering) {
      var vdata = this.getContentEntry(this.getContentId());
      if(buffering === true){ 
        sendVideoEvent(vdata,"video-buffer",playerId); 
      }
      siLog.debug('onContentBuffering: Complete');  
    },
    onTrackingContentSeek : function(playerId, contentId) {
      var vdata = this.getContentEntry(this.getContentId());
      sendVideoEvent(vdata,"video-scrub",playerId);
      siOnVideoTrackingSeek();
    },
    onTrackingContentProgress : function(playerId, dataObj) {
      var vdata = this.getContentEntry(this.getContentId());
      if(Math.floor(dataObj.percent) == 50){
        sendVideoEvent(vdata,"video-fifty_percent",playerId);
      }
    },
    onContentEnd : function(playerId, contentId) {
      var vdata = this.getContentEntry(this.getContentId());
      sendVideoEvent(vdata,"video-complete",playerId);
      siLog.debug('onContentEnd: Complete');
    },
    onContentComplete : function(playerId, contentId) {
      siOnVideoPlayerComplete(contentId);
    },
    onTrackingAdCountdown : function(playerId, trackingObj) {
      siOnVideoTrackingAdCountdown(trackingObj.secs);
    },
    onContentError : function(playerId, contentId, errorMessage) {
      siOnVideoPlayerError(errorMessage);
      if(errorMessage=='The media resource indicated by the src attribute was not suitable') {
        noMediaFile = true;
      }
    }
  });

  if(noMediaFile === true ) {
    document.getElementById(vArea).innerHTML = 'The device in use is not supported for this section of our site.';
  } else {
    cnnCVP = cvpTmp;
    // return a new named player instance
    if (vReturn == 1 || vReturn == 'true') {
      cnnCVP.embedSWF(vArea);
      return cnnCVP;
    // else use default cnnCVP
    } else {
      cnnCVP.embedSWF(vArea);
    }
  }
}


/* HTML5 PLAYER FOR VAULT SWIMSUIT VIDEOS */
function cvpHTML5Vault(videoId) {
  var videoXmlUrl = '/.element/auto/video/tvp_mdf/swimsuit-vault/'+videoId+'.xml';
  xmlhttp=new XMLHttpRequest();
  xmlhttp.open("GET",videoXmlUrl,false);
  xmlhttp.send();
  var data = xmlhttp.responseXML;
  var html5Url;
  var id = data.getElementsByTagName("video")[0].getAttribute("id");
  var videoFile = data.getElementsByTagName("file")[0].childNodes[0].nodeValue;
  if(videoFile.indexOf('2009') != -1) {
    html5Url = 'http://ht.cdn.turner.com/si/big/video/swimsuit/2009/' + id + '.mp4';
  } else if(videoFile.indexOf('2008') != -1) {
    id = data.getElementsByTagName("file")[0].childNodes[0].nodeValue.replace(".f4v", "");
    id = id.replace("/swimsuit/2008/","");
    id = id.replace(/%20/g,".");
    id = id.toLowerCase();
    if(id.indexOf('will.and.heidi1') != -1) {
      id = id.replace("1","");
    }
    html5Url = 'http://ht.cdn.turner.com/si/big/video/swimsuit/2008/' + id + '.mp4';
    html5Url = html5Url.replace("..",".");
  } else if(videoFile.indexOf('2007') != -1) {
    id = data.getElementsByTagName("file")[0].childNodes[0].nodeValue.replace(".f4v", "");
    id = id.replace("/swimsuit/2007/","");
    id = id.replace(/%20/g,".");
    id = id.toLowerCase();
    html5Url = 'http://ht.cdn.turner.com/si/big/video/swimsuit/2007/' + id + '.mp4';
    html5Url = html5Url.replace("..",".");
  } else if(videoFile.indexOf('2006') != -1) {
    id = data.getElementsByTagName("file")[0].childNodes[0].nodeValue.replace(".flv", "");
    id = id.replace("/swimsuit/2006/","");
    id = id.toLowerCase();
    if(id.indexOf('nakashima') != -1) {
      id = id.replace("04/","11/");
    }
    html5Url = 'http://ht.cdn.turner.com/si/big/video/swimsuit/2006/' + id + '.mp4';
  } else if(videoFile.indexOf('2005') != -1) {
    id = data.getElementsByTagName("file")[0].childNodes[0].nodeValue.replace(".flv", "");
    id = id.replace("/swimsuit/2005/","");
    id = id.toLowerCase();
    if(id.indexOf('annev') != -1) {
      id = id.replace("05_","05.");
    }
    html5Url = 'http://ht.cdn.turner.com/si/big/video/swimsuit/2005/' + id + '.mp4';
  } else if(videoFile.indexOf('2004') != -1) {
    id = data.getElementsByTagName("file")[0].childNodes[0].nodeValue.replace(".flv", "");
    id = id.replace("/swimsuit/2004/","");
    if(id.indexOf('noemie') != -1) {
      id = id.replace("noemie","noemi");
    }
    html5Url = 'http://ht.cdn.turner.com/si/big/video/swimsuit/2004/' + id + '.mp4';
  } else if(videoFile.indexOf('2003') != -1) {
    html5Url = 'false';
  }

  return html5Url;
}

/** HTML5 PLAYER FOR SWIMSUIT 11 VIDEOS **/
function cvpHTML5Swimsuit(videoId) {
  var videoXmlUrl = '/.element/auto/video/tvp_mdf/swimsuit/'+videoId+'.xml';
  xmlhttp=new XMLHttpRequest();
  xmlhttp.open("GET",videoXmlUrl,false);
  xmlhttp.send();
  var data = xmlhttp.responseText;
  var html5video = false;
  var matchArr;
  matchArr = data.match(/<file bitrate="400"[^>]+>([^<]+)<\/file>/);
  html5video = matchArr[1].replace(/\d+\.f4v/,'mov');
  html5video = html5video.replace(/f[l4]v$/,'mov');
  html5video = html5video.replace('_HOME','');
  html5video = html5video.replace('_MODEL','');
  html5video = html5video.replace('_VIDEO','');
  var html5url = 'http://ht.cdn.turner.com/si/big/video' + html5video;

  return html5url;
}

/** OLD VIDEO PLAYER INSTANCES **/
/*
instantiate cvp
	videoId - video to play
	vArea - div id to embed player
	vContext - player context name
 */
function cnnVideoPlayer(videoId, vArea, vContext) {
	cnnVideoNewPlayer(videoId, vArea, vContext, 0);
}

/* 
embed video in a story
	videoId - video to play
	vArea - div id to embed player
 */
function cnnStoryPlayer(videoId, vArea) {
	cnnVideoNewPlayer(videoId, vArea, "story_4_1", 0, 640, 373);
}

/* 
embed video in a story
	videoId - video to play
	vArea - div id to embed player
 */
function cnnSmallStoryPlayer(videoId, vArea) {
	cnnVideoNewPlayer(videoId, vArea, "storyEmbed", 0, 400, 225);
}

/*
cvp function for behind the mic instance
	videoId - video to play
	vArea - div id to embed player
	vContenxt - player context name
	vreturn - return instance as a var
*/
function cnnVideoNewPlayerBtm(videoId,vArea,vContext,vReturn) {
	cnnVideoNewPlayer(videoId, vArea, vContext, vReturn);
}

/*
embed video in a story
	videoId - video to play
	vContext - player context name
	playerObjId - specified id for cvp player
*/
function buildCVP(videoId,vContext,playerObjId){
	var thiscvpplayer = cnnVideoNewPlayer(videoId, '', vContext, 1);
	return thiscvpplayer;
}

/*
cvp function for swimsuit11
	videoId - video to play
	vArea - div id to embed player
	vContext - player context name
 */
function cnnSwim11Video(videoId, vArea, vContext) {
	cnnVideoNewPlayer(videoId, vArea, vContext);
}

/*
embed video externally 
*/
function si_watercooler_vid(videoId) {
	var inc = Math.floor(Math.random()*90000);
	var dom_id = "sivid_" + inc;
	document.write('<div id="'+dom_id+'"></div>');
	cnnVideoNewPlayer(videoId, dom_id, "storyEmbed", inc);
}

/* OLD VIDEO ANALYTIC DETAILS */
/* Video Analytics  */
/* These variables are necessary for vidPlay, vidFiftyPercent, vidSeek to operate properly */
var hasScrubbed = false;
var isAuto = false;
var isHalf = false;
var isBuffering = false;
var isPaused = false;
var vidObj;
var videoPage = (location.pathname.indexOf('/video') === 0) ? 'main' :
		(location.pathname.indexOf('/swimsuit') === 0 || location.pathname.indexOf('_swimsuit') === 5) ? 'swimsuit' : 'other';

if (typeof console !== "undefined" || typeof console.log !== "undefined") {
	try {
		console.log('videoPage: ' + videoPage);
	} catch (e) {}
}

/* NEW FUNCTIONS */
/* Check if apple mobile device */
function isAppleMobile() {
  var appleDevice = false;
  /* Test for iPAD */
  if(navigator.userAgent.indexOf('iPad')>-1) {
    appleDevice = true;
  }
  /* Test for iPhone, iPod */
  if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
    appleDevice = true;
  }
  return appleDevice;
}

function cnnSetVol() {
	document.getElementById('cnnSound').style.display='none';
	cnnCVP.setVolume('0.50');
}

/* CVP CALLBACK FUNCTIONS */
function siOnVideoTrackingAdCountdown(seconds) {
      try {
        siVideoTrackingAdCountdown(seconds);
      } catch(e) {
        siLog.debug('siOnVideoTrackingAdCountdown: '+e);
      }
      siLog.debug('siOnVideoTrackingAdCountdown: Complete');
}

function siOnVideoPlayerReady() {
      try {
        siSetAdSection();
      } catch(e) {
        siLog.debug('siOnVideoPlayerReady: '+e);
      }
      siLog.debug('siOnVideoPlayerReady: Complete');
}

function siOnVideoPlayerBegin(videoId) {
      try {
        siVideoBegin(cnnCVP, videoId);
      } catch(e) {
        siLog.debug('siOnVideoPlayerBegin: '+e);
      }
      siLog.debug('siOnVideoPlayerBegin: Complete');
}

function siOnVideoPlayerPlay(videoId) {
      try {
        siVideoPlay(cnnCVP, videoId);
      } catch(e) {
        siLog.debug('siOnVideoPlayerPlay: '+e);
      }
      siLog.debug('siOnVideoPlayerPlay: Complete');
}

function siOnAdStarted(token, videoId) {
      try {
        siVideoAdStarted(cnnCVP, videoId);
      } catch(e) {
        siLog.debug('siOnAdStarted: '+e);
      }
      siLog.debug('siOnAdStarted: Complete');
}

function siOnAdFinished(token, videoId) {
      try {
        siVideoAdFinished(cnnCVP, videoId);
      } catch(e) {
        siLog.debug('siOnAdFinished: '+e);
      }
      siLog.debug('siOnAdFinished: Complete');
}

function siOnVideoPlayerPlayHead(videoId, playheadTime, totalDuration) {
      try {
        siVideoPlayHead(cnnCVP, playheadTime, totalDuration);
      } catch(e) {
        siLog.debug('siOnVideoPlayerPlayHead: '+e);
      }
      siLog.debug('siOnVideoPlayerPlayHead: Complete');
}


function siOnVideoPlayerComplete(videoId) {
      try {
        siVideoComplete(cnnCVP, videoId);
      } catch(e) {
        siLog.debug('siOnVideoPlayerComplete: '+e);
      }
      siLog.debug('siOnVideoPlayerComplete: Complete');
}

function siOnVideoPlayerPause(videoId, paused) {
      try {
        siVideoPause(cnnCVP, videoId, paused);
      } catch(e) {
        siLog.debug(e);
      };
      siLog.debug('siOnVideoPlayerPause: Complete');
}

function siOnVideoTrackingSeek() {
	try {
		siVideoSeek();
	} catch(e) {
		siLog.debug(e);
	};
	siLog.debug('siOnVideoTrackingSeek: Complete');
}

function siOnVideoPlayerError(errorMessage) {
	siLog.debug('CVP error: '+errorMessage);
}
