//
// echo "王 ";
//
//
// jQuery.ui.combobox:
//
(function( jQuery ) {
	jQuery.widget( "ui.combobox", {
		_create: function() {
			var self = this,
				select = this.element.hide(),
				selected = select.children( ":selected" ),
				value = selected.val() ? selected.text() : "";

			var input = this.input = jQuery( "<input>" )
				//.insertAfter( select )
				.val( value )
				.autocomplete({
				  //oldvalue: null,
				  nMatched: 0,
					delay: 0,
					minLength: 0,
					source: function( request, response ) {
						//var matcher = new RegExp( jQuery.ui.autocomplete.escapeRegex(request.term), "i" );
						var matcher = new RegExp( request.term, "i" );
						response( select.children( "option" ).map(function() {
							var text = jQuery( this ).text();
							if ( this.value && ( !request.term || matcher.test(text) ) ) {
								return {
									label: text.replace(
										new RegExp("(" + request.term + ")", "i"),
										"<span class=\"ui-matched\">$1</span>" ),
									value: text,
									option: this
								};
						  }
						}));
					},
					select: function( event, ui ) {
						ui.item.option.selected = true;
						self._trigger( "selected", event, {
							item: ui.item.option
						});
//alert("select: " + ui.item.value);
        	  this.oldvalue = ui.item.value;
            jQuery(this).val(ui.item.value);
            this.form.submit();
					},
					change: function( event, ui ) {
						if ( !ui.item ) {
							var matcher = new RegExp( "^" + jQuery.ui.autocomplete.escapeRegex( jQuery(this).val() ) + "jQuery", "i" ),
								valid = false;
							select.children( "option" ).each(function() {
								if ( jQuery( this ).text().match( matcher ) ) {
									this.selected = valid = true;
									return false;
								}
							});
							if ( !valid ) {
								// remove invalid value, as it didn't match anything
								//jQuery( this ).val( "" );
								//select.val( "" );
								//input.data( "autocomplete" ).term = "";
								return false;
							}
						}
					}
				})
				.addClass( "ui-widget ui-widget-content ui-corner-left" )
			  .blur(function() {
          if(this.oldvalue != undefined)
          {
          	if(this.oldvalue != "")
          	{
              this.value = this.oldvalue;
            }
//alert('blur(): this.value=' + this.value);
          }
        })
        .keyup(function() {
          this.oldvalue = this.value;
        })
        .dblclick(function(event, ui) {
          // not yet done:
//alert("dblclick(): this.oldvalue=" + this.oldvalue + ", this.value=" + this.value + ", this.defaultValue=" + this.defaultValue);
          domToggleControlValue(this);
          if(this.value != "")
          {
            jQuery(this).autocomplete("search", this.value);
          }
        })
        .click(function(event, ui) {
//alert("click(): this.oldvalue=" + this.oldvalue + ", el.value=" + this.value + ", this.defaultValue=" + this.defaultValue);
//alert('click(): event=' + event.which + ', ' + event.shiftKey + ', ' + event.ctrlKey);
          if(event.shiftKey || event.ctrlKey)
          {
            jQuery(this).autocomplete("search", this.value);
          }
          else
          {
         	  if(this.oldvalue == undefined)
         	  {
              this.oldvalue = this.value;
            }
         	  else if(this.oldvalue == "")
         	  {
              this.oldvalue = this.value;
            }
            this.value = "";
          }
//alert('click(): this.oldvalue=' + this.oldvalue);
        });

			input.data( "autocomplete" )._renderItem = function( ul, item ) {
				return jQuery( "<li></li>" )
					.data( "item.autocomplete", item )
					.append( "<a>" + item.label + "</a>" )
					.appendTo( ul );
			};

      var locShowAllItems;
      if(locIsLangLocalFr())
      {
        locShowAllItems = "Afficher tous les éléments";
      }
      else if(locIsLangLocalCn())
      {
        locShowAllItems = "显示所有款项";
      }
      else
      {
        locShowAllItems = "Show All Items";
      }

			this.button = jQuery( "<button type='button'>&nbsp;</button>" )
				.attr( "tabIndex", -1 )
				.attr( "title", locShowAllItems )
				//.insertAfter( input )
				.button({
					icons: {
						primary: "ui-icon-triangle-1-s"
					},
					text: false
				})
				.removeClass( "ui-corner-all" )
				.addClass( "ui-corner-right ui-button-icon" )
				.click(function() {
					// close if already visible
					if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
						input.autocomplete( "close" );
						return;
					}

					// work around a bug (likely same cause as #5265)
					jQuery( this ).blur();

					// pass empty string as value to search for, displaying all results
					input.autocomplete( "search", "" );
					input.focus();
				});

  			var container = this.container = jQuery( "<span class=\"nowrap\"></span>" )
	  			.append(input)
	  			.append(this.button)
	  			.insertAfter( select );
		},

		destroy: function() {
			this.input.remove();
			this.button.remove();
			this.element.show();
			jQuery.Widget.prototype.destroy.call( this );
		},

		addClassMore: function(strClass) {
			this.input.addClass(strClass);
		}
	})
})( jQuery );

jQuery(function() {
	jQuery( "#categoryref,#recipesref" ).combobox();
	jQuery( "#toggle" ).click(function() {
		jQuery( "#recipesref" ).toggle();
	});

	jQuery(".khasimageyesno").click(function() {
		if(this.value == undefined)
		{
      jQuery.ideographZoomImage = (! jQuery.ideographZoomImage);
    }
		else
		{
      jQuery.ideographZoomImage = parseInt(this.value);
    }
  });

});
