$(document).ready(function() {

	/*

	 *	Generate the custom inputs

	 */

	$('#search select').each(function() {



		var newElement = $('<div></div>')

			.addClass('select');



		var current = $('<div></div>')

			.addClass('d_current')

			.appendTo(newElement);



		newElement.insertAfter($(this));

		var dropdown = $('<div></div>')

			.addClass('dropdown')

			.appendTo(newElement)

			.hide();



		//var iframe = $('<iframe></iframe>').css({width: '100%', height: '300px'}).appendTo(dropdown);



		var value = '';

		var text = '';

		var pad = '';

		

		$(this).find('*').each(function(index) {

			if(this.tagName.toLowerCase() == 'optgroup')

			{

				$('<div></div>')

					.attr('value', '-')

					.html($(this).attr('label'))

					.addClass('optgroup ' + (index == 0 ? 'first' : ''))

					.appendTo(dropdown);

				//pad = '&nbsp;&nbsp;&nbsp;';

			}

			else

			{

				$('<div></div>')

					.attr('value', $(this).val())

					.html(pad + $(this).text())

					.addClass('option ' + (index == 0 ? 'first' : ''))

					.appendTo(dropdown);



				if($(this).is(':selected'))

				{

					value = $(this).val();

					text = $(this).text();

				}

			}

		});



		var span = $('<span></span>')

			.addClass('dropdown_span')

			.appendTo(current)

			.text(text);



		var count = $(this).find('option').length;



		if(count > 10)

		{

			dropdown

				.addClass('scroll')

				.height('10em');

		}



		/*

			dropdown

				.find('div:first')

				.remove();

		*/



		$('<input type="hidden" />')

			.attr('name', $(this).attr('name'))

			.attr('value', value)

			.appendTo(newElement);



		$(this)

			.remove();

	});

	developmentDropdown();

});



$(function() {

	$('div.select')

		.click(function(e) {

			down(this, e);

		});

});



function down($this, e)

{

	if(!$($this).find('.dropdown').is(':visible')) {

		var parent = $($this);

			var _this = $this;



			if($(e.target).attr('value') === undefined)

			{

				$('div.select .dropdown')

					.hide()

					.parent()

					.css('position', 'static');



				if($.browser.msie)

				{

					$('#search input').hide();

				}



				$('.dropdown', _this)

					.css('top', parent.height() + 'px')

					.slideDown(400);



				$(_this).css('position', 'relative');

			}



			//$($this).unbind('click');



			setTimeout("dropdownClose();", 100);



	}

	else

	{

		dropdownClose();

		$(document).click();

	}

}



function dropdownClose()

{

		var bind = $(document).click(function(e) {

		var t = e.target;

		var c = $(t).attr('class');



		if(c != 'd_current' && c != 'dropdown_span' && c != 'optgroup' && $(t).attr('type')!='image')

		{

			var v = $(t).attr('value');

			var text = $(t).text();

			if(v !== undefined)

			{

				var u = $(t).parent().parent();

				u.find('span.dropdown_span').text(text);

				var menu = u.find('input[type=hidden]').attr('value', v).attr('name');

				//If selecting area menu then need to select development menus

				if(menu == 'area')

				{

					developmentDropdown();

				}

			}

			$('.dropdown', this).slideUp(200, function() {



				if($.browser.msie)

				{

					$('#search input').show();

				}



				$(this)

					.parent()

					.css('position', 'static');

			});

		}

	});

}



function developmentDropdown()

