
/*

little gallery random image swapper


*/

var tileColors = new Array("blue","yellow","teal","pink","orange");
var numTiles = 21;
var numVisibleImages = 11;
var imagePath = "/sites/all/themes/kfp/gallery-images/";

var tileImages = new Array();
var numImages = 0;
var usedTiles = new Array();
var openTiles = new Array();
var galleryControl = 'play';
var imagePosition = 0;


// fade out the next used tile
function miniGalleryFade() {
	if (usedTiles.length > 0) {
	  $('img',usedTiles[0]).fadeOut(700, function() {
	  	// remove it from the div
	  	$(this).remove();
      });
      // put it back on the open tile stack
      openTiles.push(usedTiles.shift());	
    }
    else {alert('no shufflr tiles found');}
}

// picture appear function
function miniGalleryAppear () {
   // grab the next open tile from the stack
   chosenTile = openTiles.shift();
   // make it invisible
   $(tileImages[imagePosition]).css("display","none");
   // put it in div
   $(chosenTile).html(tileImages[imagePosition]); 
   // fade it in
   $(tileImages[imagePosition]).fadeIn(700, function() { });
   imagePosition++;
   if (imagePosition == tileImages.length) imagePosition = 0;
   // register it in the used tiles stack
   usedTiles.push(chosenTile);
}
  
// advance function  
function miniGalleryNext () {
   if (galleryControl != 'play') return false;
   miniGalleryFade();
   // wait half a second before running this
   setTimeout("miniGalleryAppear()",500);
} 

function miniGalleryShowInit() {
	// create the tiles
	var cIndex = 0;
	for (i=0; i < numTiles; i++) {
		$("<div></div>").appendTo($("#KFPgallery")).addClass('shufflrTile ' + tileColors[cIndex] );
		cIndex++;
		if (cIndex == tileColors.length) {
			cIndex = 0;
			tileColors = $.shuffle(tileColors);
		}
	} 
	 
   // load up the images 
	$.getJSON('/shufflr/shufflr-ajax.php', function(data) {
      $(data).each(function(i,value){
         tileImages[i] = new Image();
         tileImages[i].src = imagePath + value; 
      });
       
      //load the deck with images
      $('.shufflrTile').each(function(i,e){openTiles.push(e)});
      
      
      //remove 6th div from play for KFP
      removedTiles = openTiles.splice(5, 1);
      $(removedTiles).each(function(i,e){
      	   $(e).removeClass("blue yellow teal pink orange");
      	});
      //put the badge in there
      badge = new Image();
      badge.src = "/sites/all/themes/kfp/images/years-of-service.png";
	  $(badge).addClass("specialBadge").click(function(){window.location="/galleries/1"});
      $(removedTiles[0]).html(badge);
      // end KFP mod
      
      // shuffle them
      openTiles = $.shuffle(openTiles);
      
      // deal out a hand
      $(openTiles).each (function(i,e){
         imagePosition = i;
         if (i == numVisibleImages) { return false;}
         $(e).html(tileImages[i]);
         // keep track of which has been used
         usedTiles.push(e);
         // remove used tile from play
         openTiles.splice($.inArray(e, openTiles), 1);
         return true;
      });
	});  // end JSON callback
}

function miniGalleryShowStart() {
	 // go to the next image every 2 seconds
    setInterval("miniGalleryNext()",2000);
}

