
RM_Url = function()
{
	this._config = [];
	this._host;
	this._domainRegex;
}

RM_Url.prototype.addUrl = function( urlCode, urlTemplate, subdomain, protocol )
{
	this._isHTTPS = false;
	
	this._config[urlCode] = {
		'urlTemplate'	: urlTemplate, 
		'subdomain'		: ( subdomain && subdomain != '' ) ? subdomain : false, 
		'protocol'		: ( protocol && protocol != '' ) ? protocol : false
	}
}

RM_Url.prototype.setHost = function(host)
{
	this._host = host;
}

RM_Url.prototype.setDomainRegex = function(domainRegex)
{
	this._domainRegex = domainRegex.substring(1,domainRegex.length-1);
}

RM_Url.prototype.setHTTPS = function(isHTTPS)
{
	isHTTPS ? this._isHTTPS = true : this._isHTTPS = false;
}

RM_Url.prototype.isCurrentMainDomain = function()
{
	var host = this._host;
	var cuttedHost = host.replace( new RegExp( this._domainRegex, 'i' ), '' );
	return ( cuttedHost == 'www' || cuttedHost == '' ); 
}

RM_Url.prototype.getProtocol = function(urlCode)
{
	if ( this._config[urlCode]['protocol'] && this._config[urlCode]['protocol'] != '' )
		return this._config[urlCode]['protocol']+'://';
	return ( this._isHTTPS ? "https://" : "http://" );
}

RM_Url.prototype.needChangeProtocol = function( urlCode )
{
	return this._config[urlCode]['protocol'] || !this._isHTTPS &&  this._config[urlCode]['protocol'].toString().toLowerCase() == 'https';
}

RM_Url.prototype.getDomainUrl = function( urlCode )
{
	var domain = this._config[urlCode]['subdomain'] && this._config[urlCode]['subdomain'] != '' ? this._config[urlCode]['subdomain'] : 'www';
	var host = this._host;
	var result;
	if( domain == '_current_' )
		result = this._host;
	else
		result = host.replace( new RegExp(this._domainRegex,'i'), domain+'.$2' );
	return result;
}

RM_Url.prototype.needUseDomain = function( urlCode )
{
	var w1 = this.getDomainUrl(urlCode);
	var c1 = this._host;
	var w2 = 'www.' + w1;
	var c2 = 'www.' + c1;
	return !(w1==c1 || w1==c2 || w2==c1 || w2==c2);
}

RM_Url.prototype.get = function( urlCode, data, args, fullUrl )
{
	var url = '';
	if ( this._config[urlCode] )
	{
		url = this._config[urlCode]['urlTemplate'];
		if ( data )
		{
			for( var param in data )
			{
				url = url.replace('$'+param,data[param]);
			}
		}
		if ( args )
		{
			if ( url.indexOf('?') == -1 )
				url += '?';
			for( var arg in args )
			{
				if($.isArray(args[arg]))
				{
					for(var key in args[arg])
					{
						if ( url.substring(url.length-1) != '?' ) 
							url += '&';
						url += encodeURIComponent(arg)+'[]='+encodeURIComponent(args[arg][key]);
					}
				}
				else
				{
					if ( url.substring(url.length-1) != '?' ) 
						url += '&';
					url += encodeURIComponent(arg)+'='+encodeURIComponent(args[arg]);
				}
			}
		}
		if ( url.indexOf('://') == -1 )
		{
			if ( url == '/' || url.substring(0,1) != '/' )
				url = '/' + url;
			if ( this.needUseDomain(urlCode) || this.needChangeProtocol(urlCode) || fullUrl )
			{
				url = this.getDomainUrl(urlCode) + url;
				url = this.getProtocol(urlCode) + url;
			}
		}
	}
	else
	{
		throw 'Unknown URL '+urlCode;
	}
	return url;
}

RM_Url.prototype.getImageUrl = function(format, file)
{
	var objectfsDepth = 3;
	return this.get(format,{
		'id'		: file.id, 
		'fid'		: file.file_device_id,
		'file'		: file.file_device_path.substring(objectfsDepth*2),
		'name'		: file.name
	});
}

RM_Url.prototype.go = function( urlCode, data, args )
{
	window.location.href = this.get( urlCode, data, args );
}

var url = new RM_Url();
