var search_form =
{
	form: '#search_form',
	search_mode: 'simple',
	search_for: 'offered',
	search_by: 'area',
	on_spareroom: false,
	toggle_on: false,
	postcode_string: "postcode", 
	collapse_panels: 'yes',
	
	initialise: function()
	{
		this.form = $(this.form);
		this.form.addClass('js');
		
		// Only do these things on SpareRoom
		if (search_form.on_spareroom)
		{
			this.set_up_toggling();
			this.set_up_search_button();
			this.set_up_styles();
		}		
		
		if (search_form.toggle_on){
			this.set_up_toggling();
		}
		
		this.set_up_search_mode();
		this.set_up_search_by();		
		this.set_up_search_for();
		
		this.add_events();
	},
	
	add_events: function()
	{
		var self = this;
		
		// Set search mode when the search mode radio buttons are changed
		$('#search_mode input[name=searchtype]').click(function(){ self.set_up_search_mode(); });
		
		// Set search by when the dropdown box changes
		$('.location_type').click(function(){ self.set_up_search_by(); });
		
		// Set search for when the search for radio buttons are changed
		$('#search_for input[name=flatshare_type]').click(function(){ self.set_up_search_for(); });
		
		// Select the available around radio button when date fields are selected
		$('td.available_around select').focus(function()
		{
			$('#available_around').trigger('click');
		});
		
		// Validate the search form upon submission
		this.form.submit(function(){
			return self.validate( self.search_by );
		});	

	},
	
	set_up_search_mode: function()
	{
		this.search_mode = $('#search_mode input[name=searchtype]:checked').val();
		
		if (this.on_spareroom)
			this.set_search_mode('advanced'); // Always advanced mode on SpareRoom
		else
			this.set_search_mode(this.search_mode); // Can switch on other sites
	},
	
	set_up_search_for: function()
	{
		this.search_for = this.form.find('input[name=flatshare_type]:checked').val();

		this.set_search_for(this.search_for);
	},
		
	set_up_search_by: function()
	{
		this.search_by = this.form.find('input[name=location_type]:checked').val();

		this.set_search_by(this.search_by);

		this.form.find('.or').hide();
	},
	
	set_up_toggling: function()
	{	
		// Add toggle links to each search section
		this.form.find('div.section').each(function()
		{
			var section = $(this);
			var id = section.attr('id');
			var title = section.attr('rel');
			section.before('<a href="#'+id+'" class="toggle" id="link_toggle_'+id+'">'+title+'</a>');
		});
		
		var toggle_links = this.form.find('a.toggle');
		
		// Add toggle events.		
		toggle_links.click(function()
		{
			$(this).next('div.section').slideToggle(100);
			$(this).toggleClass('closed');
			return false;
		});
		
		if (search_form.collapse_panels !== 'no')
		{
			// Collapse each section except the main one
			// and ones containing error fields
			toggle_links.filter(':not(:first)').each(function()
			{
				var section = $(this).next('div.section');
				
				if (!section.find('.error').length)
				{
					section.toggle();
					$(this).toggleClass('closed');
				}
			});
		}
	},
	
	// Remove the dividing line if we don't need it
	set_up_styles: function()
	{
		$('#search_form div.section fieldset').each(function()
		{
			if (!$(this).next('fieldset').length)
				$(this).css('background', 'none');
		});
	},
	
	set_up_search_button: function()
	{
		var search_button = $('#search_button');
		var src = this.static_files_server+'/img/spareroom/v3/other_items/search_now.png';
		var search_image = $('<input type="image" src="'+src+'" id="search_button" value="Search" />');
		
		// Button cannot be hidden, so we have to shift it off screen, otherwise users can't submit
		// the form by pressing enter.
		search_button.after(search_image).css({'position': 'absolute', 'top':'-1000px', 'left':'-1000px'});
	},

	// ----------------------------------------------------
	
	set_search_for: function(search_for)
	{
		switch(search_for)
		{
			case 'offered':
				this.offered();
			break;
			case 'flatstorent':
				this.flatstorent();
			break;
			case 'wanted':
			case 'buddyup':
				this.wanted();
			break;
		}
	},
	
	set_search_by: function(search_by)
	{
		// Get the search by fieldsets
		var fieldsets = this.form.find('fieldset[id^=search_by]');
		
		// Hide them all, the get just the one we want, show it, enable
		// its form elements, and focus its first form element
		fieldsets.hide()
				 .filter('[id=search_by_'+search_by+']')
				 .show()				 
				 .find(':input')
				 .removeAttr('disabled')
				 .filter(':first')
				 .focus();

		// Get the fieldsets we don't want, and disable their form
		// input - so that only the search by method we want
		// is actually used.
		fieldsets.filter(':not([id=search_by_'+search_by+'])')
			.find(':input')
			.attr('disabled', 'disabled');
	},
	
	set_search_mode: function(search_mode)
	{
		$('#search_mode input[value='+search_mode+']').attr('checked', 'checked');
	
		switch(search_mode)
		{
			case 'simple':
				this.simple();
			break;
			case 'advanced':
				this.advanced();
			break;
		}
	},
	
	// ----------------------------------------------------	
	
	buddyup: function()
	{
		// Same as wanted for now (see this.set_search_for)
	},
	
	offered: function()
	{
		// Set the Search by options available to this type of search
		var search_by = $('input[name=location_type]:checked').val();
		$('.location_type').show();
		this.set_search_by(search_by);

		$('#radius') // Specific :inputs to enable
		.show().removeAttr('disabled');
		
		$('#pets_req_option, #parking_ads_option, #short_lets_considered, #f_ensuite, #f_rooms_for, #f_no_of_rooms, #f_size_of_house, #f_show_me') // Groups of :inputs to enable
		.show().find(':input').removeAttr('disabled');
		
		$('#f_no_of_beds') // Groups of :inputs to disable
		.hide().find(':input').attr('disabled', 'disabled');
	},
	
	flatstorent: function()
	{
		// Set the Search by options available to this type of search
		var search_by = $('input[name=location_type]:checked').val();
		$('.location_type').show();
		this.set_search_by(search_by);

		$('#radius') // Specific :inputs to enable
		.show().removeAttr('disabled');
		
		$('#pets_req_option, #parking_ads_option, #f_ensuite, #f_rooms_for, #f_size_of_house, #f_no_of_beds') // Groups of :inputs to enable
		.show().find(':input').removeAttr('disabled');
		
		$('#f_show_me, #f_no_of_rooms, #f_no_of_beds_req') // Groups of :inputs to disable
		.hide().find(':input').attr('disabled', 'disabled');
		
	},
	
	wanted: function()
	{
		// Set the Search by options available to this type of search
		$('.location_type:not([for="area"])').hide();
		this.set_search_by('area');		
		
		$('#radius') // Specific :inputs to disabled
		.hide().attr('disabled', 'disabled');
		
		$('#pets_req_option, #parking_ads_option, #short_lets_considered, #f_ensuite, #f_rooms_for, #f_no_of_rooms, #f_no_of_beds, #f_size_of_house, #f_show_me') // Groups of :inputs to disable
		.hide().find(':input').attr('disabled', 'disabled');
		
		$('#f_no_of_beds_req') // Groups of :inputs to enable
		.show().find(':input').removeAttr('disabled');
	},
	
	simple: function()
	{
		$('#search_mode_simple').show();
		$('#search_mode_advanced').hide();
		$('#search_mode_advanced :input').attr('disabled', 'disabled');	
	},
	
	advanced: function()
	{
		$('#search_mode_advanced :input').removeAttr('disabled');	
		$('#search_mode_simple, #search_mode_advanced').show();	
	},	
	
	// ----------------------------------------------------	
	
	validate: function(search_by)
	{
		var validates = true;
		switch(search_by)
		{
			case 'area':
				validates = this.area_validation();
			break;
			// case 'zone': break;
			// case 'tube': break;				
			case 'commuter':
				validates = this.commuter_validation();
			break;
		}
		return validates;
	},
	
	area_validation: function()
	{
		if ($('#search_field').length && !$('#search_field').val()) {
			alert('Please enter the area/'+search_form.postcode_string); $('#search_field').focus();
			return false;
		} else if (!$('#search_field').length && !$('div.matches input:checked').length) {
			alert('Please choose a location');
			return false;		
		}
	},
	
	commuter_validation: function()
	{
		if (!$('#station_id').val()) {
			alert('Please select a station'); $('#station_id').focus();
			return false;
		}
	}
	
};

