/* -----------------------------------------------------
 *  Contains utility javascripts for handling CSS files 
 * ----------------------------------------------------- */

/**
 * Define unique namespace
 */
if (typeof(css) == "undefined") {
    css = { };
};
if (typeof(css.utils) == "undefined") {
	css.utils = { };
};

/**
 * CssLoader allows you to dynamically load and add a CSS to the document header
 */
css.utils.CssLoader = {

	/**
	 * Static function that loads a CSS from an URL and adds it to the document
	 * header. When a CSS with the same URL is already included in the document
	 * header, the CSS file will not be loaded again. 
	 * When the optional 'media' parameter is obmitted, the CSS is configured 
	 * for all media.
	 */
	load: function (url, /*optional*/ media) {

		// Prevent loading a file already loaded
		var docLinks = document.getElementsByTagName("link");
		if (docLinks.length > 0 && docLinks["href"] == url) {
			return;
		}
		
		// Optional parameters check
		var cssMedia = ( (media === undefined) || (media === null) ) ? "all" : media;
		   
		var newCssLink = document.createElement("link");
		newCssLink.setAttribute("rel", "stylesheet");
		newCssLink.setAttribute("type", "text/css");
		newCssLink.setAttribute("media", cssMedia);
		newCssLink.setAttribute("href", url);
		
		var docHeader = document.getElementsByTagName("head")[0];
		docHeader.appendChild(newCssLink);

	}

};

