var trademarks_text = "Search almost ten million trademarks";
$(document).ready(function() {
  $( "#search" ).focus(function(event) {
    value = event.currentTarget.value;
    if(value == trademarks_text) {
      event.currentTarget.value = "";
      $("#search").css('color','#000000');
      $("#search").autocomplete("enable");
    }
  } );
  $( "#search" ).blur(function(event) {
    value = event.currentTarget.value;
    if(value == "") {
      event.currentTarget.value = trademarks_text;
      $("#search").css('color','#999');
      $("#search").autocomplete("disable");
    }
  } );
  $( "#search" ).autocomplete({
      source: function( request, response ) {

      var postData = {
        "search": request.term,
        "fields": ["mark"]
      };

      console.log(JSON.stringify(postData));

      $.ajax({
        url: "http://search.trade.mar.cx/_search",
        type: "POST",
        crossDomain: true,
        contentType:"application/json",
        data: JSON.stringify(postData),
        dataType: "json",
        success: function( data, status, jq ) {
          console.log("Successful Pull");
          response(data);
        },
        error: function( jq, status, error ) {
          console.log("Failed to Connect to Server");
        }
      });
    },
    minLength: 3,
    delay: 1000,
    select: function( event, ui ) {
      $("#search").val(ui.item.value);
      $("#search_form").submit();
    },
    open: function() {
      $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
    },
    close: function() {
          $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
    }
  });
});


