/*************************************************************************/
/* This script is responsible for handling all Customer Action Shot image
 * uploads.  When an <a> element having the class 'triggersCustomerActionShotsModal'
 * is clicked, its 'rel' and 'description' attribute values are parsed and
 * a dialog box is displayed containing an upload panel.
 *
 * See also: plupload (http://www.plupload.com/)
 */
/*************************************************************************/


/*=========================================================*/
/* DOMReady ===============================================*/
/*=========================================================*/
jQuery(document).ready(function($) {
	initActionShotBindings($); // Init UI event bindings (close, open, submit)
});
/*=========================================================*/








/*=========================================================*/
/* initBindings ===========================================*/
/*=========================================================*/
function initActionShotBindings($)
{

	/*---------------------------------------*/
	// Configure Uploader
	/*---------------------------------------*/
	// Fetch the SKU from the 'rel' attr of the '.triggersCustomerActionShotsModal'
	// <a> element which is located next to or near the main product image itself
	// on the product page.
	var associatedProductSKU = $(".triggersCustomerActionShotsModal").attr("rel");

	if (! associatedProductSKU) {
		return;
	}

	var uploader = $("#actionShotsUploader").pluploadQueue({
		// General settings
		runtimes:			"flash,html5",	// gears,flash,silverlight,browserplus,html5,html4"
		url:				"/service/CustomerActionShots/uploadImage.php?associatedProductSKU=" + associatedProductSKU,
		container:			"customerActionShotsModal", // ID
		max_file_size:		"3mb",
		//chunk_size:			"1mb",
		unique_names:		true,

		// Specify what files to browse for
		filters: [
			{ title : "Image files", extensions : "jpg,jpeg,gif,png"}
		],

		headers: {
			"X-Uploaded-By": "PLUpload"
		},

		// Flash settings
		flash_swf_url:		"/skin/frontend/FactoryDirectParty/main/js/plupload/plupload.flash.swf",

		// Silverlight settings
		silverlight_xap_url: "/skin/frontend/FactoryDirectParty/main/js/plupload/plupload.silverlight.xap"
	});

	uploader.init();
	// end configure uploader
	/*---------------------------------------*/




	/*---------------------------------------*/
	/* Bind: Click: Open Modal --------------*/
	/*---------------------------------------*/
	jQuery(".triggersCustomerActionShotsModal").click(function() {

		var productDescription	= jQuery(this).attr("description");
		//var productSKU			= jQuery(this).attr("rel");
		var modalContainer		= jQuery("#customerActionShotsModal");
		var centerCoordinates	= getViewportCenterCoordinatesForElement(modalContainer);

		// Set modal's product name and SKU
		jQuery("#customerActionShotsModal").find(".productDescription").text(productDescription);

		jQuery("#customerActionShotsModal").css("left", centerCoordinates.x);
		jQuery("#customerActionShotsModal").fadeIn("slow");
	});




	/*---------------------------------------*/
	/* Bind: Submit: Upload queued images ---*/
	/*---------------------------------------*/
	jQuery("#customerActionShotsModal form").submit(function(event) {
		event.stopPropagation();
		event.preventDefault();

		var uploader = jQuery("#actionShotsUploader").pluploadQueue();

		/*---------------------------------------*/
		// Start Async Upload
		/*---------------------------------------*/
		/* Validate number of uploaded files. If
		** there are files still in the queue,
		** upload them first.
		*/
		if (uploader.total.uploaded == 0) {
			if (uploader.files.length > 0) {

				/*---------------------------------------*/
				// Bind: UploadComplete
				/*---------------------------------------*/
				uploader.bind("UploadComplete", function(up, files) {
					onUploadSuccess();
				});

				/*---------------------------------------*/
				// Bind: Error
				/*---------------------------------------*/
				uploader.bind("Error", function(up, error) {
					up.refresh();
					jQuery("#actionShotsUploadStatusContainer").text("An error occurred during your upload (" + error.message + ") and we're truly sorry about that.  Please try uploading again later.");
				});


				// Trigger upload; disable Upload button during upload.
				uploader.start();
				jQuery(".defaultButton").attr("disabled", true);
			}
			else {
				alert("You must add at least one file before you can upload.");
			}
		}
		// end upload
		/*---------------------------------------*/


		return(false);
	});
	// end Submit




	/*---------------------------------------*/
	/* Bind: Click: Close modal -------------*/
	/*---------------------------------------*/
	jQuery("#customerActionShotsModal .triggersClose").click(function(event) {
		event.preventDefault();
		event.stopPropagation();

		/*
		var uploader = jQuery("#actionShotsUploader").pluploadQueue();
		uploader.stop();
		uploader.destroy();
		*/

		jQuery("#customerActionShotsModal").fadeOut("fast");
	});
}
// end initBindings
/*=========================================================*/







/*=========================================================*/
/* onUploadSuccess ========================================*/
/*=========================================================*/
function onUploadSuccess()
{
	jQuery(".cancelButton").attr("disabled", true);
	jQuery(".defaultButton").attr("disabled", true);

	jQuery("#actionShotsUploadStatusContainer #actionShotUploadSuccessMessage").show();
	//jQuery("#actionShotsUploadStatusContainer").html("Your upload was successful and will be reviewed for quality purposes before being made visible on this site.  Thanks so much for contributing!  <a href='#' class='triggersClose'>Click to return to the site</a>");
}
/*=========================================================*/




/*=========================================================*/
/* onUploadError ==========================================*/
/*=========================================================*/
function onUploadError()
{
}
/*=========================================================*/




/*=========================================================*/
/* getViewportCenterCoordinatesForElement =================*/
/*=========================================================*/
function getViewportCenterCoordinatesForElement(element)
{
	var elementDimensions = {};
		elementDimensions.width = jQuery(element).outerWidth();
		elementDimensions.height = jQuery(element).outerHeight();

	var viewportDimensions = {};
		viewportDimensions.width = jQuery(window).width();
		viewportDimensions.height = jQuery(window).height();

	var centerCoordinates = {};
		centerCoordinates.x = (viewportDimensions.width / 2) - (elementDimensions.width / 2);
		centerCoordinates.y = (viewportDimensions.height / 2) - (elementDimensions.height / 2);


	return(centerCoordinates);
}
/*=========================================================*/




