function adjustImage(effect) {
	var winWidth = $(window).width();
	var winHeight = $(window).height();
	var imgWidth = $('#mainImage').width();
	var imgHeight = $('#mainImage').height();
	//window.status = winWidth + ' : ' + imgWidth + ' | ' + winHeight + ' : ' + imgHeight + ' | ' ;
	if (winWidth / winHeight > imgWidth / imgHeight) {
		var newHeight = (winWidth / imgWidth) * imgHeight;
		var newWidth = winWidth;
	} else {
		var newHeight = winHeight;
		var newWidth = (winHeight / imgHeight) * imgWidth;
	}
	$('#mainImage').css({top: (winHeight-newHeight)/2, left: (winWidth-newWidth)/2, height: newHeight, width: newWidth});
	$('#mainImageContainer').hide().css('visibility', 'visible').show();
	$('#loader').hide();
	if (effect) setTimeout('fadeIn()', 500);
	else $('.class').hide();
}

function fadeOut() {
	$('#mainImage').hide();
	return;
}

function fadeIn() {
	$('#mainImage').show();
}

$(document).ready(function() {

	$('a.next,a.prev,#photoThumbs a.image').click(function() {
		if (this.href.indexOf('editphoto') > 0) return true;
		var url = this.href + '?ajax=1';
		fadeOut();
		$('#loader').show();
		jQuery.getScript(url);
		return false;
	});
	
	$('#mainImage').hide();
	$('#mainImage').load(function() { adjustImage(true) });
	$(window).resize(function() { adjustImage(false) });
	adjustImage(true);

	$('#infoBox').bind('drag', function(event) {
		$(this).css({top:event.offsetY, left:event.offsetX }); 
	}); 	

	$('#infoBox').bind('dragstart', function(event) {
		$(this).css({opacity: 0.5});
	}); 	

	$('#infoBox').bind('mouseup', function(event) {
		$(this).css({opacity: 1});
	}); 
	
	$('#infoButton').click(function() {
		if ($('#infoBox').width() == 16) {
			var box = $('#infoBox').get(0);
			$('#infoBox').animate({
					width: box.saveWidth,
					height: box.saveHeight,
					top: box.saveOffset.top,
					left: box.saveOffset.left,
					opacity: 1
				}, 500, 'swing', function() { $('#infoButton').hide() });
		}
		return false;
	});

	$('#infoBox a.close').click(function() {
		var i = $('#infoButton').show().offset();
		var box = $('#infoBox').get(0);
		box.saveOffset = $('#infoBox').offset();
		box.saveWidth = $('#infoBox').width(); 
		box.saveHeight = $('#infoBox').height(); 
		$('#infoBox').css('top', box.saveOffset.top).css('left', box.saveOffset.left).animate({
				width: 16,
				height: 16,
				top: i.top,
				left: i.left,
				opacity: 0
			}, 1500, 'swing', function() {
				$('#infoButton').fadeOut(150, function() {
					$('#infoButton').fadeIn(150, function() {
						$('#infoButton').fadeOut(150, function() {
							$('#infoButton').fadeIn(150, function() {
							});
						});
					});
				});
			}
		);
		return false;
	});
	
	$('#portfolio div.item div.border,#photoThumbs div.smallItem div.border').hover(function() {
		$(this).css('background', '#FFC52F');
	}, function() {
		$(this).css('background', 'transparent');
	});
});

