
	var imageWidth;
	var imageHeight;
	
	function setCoordinates(x,y,w,h)
	{
		$('#xx').val(x);
		$('#yy').val(y);
		$('#ww').val(w);
		$('#hh').val(h);
	}
	
	function updateCoords(c)
	{
		//showPreview(c);
		$('#xx').val(c.x);
		$('#yy').val(c.y);
		$('#ww').val(c.w);
		$('#hh').val(c.h);
	};
	
	
	function checkCoords()
	{
		if (parseInt($('#xx').val())) return true;
		alert('Please select a crop region then press submit.');
		return false;
	};
	
	
	// Our simple event handler, called from onChange and onSelect
	// event handlers, as per the Jcrop invocation above
	function showPreview(coords)
	{
		if (coords.w != 0 && coords.h != 0)
		{
			
			var rx = 115 / coords.w;
			var ry = 150 / coords.h;
			
			jQuery('#preview').css({
				width: Math.round(rx * imageWidth) + 'px',
				height: Math.round(ry * imageHeight) + 'px',
				marginLeft: '-' + Math.round(rx * coords.x) + 'px',
				marginTop: '-' + Math.round(ry * coords.y) + 'px'
			});
			
		}
		
	}
	
	
	function initiateResizer(filename,lImgWidth,lImgHeight){
		
		// initiate date object for caching fix
		date = new Date();
		
		document.getElementById('cropbox').src = filename+'?'+date.getTime();
		document.getElementById('preview').src = filename+'?'+date.getTime();
		
		imageWidth = parseInt(lImgWidth);
		imageHeight = parseInt(lImgHeight);
		
		document.getElementById('cropbox').onload = function(){
			
			document.getElementById('browse-overlay').style.visibility = "hidden";
			document.getElementById('browse').style.visibility = "hidden";
			
			jQuery('#cropbox').Jcrop({
					onChange: showPreview,
					onSelect: updateCoords,
					aspectRatio: 0.77,
					setSelect:   [ 10, 10, 125, 160 ]
			});
			
			setCoordinates(10,10,125,160);
		}
		
		$("#next").fadeIn("slow");
	}
	
	
	function submitToStage5() {
		
		document.getElementById('fileForm').action="?stage=5";
		document.getElementById('fileForm').target="_top";
		document.getElementById('whichstage').value="5";
		document.getElementById('fileForm').submit();
		
	}
	
	
