
prop_details = {

	popup_showing : false,

	init : function() {

		// Add listeners to the nav items
		dom.addEvent($('prop_descriptionlink'),  'click', prop_details.show_desc);
		dom.addEvent($('prop_featureslink'),     'click', prop_details.show_features);
		dom.addEvent($('prop_rateslink'),        'click', prop_details.show_rates);
		dom.addEvent($('prop_contactlink'),      'click', prop_details.show_contact);

		// Gallery link only appears if property has images in its gallery
		if ($('prop_gallerylink')) {
			dom.addEvent($('prop_gallerylink'),  'click', prop_details.show_gallery);

			/*
			// Add an event to place the image clicked on in the photo viewing area
			dom.addEvent($('property_content'), 'click', function(event) {
				var target = dom.getTarget(event);
				if (target.className == 'prop_album_image') {
					//dom.dumpProps(target.firstChild);
					var path = target.firstChild.src;
					path = path.substring(0, path.length - 5);
					alert(path);
					var displayarea = $('photo_display');
					displayarea.innerHTML = target.firstChild.innerHTML;
				}

			});
			*/

		}

		// Delagate a couple of events to handle image popups
		if ($('image_popup')) {
			dom.addEvent($('fullpage'), 'mousedown', prop_details.dismiss_image_popup);
			dom.addEvent($('centerpanel'), 'click', prop_details.pop_image_popup);
		}
		/*
		*/

		// Availability calendar tab only appears if property has unavailable periods
		// Gallery link only appears if property has images in its gallery
		if ($('prop_availabilitylink')) {
			dom.addEvent($('prop_availabilitylink'), 'click', prop_details.show_calendar);
		}

		if ($('prop_maplink')) {
			dom.addEvent($('prop_maplink'),      'click', prop_details.show_map);
		}

		// We're delagatin' by adding the event to the property_content instead
		// of the actual links.
		dom.addEvent($('property_content'),      'click', function(event) {
			var target = dom.getTarget(event);

			if (target.className == "load_year") {
				ajax.makeRequest('get', target.href, prop_details.set_year);
				dom.stopDefault(event);
				return false;
			}
		});
	},

	pop_image_popup : function(event) {
		var target = dom.getTarget(event);
		if (target.className == 'prop_album_image') {
			//dom.dumpProps(target.firstChild);
			var path = target.firstChild.src;
			path = path.substring(0, path.length - 5);
			//alert(path);
			var displayarea = $('image_popup');
			displayarea.style.visibility = 'visible';
			displayarea.style.display    = 'block';
			displayarea.innerHTML = target.innerHTML;
			displayarea.firstChild.src = path + 'l.jpg';
			prop_details.popup_showing = true;
		}
	},

	dismiss_image_popup : function(e) {
		if (prop_details.popup_showing == true) {
			$('image_popup').style.display = 'none';
			prop_details.popup_showing = false;
		}
	},

	set_year : function() {
		var div = $('property_content');
		if (ajax.xhr.readyState == 4) {
			if (ajax.xhr.status == 200) {
				div.innerHTML = ajax.xhr.responseText;
			} else {
				div.innerHTML = "Error: could not load AJAX data " + ajax.xhr.status;
			}
			return ajax.xhr.status;
		}
	},

	show_desc : function(e) {
		sa2.copyInnerHTML('description', 'property_content', 'prop_descriptionlink');
		dom.stopDefault(e);
	},

	show_map : function(e) {
		sa2.copyInnerHTML('map', 'property_content', 'prop_maplink');
		LoadMapSearchControl();
		dom.stopDefault(e);
	},

	show_features: function(e) {
		sa2.copyInnerHTML('features', 'property_content', 'prop_featureslink');
		dom.stopDefault(e);
	},

	show_rates: function(e) {
		sa2.copyInnerHTML('rates', 'property_content', 'prop_rateslink');
		dom.stopDefault(e);
	},

	show_gallery: function(e) {
		sa2.copyInnerHTML('gallery', 'property_content', 'prop_gallerylink');
		dom.stopDefault(e);
	},

	show_contact: function(e) {
		sa2.copyInnerHTML('contact', 'property_content', 'prop_contactlink');
		dom.stopDefault(e);
	},

	show_calendar: function(e) {
		sa2.copyInnerHTML('availability', 'property_content', 'prop_availabilitylink');
		dom.stopDefault(e);
	}
}

sa2.onLoad(prop_details.init);

