var SendToAFriend = Class.create();
Object.extend(SendToAFriend.prototype, {

	initialize: function()
	{
		this.elementId = 'sendToFriend';
		Event.observe(window, 'load', this.onLoad.bind(this));
	},
	
	onLoad: function() 
	{
		$$('a.send-to-a-friend').each(function(item) {
			item.observe('click', this.showWindow.bindAsEventListener(this))
		}, this);
	},
	
	showWindow: function(e)
	{
		e.stop();
		new Ajax.Request(BASE_URL + '/' + LANGUAGE + '/contact/send-to-friend', {
			onSuccess: this.onResponse.bind(this)
		})
	},
		
	onResponse: function(transport)
	{
		
		$(document.body).insert(transport.responseText);
		$$('#' + this.elementId + ' div.btn-close').each(function(item) { item.observe('click', this.destroy.bind(this));}, this);
		$('btnSend').observe('click', this.submit.bindAsEventListener(this));
	},
	
	submit: function(e)
	{
		e.stop();
		var form = $('form');
		
		var data = form.serialize(true);
		data.url = window.location.href;
		
		new Ajax.Request(form.action, {
			parameters: data
			//onSuccess: function(transport) {$('sendToFriendTitle').update(transport.responseText)}
		});
		
		form.remove();
		$('sendToFriendTitle').update('The link has been sent to your friend.<br /> You can close this window.');
	},
	
	destroy: function()
	{
		$(this.elementId).remove();
	}
	
});

new SendToAFriend();
