/**
 * 	Joomla paths utility
 *
 *	v 1.5.2
 *	problem: SEF influences location.pathname
 * 	input: template.js file in header or URLS.template
 *	output: URLS: .domain, .base, .base_absolute, .template
 */

// check for local storage support
// ref: http://diveintohtml5.org/storage.html
// if (typeof Browser.Features['localStorage'] == "undefined") Browser.Features['localStorage'] = ;

var ls = ls || (function () {
	try { return 'localStorage' in window && window['localStorage'] !== null; }
	catch(e){ return false; }
})();

// main paths
if (typeof URLS !== "object" && typeof URLS.template !== "string") {
/* URLS = */ (function() {
		URLS.useAbsolute = false;
		if (typeof ls !== "boolean") { ls = false; }
		
		if (ls && sessionStorage.getItem('URLS')) {
			// clear sessionStorage.clear();
			URLS = JSON.parse(sessionStorage.getItem('URLS'));
			URLS.js = URLS.js;
			if (typeof URLS.base == "string" && typeof URLS.template == "string") return URLS;
		}
		
		// var RXpaths = new RegExp(/^(https?:\/\/.*?)?\/(.*?\/)?(templates\/.+?\/).*?\/template.js$/);
		// var RXpaths = new RegExp(/^((?:https?:)?\/\/.+?)?(\/.*)?\/(templates\/.+?\/)(.*\/)?(.+\..+)?$/);
		var RXpaths = new RegExp(/^((?:https?:)?\/\/.+?)?(\/.*)?\/+?(templates\/.+?\/)(.*\/)?(.+\..+)?$/i);
		
		// get template.js path
		// http://stackoverflow.com/questions/984510/what-is-my-script-src-url/984656#984656
		if (typeof URLS.template == "string"){
			var templatePath = URLS.template;
		} else {
			var templatePath = (function(scripts) {
				var scripts = document.getElementsByTagName('script'), 
					script = scripts[scripts.length - 1];
				return (script.getAttribute.length !== undefined) ? script.src : script.getAttribute('src', 4);
			}());
		}
		
		// autoset based on path
		if (typeof URLS.useAbsolute !== "boolean"){
			URLS.useAbsolute = templatePath.test(/^((?:https?:)\/\/).*$/i) ? false : false;
		}
		
		// get domain root
		if (typeof URLS.server !== "string") {
			if (templatePath.match(RXpaths)[1]) {
				URLS.server = templatePath.match(RXpaths)[1];
			} else {
				URLS.server = window.location.protocol + "//" + window.location.host + "/";
			}
		}
		
		// get base
		if (typeof URLS.base !== "string") {
			if(templatePath.match(RXpaths)[2]) {
				URLS.root = templatePath.match(RXpaths)[2];
			} else {
				URLS.root = "";
			}
		
			// get base_aboslute
			if (URLS.root == "" || URLS.root.test(/^(\/.*)$/)){
				URLS.root_absolute = URLS.server + URLS.root;
			// already absolute
			} else if (URLS.root.test(/^(?:https?:)\/\/.*$/i)) {
				URLS.root_absolute = URLS.root;
				URLS.root = URLS.root_absolute.replace(URLS.server, "");
			} else {
			// relative no trailing path
			}
			
			URLS.base = URLS.useAbsolute? URLS.root_absolute : URLS.root;
			URLS.base += "/";
		}
		
		// construct template path
		if (typeof URLS.template !== "string") {
			URLS.template = URLS.base + templatePath.match(RXpaths)[3];
		}
		
		if (ls) sessionStorage.setItem('URLS', JSON.stringify(URLS));
		return URLS;
	}(URLS));
} else if (URLS.template.slice(-1) != "/"){
	URLS.template += "/";
}


// template & Joomla paths
(function(){
	if (ls && sessionStorage.getItem('URLS')) {
		return URLS = JSON.parse(sessionStorage.getItem('URLS'));;
	}
	
	if (typeof URLS.base !== "string"){
		URLS.base = URLS.template.match(/(.*\/)?(templates\/.+)\/?/i)[1];
	}
	
	var subPaths = {
		template: {
			js: 		"js/",
//			classes: 	"js/classes/",
			css: 		"css/"
//			images: 	"images/"
		},
		base: {
//			media:		"media/",
			mediaImages: "media/images/"
		}
	}
	
	// merge template sub paths
	var mergeSubPaths = function( subPaths, subfolder, URLS){
		if (typeof subPaths !== "object" || subPaths.length < 1 || typeof URLS !== "object") return false;
		
		for (var k in subPaths){
			if (typeof URLS[subPaths[k]] == "string") continue; // don't overwrite
			URLS[k] = subfolder + subPaths[k];
		}
		return URLS;
	}
	
	for (var k in subPaths){
		if (typeof URLS[k] !== "string") continue;
		URLS = mergeSubPaths(subPaths[k], URLS[k], URLS);
	}
	
	if (ls) sessionStorage.setItem('URLS', JSON.stringify(URLS));
	return URLS;
	
}(URLS));

// compatibility
templatePath = URLS.template;
basePath = URLS.base;
