
var objCropper = null; // object for cropper
var sCurrentImageSrc = "";

var HiddenCropValuesId = ""; // needs to be global so Cropper can access it in onEndCrop

var StageCropMinWidth = 385;
var StageCropTopButtonsHeight = 44;
var CropWidth = 288;
var CropHeight = 216;
var LoadingImageUrl = "../app_themes/register/images/petprofile/loader.gif";

function Reset(imgCroppedId)
{
	$(imgCroppedId).src = "";
}

// called from Flash: The upload starts
function OnStartUpload(imgCroppedId, errorMessageId)
{
	// Clear the error message
	$(errorMessageId).innerHTML = "";		

	if(sCurrentImageSrc == "")
		sCurrentImageSrc = $(imgCroppedId).src;
		
	// Show Load
	$(imgCroppedId).src = LoadingImageUrl + "?" + Math.random();	
}


// called from Flash: The upload completes
function OnEndUpload(imageUploaded, imageUploadedWidth, imageUploadedHeight, hfImageInfoId, hfCropValuesId, imgCropStageId, cropPopupId)
{	
	sCurrentImageSrc = "";

	HiddenCropValuesId = hfCropValuesId;	
	
	// Show the crop popup
	$(cropPopupId).style.display = "block";		
	
	// Show the image in the cropping window 
	ShowImageUploaded(imageUploaded, imageUploadedWidth, imageUploadedHeight, imgCropStageId);
	
	// Set the Image name, width and height returned from flash
	$(hfImageInfoId).value = imageUploaded + "," + imageUploadedWidth + "," + imageUploadedHeight;			
		
	objCropper = new Cropper.Img(imgCropStageId, { ratioDim:{x:CropWidth,y:CropHeight}, displayOnInit: true, onEndCrop: OnEndCrop } ); 
}    	

// called from Flash in case there's an error (file's too big)
function OnError(imgCroppedId, errorMessageId)
{
	$(imgCroppedId).src = sCurrentImageSrc;		
	$(errorMessageId).innerHTML = "The file is too big";		
}	

function OnEndCrop(coords, dimensions) 
{		
	$(HiddenCropValuesId).value = coords.x1 + "," + coords.y1 + "," + dimensions.width + "," + dimensions.height;
}
	
function OnEndCropSubmit()
{
	if(objCropper != null)
		objCropper.remove();
}		
	
function ShowImageUploaded(imageUploaded, imageUploadedWidth, imageUploadedHeight, imgCropStageId)
{
	imageUploadedWidth = parseInt(imageUploadedWidth);
	imageUploadedHeight = parseInt(imageUploadedHeight);

	// Show the image (make sure it doesn't get cached)
	$(imgCropStageId).src = imageUploaded + "?" + Math.random();
	
	var iStageWidth = $(imgCropStageId).width; // stage width
	var iStageHeight = $(imgCropStageId).height; // stage height

	// Scale the image to fit
	var iScale = 0;	
	var iScaleX = iStageWidth / imageUploadedWidth;
	var iScaleY = iStageHeight / imageUploadedHeight;
	
	// Take the smallest scale
	if(iScaleY < iScaleX)
		iScale = iScaleY;
	else
		iScale = iScaleX;		
	
	 // Set the image to fit into the popup	 
	$(imgCropStageId).width = imageUploadedWidth * iScale;			
	$(imgCropStageId).height = imageUploadedHeight * iScale;	
}		

function CancelCropPopup(cropPopupId, btnDeleteId, flashContainerId)
{
	if(objCropper != null)
		objCropper.remove();

	// Hide the crop popup
	$(cropPopupId).style.display = "none";		

	// hide the delete button
	$(btnDeleteId).style.display = "none";		

	// show the flash upload
	$(flashContainerId).style.display = "block";
}	
