/// <reference path="jquery-vsdoc.js"/>
var litium = {};

litium.cc = {

	log: function() {
		if (window.console && window.console.log) {
			window.console.log(arguments[0]);
		} else if (window.opera) {
			window.opera.postError(arguments[0]);
		} else {
			alert(arguments[0]);
		}
	},

	loader: (function() {
		var defaultShowDelay = 500, // Number of milliseconds to delay the showing with.
			defaultZIndex,
			id = 'loading', // Html id attribute.
			src = '/ui/img/loader/loading.gif',
			showTimer;

		var getLoader = function() {
			return $('#' + id);
		};

		return {
			load: function() {
				/* Creates an element with the specified id (if it doesn't already exist) and appends it to the dom. */
				var $img,
					$loader = getLoader();
				if (!$loader.length) {
					$loader = $(document.createElement('div'));
					$loader.attr('id', id);
					$img = $(document.createElement('img'));
					$img.attr({
						'src': src,
						'alt': ''
					})
					.appendTo($loader);
					$loader.hide().appendTo('body');
					defaultZIndex = $loader.css('z-index');
				}
			},
			show: function(zIndex, delay) {
				// Shows the loader element.
				var $loader = getLoader();
				if (!$loader.length) {
					this.load();
				}
				if (zIndex) {
					$loader.css('z-index', zIndex);
				}
				if (delay <= 0) {
					$loader.show();
				} else {
					showTimer = setTimeout(function() { // Show the image after an initial time period.
						$loader.show();
					}, delay || defaultShowDelay);
				}
			},
			hide: function() {
				// Hides the loader element.
				if (showTimer) {
					clearInterval(showTimer);
				}
				var $loader = getLoader();
				if ($loader.length) {
					$loader.hide();
					if ($loader.css('z-index') !== defaultZIndex) {
						// Reset the default z-index value if another value was set.
						$loader.css('z-index', defaultZIndex);
					}
				}
			}
		};
	})()
};
