function search_model( options ) {
	
	if (( options.location ) || ( options.camera ) || ( options.camera_models ) || ( options.support_gear ) || ( options.specialties ) ) {
		
		var base_url = window.location;
		
		// execute search ajax
		$.ajax({
			url: '/index.php/search/results',
			dataType: 'html', /* json */
			data: 'location=' + options.location + '&distance=' + options.distance + '&camera=' + options.camera + '&camera_models=' + options.camera_models + '&support_gear=' + options.support_gear + '&specialties=' + options.specialties ,
			beforeSend: function () {
				var o = {
					view: {
						header_message: 'Loading...',
						data: ''
					}
				};
				// send object to controller so that it can pass it to the view
				search_controller(o);
			},
			success: function (data) {
				var o = {
					view: {
						data: data
					}
				};
				search_controller(o);
			},
			error: function (){
				// if http error send back to view
				var o = {
					view: {
						header_message: 'Couldn\'t communicate with server. Please make sure you are connected to the Internet.'
					}
				};
				search_controller(o);
			}
		})
		
	}
	
}

function search_controller( options ) {	
	
	if(typeof options !== 'undefined' &&
	   ((typeof options.search_location !== 'undefined') || (typeof options.search_camera !== 'undefined') || (typeof options.search_camera_models !== 'undefined') || (typeof options.search_support_gear !== 'undefined') || (typeof options.search_specialties !== 'undefined'))   )	{
		
		
		var location = (typeof options.search_location !== 'undefined') ? options.search_location.val() :  '';
		var distance = (typeof options.search_distance !== 'undefined') ? options.search_distance.val() :  '';
		var camera = (typeof options.search_camera !== 'undefined') ? options.search_camera.val() :  '';
		var camera_models = (typeof options.search_camera_models !== 'undefined') ? options.search_camera_models.val() :  '';
		var support_gear = (typeof options.search_support_gear !== 'undefined') ? options.search_support_gear.val() :  '';
		var specialties = (typeof options.search_specialties !== 'undefined') ? options.search_specialties.val() :  '';
		
		options.location = location;
		options.distance = distance;
		options.camera = camera;
		options.camera_models = camera_models;
		options.support_gear = support_gear;
		options.specialties = specialties;
		
		// we only want one search_timeout to be running at any one time
		clearTimeout(search_timeout);
		
		// if you hit enter it will make the ajax call immediately
		if (typeof options.e !== 'undefined' && options.e.keyCode == 13) {
			search_model(options);
			return false;
		}
		
		// send ajax call 400ms after last keyup
		/*
		search_timeout = setTimeout(
			function () {
				// send search value to model
				search_model(options);
			},
			400
		);
		*/
		
	}
	
	// if there's ajax data, send it to the view
	if ( typeof options !== 'undefined' && typeof options.view !== 'undefined' ) {
		search_view(options);
	}
	
}

function search_view( options ) {
	
	if ( typeof options !== 'undefined'&& typeof options.view !== 'undefined' && typeof options.view.data !== 'undefined' ) {
		
		var data = options.view.data;
		
		//Set the results text.
		$('#search-results').html(data);
		
		// open only if hidden
		//$('#search-results:hidden').slideDown(400);
		$('#search-results').show(400);

		// change header message
		header_message_helper( (data.split('User #').length - 1) + ' Result' + (data.split('User #').length == 2 ? '' : 's') + ' Found.' );
		
	}
	
	if( typeof options !== 'undefined'&& typeof options.view !== 'undefined' && typeof options.view.header_message !== 'undefined' ) {
		header_message_helper( options.view.header_message );
	}
}


function header_message_helper( message ) {

	// fade out, insert text, fade in
	$('#search-results-header')
		.animate(
			{
				'opacity': 0
			},
			'fast', 
			function () { 
				$(this).text(message).animate(
					{
						'opacity': 1
					},
					'slow'
				)
			});
}

$.fn.dds_after = function () {
	return this.each(function () {
		//console.log(this);
	})
}

function dds_after( dt ) {
	// next element after the dt, hopefully a dd
	var next = dt.next();
	
	var dds = [];
	
	i = 0;
	
	// if there's even an element after
	if( next.get(0) ) {
		
		// while I still find dd's
		while( next.get(0).nodeName == 'DD' ) {
			
			// add to dds array
			dds.push(next);
			
			// get next dd
			next = $(next).next();		
			
			// if there aren't any dom elements next break out
			if( next.length == 0 ) {
				break;
			}
			
			// prevents endless loops in case of emergency
			i++;
			if( i > 500 ) {
				break;
			}
		}
	}
	return dds;
}

function remove_dds( dt ) {
	
	dds = dds_after( dt );
	
	for( key in dds ) {
		
		// my intentions were to smoothly fade out before removing
		/*dds[key].hide(
			'slow', 
			function() {
				// remove dds
				dds[key].remove();	
			}
		);*/
		
		// remove dds
		dds[key].remove();
		
	}
}

$(document).ready( function() {
	
	// GLOBAL VARIABLES
	search_timeout = null;

	$('#search_location, #search_support_gear').keyup(function(e){
		var o = {};
		o.search_location = $('#search_location');
		o.search_distance = $('#search_distance');
		o.search_camera = $('#search_camera');
		o.search_camera_models = $('#search_camera_models');
		o.search_support_gear = $('#search_support_gear');
		o.search_specialties = $('#search_specialties');
		o.e = e;
		search_controller(o);
	});
	
	$('#search_submit_button').click(function(e){
		var o = {};
		o.search_location = $('#search_location');
		o.search_distance = $('#search_distance');
		o.search_camera = $('#search_camera');
		o.search_camera_models = $('#search_camera_models');
		o.search_support_gear = $('#search_support_gear');
		o.search_specialties = $('#search_specialties');
		o.e = e;
		o.e.keyCode = 13; //Simulate an enter
		search_controller(o);
	});
	
	
	
});