/**
 * Nodes root object
 *
 * Contains a few utility functions
 *
 * @copyright Nodes ApS 2010-2011 <tech@nodes.dk>
 * @author Christian Winther <cw@nodes.dk>
 * @since 08.03.2011
 */
var Nodes = {
	/**
	 * Ensure that a "namespace" exists
	 *
	 * Accepts dot notations in the parameter
	 * Accepts any number of arguments
	 *
	 */
	ns : function() {
		var o, d;
		jQuery.each(arguments, function(k, v) {
			d = v.split(".");
			o = window[d[0]] = window[d[0]] || {};
			jQuery.each(d.slice(1), function(k, v2) {
				o = o[v2] = o[v2] || {};
			});
		});
		return o;
	},

	config : function(path) {
		var o, d;

		d = path.split(".");

		if (!window.Nodes.Configuration || !window.Nodes.Configuration[d[0]]) {
			return null;
		}

		o = window.Nodes.Configuration[d[0]];
		jQuery.each(d.slice(1), function(k, v2) {
			o = o[v2] = o[v2] || null;

			if (!o) {
				return false;
			}
		});

		return o;
	},

	translate : function(key) {
		return this.config('L10n.' + key);
	}
};

// Ensure the Nodes.Router namespace exists
Nodes.ns('Nodes.Router');

/**
 * Router / URL specific helper methods
 *
 * @copyright Nodes ApS 2010-2011 <tech@nodes.dk>
 * @author Christian Winther <cw@nodes.dk>
 * @since 08.03.2011
 */
Nodes.Router = {
	/**
	 * General purpose function to enforce application specific urls
	 *
	 * Adds support for Application.use_ssl for URLs and Application.use_absolute_urls
	 *
	 * @param string url The URL you want to process
	 * @return string The URL after any transformations
	 */
	url : function(url) {
		// Avoid leaking
		var url = url;

		// Don't do anything if we are not inside an application scope
		if (!Nodes.config('Application')) {
			return url;
		}

		// Don't mess with absolute Uniform Resource Identifiers
		if (this.isAbsoluteURI(url)) {
			return url;
		}

		// Enforce SSL urls
		if (Nodes.config('Application.use_ssl')) {
			if (!this.isSslEnabled(url)) {
				url = 'https://' + document.location.host + url;
			}
			return url;
		}

		// Enforce aboslute urls
		if (Nodes.config('Application.use_absolute_urls')) {
			if (!this.isSslEnabled(url)) {
				url = 'http://' + document.location.host + url;
			}
			return url;
		}

		return url;
	},

	/**
	 * Check if a url - or the current one - is ssl enabled
	 *
	 * @param string|null url The URL to check for ssl enabling - if empty document.location.href is used
	 * @return boolean
	 */
	isSslEnabled : function(url) {
		if (typeof url == 'undefined') {
			url = document.location.href;
		}

		return url.indexOf('https://') != -1 || (this.isAbsolute(url) && document.location.protocol == 'https');
	},

	/**
	 * Check if a string is an absolute URI
	 *
	 * aka. http:// and the all shebang
	 *
	 * @param string url
	 * @return boolean
	 */
	isAbsoluteURI : function(url) {
		return url.indexOf('://') != -1;
	},

	/**
	 * Check if a string is an absolut path
	 *
	 * aka. check if it starts with /
	 *
	 * @param string url
	 * @return boolean
	 */
	isAbsolute : function(url) {
		return url.indexOf('/') == 0;
	},

	/**
	 * Get the absolute URL for the current Page
	 *
	 * @return string
	 */
	pagePrefix : function(url) {
		if (!Nodes.config('Page.slug')) {
			return null;
		}

		return this.url('/' + Nodes.config('Page.slug'))
	},

	/**
	 * Get the absolute URL for the current application
	 *
	 * @return string
	 */
	applicationPrefix : function() {
		if (!Nodes.config('Application.slug')) {
			return null;
		}

		return this.url('/' + Nodes.config('Page.slug') + '/' + Nodes.config('Application.slug'));
	}
};
