//function showPreview(coords, width, height) {
//	var rx = 100 / coords.w;
//	var ry = 100 / coords.h;
//	
//	$('#preview').css({
//		width: Math.round(rx * width) + 'px',
//		height: Math.round(ry * height) + 'px',
//		marginLeft: '-' + Math.round(rx * coords.x) + 'px',
//		marginTop: '-' + Math.round(ry * coords.y) + 'px'
//	});
//}

function updateCoords(c)
{
	$('#x').val(c.x);
	$('#y').val(c.y);
	$('#w').val(c.w);
	$('#h').val(c.h);
	
//	// Crop preview
//	var width = $('#img_width').val();
//	var height = $('#img_height').val();
//	showPreview(c, width, height);
};

$(document).ready( function() {
	$('#cropbox').Jcrop({
		onChange: updateCoords,
		onSelect: updateCoords,
		aspectRatio: 1
	});
});

// Upload form
function uploadCheck(form) {
	var image = form.image.value;
	
	// File field empty
	if(trim(image) == "") {
		$('.error').hide();
		$('.upload_error').fadeOut('slow', function() {
			$(this).html('Please enter a file.').fadeIn('slow')
		});
		return false;	
	}
	
	return true;
}

// Crop image form
function cropCheck(form) {
	var image = form.image.value;
	var user = form.user.value;
	var x = form.x.value;
	var y = form.y.value;
	var w = form.w.value;
	var h = form.h.value;
	
	// Crop fields empty
	if(x == "") {
		$('.crop_error').fadeOut('slow', function() {
			$(this).html('Please select a crop region.').fadeIn('slow')
		});
		return false;
	
	// Success!
	} else {
		$.post('/actions/crop-image.php',
			{image: image, user: user, x: x, y: y, w: w, h: h},
			function(data) {
				
				// Crop error
				if (data == 'fail_crop') {
					$('.crop_error').fadeOut('slow', function() {
						$(this).html('There was an error cropping the image, please try again.').fadeIn('slow')
					});
					return false;
				
				// Successful image crop!
				} else {
					document.location = data;
				}
			});
	}
	
	return false;
}

// White Space!!!
function trim(s){
	return s.replace(/^\s*(.*?)\s*$/,"$1");
}