{

	var keep_options = true;

	

	var menu = $('#search .select:nth-child(2)');

	var dropdown = $('.dropdown', menu);

	var selected_area = $('#search .select:nth-child(1) span.dropdown_span').text() != 'Area';



	$('div', dropdown).show();



	//If we haven't selected an area, lets get all divs

	if(!selected_area)

	{

		var options = $('div', dropdown);

		options.show();

		var siblings = options;

	}

	//Find the optgroup with the area we've selected, show it all, get all the next options and show those until the next optgroup is reached

	else

	{

		$('div', dropdown).hide();

		var options = $('div:contains(\', ' + $('#search .select:nth-child(1) span.dropdown_span').text() + '\')', dropdown);

		options.show();

		var siblings = options.nextAll();

	}

	options = siblings

		.filter(function(){

			if($(this).hasClass('optgroup') && selected_area)

			{

				keep_options = false;

			}

			return keep_options;

		})

		.show();

	

	var selected = options

		.filter(function(){

			if($(this).text().toLowerCase() == menu.find('span.dropdown_span').text().toLowerCase())

			{

				return true;

			}

			return false;

		});



	selected = selected.length == 0 ? options[0] : selected[0];



	/*We'll select the first option in the list now

	var option = $(selected);

	var text = option.text();

	menu.find('span.dropdown_span').text(text);

	menu.find('input[type=hidden]').attr('value', option.attr('value'));*/

}





/*

 *	Prepopulate heading input fields with their titles

 *	and handle click states etc

 */

$(function() {

	$('#header input[type=text]').each(function() {

		var _this = $(this);

		if(_this.val()=="")

		{

			_this.val(_this.attr('title'));

		}

		_this.focus(function() {

			if(_this.val()==_this.attr('title'))

			{

				_this.val('');

			}

		});

		_this.blur(function() {

			if(_this.val()=='')

			{

				_this.val(_this.attr('title'));

			}

		});

	});

});

/**

 *	Handle the results

 */

$(function() {

	$('.results .item').hover(

		function()

		{

			$('.inner', this)

				//.stop()

				.css({color: '#ffffff'})

				.css({backgroundColor: '#ecf4ea'})

				.find('img.image');

				//.stop()

				//.animate({marginRight: '23px'}, {queue: false}, 500);

		},

		function()

		{

			$('.inner', this)

				.css({color: '#ecf4ea'})

				.css({backgroundColor: '#ffffff'})

				.find('img.image');

				//.stop()

				//.animate({marginRight: '1px'}, 300);

		}

	);



});



/*

 * 'My Homes' overlays. Not exactly search results,

 * but all based on the same markup...

 */

$(function()

{

	$('#my_homes').find('p.is_sold, p.is_reserved').each(function() {

		var text = $(this).html();

		var colour = $(this).attr('class')=='is_reserved' ? '#437a39' : '#be1e2d';



		var parent = $(this).parent().parent().parent();



		var div = $('<div></div>')

						.attr('class', 'overlay')

						.css({

							position: 'absolute',

							top: 0,

							left: 0,

							width: parent.width(),

							height: parent.height(),

							backgroundColor: colour

						})

						.animate({opacity: 0.9}, 0)

						.appendTo(parent)

						.hover(

							function() {

								$(this).animate({opacity: 1}, 500);

							},

							function() {

								$(this).animate({opacity: 0.9}, 500);

							});



		var link = $(this).find('a').attr('href');



		var anc = $('<a></a>').css({

						display: 'block',

						position: 'absolute',

						height: parent.height(),

						width: parent.width(),

						outline: 0,

						textAlign: 'right',

						'text-decoration': 'none'

					})

					.attr('href', link)

					.appendTo(div);



		var span = $('<span></span>')

						.css({

							display: 'block',

							position: 'absolute',

							top: 20,

							right: 20,

							textTransform: 'uppercase'

						})

						.html(text)

						.appendTo(anc);



		span.find('a')

			.css({

				textDecoration: 'none',

				color: '#ffffff',

				fontSize: '1.3em',

				fontWeight: 'bold',

				outline: 0

			})

			.find('strong').css({fontWeight: 'bold'});

	});

});



function goToMapLocation(area)

{

	$('#search input[type=hidden]').val('-');



	$('#search .controls .dropdown:first').find('div').each(function() {

		var z = $(this).text();

		if(z.toLowerCase() == area.toLowerCase())

		{

			$('#search input[type=hidden]:first').attr('value', $(this).attr('value'));

			$('#search form').submit();

		}

	});

}