/** 
 * @fileoverview
 * @author			Michael Ord <michael.ord@think.eu>
 * @version			0.1
 * @class
 * @requires		YAHOO.util.Dom
 * @requires		YAHOO.util.Event
 * 
 * @file			init.js
 * @description		
 */

window.ThinkCo = window.ThinkCo || {};

/**
 * TODO Description
 * @method namespace
 * @param {Object} ns
 */
ThinkCo.namespace = function (ns) {

	if (!ns || !ns.length) {
		return null;
	};

	var levels	= ns.split (".");
	var nsobj	= ThinkCo;

	for (var i = (levels [ 0 ] == "ThinkCo") ? 1 : 0; i < levels.length; ++i) {
		nsobj [ levels [ i ] ] = nsobj [ levels [ i ] ] || {};
		nsobj = nsobj [ levels [ i ] ];
	};

	return nsobj;
};

ThinkCo.namespace ('Enhance');

/**
 * TODO Description
 * @method TH_NK_Enhance
 */
ThinkCo.Enhance = function (styleName) {
	// get all the link elements in the page
	var tmp_link	= document.getElementsByTagName ('link');
	// loop through all the elements
	for (var i = 0; i < tmp_link.length; i++) {
		// quick reference to the element
		var tmp_ref	= tmp_link [ i ];
		// check for attribute
		if (tmp_ref.getAttribute ('rel')) {
			// if the element is a stylesheet
			if (tmp_ref.getAttribute ('rel').toLowerCase () == 'stylesheet') {
				// get the source
				var tmp_src		= tmp_ref.getAttribute ('href');
				// regular expression to strip down the filename
				var tmp_regEx	= /\/([a-zA-Z0-9]+).css/;
				// split the filename based on the regular expression
				var tmp_matches	= tmp_src.match (tmp_regEx);
				// if there are matches
				if (tmp_matches) {
					// create and build a new stylesheet that is used to import the enhanced styles
					var tmp_href	= tmp_ref.getAttribute ('href').replace (tmp_matches [ 1 ], styleName);
					var tmp_el		= YAHOO.util.Dom.create ('link', { rel : 'stylesheet', href : tmp_href, type : 'text/css' });
					var tmp_head	= document.getElementsByTagName ('head') [ 0 ];
					tmp_head.appendChild (tmp_el);
					return;
				};
			};
		};
	};
}

/**
 * add enhance class to the body (which should have the id of 'iRoot')
 */
YAHOO.util.Event.onDOMReady ( function () {
	if (YAHOO.util.Dom.get ('iRoot')) {
		YAHOO.util.Dom.addClass	('iRoot', 'enhance');
	};
});