/**
* url.js
*
* UrlInfo and UrlManager : Tools for URL (Need framework Prototype)
*
* @package K3soft
* @author Maxime Lamontagne <mlamontagne@k3media.com>
* @version 10.01.12
* @since Version 10.01.21
*
* @todo method UrlManager.toSSL
* @todo method UrlManager.removeSSL
*
*	Samples (look sample.html for more details)
*
*	url = 'file:///var/www/html/k3_projects/k3soft/js/url/sample.html?var1=value1';
*
*	urlInfo = new UrlInfo(url); // or
*	UrlInfo.getInstance(url) (static way); // or
*	url.toUrlInfo() (prototype way)
*
*	UrlInfo.getParam('var1');
*
*	UrlManager.current().setParams({var1:'value1'});
*	UrlManager.current().removeGet(['var1']).addGet({var2:'value2'})
*
*/


/**
*
* UrlInfo (Read-Only Mode)
*
* Class.prototype UrlInfo
*/

/*****************************************************************************/

var UrlInfo = Class.create();
UrlInfo.prototype = {

	source : null,
	element: null,
	protocol: null,
	host: null,
	port: null,
	query: null,
	params: null,
	file: null,
	hash: null,
	path: null,
	relative: null,
	segments: null,
	uri: null,

	initialize: function(url)
	{
		if(!url) url = window.location.href;

		this.source = url;
		this.element = document.createElement('a');

		this.element.href = this.source;

		this.protocol = this.element.protocol.replace(':','');
		this.host = this.element.hostname;
		this.port = this.element.port;
		this.query = this.element.search;
		this.params = this.getParams();
		this.file = (this.element.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1];
		this.hash = this.element.hash.replace('#','');
		this.path = this.element.pathname.replace(/^([^\/])/,'/$1');
		this.relative = (this.element.href.match(/tp:\/\/[^\/]+(.+)/) || [,''])[1];
		this.segments = this.element.pathname.replace(/^\//,'').split('/');
		this.uri = this.protocol+':/'+this.path;

		return this;
	},

	toString: function()
	{
		return this.source;
	},
	_toString: function()
	{
		return this.source;
	},

	getParams: function()
	{
		var ret = {},
		seg = this.element.search.replace(/^\?/,'').split('&');

		for (i=0;i<seg.length;i++)
		{
			if(seg[i])
			{
				s = seg[i].split('=');
				ret[s[0]] = s[1] || s[1] == '' ? s[1] : null;
			}
		}

		this.params = ret;
		return this.params;
	},

	paramExists: function(aVar)
	{
		value = $H(this.params).get(aVar);
		return (value || value == '' || typeof(value) == 'object') ? true : false;
	},

	getParam: function(aVar)
	{
		value = $H(this.params).get(aVar);
		return this.paramExists(aVar) && value != '' ? value : null;
	},

	isSSl: function()
	{
		return (this.protocol && this.protocol == 'https') ? true : false;
	}
}


/**
* UrlInfo (Read-Only Mode)
*
* static UrlManager
*/

UrlInfo.getInstance = function(url)
{
	return new UrlInfo(url);
}

UrlInfo.current = function()
{
	return UrlInfo.getInstance();
}

UrlInfo.getParams = function(url)
{
	return UrlInfo.getInstance(url).getParams();
}
UrlInfo.getUrlParams = UrlInfo.getParasm;

UrlInfo.getParam = function(aVar, url)
{
	return UrlInfo.getInstance(url).getParam(aVar);
}
UrlInfo.getUrlParam = UrlInfo.getParam;

UrlInfo.paramExists = function(aVar, url)
{
	return UrlInfo.getInstance(url).paramExists(aVar);
}
UrlInfo.urlParamExists = UrlInfo.paramExists;

UrlInfo.isSSL = function(url)
{
	return UrlInfo.getInstance(url).isSSL;
}
UrlInfo.urlIsSSL = UrlInfo.isSSL;


UrlInfo.urlEncode = function(url)
{
	return url.escapeHTML();
}

UrlInfo.urlDecode = function(url)
{
	return url.unescapeHTML();
}

/******************************************************************************
******************************************************************************/




/**
* UrlManager (Edit Mode)
*
* Class UrlManager
*/

/*****************************************************************************/

var UrlManager = Class.create(UrlInfo, {

	setParams: function(params)
	{
		if(!params || typeof(params)!= 'object')
		{
			alert("Error: (arguments)params must be an object {var1:'value1', var2:'value2'}");
			return false;
		}

		newUrl = this.protocol+'://'+this.host+this.path;

		if(!(Object.isHash(params)))
		{
			params = $H(params);
		}

		if(params.size() > 0)
		{
			newUrl += '?';

			ct = 0;
			params.each(function(param)
			{
				ct++;
				if(ct>1) newUrl += '&';

				newUrl += UrlInfo.urlEncode(param.key);

				if(param.value || param.value == '')
				{
					newUrl += '='+UrlInfo.urlEncode(param.value);
				}

			}.bind(this));
		}

		if(this.hash) newUrl += '#'+this.hash;

		this.initialize(newUrl);

		return this;
	},
	setUrlParams: function(params)
	{
		return this.setParams(params);
	},

	addGet: function(params)
	{
		return this.setParams(Object.extend(this.getParams(), params || { }));
	},
	addUrlGet: function(params)
	{
		return this.addGet(params);
	},

	removeGet: function(vars)
	{
		if(typeof(params)== 'object')
		{
			alert("Error: (arguments)vars must be an array ['var1', 'var2']");
			return false;
		}

		if(typeof(vars) == 'string') vars = [vars];

		params = $H(this.params);
		vars.each(function(values)
		{
			params.unset(values);

		}.bind(this));

		return this.setParams(params.toObject());
	},
	removeUrlGet: function(vars)
	{
		return this.removeGet(vars);
	},

	toSSL: function()
	{
		return this;
	},
	urlToSSL: function()
	{
		return this.toSSL();
	},

	removeSSL: function()
	{
		return this;
	},
	urlRemoveSSL: function()
	{
		return this.toSSL
	}
})
/******************************************************************************
******************************************************************************/


/**
* UrlManager (Edit Mode)
*
* static UrlManager
*/

/*****************************************************************************/

UrlManager.getInstance = function(url)
{
	return new UrlManager(url);
}

UrlManager.current = function()
{
	return UrlManager.getInstance();
}

UrlManager.setParams = function(params, url)
{
	return UrlManager.getInstance(url).setParams(params);
}
UrlManager.setUrlParams = UrlManager.setParams;


UrlManager.addGet = function(params, url)
{
	return UrlManager.getInstance(url).addGet(params);
}
UrlManager.addUrlGet = UrlManager.addGet;


UrlManager.removeGet = function(vars, url)
{
	return UrlManager.getInstance(url).removeGet(vars);
}
UrlManager.removeUrlGet = UrlManager.removeGet;


UrlManager.toSSL = function(url)
{
	return UrlManager.getInstance(url).toSSL();
}
UrlManager.urlToSSL = UrlManager.toSSL;

UrlManager.removeSSL = function(url)
{
	return UrlManager.getInstance(url).removeSSL();
}
UrlManager.urlRemoveSSL = UrlManager.removeSSL;



/**
* String (Extended)
*
* prototype String extended methods
*/

String.prototype.setUrlParams = function(params)
{
	return UrlManager.setUrlParams(params, this)._toString();
}

String.prototype.addUrlGet = function(params)
{
	return UrlManager.addUrlGet(params, this)._toString();
}

String.prototype.removeUrlGet = function(vars)
{
	return UrlManager.removeUrlGet(vars, this)._toString();
}

String.prototype.urlToSSL = function()
{
	return UrlManager.urlToSSL(this)._toString();
}

String.prototype.urlRemoveSSL = function()
{
	return UrlManager.urlRemoveSSL(this)._toString();
}

String.prototype.toUrlInfo = function()
{
	return new UrlInfo(this);
}

String.prototype.toUrlManager = function()
{
	return new UrlManager(this);
}
