// Speed of the automatic slideshow
var slideshowSpeed = 6000;

// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [ {
    "title" : "1",
    "image" : "http://www.sipkegelling.nl/www/themes/default/graphics/style/1.jpg",
    "url" : "http://strategischlezenspellen.nl/",
    "firstline" : "<a href='http://strategischlezenspellen.nl/' target='_blank'><img src='http://www.sipkegelling.nl/www/themes/default/graphics/style/leader_1.jpg' width='626' height='210' border='0'></a>",
  "secondline" : "<a style='color: #FFF; font-size:20px;' href='http://strategischlezenspellen.nl/' target='_blank'>Strategisch lezen & spellen</a><div style='margin-top: 5px;'><span style='line-height:160%;'><a style='color: #FFF;' href='http://strategischlezenspellen.nl/' target='_blank'>Strategisch lezen en spellen is een lees- en spellingsmethode met een vernieuwde aanpak. De strategische benadering is gebaseerd op neurologische inzichten betreffende de lees- en spellingsdidactiek. Met deze methode kan zeer adaptief lees- en spellingsonderwijs gegeven worden.</a></span></div><div style='margin-top: 5px'><a style='color: #FFF;' href='http://strategischlezenspellen.nl/' target='_blank'><b>Lees verder</b></a></div></a>"
  }, {
    "title" : "2",
    "image" : "http://www.sipkegelling.nl/www/themes/default/graphics/style/2.jpg",
    "url" : "http://www.manvrouwportal.nl/",
    "firstline" : "<a href='http://www.manvrouwportal.nl/' target='_blank'><img src='http://www.sipkegelling.nl/www/themes/default/graphics/style/leader_2.jpg' width='626' height='210' border='0'></a>",
    "secondline" : "<a style='color: #FFF; font-size:20px;' href='http://www.manvrouwportal.nl' target='_blank'>Man / Vrouw spel</a><div style='margin-top: 5px;'><span style='line-height:150%;'><a style='color: #FFF;' href='http://www.manvrouwportal.nl' target='_blank'> Het man-vrouw spel is een kaartspel met masculiene en feminiene eigenschappen, dat als familiespel, coachtool of trainingsinstrument ingezet kan worden. Hiermee verbonden is deze man-vrouw test of meter. De m/v test inventariseert je masculiene en feminiene eigenschappen en meet de verhouding daar tussen. </a></span> <a style='color: #FFF;' href='http://www.manvrouwportal.nl' target='_blank'><b>Lees verder</b></a></div>"
  }, {
    "title" : "3",
    "image" : "http://www.sipkegelling.nl/www/themes/default/graphics/style/3.jpg",
    "url" : "http://www.veiligheidnet.nl/",
    "firstline" : "<a href='http://www.veiligheidnet.nl/' target='_blank'><img src='http://www.sipkegelling.nl/www/themes/default/graphics/style/leader_3.jpg' width='626' height='210' border='0'></a>",
    "secondline" : "<a style='color: #FFF; font-size:20px;' href='http://www.veiligheidnet.nl/' target='_blank'>VeiligheidNET</a><div style='margin-top: 5px;'><span style='line-height:150%;'><a style='color: #FFF;' href='http://www.veiligheidnet.nl/' target='_blank'>De missie van VeiligheidNet is een one-stop-shop te bieden voor studiedagen, boeken en software op het gebied van integrale veiligheid voor zowel de professional als ook voor werkgevers en werknemers. VeiligheidNet wil dit bereiken door een intensieve samenwerking met partijen in het werkveld. </a></span></div><div style='margin-top: 5px'><a style='color: #FFF;' href='http://www.veiligheidnet.nl/' target='_blank'><b>lees verder</b></a></div>"
  }, {
    "title" : "4",
    "image" : "http://www.sipkegelling.nl/www/themes/default/graphics/style/4.jpg",
    "url" : "http://www.coachingnet.nl/",
    "firstline" : "<a href='http://www.coachingnet.nl' target='_blank'><img src='http://www.sipkegelling.nl/www/themes/default/graphics/style/leader_4.jpg' width='626' height='210' border='0'></a>",
    "secondline" : "<a style='color: #FFF; font-size:20px;' href='http://www.coachingnet.nl/' target='_blank'>CoachingNet</a><div style='margin-top: 5px;'><span style='line-height:160%;'><a style='color: #FFF;' href='http://www.coachingnet.nl' target='_blank'>CoachingNet bv is een aparte uitgeverij welke zich bezighoudt met het ontwikkelen van softwaretools voor de coach en de coachende manager, opleider, personeelsfunctionaris en veranderaar. </a></span></div><div style='margin-top: 5px'><a style='color: #FFF;' href='http://www.coachingnet.nl' target='_blank'><b>lees verder</b></a></div>"
  }, {
    "title" : "5",
    "image" : "http://www.sipkegelling.nl/www/themes/default/graphics/style/2.jpg",
    "url" : "http://www.manvrouwspel.nl/",
    "firstline" : "<a href='http://www.manvrouwspel.nl/' target='_blank'><img src='http://www.sipkegelling.nl/www/themes/default/graphics/style/leader_2.jpg' width='626' height='210' border='0'></a>",
    "secondline" : "<a style='color: #FFF; font-size:20px;' href='http://www.manvrouwspel.nl' target='_blank'>Man / Vrouw spel</div><div style='margin-top: 5px;'><span style='line-height:160%;'><a style='color: #FFF;' href='http://www.manvrouwspel.nl' target='_blank'> Het man-vrouw spel is een kaartspel met masculiene en feminiene eigenschappen, dat als familiespel, coachtool of trainingsinstrument ingezet kan worden. Hiermee verbonden is deze man-vrouw test of meter. De m/v test inventariseert je masculiene en feminiene eigenschappen en meet de verhouding daar tussen.</a> </span></div><div style='margin-top: 5px'><a style='color: #FFF;' href='http://www.manvrouwspel.nl' target='_blank'><b>lees verder</b></a></div>"
  }
];



