$(document).ready(function()
{

// building blocks built on page load

$('body').after().append('<div id="overlay">\n</div>\n<div id="image-display">\n\t<p></p>\n</div>\n');

$('body').after().append('<div id="icons-key-display">\n\t<div id="icons-content">\n\n</div>\n<span>[<a href="#">close window</a>]</span></div>\n');

// expand & collapse functions for hotel list

expand = function()
	{
	$(this).attr(
	{ 
          src: "/images/layout/minus-icon",
          alt: "hide description"
        });
	// $(this).parent().addClass('contract').removeClass('expand');
	
	$(this).parent().next().slideDown();
	}

collapse = function()
	{
	$(this).parent().next().slideUp();
	// $(this).parent().removeClass('contract').addClass('expand');
	$(this).attr(
	{ 
          src: "/images/layout/plus-icon",
          alt: "read more"
        });
	}

$('.hotel-list').addClass('tree');
$('.hotel-list dt').addClass('expand');
$('.hotel-list dt.expand').prepend('<img src=\"/images/layout/plus-icon\" alt=\"read more\" />');
$('.hotel-list dt.expand img').toggle(expand, collapse);


$('#image-gallery').prev().append('<span>[Click on an image to enlarge]</span>');

$('#image-gallery img').css('cursor', 'pointer').error(function()
{
  $(this).hide();
});

// image popups
	
$('#image-gallery img').click(function() 
	{
	var source = $(this).attr('src');
	$('#overlay').show().css('opacity' , 0.4);
	$('#image-display').fadeIn();
	$('#image-display p').html('<span>close</span>').prepend('<img src="' + source + '" alt="Enlarged hotel image " />');
	$('select').css('visibility' , 'hidden');
    	});

$('#image-display p').click(function()
	{
	$('#overlay').hide();
	$('#image-display').hide();
	$('select').removeAttr('style');
	});

// search results pages 

$('div.result').hover(function()
	{
	$(this).addClass('hover');
	},function()
	{
	$(this).removeClass('hover');
	});

function clickThrough()
	{
	var $this = $(this);
	var anchor = $this.children().children().attr("href");
	window.location = anchor;
	return false;}
	
$('div.result').click(clickThrough);


// simple validation for search

$('.search-box input.submit').click(function()
	{
	var searchValue = $('input#search-content').val();
	if (searchValue == 0)	
		{ 
		$('.search-box p.error').show();
		return false;
		}
	});

// simple validation contact

$('#contact-form input.submit').click(function()
	{
	var nameValue = $('#name').val();
	var emailValue = $('#email').val();
	var atSymbol = emailValue.lastIndexOf('@');
	var commentsValue = $('#comments').val();
		
	if (nameValue == 0)	
		{ 
		$('#contact-form span.error:nth(0)').addClass('visible');
		return false;
		}

	if (atSymbol < 1 || (atSymbol + 1) == emailValue.length)	
		{ 
		$('#contact-form span.error:nth(1)').addClass('visible');
		return false;
		}
	
	if (commentsValue <= 1)	
		{ 
		$('#contact-form span.error:nth(2)').addClass('visible');
		return false;
		}

	});

// icons key popup

$('#amenities a').click(function()
	{
	$('select').css('visibility' , 'hidden');
	$('#overlay').show().css('opacity' , 0.4);
	$('#icons-key-display').fadeIn(500);
	$('#icons-content').load("/hotel-icons-key.php #icons-key");
	return false;
	});
	
$('#icons-key-display a').click(function()
	{
	$('#overlay').hide();
	$('#icons-key-display').hide();
	$('select').removeAttr('style');
	return false;
	});
});


