///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// User Selector Class
function user_selector(options) {
  this.options = options;
  
  this.id = this.options.id;
  this.ajax_url = 'ajax.php';
  this.ajax_name = 'autocomplete_user'
  
  // styles
  this.input_class = 'form_input_big';
  
  // id for elements
  this.input_id = this.id+"_input";
  this.span_id  = this.id+"_span";
  this.hints_id = this.id+"_hints";
  this.form_input_id = this.id+"_forminput";  
  this.indicator_id = this.id+"_indicator";
  
  // State of location
  this.user_id = this.options.user_id ? this.options.user_id : 0;
  this.user_name = this.options.user_name ? this.options.user_name : "";
  
  // Options
  this.form_width = this.options.width != null ? this.options.width : 150;
  
  this.autocomplete = null;
}

user_selector.prototype.generate_html = function() {
  var html = "";

  if(this.user_id != null) {
    html += '<span><input id="'+this.input_id+'" name="'+this.input_id+'" class="'+this.input_class+'" type="text" style="width:'+this.form_width+'px;" value="'+this.user_name+'" /></span>';
    html += '<span style="margin-left:5px;vertical-align:middle;display:none" id="'+this.indicator_id+'"><img src="layout/progress.gif"></span>';
    html += '<div id="'+this.hints_id+'" class="autocomplete"></div>';
    html += '<input id="'+this.form_input_id+'" name="'+this.form_input_id+'" type="hidden" value="'+this.user_id+'" />';
  }
  else {
    html += '<span><input id="'+this.input_id+'" name="'+this.input_id+'" class="'+this.input_class+'" type="text" style="width:'+this.form_width+'px" /></span>';
    html += '<span style="margin-left:5px;vertical-align:middle;display:none" id="'+this.indicator_id+'"><img src="layout/progress.gif"></span>';
    html += '<div id="'+this.hints_id+'" class="autocomplete"></div>';
    html += '<input id="'+this.form_input_id+'" name="'+this.form_input_id+'" type="hidden" value="0" />';
  }
  
  $(this.id).innerHTML = html;
}

user_selector.prototype.init_ajax = function() {
  var autocomplete_param = '_action='+this.ajax_name+'&input_id='+this.input_id;
  this.autocomplete = new Ajax.Autocompleter(this.input_id, this.hints_id, this.ajax_url, {indicator:this.indicator_id, object:this, updateElement : this.select, parameters: autocomplete_param});
}

user_selector.prototype.select = function(li) {
  // Parsed variables
	var user_id = li.id;
	
	var str = $('user_select_'+user_id).innerHTML;
	str = str.replace(/\<b\>/gi, "");
	str = str.replace(/\<\/b\>/gi, "");
  
	$(this.input_id).value = str;
  $(this.form_input_id).value = user_id;
}

// Outside functions
function user_selector_select_user(id, user_id, user_name) {
	$(id+"_input").value = user_name;
  $(id+"_forminput").value = user_id;
}

