
/*
  **  Script to perform home page slideshow
*/

var slideShow = {};

// Timer
slideShow.timer = null;

slideShow.init = function() {
  // Start
  slideShow.cur = 1;
  // Total images
  slideShow.cnt = $("#banner img").length;
  // Show first image
  $("#banner img:nth-child("+slideShow.cur+")").fadeIn(1500);
  
  $("#banner img, #left_arrow, #right_arrow").hover(function() {
    window.clearInterval(slideShow.timer);
  }, function() {
    slideShow.timer = window.setInterval("slideShow.showNext()", 5000);
  });
  
  $("#left_arrow").click(function() {
    slideShow.showPrev();
    return false;
  });
  $("#right_arrow").click(function() {
    slideShow.showNext();
    return false;
  });
  
  //slideShow.timer = window.setTimeout(function()  { slideShow.showNext(); } ,5000)
  slideShow.timer = window.setInterval("slideShow.showNext()", 5000);  
}

slideShow.showNext = function() {

  if(slideShow.cur + 1 > slideShow.cnt)  {
    $("#banner img:nth-child("+slideShow.cur+")").fadeOut(1500);
    $("#banner img:nth-child(1)").fadeIn(1500);
    slideShow.cur = 1;    
  } else {
    $("#banner img:nth-child("+slideShow.cur+")").fadeOut(1500);
    $("#banner img:nth-child("+(slideShow.cur + 1)+")").fadeIn(1500);        
    slideShow.cur++;
  }
  //slideShow.timer = window.setTimeout(function()  { slideShow.showNext(); } ,5000)  

}

slideShow.showPrev = function() {

  if(slideShow.cur - 1 < 1)  {
    $("#banner img:nth-child("+slideShow.cur+")").fadeOut(1500);
    $("#banner img:nth-child("+slideShow.cnt+")").fadeIn(1500);
    slideShow.cur = slideShow.cnt;    
  } else {
    $("#banner img:nth-child("+slideShow.cur+")").fadeOut(1500);
    $("#banner img:nth-child("+(slideShow.cur - 1)+")").fadeIn(1500);        
    slideShow.cur--;
  }
  //slideShow.timer = window.setTimeout(function()  { slideShow.showPrev(); } ,5000)  

}

$(document).ready(function()  {
  slideShow.init();
});
