﻿/* Plug-in Development pattern tutorial - http://www.learningjquery.com/2007/10/a-plugin-development-pattern */

/* 
* Progress Panel jQuery plug-in
* 
* Usage:
*
* class="progresspanel { showDelay: 0, hideTimeout: 20000, position: 'middle' }"
* class="progresspanel"
* AND
* $('.progresspanel').progresspanel();
*
* OR
* $('#linkID').progresspanel({ showDelay: 0, hideTimeout: 20000, position: 'middle'	});
* $('#linkID').progresspanel();
*
* Parameter Defaults:
*
*	showDelay: 0 - the delay in milliseconds before showing the panel 
*	hideTimeout: 20000 - the timeout in milliseconds before hiding the panel
*	position: 'middle' - the placement of the panel: 'middle' of the page, 'left' of the control, 'right' of the control 
*
*/

//
// create closure
//
(function($) {
	//
	// plugin definition
	//
	$.fn.progresspanel = function(options) {

		// Extend our default options with those provided.
		// Note that the first arg to extend is an empty object -
		// this is to keep from overriding our "defaults" object.
		var opts = $.extend({}, $.fn.progresspanel.defaults, options);

		// Support the Metadata Plugin
		// <div class="progresspanel {showDelay: 0, hideTimeout: 20000, position: 'middle' }">
		return this.each(function() {
			$(this).click(function(event) {
				// Prevent double submit
				if (this.beenSubmitted == true) {
					return false;
				} else {
					this.beenSubmitted = true;
				}
				var $this = $(this);
				// build element specific options
				var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
				window.setTimeout(function() { showProgressPanel(event, o.hideTimeout, o.position) }, o.showDelay);
			});
		});
	};

	function showProgressPanel(event, hideTimeout, relativePosition) {

		// Check if the Page has Validation on it
		if (typeof (Page_IsValid) != 'undefined') {
			// If the Page isn't Valid, we will not show the Progress Panel
			if (typeof (Page_ClientValidate) == 'function') {
				var oldPage_IsValid = Page_IsValid;
				var oldPage_BlockSubmit = Page_BlockSubmit;
				if (Page_ClientValidate() == false) {
					Page_IsValid = oldPage_IsValid;
					Page_BlockSubmit = oldPage_BlockSubmit;
					return;
				}
			}
		}

		// change the cursor to a wait cursor
		$('body').css("cursor", "progress");

		// Disable the element so it cannot be clicked again
		$(event.target).attr('disabled', 'disabled');

		// get element position
		var position = $(event.target).offset();
		var elementWidth = $(event.target).width();

		if (relativePosition === undefined) {
			relativePosition = 'middle';
		}

		// position image
		switch (relativePosition) {
			case 'right':
				$('#progressIndicator').css({ top: position.top - 15 + "px", left: position.left + elementWidth + 30 + "px" }).show();
				break;
			case 'left':
				$('#progressIndicator').css({ top: position.top - 15 + "px", left: position.left - elementWidth - 220 + "px" }).show();
				break;
			case 'middle':
			default:
				$('#progressIndicator').css({ position: "fixed", top: "50%", left: "50%" }).show();
		}
		window.setTimeout(function() { hideProgressPanel(event.target) }, hideTimeout);  // handles hiding the progress panel should the operation time out		
	};

	function hideProgressPanel(srcElement) {
		$('#progressIndicator').hide();
		$('body').css("cursor", "auto");
		// Enable the element so it can be clicked again
		$(srcElement).attr('disabled', '');
		srcElement.beenSubmitted = false;
	};

	// plugin defaults - added as a property on our plugin function
	$.fn.progresspanel.defaults = {
		showDelay: 0, /* the delay in milliseconds before showing the panel */
		hideTimeout: 20000, /* the timeout in milliseconds before hiding the panel */
		position: 'middle' /* the placement of the panel: 'middle' of the page, 'left' of the control, 'right' of the control */
	};

	// end of closure
})(jQuery);
