if(!window.blackbox) window.blackbox = new BlackBox();

var send2friend_container = null;

function send2friend (id, url)
{
	if(isNaN(id)) return false;
	
	// Create interface if none exists
	if(!send2friend_container)
	{
		// Create wrapper
		var wrapper = new Element('div', {'id':'popup-container'});
		// Create content container
		var content = wrapper.appendChild(new Element('div', {'id':'popup-wrapper'}));
		// Create header
		var header = content.appendChild(new Element('div', {'id':'popup-header'}));
		// Add header elements
		header.appendChild(new Element('img', {'src':'/images/global/popup-header.gif', 'alt':''}));
		
		// Add close control to header
		var ctrl_close = header.appendChild(new Element('a', {'id':'closeWindow', 'href':'#'}));
		ctrl_close.appendChild(new Element('img', {'src':'/images/auctions/btn-close-popup.gif', 'alt':'Close'}));
		// Add behaviours
		Event.observe(ctrl_close, 'click', blackbox.hide.bind(blackbox));
		
		// Add text body
		var text = content.appendChild(new Element('div', {'id':'popup-content'}));
		text.appendChild(new Element('p').update('Know a fellow traveller who might be interested in this journey?<br />Fill in the details below to email this journey to a friend.'));
		text.appendChild(new Element('label', {'for':'sender_name'}).update('Your name'));
		var sender_name = text.appendChild(new Element('input', {'id':'sender_name', 'maxlength':'50'}));
		text.appendChild(new Element('label', {'for':'sender_email'}).update('Your email'));
		var sender_email = text.appendChild(new Element('input', {'id':'sender_email'}));
		text.appendChild(new Element('span', {'style':'display:block;clear:both;margin-bottom:10px;'}));
		text.appendChild(new Element('label', {'for':'recipient_name'}).update('Friend\'s name'));
		var recipient_name = text.appendChild(new Element('input', {'id':'recipient_name', 'maxlength':'50'}));
		
		text.appendChild(new Element('label', {'for':'recipient_email'}).update('Friend\'s email'));
		var recipient_email = text.appendChild(new Element('input', {'id':'recipient_email'}));
		var validation_msg = text.appendChild(new Element('div', {'id':'validation-msg', 'style':'display:none'}));
		validation_msg.appendChild(new Element('div').update('Could not send email, please make sure all fields are<br />complete and both email addresses are valid.'));
		text.appendChild(new Element('span', {'style':'display:block;clear:both;margin-bottom:10px;'}));
		
		text.appendChild(new Element('label', {'for':'recipient_message'}).update('Optional message'));
		var recipient_message = text.appendChild(new Element('textarea', {'id':'recipient_message'}));
		text.appendChild(new Element('span', {'style':'display:block;clear:both;margin-bottom:10px;'}));
		
		var response_panel_wrapper = text.appendChild(new Element('div', {'id':'response-panel', 'style':'display:none;'}));
		var response_panel = response_panel_wrapper.appendChild(new Element('div'));
		response_panel.appendChild(new Element('p', {'style':'margin-left:100px;'}));

		var ctrl_more = response_panel.appendChild(new Element('a', {'href':'#'}).update('Forward to another friend &gt;&gt;'));
		Event.observe(ctrl_more, 'click', function($event) {
			Event.stop($event);
			$('recipient_name').value = '';
			$('recipient_email').value = '';
			
			$('response-panel').fade({duration:0.25, queue:{scope:'s2f:success', position:'end'}});
			$('submit-panel').appear({duration:0.25, queue:{scope:'s2f:success', position:'end'}});
		});
		
		var ctrl_back = response_panel.appendChild(new Element('a', {'href':'#'}).update('Back to journey &gt;&gt;'));
		ctrl_back.addClassName('quiet');
		Event.observe(ctrl_back, 'click', blackbox.hide.bind(blackbox));
		
		var submit_panel_wrapper = text.appendChild(new Element('div', {'id':'submit-panel'}));
		var submit_panel = submit_panel_wrapper.appendChild(new Element('div'));


		var ctrl_submit = submit_panel.appendChild(new Element('a', {'href':'#'}).update('<b>Send</b>'));
		ctrl_submit.addClassName('btn-send');
		Event.observe(ctrl_submit, 'click', function($event){
			Event.stop($event);
	
			// Sender name
			if($F('sender_name') == '') $('sender_name').addClassName('invalid');
			else $('sender_name').removeClassName('invalid');
			
			// Sender email
			if(!/^([a-zA-Z0-9\-\.])+\@([a-zA-Z0-9\-\.])+\.([a-zA-Z]{2,4})$/.test($F('sender_email'))) $('sender_email').addClassName('invalid');
			else $('sender_email').removeClassName('invalid');
	
			// Recipient name
			if($F('recipient_name') == '') $('recipient_name').addClassName('invalid');
			else $('recipient_name').removeClassName('invalid');
			
			// Recipient email
			if(!/^([a-zA-Z0-9\-\.])+\@([a-zA-Z0-9\-\.])+\.([a-zA-Z]{2,4})$/.test($F('recipient_email'))) $('recipient_email').addClassName('invalid');
			else $('recipient_email').removeClassName('invalid');
			
			if($event.target.parentNode.parentNode.parentNode.select('.invalid').length) { $('validation-msg').slideDown({duration:0.25}); }
			else {
				if($('validation-msg').getStyle('display') != 'none') $('validation-msg').slideUp({duration:0.25});
				
				// Add ajax loader animation
				
				// Send ajax request
				new Ajax.Request('/utility/act_send2friend.cfm', {

					method: 'post',
					parameters: {
						sender_name:$F('sender_name'),
						sender_email:$F('sender_email'),
						recipient_name:$F('recipient_name'),
						recipient_email:$F('recipient_email'),
						recipient_message:$F('recipient_message'),
						journey_id:id,
						url:url
					},
					evalJSON: true,
					onSuccess: function(transport) {
						// Translate response
						var response = transport.responseJSON;
						// Check success of action
						if(response.success == true) {
							$('response-panel').select('p')[0].update('Thank you! We\'ve just sent this to<br />'+$F('recipient_email'));
							
							$('submit-panel').fade({duration:0.25, queue:{scope:'s2f:success', position:'end'}});
							$('response-panel').appear({duration:0.25, queue:{scope:'s2f:success', position:'end'}});
						} else {
							modalContainer.hide();
						}
					},
					onFailure: function(error) {
						var forceClose = blackbox.hide.bind(blackbox);
						forceClose();
					}
				});
			}
	
		});
		
		var ctrl_cancel = submit_panel.appendChild(new Element('a', {'href':'#'}).update('&gt; Cancel, I\'ve changed my mind'));
		ctrl_cancel.addClassName('quiet');
		Event.observe(ctrl_cancel, 'click', blackbox.hide.bind(blackbox));
		
		// Set external alias
		send2friend_container = wrapper;
		
	}
	
	// Track event
	if(pageTracker._trackEvent) pageTracker._trackEvent('Social: Share', 'Send to a Friend', url);

	// Write to modal window
	blackbox.write(send2friend_container);
	
	// Return false to cancel the event
	return false;
};
