// JavaScript Document
	$(document).ready(function(){	
							   
	$("div#searchFormDiv").hide(); // hide the standard form
	
	$("div#searchFormAlt").show(); // show the alternative form

	$('#searchFormAlt a.dropSelect').click(function(){
		var id = this.id;
	    var whichDrop = id.substr(0, id.length - 4);
		doDrop(whichDrop);
		$('div#searchFormAlt input.submitButton').focus();
		return false;
	});

	$('#areaDropDown a').click(function(){
		selectDropDownOption(this);
		return false;
	});
	$('#bedroomDropDown a').click(function(){
		selectDropDownOption(this);
		return false;
	});
	$('#priceDropDown a').click(function(){
		selectDropDownOption(this);
		return false;
	});
	
	$('#phoneDropDown a').click(function(){
		selectDropDownOption(this);
		return false;
	});
	
	 $('.radios a').click(function(){
		selectRadio(this);
		return false;
	 });


	$('div#searchFormAlt input.submitButton').click(function(){
		sendAltForm();
});
	
	$(document).click(function() {
	$("div.dropdown").fadeOut("fast");
	});


	if ($('searchFormAlt')) { $('searchFormAlt').css('display') == 'inline'; } //http://www.dougboude.com/blog/1/2008/12/Elementshowhide-anomoly-in-Prototype.cfm
	$('div#areaDrop').show();
    $('div#priceDrop').show();
	$('div#bedroomDrop').show();

	
});



function doDrop(dropdown){
	$("div#"+dropdown+"DropDown").toggle();
	//show hide drop downs
	//if required close other drop down 
	if (dropdown == "area" && $('bedroomDropDown:visible')){
		$('#bedroomDropDown').fadeOut("fast");$('#priceDropDown').fadeOut("fast");$('#phoneDropDown').fadeOut("fast");	
	} else if (dropdown == "bedroom" && $('areaDropDown:visible')){ //don't think users can get to this menu when the other is open but _feels_ cleaner
		$('#areaDropDown').fadeOut("fast");$('#priceDropDown').fadeOut("fast");$('#phoneDropDown').fadeOut("fast");	
	}
	else if (dropdown == "price" && ($('bedroomDropDown:visible' || $('#areaDropDown:visible')) )){ //don't think users can get to this menu when the other is open but _feels_ cleaner
		$('#bedroomDropDown').fadeOut("fast");$('#areaDropDown').fadeOut("fast");$('#phoneDropDown').fadeOut("fast");			
	}

}

function selectDropDownOption(e){
	var dropdown = $("#"+e.id).parent().attr("id");
	var startPoint = dropdown.length - 8; 
	var whichDropDown = dropdown.substr(0, startPoint); // find a better way
	var selected  = $(e).html(); // get text of selected
	$('#'+whichDropDown+'Drop').html(selected+'<span></span>'); //update selected item
		$("div.dropdown").fadeOut("fast");
	//doDrop(whichDropDown); //close the drop down
}


function selectRadio(e){
			var val_r = e.title;
			//$(e).toggleClass("on");
			// $(this).toggleClass("expand"); 
			$('.radios a').removeClass("on");
		    $(e).toggleClass("on"); 
			$('input#furnishedDummy').val(val_r);
			$('div#searchFormAlt input.submitButton').focus();
}

function sendAltForm(){
	//tidy up input
	var toReplace = "<span></span>";
	var areaData = $("#areaDrop").html().replace(toReplace, '');
	var bedroomData = $("#bedroomDrop").html().replace(toReplace, '');
	var priceData = $("#priceDrop").html().replace(toReplace, '');
	var priceData = $("#priceDrop").html().replace(toReplace, '');
	
    //check to see if we have a valid input - if not send all
	if(areaData == 'All Areas'){
		areaData = 'all';
	}
	if(bedroomData == 'No minumum'){
		bedroomData = 'all';
	}
	if(priceData == 'Any price'){
		priceData = 'all';
	}
	// validation?
	$('input#areaDummy').val(areaData);
	$('input#bedroomDummy').val(bedroomData);
	$('input#priceDummy').val(priceData);

	
		if ( $("#phoneDrop").length > 0 ){ 
		var phoneData = $("#phoneDrop").html().replace(toReplace, '');
			if(phoneData == 'Type'){
			phoneData = '';
			}
		$('input#phoneDummy').val(phoneData);
		};
	
	$('#searchFormAlt').submit();

}