/** Telia Core
 * @author Jon Ege Ronnenberg (joro32)
 */
Telia = function($){
	/* An Array of functions to initialize */
	var _initList = [];
	/* A boolean value indicating wheather or not to log load time ect. */
	var _log = false;
	var _cssList = [], _jsList = [];

	/**
	 * Checks the browser and adds classes to the body to reflect it.
	 */
	var browserDetect = function(){		
		var userAgent = navigator.userAgent.toLowerCase();
		$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
		
		// Is this a version of IE?
		if ($.browser.msie) {
			$('body').addClass('browserIE');
			
			// Add the version number
			$('body').addClass('browserIE' + $.browser.version.substring(0, 1));
		}		
		
		// Is this a version of Chrome?
		if ($.browser.chrome) {
		
			$('body').addClass('browserChrome');
			
			//Add the version number
			userAgent = userAgent.substring(userAgent.indexOf('chrome/') + 7);
			userAgent = userAgent.substring(0, 1);
			$('body').addClass('browserChrome' + userAgent);
			
			// If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
			$.browser.safari = false;
		}
		
		// Is this a version of Safari?
		if ($.browser.safari) {
			$('body').addClass('browserSafari');
			
			// Add the version number
			userAgent = userAgent.substring(userAgent.indexOf('version/') + 8);
			userAgent = userAgent.substring(0, 1);
			$('body').addClass('browserSafari' + userAgent);
		}
		
		// Is this a version of Mozilla?
		if ($.browser.mozilla) {
		
			//Is it Firefox?
			if (navigator.userAgent.toLowerCase().indexOf('firefox') != -1) {
				$('body').addClass('browserFirefox');
				
				// Add the version number
				userAgent = userAgent.substring(userAgent.indexOf('firefox/') + 8);
				userAgent = userAgent.substring(0, 1);
				$('body').addClass('browserFirefox' + userAgent);
			}
			// If not then it must be another Mozilla
			else {
				$('body').addClass('browserMozilla');
			}
		}
		
		// Is this a version of Opera?
		if ($.browser.opera) {
			$('body').addClass('browserOpera');
		}
	};
	return {
	    /**
	    * Internal initiating function. Do not use, unless you want to run all initiating functions again.
	    */
	    init: function(){
            // sort init list by priority
            _initList.sort(function(a, b){
                return b.priority - a.priority;
             });
             // initialize objects in init list
             $.each(_initList, function(i, val){
                // totally unsecure btw
                eval(val.fnName + "()");
                // a secure way would be to pass in the function (not just the name)
                // and the object which contains the function and then use the
                // javascript call method to run the code in the right context. Eg:
                // function.call(object);
             });
		 },
		/**
		 * Add functions (e.i. methods) to initiate on DOM ready
		 * @method addInits
		 * @param {Object} option Contains fnName and priority, where the former is the full namespace path to the function, unless inside the Telia namespace (in which case you can just use the function name). Priority is when to execute the function where high value goes before low. 10 is default.
		 */
		addInits: function(option){
			option.priority = option.priority || 10;
			_initList.push(option);
		},
		/**
		 * 
		 * @param {String} id
		 * @param {String} href
		 * @return {Bool} A boolean indicating if the attachment went succesful
		 */
		attachJS: function(id, href){
			// check if the style sheet is already attached
			if(document.getElementById(id)){
				return false;
			}
			var h = document.getElementsByTagName("head")[0];
			var script = document.createElement('script');
			script.id = id;
			script.type = "text/javascript";
			script.src = href;
			// add the script and put a reference in internal js list
			_jsList.push(h.appendChild(script));
			return true;
		},
		/**
		 * @method attachCSS
		 * @param {String} id An identifier for the style sheet
		 * @param {String} href The URL to the style sheet
		 */
		attachCSS: function(id, href){
			// check if the style sheet is already attached
			if(document.getElementById(id)){
				return false;
			}
			// attach a style sheet to the first element in the document (e.g. head element)
			var head = document.documentElement.firstChild;
			var link = document.createElement('link');
			link.id = id;
			link.type = "text/css";
			link.rel = "stylesheet";
			link.href = href;
			// add the style sheet and put a reference in internal css list
			_cssList.push(head.appendChild(link));
			return true;
		}
	};
}(jQuery);
$(function(){
	// Enable jQuery to work with ASP.NET AJAX
	jQuery.noConflict();
	
	Telia.addInits({
		fnName: "browserDetect",
		priority: 10
	});
	Telia.init();
})