$(function(){
	setupAutoFill();
//	setupAutoComplete(); //phase 2
});

/* There are 2 modes to this method,
 * 1)Put this function on an input element that has a corresponding button with an idea of the input button + -button appended at the end
 * i.e. <input id="do-something" value="72" /> <button id="do-something-button" rel="/path/to/rest/action/"></button>
 * $('#do-something').requestJSON('click-input',my_callback_function); will send a REST request to /path/to/rest/action/72
 * 
 * 2)Put this method on any thing, and set the the rel attribute to a backend method, this could be used to refresh an elements html, A value is optional
 * i.e. <select id="do-something2" rel="/path/to/rest/action/" value="72"></select>
 * $("#do-something2").requestJSON('none',my_callback_function); will send a REST request to /path/to/rest/action/72
 *
 * @param function
 */
jQuery.fn.requestJSON = function(obj){
	//allows this to be an array of objects i.e $('.someclass').requestJSON, but still allows a single element call i.e $('#someid').requestJSON
	if(obj.action == 'click-input'){
		$(this).each(function(){
				var input_object = $(this);
				var button_object_class = '#'+input_object.attr('id')+'-button';
				$(button_object_class).live('click',function(){
					//alert(input_object.val());
					var value = $(input_object).attr('value');
					var func = function(data){obj.after(data)};
					$.getJSON(obj.url+value,func);

					return false;
					});

				});
	}
	else if(obj.action == 'change'){
		$(this).each(function(){
				var select_object = $(this);
				var select_object_id = '#'+select_object.attr('id')+' :selected';
				select_object.change(function(){
					//the option that is selected
					var selected = $(select_object_id);
					//the value being sent 
					var selected_value = selected.attr('value');
					var func = function(data){obj.after(data,selected)};
					$.getJSON(obj.url+selected_value,func);

					});
		});
	}
	else if(obj.action == 'none'){
			var rel = $(this).attr('rel');
			var value = $(this).attr('value');
			var func = function(data){obj.after(data)};
			//if value is not 0 appened it to the rel path
			//else just send request to rel
			if(!value){
				$.getJSON(rel+value,func);
			}
			else{
				$.getJSON(rel,func);
		
			}
	}
}


/* Put this on a button, it then attempts to serialize a form with an id of the button + '-form', then posts to the form action
 * and then, by default, will attempt to refresh a div with an id of the button + '-div', or do whatever action is specified in the passed func
 *
 * @param function
 */
jQuery.fn.submitWithAjax = function(func)
{
	$(this).live('click',function(){
		var form_id ='#'+$(this).attr('id')+'-form';
		if(typeof func != 'function'){
			var div_id = '#'+$(this).attr('id')+'-div';
			func =function(response){
				$(div_id).html(response);	
			}
		}

		$.post($(form_id).attr('action'), $(form_id).serialize(), func);
	});
};

jQuery.fn.handle_upload = function(attr){
	 var el = $(this);

	 //pull the id of the input off the end of the id
	 var input_id = $(this).attr('id').match('[a-zA-Z0-9]+$');
	 input_id ='input#'+input_id;

	 //on change load 'el' with the value of the input_id element
	 $(input_id).change(function(){
			if(attr == 'html'){
			 $(el).html($(this).attr('value'));
			}
			else{
			 $(el).attr(attr,$(this).attr('value'));
			}
	});
}

//this clears the input text fields when clicked on and restores the text if they are left blank
function setupAutoFill() {
	//look for text fields and textareas that have the "wipe" class
	$('input.wipe, textarea.wipe').each(function() {
		//stash the current value
		var v = $(this).val();
		$(this).focus(function(){
			//blank out the input field on focus
			if ($(this).val() == v) {
				$(this).val("");
				//this prevents our "grayed out" default text from looking the same as what the user types in
				$(this).removeClass('prepopulated'); 
			} 
		}).blur(function(){
			//restore previous value if field was left blank
			if ($(this).val() == "") {
				$(this).val(v);
				//this prevents our "grayed out" default text from looking the same as what the user types in
				$(this).addClass('prepopulated');
			}
		});		
	});
}

function setupAutoComplete(){
	var search_url = '/search/agency_autocomplete';
	$('#search-agency').autocomplete(search_url, {
		max: 6,
		minChars: 2,
		//row is the results row, row position starts at 1, total is number of items in the list of results, 
		formatItem: function(row, row_position, total, search_term){
			return '<a href="#">' + row[0] + '</a>';
		}
	});
}
