var gallery = [];
var maxThumbs = 8;
var groupIndex = 0;
var artistID = "";


function gallery_prepare() {
	artistID = $('#main #content').attr('data-artist-id');
	$('#loader').append('<img src="/wp-content/themes/guelphstudiotour/img/loader.gif" height="32" width="32" alt="Loading..." />');
	init_gallery();
}


// Initialize Gallery
function init_gallery() {

	$.getJSON('/wp-content/themes/guelphstudiotour/ajax/ajax-gallery.php', {
		artist_id: artistID,
		num_thumbs: maxThumbs
	},
	function(data) {
		gallery = data;
		populate_thumbs();
		
		if(artistID == undefined){
			var slideshow = setInterval(populate_thumbs, 10000);
		}
	});
}


// Populate Thumbs
function populate_thumbs() {
	var numThumbs = maxThumbs;
	var counter = (groupIndex*maxThumbs);
	var counterEnd = (counter + numThumbs);
	var pos = 0;
	
	if(counterEnd > gallery.length){
		counterEnd = gallery.length;
	}

	$('ul.artist-list li').removeClass('active');
	$('ul.gallery-list').fadeOut('slow', function(){	
		
		$('ul.gallery-list li').empty();
		
		if(numThumbs > 0) {
			if(artistID == undefined){
				for (var i=counter;i<counterEnd;i++) {
					pos++;
					$('ul.gallery-list li.pos-'+pos).append('<a href="'+gallery[i].link+'"><img src="'+gallery[i].thumb+'" width="120" height="120" data-artist-id="'+gallery[i].id+'" data-artist-name="'+gallery[i].name + '" alt="" /></a>');
					$('ul.artist-list li[data-artist-id="'+ gallery[i].id +'"]').addClass('active');
				}
				
				//if less than maxThumbs, fill in with random thumbs
				if((pos < maxThumbs) && (gallery.length > maxThumbs)){
					var randomFiller = shuffle(gallery.slice(0, (gallery.length-pos)));
					var fillerEnd = (maxThumbs-pos);
					
					for(var j=0;j<fillerEnd;j++){
						pos++;
						$('ul.gallery-list li.pos-'+pos).append('<a href="'+randomFiller[j].link+'"><img src="'+randomFiller[j].thumb+'" width="120" height="120" data-artist-id="'+randomFiller[j].id+'" alt="" /></a>');
						$('ul.artist-list li[data-artist-id="'+ randomFiller[j].id +'"]').addClass('active');
					}
				}
			}else{
				for (var i=counter;i<counterEnd;i++) {
					pos++;
					$('ul.gallery-list li.pos-'+pos).append('<a href="'+gallery[i].link+'"><img src="'+gallery[i].thumb+'" width="120" height="120" data-thumb-id="'+gallery[i].id+'" alt="" /></a>');
				}
			}
		}
	});
	
	$('ul.gallery-list').fadeIn('slow');
	$('#loader').html('');

	groupIndex++;
	
	if((groupIndex*maxThumbs) > gallery.length){
		groupIndex = 0;
	}
}


// Randomize Array
/* Code courtesy of Cristoph on Stack Overflow http://stackoverflow.com/questions/962802/is-it-correct-to-use-javascript-array-sort-method-for-shuffling */
function shuffle(array){
    var tmp, current, top = array.length;

    if(top){ 
    	while(--top) {
	        current = Math.floor(Math.random() * (top + 1));
	        tmp = array[current];
	        array[current] = array[top];
	        array[top] = tmp;
    	}
    }

    return array;
}

