window.addEvent("domready",function() {
	/**
	 * my itinerary
	 */
	var itinerary = $('listingTools-itinerary');
	itinerary.addEvent("click", function(e) {
		e.preventDefault();
		new Request({
			url: siteAddress + 'addToItinerary/'+listing_id,
			onRequest: function() {
				$(document.body).getElement('.tip-text').set('text', 'adding...');
				itinerary.store('tip:text', 'adding...');
			},
			onComplete: function() {
				$(document.body).getElement('.tip-text').set('text', 'Added to My Itinerary');
				itinerary.store('tip:text', 'Added to My Itinerary');
			}
		}).send();
	});
	/**
	 * email to a friend
	 */
	$('listingTools-email').addEvent("click", function() {
		$('email-foldout').setStyle('display', 'block');
	});
	/**
	 * print listing
	 */
	$('listingTools-print').addEvent("click", function() {
		new popUp(siteAddress+'printListing/'+listing_id);
	});

	//contact form submission
	if($('contactForm')) {
		$('contactForm').addEvent('submit', function(e) {
			e.stop();
			var submit = this.getElement('input[type=submit]').set('opacity', 0.4);
			//Empty the log and show the spinning indicator.
			var log = $('contactForm_log').empty().addClass('ajax-loading');
			//Set the options of the form's Request handler.
			//("this" refers to the $('myForm') element).
			this.set('send', {
				url: this.get('action')+'&from=ajax',
				onComplete: function(response) {
					response = JSON.decode(response);
					log.removeClass('ajax-loading');
					log.set('html', response['html']);
					if(response['success']) {
						$('listingContactFormInside').empty();
					} else {
						submit.set('opacity', 1);
					}
				}
			});
			//Send the form.
			this.send();
		});
	}
});