/* JavaScript
 */
/* Variables */
var gutter = 6;


/* Functions */
// Loads an image from the list
function loadAnImage() {
	if (IMGS.length > 0) {
		// Get details
		var data = IMGS.shift();
		var img = new Image();
		
		// Create the container for the image
		$('#pieces').append('<li class="focus" style="height:' +data['h']+ 'px;"></li>');
		
		// Load event
		$(img).load(function () {
			// Remove any placeholder
			$('#pieces .placeholder').remove();
			// Set the image attributes
			$(this).attr('width', data['w']);
			$(this).attr('height', data['h']);
			// Set the image to hidden by default
			$(this).hide();
			// Add image to current list item
			$('#pieces .focus').append(this);
			$('#pieces .focus').removeClass('focus');
			// Once done, fadein
			$(this).fadeIn("slow", function() {
				loadAnImage();
			});
		});
		$(img).attr('src', data['s']);
	}
}


/* Setup */
$(document).ready(function() {
	
	// Menu
	$('.work > li > a').click(function() {
		if ($('ul.open').parent().attr('id') != $(this).parent().attr('id')) {
			$('ul.open').slideToggle(400, 'easeOutExpo');
			$('ul.open').toggleClass('open');
		}
		$(this).next().slideToggle(400, 'easeOutExpo');
		$(this).next().toggleClass('open');
		return false;
	});
	
	// Information: Hide
	$('.project #info').hide();
	
	// Information: Events
	$('.project #pieces').mouseenter(function() {
		// Display info
		$(this).prev().fadeIn(300, 'easeOutQuad');
		
		// Hide again
		$(this).prev().mouseleave(function() {
			$(this).fadeOut(200, 'easeOutQuad');
		});
	});
	
	// Project: Load Images
	if (typeof(IMGS) != 'undefined') {
		// Update info panel height
		var areaheight = -gutter;
		var i=0; while(i < IMGS.length) {
			areaheight += IMGS[i++]['h'] + gutter;
		}
		$('.project #pieces').height(areaheight);
		$('.project #info').height(areaheight);
		
		// Load the first image
		loadAnImage();
	} else {
		var areaheight = $('#pieces li img').attr('height');
		$('#pieces').height(areaheight);
		$('#info').height(areaheight);
	}
	
	// Project: Preload Title
	$('.project h2').each(function() {
		var expr = new RegExp("\/media\/work\/(.*)\/_title.png");
		var match = expr.exec($(this).attr('style'));
		jQuery("<img>").attr("src", match[0]);
	});
	
	// IE Fixes
	if(jQuery.browser.msie) {
		$('.col:last-child').addClass('last-child');
	}
	
});