addLoadListener(init);
function init() {
  if ($('mvTabs')) {
    Timer.init(cycleFunc,5000,true);
    Timer.start();
  }
  sfHover('topNav');
  mcAccessible('topNav');
}
Math.rand=function(max){
    var ranNum= Math.floor(Math.random()*max);
    return ranNum;
}



function cycleFunc() {
//alert('test');
  var listContainer = 'mvTabAll_content';
  var listLinks = $(listContainer).getElementsByTagName('a');
  var list = [];
  var c = 0;
  for (var lli=0;lli<listLinks.length;lli++) {
    //.getElementsByClassName('tabLink');
    if (listLinks[lli].className=='tabLink') {
      list[c]=listLinks[lli];
      c++;
    }
  }
  //alert(list.length);
  //alert(list[0].parentNode.parentNode.parentNode.className)
  //alert(list[1].parentNode.parentNode.className.indexOf('active'))
  var stopNow = false;
  for (var i=0;i<list.length;i++) {
    if (stopNow===true) {
      //get the ID of the link
      //  id="link_vid_#" where vid_# is the id# of the video
      srchStr = 'link_vid_';
      if (list[i].id.indexOf('tab1_')>0) { srchStr+='tab1_'; }
      if (list[i].id.indexOf('tab2_')>0) { srchStr+='tab2_'; }
      if (list[i].id.indexOf('tab3_')>0) { srchStr+='tab3_'; }
      vidId = list[i].id.substring(list[i].id.indexOf(srchStr)+srchStr.length);
      switchVid(vidId,$(list[i].parentNode.parentNode.parentNode.id), true)
      stopNow=false;
      break;
    }
    if (list[i].parentNode.parentNode.parentNode.className.indexOf('active')>0) {
      //then we need the next el...
      stopNow=true;
      if ((i+1)>=list.length){
        i=-1;
      }
      continue;
    }
  }

}



//function switcher(videoId, callingEl, srcEvent) {
//  if (srcEvent!='timer') {
//    Timer.stop();
//    switchVid(videoId,callingEl);
//  }
//}
function goToVideo(callingEl) {
  l = $(callingEl).getElementsByTagName('a')[0];
  window.location=l.href;
}

function switchVid(videoId, callingEl, calledByFunc) {
  if (calledByFunc!==true) {
    Timer.stop();
  }
  var vidsListContainer = 'mvTabsContent';
  //get the link with the correct ID
  l = $(callingEl).getElementsByTagName('a')[0];
  elId = l.id;
  //postfix _content to the ID to address the content div for this tab.
  elId = elId+"_content";
  var videoIdPrefix = '';
  if ($(l).id.indexOf('tab1_')>0) { videoIdPrefix = 'tab1_'; }
  if ($(l).id.indexOf('tab2_')>0) { videoIdPrefix = 'tab2_'; }
  if ($(l).id.indexOf('tab3_')>0) { videoIdPrefix = 'tab3_'; }
  contentEls = $('mainVideoImage').getElementsByClassName('vidContainer');
  vidListEls = $(vidsListContainer).getElementsByClassName('tabItem');
  tmpElId = 'vid_'+videoIdPrefix+videoId;
  for (var i=0; i<contentEls.length; i++) {
    if ($(contentEls[i]).id.lastIndexOf(tmpElId)>-1) {
      elId = $(contentEls[i]).id;
      break;
    }
  }

  /* now find the div which has our 'tmpElId'*/
  for (var i=0; i<vidListEls.length;i++) {
    $(vidListEls[i]).className='tabItem';
  }
  $(callingEl).className+=' active';
  for (var i=0; i<contentEls.length; i++) {
    if ($(contentEls[i]).className.indexOf('active')!==-1) {
      $(contentEls[i]).className='vidContainer';
      if ($(contentEls[i]).style.display!='none') {
        //new Effect.SwitchOff($(contentEls[i]),{duration:0.8});
        //new Effect.SwitchOff($(contentEls[i]),{duration:0.8});
        $(contentEls[i]).hide();
      }
      break;
    }
    $(contentEls[i]).style.display='none';
  }
  //setTimeout('showEl("'+elId+'")',700);
  //alert(elId)
  $(elId).show();
  if (elId.indexOf('_content')>0) {
    elId = elId.replace(/_content/,'');
  }
  $(elId).className+=" active";

}

