// Sitewide indicators: error messages, loading indicators, etc.
cnp.util.indicator = {
	image : {
		location : "/i/production/indicators/",
		loading : {
			spinner : {
				circles : "ICN_spinner_circles.gif",
				dashes : "ICN_spinner_dashes.gif",
				arrows : "ICN_spinner_arrows.gif",
				line : "ICN_spinner_line.gif"
			}
		}
	},
	
	/**
	 * Loading indicator constructor
	 * 
	 * @constructor
	 * @param {Object_Literal} params	{type}
	 * 
	 */
	Loading : function(params){
		// Private vars
		var _pageElement, 
			_indicator;
		
		this.setElement = function(element) {
			_pageElement = element;
			return this;
		}
		this.getElement = function() {
			return _pageElement;
		}
		this.setIndicator = function(type) {
			var indicatorRoot = cnp.util.indicator.image;
			_indicator = jQuery("<img src='" + indicatorRoot.location + indicatorRoot.loading.spinner.circles + "'/>");
			return this;
		}
		this.getIndicator = function() {
			return _indicator;
		}
		this.draw = function() {
			(function($, indicator){
				$(indicator.getElement()).html(indicator.getIndicator());
			})(jQuery, this);
			return this;
		}
		this.erase = function() {
			(function($, indicator){
				$(indicator.getIndicator()).remove();
			})(jQuery, this);
			return this;
		}
		
		this.setIndicator();
		
	}
};

// AHAH object; processes XHR request and directly injects HTML snippet into given DOM element
cnp.util.AHAH = {
	/**
	 * AHAH Loader constructor
	 * 
	 * @constructor
	 * @param {Object_Literal} params	{location, element}
	 * 
	 */
	Loader : function(params) {
		// Private vars
		var _contentLocation,
			_pageElement;
		
		// Privileged methods
		this.setLocation = function(location) {
			_contentLocation = location;
			return this;
		}
		this.getLocation = function() {
			return _contentLocation;
		}
		
		this.setElement = function(element) {
			_pageElement = element;
			return this;
		}
		this.getElement = function() {
			return _pageElement;
		}
		
		this.load = function() {
			(function($, loader) {
				new cnp.util.indicator.Loading().setElement(loader.getElement()).draw();
				$.ajax({
					type : "GET",
					url : loader.getLocation(),
					dataType : "html",
					success : function(data) {
						$(loader.getElement()).html(data);
					},
					error : function(xhr, errorMsg, exceptionObj) {
						loader.getElement().html("<!-- * * * Error loading content * * * -->");
					}
				})
			})(jQuery, this);
			return this;
		}
		
		// Init
		if (params) {
			if (params.location) this.setLocation(params.location);
			if (params.element) this.setElement(params.element);
		}
	}
};