/*
File:		loaderStatus.js
This file is used to implement a jQuery loader status plugin

Section:	Version History
03/06/2009 (DJO) - Created File
*/

jQuery.status = {
	// define some internal classes
	Message: function(messageText, options) {
		var fnresult = {}; 
		
		// extend this with the specificed options
		jQuery.extend(options, fnresult);
		
		// set the text
		fnresult.text = messageText;
		
		// define the apply function
		fnresult.apply = function(container) {
			jQuery(container).html("<p>" + this.text + "</p>");
		}; // apply
		
		// return the option
		return fnresult;
	}, // Message
	
	// initialise some constants
	DEFAULT_STATUSOPTS: {
	},

	// the status areas
	containers: [],
	messages:[],
	
	displayStatus: function() {
		if (this.messages.length > 0) {
			// initialise variables
			var message = this.messages[this.messages.length -1];
			
			// apply the message to each container
			this._eachContainer(function(container) {
				message.apply(container);
			});
		}
		else {
			// clear the content of the containers 
			this._eachContainer(function(container) {
				jQuery(container).html("");
			});
		} // if..else
	}, // displayStatus
	
	push: 	function(strMessage, options) {
		// initialise the options with the default options
		jQuery.extend(this.DEFAULT_STATUSOPTS, options);
		
		// create the new message and then display the status
		this.messages.push(new jQuery.status.Message(strMessage, options));
		this.displayStatus();	
	}, // push
	
	pop:		function() {
		// pop the top message and display the status
		this.messages.pop();
		this.displayStatus();
	}, // pop
	
	/* private methods */
	
	_eachContainer: function(f) {
		for (var ii = 0; ii < this.containers.length; ii++) {
			if (f) {
				f(this.containers[ii]);
			} // if
		} // for
	} // eachContainer
}; // statusMessageMgr

jQuery.fn.statusArea = function() {
	return this.each(function() {
		// add the current area to the status areas
		jQuery.status.containers.push(this);
	});
}; // statusArea