$(document).ready(function() {
	Drupal.jsAC.prototype.select = function (node) {
	    if (this.input.id=="edit-searchstring") {
	    	var _input = $('.sfr_jt #edit-searchstring');
	    	if (_input.size()) {
	    		//SFR JT 
		        $("#sphinx-simple-search-form").unbind("submit");
//		        this.input.value = node.nid;
//		        console.log(node.nid);
		        window.location.href = node.nid;
//		        console.log(node.autocompleteValue);
	    	}
	    	else {
//	    		SFR LC
		        this.input.value = node.autocompleteValue;
		        $("#sphinx-simple-search-form").unbind("submit");
		        $("#sphinx-simple-search-form").submit();
	    	}
	    } else if(this.input.id=="edit-searchartiste") {
	        this.input.value = node.autocompleteValue;
	        $(".form_search_filt").unbind("submit");
	        $(".form_search_filt").submit();
	    } else if(this.input.id=="edit-searchvideo") {
	        this.input.value = node.autocompleteValue;
	        $(".form_search_sort").unbind("submit");
	        $(".form_search_sort").submit();
	    }
	};
	
	if ($(".sfr_jt").size()) {
		Drupal.jsAC.prototype.onkeydown = function (input, e) {
			  if (!e) {
			    e = window.event;
			  }
			  switch (e.keyCode) {
			    case 40: // down arrow
			      this.selectDown();
			      return false;
			    case 38: // up arrow
			      this.selectUp();
			      return false;
			    case 13: // Enter
//			    	console.log(this.selected.nid);
                    if(this.selected.nid) {
                    	window.location.href = this.selected.nid; 
                        return false;
                    }
                    return true;
			    default: // all other keys
			      return true;
			  }
		};
	
		Drupal.jsAC.prototype.found = function (matches) {
			  // If no value in the textfield, do not show the popup.
			  if (!this.input.value.length) {
			    return false;
			  }
			  
//			  console.log(matches);
			  
			  // Prepare matches
			  var ul = document.createElement('ul');
			  var ac = this;
			  for (key in matches) {
			    var li = document.createElement('li');
			    li.autocompleteValue = matches[key].replace(/ \(Membre\)| \(Concours\)| \(News\)/, "");
			    // Adding the nid of the item to the li object.
			    li.nid = key;
			    $(li)
			      .html('<div>'+ matches[key] +'</div>')
			      .mousedown(function () { ac.select(this); })
			      .mouseover(function () { ac.highlight(this); })
			      .mouseout(function () { ac.unhighlight(this); });
			    $(ul).append(li);
			  }
	
			  // Show popup with matches, if any
			  if (this.popup) {
			    if (ul.childNodes.length > 0) {
			      $(this.popup).empty().append(ul).show();
			    }
			    else {
			      $(this.popup).css({visibility: 'hidden'});
			      this.hidePopup();
			    }
			  }
		};
	}
	
	var select_categories = $('.sfr_jt #edit-iid');
	if (select_categories.size()) {
		initCategorieChangeEvents(select_categories);
	}
	
	
});

function initCategorieChangeEvents(elem) {
	elem.unbind('change', processCategorieChange);
	elem.bind('change', processCategorieChange);
}

function processCategorieChange() {
	var _this = $(this).unbind('change', processCategorieChange);
	var input_autocomp = $('.sfr_jt #edit-searchstring-autocomplete');
	
	if (input_autocomp.size()) {
		var categorie = _this.val().toLowerCase().replace(/ /, "-");
		categorie = categorie == "tous" ? "" : "/" + categorie;
		input_autocomp.val("/find/autocomplete" + categorie);
		
		var acdb = [];
	    var uri = input_autocomp.val();
	    acdb[uri] = new Drupal.ACDB(uri);
	    var input = $('.sfr_jt #edit-searchstring').unbind();
	    new Drupal.jsAC(input[0], acdb[uri]);
	}
	
	initCategorieChangeEvents(_this);
}
