var userInteraction = false;
var currentId = 0;
var changeImage = null;
var imageCount = 0;
var isInitialCall = true;
var activeButton = 1;
$(document).ready(function(){  
  imageCount = $("#buttonList").children().length;
  initializeSlideShow();
  
  $("#slideControlButtons ul li a").click(function(e){
    e.preventDefault();
    userInteraction = true;
    currentId=$(this)[0].id;
    activeButton = $(this)[0].id;
    showNextSlideshowImage(currentId);
    changeSlideshowButton("active",activeButton);    
  });
  
  $("#slideControlButtons ul li a").mouseover(function () {
   if($(this)[0].id!=activeButton){    
    changeSlideshowButton("mouseover",$(this)[0].id);       
   }
  });

  $("#slideControlButtons ul li a").mouseout(function () {
   if($(this)[0].id!=activeButton){
    changeSlideshowButton("mouseout",$(this)[0].id);   
   }
  });
  
});


function showNextSlideshowImage(){
  clearTimeout(changeImage);
  $("div.slide").css("display","none");  
  if(!userInteraction){  
    currentId++;     
    activeButton = currentId;
    $("#slide"+currentId+"").fadeIn(500);
    initializeSlideShow();
  }else{
    $("#slide"+currentId+"").fadeIn(500);
  }    
  $("#slideControlText").html($("#slide"+currentId+" div.caption").html()); 
  if(currentId==imageCount){
    currentId=0; 
  }     
}

function initializeSlideShow(){  
  if(isInitialCall){
   isInitialCall=false;
   showNextSlideshowImage();
  }else{
    changeSlideshowButton("active",currentId);    
    changeImage = setTimeout("showNextSlideshowImage()",7000);
  }
}


function changeSlideshowButton(changeToState,navPoint){  
  if(changeToState=="mouseover"){
    $("ul#buttonList li#button"+navPoint).css("backgroundImage","url('framework/skins/lhtportal/img/slideshow_button_mo.gif')");
  }else if(changeToState=="mouseout"){
    $("ul#buttonList li#button"+navPoint).css("backgroundImage","url('framework/skins/lhtportal/img/slideshow_button.gif')");
  }else if(changeToState=="active"){
    $("ul#buttonList li").css("backgroundImage","url('framework/skins/lhtportal/img/slideshow_button.gif')");
    $("ul#buttonList li a").css("color","#666666");
    $("ul#buttonList li#button"+navPoint).css("backgroundImage","url('framework/skins/lhtportal/img/slideshow_button_hi.gif')");
    $("ul#buttonList li#button"+navPoint+" a#"+navPoint).css("color","#ffffff");
  
  }
  
}