/**
*The jquery loading plug-in is a function for displaying an animated image in the browser window during an ajax call.
*It allows you to customize the animation and position in the browser window in one line of code. 
*@requires jQuery
*@namespace jQuery.fn
*@name loading
*@param {String} mode show or hide animation
*@param {Json Object} options An object of customizations 
*@throws {InvalidArgument} If a position is provided that is unavailable this exception is thrown.
*@version 0.2
*@author Wilby C. Jackson Jr. (jacksw02 at gmail dot com)
*@example
*   $(document).ready(function() {
*
*      $.ajaxStart(function() {
*         $("#mydiv").loading("show", {animation: "3D-House"});
*      });
*
*      $.ajaxComplete(function() {
*         $("#mydiv").loading("hide");
*      });
*
*   }); 
*/
(function($) {
	$.fn.loading = function(mode, options) {
		
		//Spelling and Case matter for animation, must match file name.
		//nyi == not yet implemented
		var settings = $.extend({
			"animation": "3D-Globe",
			"ext": ".gif",
			"position": "middle_center",
			"border": false /* nyi */,
			"transparentBG": true /* nyi */,
			"modal" : false /* nyi */,
			"imgHeight": 64 /* nyi */,
			"imgWidth": 64 /* nyi */,
			"zIndex" : 2147483647,
			"show": mode == "show" ? true : false,
			"loadingFolderRelativePath": "loading",
			"parentContainer" : "body" // must be a jQuery selector
		}, options || {});


		var winHeight = $(window).height(),
	    	winWidth = $(window).width(),
		elHeight = $(this).height(),
		elWidth = $(this).contents().width(),
		posHeight = -1, 
		posWidth = -1;
		

		var placement = {			
			// middle is the vertical middle of the window, center is the horizontal center
			// vertical position is always the first part of the function name
			"middle_left" : function(element) {
				//The height was off by approximately .5 inch. Eyebald it for there.
				posHeight = winHeight / 2.05 - elHeight / 2;
				$(element).remove().appendTo(settings.parentContainer).css(
				{"position" : "absolute", "top" : posHeight, "left" : "1em", "zIndex" : settings.zIndex });
			},			

			"middle_center" : function(element) {
				posHeight = winHeight / 2.05 - elHeight / 2;
				posWidth = winWidth / 2 - elWidth / 2;
				$(element).remove().appendTo(settings.parentContainer).css(
				{"position" : "absolute", "top" : posHeight, "left" : posWidth, "zIndex" : settings.zIndex });
			},

			"middle_right" : function(element) {
				posHeight = winHeight / 2.05 - elHeight / 2;
				$(element).remove().appendTo(settings.parentContainer).css(
				{"position" : "absolute", "top" : posHeight, "right" : "1em", "zIndex" : settings.zIndex });
			},

			"top_left" : function(element) {
				$(element).remove().appendTo(settings.parentContainer).css(
				{"position" : "absolute", "top" : "1em", "left" : "1em", "zIndex" : settings.zIndex });
			},

			"top_center" : function(element) {
				posWidth = winWidth / 2 - elWidth / 2;
				$(element).remove().appendTo(settings.parentContainer).css(
				{"position" : "absolute","top" : "1em", "left" : posWidth, "zIndex" : settings.zIndex });
			},

			"top_right" : function(element) {
				$(element).remove().appendTo(settings.parentContainer).css(
				{"position" : "absolute","top" : "1em", "right" : "1em", "zIndex" : settings.zIndex });
			},			

			"bottom_left" : function(element) {
				$(element).remove().appendTo("body").css(
				{"position" : "absolute","bottom" : "1em", "left" : "1em", "zIndex" : settings.zIndex });
			},	
		
			"bottom_center" : function(element) {
				posWidth = winWidth / 2 - elWidth / 2;
				$(element).remove().appendTo(settings.parentContainer).css(
				{"position" : "absolute","bottom" : "1em", "left" : posWidth, "zIndex" : settings.zIndex });
			},

			"bottom_right" : function(element) {
				$(element).remove().appendTo(settings.parentContainer).css(
				{"position" : "absolute","bottom" : "1em", "right" : "1em", "zIndex" : settings.zIndex });
			}
		};
		

		return this.each(function() {    
			if (settings.show == true) {
				if($(this).children().length == 0) {				
					settings.loadingFolderRelativePath = settings.loadingFolderRelativePath.replace(/\/$/, "");
					var img = "<img id='jquery-loading' src='_' alt='Loading...' />";
					img = img.replace("_", settings.loadingFolderRelativePath + "/" + settings.animation + settings.ext);
					$(this).append(img);		
				    	if(placement[settings.position]) {
						placement[settings.position](this);
					}
					else {
						throw new Error("Invalid Argument: The selected position is not available.");
					}
				}
			}
			else {
				$(this).empty();
			}
		});
	};
})(jQuery);
