+
+var availableTags = [
+<?php
+
+foreach(array_keys($userAllTags) as $key) {
+ print '"'.$userAllTags[$key]['tag'].'",'."\n";
+}
+?>
+];
+function split( val ) {
+ return val.split( /,\s*/ );
+}
+function extractLast( term ) {
+ return split( term ).pop();
+}
+
+$( "#tags" )
+ // don't navigate away from the field on tab when selecting an item
+ .bind( "keydown", function( event ) {
+ if ( event.keyCode === $.ui.keyCode.TAB &&
+ $( this ).data( "autocomplete" ).menu.active ) {
+ event.preventDefault();
+ }
+ })
+ .autocomplete({
+ minLength: 2,
+ delay: 50,
+ source: function( request, response ) {
+ // if the term is >2 in legth delegate back to autocomplete
+ if (extractLast ( request.term ).length < 3) {
+ response( [] );
+ } else {
+ response( $.ui.autocomplete.filter(
+ availableTags, extractLast( request.term ) ) );
+ }
+ },
+ focus: function() {
+ // prevent value inserted on focus
+ return false;
+ },
+ select: function( event, ui ) {
+ var terms = split( this.value );
+ // remove the current input
+ terms.pop();
+ // add the selected item
+ var newterm = ui.item.value;
+ terms.push( newterm );
+
+ $("#popularTags").children().each(function() {
+ if (this.innerHTML == newterm) {
+ this.className = 'selected';
+ }
+ });
+ // add placeholder to get the comma-and-space at the end
+ terms.push( "" );
+ this.value = terms.join( ", " );
+ return false;
+ }
+});
+
+