function resize_image() {
	var win_width = $(window).width();
	var win_height = $(window).height();
	var win_aspect_ratio = win_width / win_height;

	current_width = $('#image img').width();
	pic_width =  $('#image img').attr('startwidth');

	current_height = $('#image img').height();
	pic_height =  $('#image img').attr('startheight');
	
	pic_aspect_ratio = pic_width / pic_height;
	
	if (win_aspect_ratio > pic_aspect_ratio) {
		// scale pic to fit window width
		var new_height = (win_width / pic_width) * pic_height;
		var new_width = win_width;
	} else {
		// scale pic to fit window height		
		var new_height = win_height;
		var new_width = (win_height / pic_height) * pic_width;
	}
	
	// center image
	new_top = 0 - ((new_height - win_height) / 2);
	new_left =  0 - ((new_width - win_width) / 2);
//	alert('new_top: ' + new_top + 'new_left: ' + new_left);
	$('#image img').css({top: new_top, left: new_left});

	$('#image img').css({height: new_height, width: new_width});

	
	$('#image').css({visibility:"visible", display:"inline"});
	
	
}
function interact() {
	//Fix height of hidden divs
	$(".toggle .section").each(function(){
    	$(this).css("height", $(this).height() + "px");
		$(this).hide();
    });
	// Remove dotted line in IE
	$('a').click(function() {
  		this.blur();
	});
	$('#title a').click(function() {
		$('#content').slideToggle();
		return false;
	});
	$('#arrows div').hover(
		function() {
			$(this).css('background-position','0 -16px');
		},
		function() {
			$(this).css('background-position','0 0');
		}
	);
	$('.portfolio li a').preload();
	// Set Link Click Behaviour
	$('.toggle h2').click(function() {
		var checkElement = $(this).next();
		if (checkElement.is(':visible')) {
			checkElement.removeClass('active').slideUp();
		} else {
			$('.toggle .active').removeClass('active').slideUp()
			checkElement.addClass('active').slideDown();
		}
		return false;
	});
	$('.portfolio li a').click(function() {
		$('.portfolio li').removeClass('current');
		$(this).parent().addClass('current');
		$('#image').css({visibility:"hidden"});
		$('#image img').attr('src', $(this).attr('href')).attr('startwidth',$(this).attr('newWidth')).attr('startheight',$(this).attr('newHeight'));
		resize_image();
		$('#caption').html($('img', this).attr('alt').replace(/\n/g,"<br /> \n"));
		return false;
	});
	$('#arrows #next, #image img').click(function() {
		next = $('.portfolio li.current').next();
		$('.portfolio li').removeClass('current');
		if ($(next).length <= 0) {
			next = $('.portfolio li:first')
		}
		$(next).addClass('current');
		$('#image').css({visibility:"hidden"});
		$('#image img').attr('src', $("a", next).attr('href')).attr('startwidth',$("a", next).attr('newWidth')).attr('startheight',$("a", next).attr('newHeight'));
		resize_image();
		$('#caption').html($('img', next).attr('alt').replace(/\n/g,"<br /> \n"));
		return false;
	});
	$('#arrows #previous').click(function() {
		prev = $('.portfolio li.current').prev();
		$('.portfolio li').removeClass('current');
		if ($(prev).length <= 0) {
			prev = $('.portfolio li:last')
		}
		$(prev).addClass('current');
		$('#image img').attr('src', $("a", prev).attr('href')).attr('startwidth',$("a", prev).attr('newWidth')).attr('startheight',$("a", prev).attr('newHeight'));
		resize_image();
		$('#caption').html($('img', prev).attr('alt').replace(/\n/g,"<br /> \n"));
		return false;
	});
}
$(window).resize(function(){
	resize_image();
 });
$(document).ready(function() {
	interact();
	resize_image();
});