$(document).ready(function () {

	if ($("body.home").length > 0) {
		rescale();
		$(window).resize(function(){
			rescale();
		});
	}

	$('ul#worklist').hide();
	$('ul#tearsheetslist').hide();

	$('a.openclose').click(function () { 
		$('ul#worklist').slideToggle('fast');
		$(this).toggleClass("active");
		return false;
    });  
    
   	$('a.openclosetear').click(function () { 
		$('ul#tearsheetslist').slideToggle('fast');
		$(this).toggleClass("active");
		return false;
    });   

	$('a.texttoggle').click(function () { 
		//$('#text').slideToggle('fast');
		//$('#text').fadeIn('slow');
		$('.slideshow').toggle();
		$('#text').toggle();
		$(this).toggleClass("active");
		return false;
    }); 


	$('ul#work').cycle({ 
		nowrap: 0,
		fx: 'fade',
		sync:0,
		speed: 700,
		height: 'auto',
		timeout: 0,
		cleartypeNoBg: true,
		after: onAfter,
		prev: 'a.prev',
		next: 'a.next'
	});
	
	/* Fix bug in IE7 with a bit of Jquery, yay! */
	
	var zIndexNumber = 1000;
	$('div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});
	

});

function onAfter(curr,next,opts) {
	var caption = (opts.currSlide + 1) + ' / ' + opts.slideCount;
	$('#counter').html(caption);
}

/* 
	This needs to be changed to this at some point, looks better
	http://buildinternet.com/project/supersized/ 
	Credit for current version: Andrew Fox/fox-land.co.uk	
*/

function rescale() {
	var max_height = $(window).height();
	var max_width = $(window).width();
	var min_width = 960;

	var height = $("#background img").height();
	var width = $("#background img").width();
	var ratio = height/width;
	ratio = max_height / height;
	height = height * ratio;
	width = width * ratio;

	if (width < max_width) {
		ratio = max_width / width;
		height = height * ratio;
		width = width * ratio;
	}

	$("#background img").height(height);
	$("#background img").width(width);
	$("#background").height(max_height);

	// set top/left
	var top = $("#background img").offset().top;
	var left = $("#background img").offset().left;
	top = Math.round((max_height - height)/2);
	left = Math.round((max_width - width)/2);
	$("#background img").css("margin-top",top);
	$("#background img").css("margin-left",left);

}
