/* ================================ +
   	       SeederDeBoer
 + ================================ */



/* 
** Made by: Studio Parkers / Edgar Vijgeboom, Thomas Lievestro
** Email:	info@studioparkers.nl
** Website:	www.studioparkers.nl
*/

/* ================================ +
                GENERAL
 + ================================ */


function initBgSlideshow(section){
    
  $.getJSON('wp-content/themes/toolbox/scripts/'+section+'.json', function(data) {
    
      var img_array = [];
      var quotes = [];
      activeImage  = $('#bg .bg_image').css('background-image').replace(/"/g,"").replace(/url\(|\)$/ig, "");
      
      $.each(data, function(i, item) {
        // console.log(item);
        if(item.foto != activeImage) {
          img_array.push(item.foto);
          quotes.push(item.quote+item.naam);
        } else {
          img_last    = item.foto;
          quote_last  = item.quote+item.naam;
        }
      });
      
      // Put the active image last in the array
      img_array.push(img_last);
      quotes.push(quote_last);
      
     /* Preload images */
     $.preLoadImages(img_array);
     

      function bgSlideShow() {
    
      $('#bg').append(
        $('<div><img src="http://www.seederdeboer.nl/wp-content/themes/toolbox/images/mask_bg.png"/></div>').addClass('bg_image').css({'background-image': 'url("'+ img_array[0] +'")'}).hide()
      
      );
      
      $('#wrapper').append(
        $('<div class="payoff"><p>'+ quotes[0] +'</p></div>').hide()
      );
      
      
      $('#bg .bg_image:last').fadeIn(2000, function() {
        $('#bg .bg_image:first').remove();
        
        //remove current image from array
        var current_background = img_array.shift();
        //add current image to end of array
        img_array.push(current_background); 
      });
       $('#wrapper .payoff:first').fadeOut(1000,function() {$(this).remove() });
       $('#wrapper .payoff:last').fadeIn(2000, function() {
      
        
        //remove current image from array
        var current_quote = quotes.shift();
        //add current image to end of array
        quotes.push(current_quote); 
      });
        
      setTimeout(function(){
        bgSlideShow();
      },6000);
    }   


      setTimeout(function(){
      bgSlideShow();
       },4000);
  
    })
 }
	
	

/* document ready functions */
$(document).ready(function(){	

  if ($('.home').length) { initBgSlideshow('partners') } else if ($('.page-template-homepage-php').length || $('.page-id-1548').length) { initBgSlideshow('medewerkers') };

	$("#quotes").easySlider({
		auto: 			true,
		continuous: 	true,
		prevId: 		'prev_arrow',
		nextId: 		'next_arrow',
		speed: 			1000,
		pause:			6000


	});

});	
	

(function($){

	/* Preload images */
	var cache = [];
	// Arguments are image paths relative to the current page.
	$.preLoadImages = function() { 
	var args_len = arguments[0].length;
	for (var i = args_len; i--;) {
		var cacheImage = document.createElement('img');
			cacheImage.src = arguments[0][i];
			cache.push(cacheImage);
		}
	}
	
	//Shuffle function 
	$.fn.shuffle = function() {
	    return this.each(function(){
	      var items = $j(this).children();
	      return (items.length)
	        ? $j(this).html($j.shuffle(items))
	        : this;
	    });
	}

	$.shuffle = function(arr) {
		for(
			var j, x, i = arr.length; i;
		      j = parseInt(Math.random() * i),
		      x = arr[--i], arr[i] = arr[j], arr[j] = x
		);
		return arr;
	}
})(jQuery);

	
