$(document).ready(function() {
	// apply PNG fix for IE 6
	$("body").fixPng("/images/pixel.gif");
	
	// store key gallery references
	var galleryOutline = $('.gallery .outline');
	var galleryFrame = $('.gallery .frameBox');
	var galleryPhoto = $('#galleryPhoto');
	
	// store currently selected thumb reference
	var gallerySelection = $('.thumbs .selected');
	
	// add click handler to the thumb links and video tour button link 
	$('.thumbs a, .videoTourBtn a').click(function(event) {
		// store the media source path
		var mediaSrc = $(this).attr('rel');
				
		if (!$(this).is('.selected')) {
			// swap the old and new selected class
			gallerySelection.removeClass('selected');
			gallerySelection = $(this).addClass('selected');
			
			if ($(this).parent().is('.videoTourBtn')) {
				// add the video tour container and alternate content
				galleryOutline.prepend('<div class="videoTour"><p id="swfVideoTour">Viewing this video tour requires Flash Player 7.<br /><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p></div>');
				
				// hide the main photo and frame
				galleryFrame.hide();
				galleryPhoto.fadeTo(0, 0);
				
				// embed the video tour movie
				var flashvars = {};
				var params = {
					allowfullscreen: 'true',
					bgcolor: '#FBF9DC',
					wmode: 'transparent'
					};
				var attributes = {};
				
				swfobject.embedSWF(mediaSrc, 'swfVideoTour', '448', '280', '7.0.0', '/flash/expressInstall.swf', flashvars, params, attributes);
				
				// track the video page view
				pageTracker._trackPageview($(this).parents('.propDetail').attr('rel') + 'video-tour/');

			} else {
				// remove the video tour container and show the photo frame
				galleryOutline.find('.videoTour').remove();
				galleryFrame.show();
				
				// swap out the old and new photos
				if (galleryPhoto.attr('src') != mediaSrc) {
					galleryPhoto
						// create a load handler
						.load(function(){
							// fade in the photo
							$(this).fadeTo('slow', 1);
							
							// clear the load handler
							$(this).unbind('load');
						})
						
						// fade out the current image
						.fadeTo('fast', 0, function() {
							// load the new image
							$(this).attr('src', mediaSrc);
						});
				} else {
					// already the loaded image; just show it
					galleryPhoto.fadeTo('slow', 1);
				}
			}
		}
		
		// prevent default link action (if the photo frame exists; not accessing video tour directly)
		if (galleryFrame.length) {
			event.preventDefault();
		}
		
		// remove the focus outline
		$(this).blur();
	});
});