var initialCall     = true;
var currentPane     = 1;
var tabClicked      = false;
var lastPane        = 0 ;
var paneCount       = 4;
var activeNavPoint  = "";
var flashVersion    = "9.0.45";
var flashAvailable  = false;
var changeTextlayer = null;
var startMovie      = null;
var restartLooping  = null;
var currentAnimation;

if(swfobject.hasFlashPlayerVersion(flashVersion)){
  flashAvailable =true;
}

function startMovieAfterTimeout(currentPane){ 
  if(flashAvailable){
   jQuery('#infolayer'+currentPane).fadeOut(500);
   document.getElementById('panes').playVideo();
  }
}

function switchPanes(currentId){    
  clearTimeout(changeTextlayer);
  clearTimeout(startMovie);
  clearTimeout(restartLooping);
  $("div.pane").css("display","none");
  $(".infolayerRight").css("display","none");
  $(".infolayerLeft").css("display","none");
  $("#pane"+currentId+"").fadeIn(500);
  changeTextlayer = setTimeout("$('#infolayer"+currentId+"').fadeIn(500)",2000);
  startMovie = setTimeout("startMovieAfterTimeout("+currentId+")",9000);
  try {
    document.getElementById('panes').showVideo(currentId-1);
  }catch (e) {}
}

function showNextPane(){
  clearTimeout(changeTextlayer);
  clearTimeout(startMovie);
  clearTimeout(restartLooping);
  if(!tabClicked){
    lastPane = currentPane;  
    currentPane++;
    initializePage(currentPane);
    if(currentPane==paneCount){
      currentPane=0; 
    }
  }else{ 
    if(flashAvailable){
      clearTimeout(changeTextlayer);
      clearTimeout(startMovie);
      clearTimeout(restartLooping);
      $("#infolayer"+currentPane+"").fadeIn(500);
      tabClicked = false;
      
      if(currentPane == paneCount){
        currentPane=0; 
      }
      currentPane++;
      restartLooping = setTimeout("initializePage(" + currentPane + ")", 10000);
      
      if(currentPane == paneCount){
        currentPane=0; 
      }
    }
  }
}


function animateButton(tabId){
  if(currentAnimation!=null){
    currentAnimation.stop();
  }
  currentAnimation = $("#tabs #tabs"+tabId).animate({marginTop: "-=17px"}, 500 );
}

function changeButton(navId,changeTo){  
  var appendName = "";
  if(navId!="tabs1"){
    appendName="_small";
  }
  if(changeTo=="mouseover" && navId!=activeNavPoint){
    $("#tabs #"+navId).css("background-image","url('framework/skins/lhtportal/img/mediastage_tab_background_mouseover"+appendName+".gif')");   
  }else if(changeTo=="mouseout" && navId!=activeNavPoint){
    $("#tabs #"+navId).css("background-image","url('framework/skins/lhtportal/img/mediastage_tab_background"+appendName+".gif')");    
  }else if(changeTo=="activate"){
    $("#tabs .tabs").css("background-position","0 -17px"); 
    $("#tabs #tabs1").css("background-position","0 -16px");
    $("#tabs .tabs").css("margin-top","17px");   
    $("#tabs .tabs").css("background-image","url('framework/skins/lhtportal/img/mediastage_tab_background"+appendName+".gif')");
    $("#tabs #tabs1").css("background-image","url('framework/skins/lhtportal/img/mediastage_tab_background.gif')");
    $("#tabs #"+navId).css("background-image","url('framework/skins/lhtportal/img/mediastage_tab_background_active"+appendName+".gif')");  
    $("#tabs #"+navId).css("height","96px"); 
    $("#tabs #"+navId).css("background-position","0 0");     
  }  
}

function initializePage(paneNo){     
  if(initialCall){
    $("#tabs").css("top","255px");
    initialCall=false;
  }  
  
  if(!tabClicked && !flashAvailable){
    setTimeout("showNextPane()",10000);
  }else if(tabClicked && flashAvailable){
    return false;  
  }
  
  
  switchPanes(paneNo); 
  activeNavPoint="tabs"+paneNo;
  changeButton("tabs"+paneNo,"activate");
  animateButton(paneNo);
}

$(document).ready(function(){  
  initializePage(currentPane);  
  $("#tabs div a").click(function(){
    if(!tabClicked){
      tabClicked=true;
    }       
    if(currentPane!=$(this)[0].id){
      var currentId=$(this)[0].id;
      currentPane = $(this)[0].id;
      activeNavPoint="tabs"+currentId;
      switchPanes(currentId);   
      changeButton('tabs'+currentId,'activate');
      animateButton(currentId);
      lastPane = currentId;
    }
    return false;
  });

  $("#tabs div a").mouseover(function () {
    changeButton('tabs'+$(this)[0].id,'mouseover');
  });

  $("#tabs div a").mouseout(function () {
    changeButton('tabs'+$(this)[0].id,'mouseout');
  });

});
    