/*
 
 -----------------------------------------
   media-manager.js
 -----------------------------------------

 * @version 0.1
 * @author LBi - http://www.lbi.com/en
 * @requires jQuery Core 1.3.2 - http://www.jquery.com/
 * @requires jQuery Validation plugin 1.5.1 - http://plugins.jquery.com/project/validate

*/



/**
 * MediaManager that allows media to be uploaded and edited
 * @namespace MediaManager that allows media to be uploaded and edited
 * @member BTT
 */
BTT.MediaManager = {
	
	// Class required for media manager functionality
	mediaManagerClass: '*.JS_mediaManager',
	
	
	// Setups up functionality for uploading media items from PC
	setupMediaUpload: function() {
		var mediaManager = $(BTT.MediaManager.mediaManagerClass);
	},
	
	
	// Sets up an image cropper
	setupImageCropper: function() {

		// Set variables
		var cropImage = $("img.JS_cropImage"),
			cropPreview = $("img.JS_cropPreview");
	
		// Variables for retrying
		var timesToRetry = 20,
			retryCount = 0;
		
		// Implement cropper functionality
		function setupCropper() {
			
			// If image isn't loaded yet, attempt again in 1 seconds
			if(($(cropImage).width() === 0) && (retryCount < timesToRetry)) {
				
				var attemptSetupCropper = setTimeout(
					function(){
						retryCount = retryCount + 1;
						setupCropper();
					}, 1000 );
				
			}
			
			// If image still isn't loaded after x seconds, abort setup of cropper
			else if( ($(cropImage).width()) === 0) {
				// Give up setting cropper
			}
			
			// Image is loaded: Success! Load image cropper
			else {
				
				var	cropPreviewWidth = cropPreview.parent("*.imagePanel").width() || 0,
					cropPreviewHeight = cropPreview.parent("*.imagePanel").height() || 0;
					
				var originalWidth = Number( $('#sourceWidth').val() ) || 0,
					originalHeight = Number( $('#sourceHeight').val() ) || 0;
					
				var currentWidth = cropImage.width() || 0,
					currentHeight = cropImage.height() || 0;
				
				var cropWidth = Number( $('#cropWidth').val() ) || 0,
					cropHeight = Number( $('#cropHeight').val() ) || 0,
					cropX = (Number( $('#cropX').val() ) / originalWidth) * currentWidth || 0,
					cropY = (Number( $('#cropY').val() ) / originalHeight) * currentHeight || 0,
					cropX2 = (cropWidth / originalWidth) * currentWidth + cropX,
					cropY2 = (cropHeight / originalHeight) * currentHeight + cropY;
					
				var cropCoordinates = [	cropX, cropY, cropX2, cropY2 ];
				
				var targetMinHeight = Number( $('#minCropHeight').val() ),
					targetMinWidth = Number( $('#minCropWidth').val() );
					
				var cropperMinWidth = (targetMinWidth / originalWidth) * currentWidth,
					cropperMinHeight = (targetMinHeight / originalHeight) * currentHeight;
					
				var fixAspectRatio = $('#fixAspectRatio').val(),
					aspectRatio = fixAspectRatio === "true" ? (targetMinWidth / targetMinHeight) : 0;
				

				// Update coordinates & hidden input fields with cropping details
				var updatePage = function (coords, cropPreviewWidth, cropPreviewHeight, imageWidth, imageHeight, originalWidth, originalHeight) {
				
					var previewWidth = cropPreviewWidth / coords.w,
						previewHeight = cropPreviewHeight / coords.h;
					
					var newCropWidth = Math.round( ((coords.x2 - coords.x) / imageWidth) * originalWidth ) || 0,
						newCropHeight = Math.round( ((coords.y2 - coords.y) / imageHeight) * originalHeight ) || 0;
				
					var newX = Math.round( ((coords.x) / imageWidth) * originalWidth ) || 0,
						newY = Math.round( ((coords.y) / imageHeight) * originalHeight ) || 0;
						
					cropPreview.css({
						width: Math.round(previewWidth * imageWidth) + 'px',
						height: Math.round(previewHeight * imageHeight) + 'px',
						marginLeft: '-' + Math.round(previewWidth * coords.x) + 'px',
						marginTop: '-' + Math.round(previewHeight * coords.y) + 'px'
					});
		
					$('#cropX').val( newX );
					$('#cropY').val( newY );
					$('#cropWidth').val( newCropWidth );
					$('#cropHeight').val( newCropHeight );
					
				};
				
				
				// Crop the image
				cropImage.Jcrop({
					aspectRatio:  aspectRatio,
					setSelect:  cropCoordinates,
					minSize:  [ cropperMinWidth, cropperMinHeight ],
					onChange:  function(coords){
									updatePage(coords, cropPreviewWidth, cropPreviewHeight, currentWidth, currentHeight, originalWidth, originalHeight );
								},
					onSelect:  function(coords){
									updatePage(coords, cropPreviewWidth, cropPreviewHeight, currentWidth, currentHeight, originalWidth, originalHeight );
								}
		
				});

					
			}
		
		}
		
		setupCropper();
		
		
	},
	
	
	/**
	 * Sets up the media editor for determining file-upload progress
	 */
	setupFileUpload: function(){
		
		
		
		
	},
	
	
	
	// Setup media manager
	init : function() {
		
		// Only run intialisation code if media manager exists on page
		if ($(BTT.MediaManager.mediaManagerClass).length === 0 ) { return; }

		// Upload functionality
		BTT.MediaManager.setupMediaUpload();
		// Libary functionality
		// Clear functionality
		// Save media functionality
		
		// Cropper functionality
		BTT.MediaManager.setupImageCropper();
	}
	
};



/* Initialise when whole page is loaded
-------------------------------- */
$(document).ready(function(){
	BTT.MediaManager.init();
});

