var Search = {
  default_text: 'Search by keyword',
  init: function() {
    this._bindActions();
    $('#search_keyword').css('color', 'gray');
  },

  _bindActions: function() {

    var obj = this;
    $('#search_keyword').focus(function() {
      $('#search_keyword').css('color', 'black');
      $(this).val('');
    });

    $('#search_keyword').blur(function() {
      if($(this).val() == '') {
        obj._blurKeyword();
      }
    });

    $('#search_form').ajaxForm({
      beforeSend: function(xhr) {
        xhr.setRequestHeader("Content-type", "text/html;charset=iso-8859-1");
        if(xhr.overrideMimeType) {
          xhr.overrideMimeType('text/html; charset=iso-8859-1');
        }
      },
      success: function(responseText) {
        GaspareStore.showPopup(responseText, Category);
        obj._checkResults();
        obj._blurKeyword();
      }
    });


    $('#search_button').click(function(e) {
      e.preventDefault();
      var keyword = $('#search_keyword');
      if ( (keyword.val() != this.default_text) && (keyword.val() != '') ){
        $('#search_form').submit();
      } else {
        alert('Please enter a valid search term');
      }
    });
  },

  _blurKeyword: function() {
      var keyword = $('#search_keyword');
      keyword.val(this.default_text);
      $('#search_keyword').css('color', 'gray');
  },

  _checkResults: function() {
    // if 0 results show a few of our products
    if ($('.category_product').length != 0) {
      return false;
    }

    var message =
      '<p class="empty_result_message">The search for "' + $('#keyword_search').html() + '" did not produce any results.';
    message = message + '<br/>Others have enjoyed eating and giving these:</p>';
    $('#category_product_block').css('textAlign', 'left');
    $('#category_product_block').html(message); 

    // pull 6 random products 
    var items = $('#most_popular').find('a');
    var numbers = [];

    for(var count = 0; count < 6; count++) {
      do {
        var random_item = Math.floor(Math.random()*(items.length));
        if (numbers.length == 0) {
          numbers.push(random_item);
        } else {
          if ($.inArray(random_item, numbers) == -1) {
            numbers.push(random_item);
          }
        }
      } while (numbers.length != count+1);
      

      var cloned_product = $(items[random_item]).clone();

      // replace thumbnail with non-thumbnail
      var cp_src = cloned_product.find('img').attr('src');
      var non_thumb = cp_src.replace('-thumb', '');
      
      cloned_product.addClass('product_from_selection');
      cloned_product.find('img').attr('src', non_thumb);
      cloned_product.find('img').attr('width', 158);
      cloned_product.find('img').attr('height', 158);

      cloned_product.css('marginLeft', '35px');
      cloned_product.css('marginBottom', '30px');
      cloned_product.css('display', 'block');
      cloned_product.css('float', 'left');

      $('#category_product_block').append(cloned_product);
      if (count == 2) {
        $('#category_product_block').append($('<div class="clear"></div>'));
      }

    }

    //bind click to open products
    $('#category_product_block').find('a').each(function() {

      // set link to "loadPopup"
      $(this).click(function(e) {
        // prevent default link action
        e.preventDefault();
        // load the popup for a Product
        GaspareStore._loadPopup(GsProduct, $(this).attr('href'));

        return false;
      });
    });

  }
};

