window.addEvent('domready', function(){
	var qs = new Querystring();
	var orig_name = qs.get('orig_name' );
	var orig_url = qs.get('orig_url' );
	
	$('referer_url').empty();

	if (orig_name && orig_url) {
		
		var opts = {
			domain : window.location.hostname,
			path : '/'};
		document.cookie = "orig_name=" + encodeURIComponent(orig_name);
		document.cookie = "orig_url=" + encodeURIComponent(orig_url);
		//Cookie.write('orig_name', orig_name, opts);
		//Cookie.write('orig_url', orig_url, opts);
		setRefererUrl(orig_name, orig_url);
		//console.log(opts, orig_name, orig_url);

	}
	else {
		if(Cookie.read('orig_name') != null)
		setRefererUrl (Cookie.read('orig_name'), Cookie.read('orig_url'));
	}
	//alert(document.cookie);
});

function setRefererUrl (orig_name, orig_url)
{
	$('referer_url').set('html', '<a href="http://'+orig_url+'" class="sous_page">'+orig_name+'</a>');
}

function Querystring(qs) { // optionally pass a querystring to parse
	this.params = {};
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}
