var Mansef_Url = new Class({	
		
	initialize: function(target) {		
//		this.host = '';
		this.hostname = '';
		this.href = String(target);
		this.pathname = '';
		this.port = '';
		this.protocol = 'http';
		this.search = '';
		this.parse();
		this.params = new Mansef_Url_Params(this.search);
	},

	parse: function() {
		var s = String(this.href);
		var pieces = s.split('?');
		this.search = pieces[1];
		var matches = String(pieces[0]).match(/(?:(\w+):\/\/)?([\w\.-]+)(?::?(\d*))(\/.+)?/im);
		if(String(matches[1]).length > 0) {
			this.protocol = matches[1];
		}
//		this.host = matches[2];
		this.hostname = matches[2];
		this.port = matches[3]
		if(matches[4] != undefined) {
			this.pathname = matches[4];
		}
	},

	
	assemble: function() {
		var o = this.protocol + "://" + this.hostname;
		if(String(this.port) > length) {
			o += ':' +  this.port;
		}
		if(this.pathname != undefined) {
			o += this.pathname;
		} else {
			o += '/';
		}
		params = this.params.assemble();
		if(String(params).length > 0) {
			o += '?' + params;
		}
		return o;
	}

});

var Mansef_Url_Params = new Class({
	
	initialize: function(string) {
		this.items = {};
		if(string != undefined) {
			var list = String(string).replace(/^\?/, "").split("&");
			list.each(function(item, index) {
				items = String(item).split("=");
				this.add(items[0], items[1]);
			}, this);
		}
	},
	
	add: function(key, value) {
		this.items[key] = value;
	},
	
	has: function(key) {
		for(i in this.items) {
			if(i === key) {
				return true;
			}
		}
		return false;		
	}, 
	
	get: function(key) {
		return this.items[key]
	},
	
	remove: function(key) {
		delete this.items[key];
	},
	
	assemble: function() {		
		var output = "";
		for(var i in this.items) {
			output += "&" + i + "=" + this.items[i];
		}
		return String(output).replace(/^\&/, "");
	}	
});