function switchVidFromParent(pId) {
  el = $(pId);
  l = el.getElementsByTagName('a')[0];
  eval(l.onclick());
}

function showEl(elId) {
  //new Effect.Grow(elId, {duration:1});
  //new Effect.Appear(elId, {duration:1.1});
  //new Effect.SlideDown(elId, {duration:1.2});
}

/** tabSwitch(tabContainer, callingEl)
 *  This tabSwitch method will activate/deactivate tabs and their content.
 *
 *  @param tabContainer {String/Object} String or Object reference to the tabContainer
 *  @param callingEl {String/Object} String or Object reference to the calling element
 *
 *  @requires $()
 *
 *  @returns {void}
 */
function tabSwitch(tabContainer, callingEl) {
  if (Timer.intervalId){ Timer.stop();}
  // get all tabs in tab container and remove the class value
  var tabEls = $(callingEl).parentNode.parentNode.getElementsByTagName('li');
  for (var i=0; i<tabEls.length; i++) {
    $(tabEls[i]).className='';
  }

  //set the parent node of the calling element to active and get its ID
  $(callingEl).parentNode.className='active';

  elId = $(callingEl).id;
  elId = elId.substring(elId.lastIndexOf('_')+1);
  //alert(cleaned_elId);

  //postfix _content to the ID to address the content div for this tab.
  elId = elId+"_content";

  // now hide all the divs in the container with class=tabContent
  if (isIE6OrLess()) {
    tmpEls = $(tabContainer).getElementsByTagName('div');
    var contentEls = Array();
    var ti = 0;
    for(var n=0;n<tmpEls.length;n++) {
      if (tmpEls[n].className=='mvTabContent') {
        contentEls[ti]=tmpEls[n];
        ti++;
      }
    }
  }
  else {
    contentEls = $(tabContainer).getElementsByClassName('mvTabContent');
  }
  for (var i=0; i<contentEls.length; i++) {
    //alert($(contentEls[i].className));
    if ($(contentEls[i]).style.display!='none') {
      //new Effect.SlideUp($(contentEls[i]));
      disEl = $(contentEls[i]);
      break;
    }
  //  $(contentEls[i]).style.display='none';
  }
  if (disEl.id != $(elId).id) {
    new Effect.SlideDown($(elId), {duration:1.2});
    new Effect.SlideUp(disEl, {duration:0.8});
  }
//  $(elId).style.display='block';
}



/** isIE6OrLess()
 *  for some of our methods its neccesary to know if we're dealing with an old browser
 *
 *  @returns {bool}
 */
function isIE6OrLess() {
    //Detect < IE6
    version=0
    if (navigator.appVersion.indexOf("MSIE")!=-1){
        temp=navigator.appVersion.split("MSIE");
        version=parseFloat(temp[1]);
    }
    if (version<=6.0 && version !=0) { //NON IE browser will return 0
      return true;
    }
    return false;
}

/** isIE()
 *  check if the browser is IE
 *
 *  @returns {bool}
 */
function isIE() {
    //Detect IE
    version=0
    if (navigator.appVersion.indexOf("MSIE")!=-1){
      return true;
    }
    return false;
}


/** guid()
 * Generate a uniqueID
 * @returns {int}
 */
var guid = (function() {
    // same guid counter, but we keep it hidden inside here
    var guid_counter = 0;

    // return a function that has access to guid_counter
    return function() {
        // whenever guid() is called, return and increment the counter
        return guid_counter++;
    };

})();