$(document).ready(function() {
    
  // Backwards navigation
  $("#back").click(function() {
    stopAnimation();
    navigate("back");
  });
  
  // Forward navigation
  $("#next").click(function() {
    stopAnimation();
    navigate("next");
  });
  
  var interval;
  $("#control").toggle(function(){
    stopAnimation();
  }, function() {
    // Change the background image to "pause"
    $(this).css({ "background-image" : "url(http://www.sipkegelling.nl/www/themes/default/graphics/style/btn_pause.png)" });
    
    // Show the next image
    navigate("next");
    
    // Start playing the animation
    interval = setInterval(function() {
      navigate("next");
    }, slideshowSpeed);
  });
  
  
  var activeContainer = 1;  
  var currentImg = 0;
  var animating = false;
  var navigate = function(direction) {
    // Check if no animation is running. If it is, prevent the action
    if(animating) {
      return;
    }
    
    // Check which current image we need to show
    if(direction == "next") {
      currentImg++;
      if(currentImg == photos.length + 1) {
        currentImg = 1;
      }
    } else {
      currentImg--;
      if(currentImg == 0) {
        currentImg = photos.length;
      }
    }
    
    // Check which container we need to use
    var currentContainer = activeContainer;
    if(activeContainer == 1) {
      activeContainer = 2;
    } else {
      activeContainer = 1;
    }
    
    showImage(photos[currentImg - 1], currentContainer, activeContainer);
    
  };
  
  var currentZindex = -1;
  var showImage = function(photoObject, currentContainer, activeContainer) {
    animating = true;
    
    // Make sure the new container is always on the background
    currentZindex--;
    
    // Set the background image of the new active container
    $("#headerimg" + activeContainer).css({
      "background-image" : "url(" + photoObject.image + ")",
      "display" : "block",
      "z-index" : currentZindex
    });
    
    // Hide the header text
    $("#headertxt").css({"display" : "none"});
    
    // Set the new header text
    $("#firstline").html(photoObject.firstline);
    $("#secondline")
      .attr("href", photoObject.url)
      .html(photoObject.secondline);
    $("#pictureduri")
      .attr("href", photoObject.url)
      .html(photoObject.title);
    
    
    // Fade out the current container
    // and display the header text when animation is complete
    $("#headerimg" + currentContainer).fadeOut(function() {
      setTimeout(function() {
        $("#headertxt").css({"display" : "block"});
        animating = false;
      }, 100);
    });
  };
  
  var stopAnimation = function() {
    // Change the background image to "play"
    $("#control").css({ "background-image" : "url(http://www.sipkegelling.nl/www/themes/default/graphics/style/btn_play.png)" });
    
    // Clear the interval
    clearInterval(interval);
  };
  
  // We should statically set the first image
  navigate("next");
  
  // Start playing the animation
  interval = setInterval(function() {
    navigate("next");
  }, slideshowSpeed);
  
});
