// Globals
var imgHovered = new Array();
var anims = 0;

// Init
$(function(){

	var imgCount = $('#gallery li img').size();
	
	// Prep images
	$('#gallery li img').each(
		function(i) {
			
			// Class the images by dimension ratio
			var ratio;
			var img = new Image();
			img.onload = (function (img) { return function () {
				ratio = img.width / img.height;
				if (ratio < 1) {
					$($('#gallery li img')[i]).addClass('a');
				}
				else if (ratio > 2) {
					$($('#gallery li img')[i]).addClass('c');
				}
				else {
					$($('#gallery li img')[i]).addClass('b');
				}
				imgCount--;
				if (imgCount < 1) {
					// Fire away
					playGallery()
				}
			}; })(img); //workaround for Safari stupidity ('this' points to window)
			img.src = $(this).attr('src');
			
			// Bind hover event (deprecated in v3)
			var imgPos;
			$(this).hover(
				function() {
					//$('#gallery .info').html('<img src="'+ $(this).attr('longdesc') +'" title="'+ $(this).attr('title') +'" />');
					imgPos = $(this).parent('li').attr('class');
					imgPos = imgPos.replace(/pos\-/, '');
					imgHovered[imgPos] = $(this).attr('class');
				},
				function() {
					imgHovered = new Array();
				}
			);
			
		}
	);
	
	// Bind shuffle
	$('#gallery .switch a').click(
		function(){
		  if (anims < 1) {
		  	playGallery();
		  }
			this.blur();
		  return false;
		}
	);
	
	// Set interval
	//var gInterval = window.setInterval("playGallery()", 15000);

});

function playGallery() {

	// Generate a batch of images and display
	var positions = new Array();
	var validClass;
	var useClass;
	for (var i = 0; i < 8; i++) {
		
		// Hide img in current position
		$('#gallery li.pos-'+ i).css('display', 'none');
		//$('#gallery li.pos-'+ i).css('display', 'none').clone().prependTo($('#gallery ul')).fadeOut(5000, function(){$(this).remove();});
		
		// Determine a valid class for our current position, var i
		validClass = new Array('a','b','c');
		
		// I know there's a mathematical pattern in here somewhere, but hell with it!
		i = parseInt(i);
		switch (i) {
			case 0:
				if (imgHovered[1]) {validClass = new Array('a','b');}
				if (imgHovered[4]) {validClass = new Array('b','c');}
				break;
			case 1:
				if (positions[0] == 'c') {
				 continue;
				}
				if (imgHovered[2]) {validClass = new Array('a','b');}
				if (imgHovered[5]) {validClass = new Array('b','c');}
				if (imgHovered[4] == 'c') {validClass = new Array('b');}
				break;
			case 2:
				if (positions[1] == 'c') {
				 continue;
				}
				if (imgHovered[3]) {validClass = new Array('a','b');}
				if (imgHovered[6]) {validClass = new Array('b','c');}
				if (imgHovered[5] == 'c') {validClass = new Array('b');}
				break;
			case 3:
				if (positions[2] == 'c') {
				 continue;
				}
				validClass = new Array('a','b');
				if (imgHovered[7] || imgHovered[6] == 'c') {validClass = new Array('b');}
				break;
			case 4:
				if (positions[0] == 'a') {
				 continue;
				}
				validClass = new Array('b','c');
				if (positions[1] == 'a' || imgHovered[5]) {
				 validClass = new Array('b');
				}
				break;
			case 5:
				if (positions[1] == 'a' || positions[4] == 'c') {
				 continue;
				}
				validClass = new Array('b','c');
				if (positions[2] == 'a' || imgHovered[6]) {
				 validClass = new Array('b');
				}
				break;
			case 6:
				if (positions[2] == 'a' || positions[5] == 'c') {
				 continue;
				}
				validClass = new Array('b','c');
				if (positions[3] == 'a' || imgHovered[7]) {
				 validClass = new Array('b');
				}
				break;
			case 7:
				if (positions[3] == 'a' || positions[6] == 'c') {
				 continue;
				}
				validClass = new Array('b');
				break;
		}
		validClass.sort(randOrd);
		useClass = validClass[0];
		
		// Track which class was used
		positions[i] = useClass;
		
		// Get an image of that class and display
		$('#gallery li img.'+ useClass).each(
			function() {
				if ($(this).parent('li').css('display') == 'none') {
					anims++;
					$(this).parent('li').removeClass().addClass('pos-'+ i).fadeIn(randomSpeed(), function(){anims--;});
					return false;
				}
			}
		);
		
	}

}

function randomSpeed() {
	var ranNum = 300 + Math.floor(Math.random()*1200);
  return ranNum;
}

function randOrd(){ return (Math.round(Math.random())-0.5); }