/*
 * Functions that work off of a set of jQuery elements must be defined off of the $.fn variable.
 * i.e. $.fn.pianoKey = function()...
 * This would be called from a selector such as $("tr").pianoKey();
 * 
 * Custom jQuery functions that do not work should be defined directly off of the jQuery object.
 * i.e. $.lockPanel(); 
 */
 
(function($){
	/**
	 * Returns the (numeric) id that was attached to an id attribute. We typically put the numeric id
	 * after a string and underscore in the id attribute, which looks like id="contact_3".
	 * This is really just a shortcut for $("selector").attr("id").split("_")[1]
	 * @param int index Pass an index to use after the split instead of the default value 1
	 */
	$.fn.id = function(index){
		if(typeof index == "undefined")
			index = 1;
		return this.attr("id").split("_")[index];
	};
	
	$.fn.pianoKey = function(oddClass, evenClass){
		// use odd and even as default class names if none were specified
		if(typeof oddClass == "undefined")
			oddClass = "odd";
		if(typeof evenClass == "undefined")
			evenClass = "even";

		// re-class all the elements
		this.each(function(i){
			$(this).removeClass(oddClass).removeClass(evenClass).addClass(i % 2 ? oddClass : evenClass);
		});
	};

	/**
	 * Covers the screen with a lovely white overlay. Useful as a loading/working screen.
	 */
	$.lockPanel = function(callback){
		if(typeof $("#lockedOverlay").attr("id") != "undefined")
			return;

		$("body").append('<div id="lockedOverlay"><\/div>');
		$("#lockedOverlay").css({
			position: "absolute",
			top: 0,
			left: 0,
			width: "100%",
			height: $("body").outerHeight(),
			background: "#FFF",
			opacity: 0,
			zIndex: 500
		}).animate({
			opacity: 0.75
		}, 250, function(){
			if(typeof callback == "function")
				callback();
		});
	};//lockPanel
	$.unlockPanel = function(callback){
		if(typeof $("#lockedOverlay").attr("id") == "undefined")
			return;

		$("#lockedOverlay").animate({
			opacity : 0
		}, 250, function(){
			$(this).remove();
			if(typeof callback == "function")
				callback();
		});
	};//unlockPanel

	$.alert = function(p1, pColor, pTimeout){
		
		// support for passing parameters in via an object
		var text = "";
		if(typeof p1 == "object"){
			text = p1.text;
			pColor = p1.color;
			pTimeout = p1.timeout;
			pCallback = p1.callback;
		}
		else
		{
			text = p1;
		}
		
		// First off, if the text is left blank, just attempt to clear an alert
		if (typeof text == "undefined") {
			$("#fnAlert").remove();
			$.unlockPanel();
			return;
		}

		// If there's already an alert on the screen, swap it out
		if (typeof $("#fnAlert").attr("id") != "undefined") {
			$("#fnAlert").remove();
			$.alert(text, pColor, pTimeout);
			return;
		}

		/**
		 * Data grooming begins.
		 */

		// Establish our defaults
		color = "#555";
		timeout = null;
		callback = null;

		// Figure out what the color parameter is doing... If the color is indeed a color, swap it out
		if (typeof pColor == "string" && pColor.match(/^#[0-9a-f]{3}$|^#[0-9a-f]{6}$/i))
			color = pColor;
		// Otherwise if a timeout was used instead of color
		else if (typeof pColor == "number")
			timeout = pColor;

		// If we were given a timeout parameter, and if the timeout looks good, use that
		if (typeof pTimeout == "number")
			timeout = pTimeout;

		if (typeof pCallback == "function")
			callback = pCallback;

		/**
		 *  From this point on we will ONLY USE the groomed data
		 */
		// If we've don't get have a locked screen, lock it then create the alert
		if (typeof $("#lockedOverlay").attr("id") == "undefined") 
			$.lockPanel(function(){
				$._alertCreate(text, color, timeout, callback);
			});
		// Otherwise just create the alert like normal
		else
			$._alertCreate(text, color, timeout, callback);
	};//alert

	$.alertPosition = function(){
		$("#fnAlert").css({
			top: (Math.floor($(window).height() / 2) - Math.floor($("#fnAlert").outerHeight() / 2)),
			left: (Math.floor($(window).width() / 2) - Math.floor($("#fnAlert").outerWidth() / 2))
		});
	};//alertPosition

	$._alertCreate = function(text, color, timeout, callback){
		// This should ONLY be fed good information. The grooming happens in $.alert
		$("body").append('<div id="fnAlert" style="display: inline; position: fixed; padding: 30px; border: 2px #AAA solid; font-size: 16pt; color: ' + color + '; text-align: center; background: #FFF; z-index: 501;">' + text + '<\/div>');

		$.alertPosition();

		// Check the timeout one more time just in case
		if(timeout != null && typeof timeout == "number")
			setTimeout(function(){
				if(typeof callback == "function")
					callback();
				else
					$.alert();
			}, timeout);
	};//_alertCreate

	/**
	 * Change 1000000 to 1,000,000
	 */
	$.numberFormat = function(num){
		return (num + "").split("").reverse().join("").match(/.{1,3}/g).join(",").split("").reverse().join("");
	}

})(jQuery);
