

$(document).ready(function() {
  
  //Product Items
  var compareItem          = 0;
  var compareItemLimit     = 3;
  var currentItemID        = '';
  var currentProductItem   = '';
  var currentGroupItem     = '';
  
  var processingAJAX       = false;
  
  var dataurl = SITE_URL + 'ajax/products/getProductList';
  
  String.prototype.trim = function() {

    return this.replace(/^\s+|\s+$/g, '');
  }
  
  //Convert ASCII code to character
  String.prototype.asciiDecode = function() {

    return this.replace(/&#(\d+);/g, function (m, n) { 
      return String.fromCharCode(n); 
      })
  }
  
  function limitChars(name, limit) {

    if(name != null || name == '') {
      if(name.length > limit) name = name.slice(0, limit).trim() + '...';
    }

    return name;
  }
  
  function format(item) {

    return '<p>'+item.group+'<br><span>'+item.product+'</span></p>';
  }
  
  function searchAutoCompleteUI(stage, result) {
    
    if( stage == 'init' ) {
      $("#searchAutoComplete").attr("disabled", "disabled");
      $("#searchAutoCompleteButton").attr("disabled", "disabled");   
    } else {
      
      if( result != '' ) {
        $('#searchAutoComplete').val('LOADING PAGE...');
        window.location = result;
      } else {
          alert('There is no information for selected product.');
          $("#searchAutoComplete").removeAttr("disabled");
          $('#searchAutoCompleteButton').removeAttr("disabled");
          $("#searchAutoComplete").val('');
          processingAJAX = false;
      }
    }
     
  }
  
  function productListAutoCompleteUI(stage, result) {
    
    if( stage == 'init' ) {
      $('#loadingProductPageURL').html('checking product information').show();
      $("#autocompleteGoToProductPage").attr("disabled", "disabled");
      $("#productListAutoComplete").attr("disabled", "disabled");   
    } else {
      
      if( result != '' ) {
        $('#loadingProductPageURL').html('loading page');
        window.location = result;
      } else {
          alert('There is no information for selected product.');
          $('#autocompleteGoToProductPage').removeAttr("disabled");
          $("#productListAutoComplete").removeAttr("disabled");
          $('#loadingProductPageURL').html('').hide();
          processingAJAX = false;
      }
    }
     
  }
  
  $("#productListAutoComplete, #searchAutoComplete").autocomplete(dataurl, {
      multiple: false,
      parse: function(data) {
      return $.map(eval(data), function(row) {
        return {
                  data:   row,
                  value:  row.group,
                  result: limitChars(row.group, 25).asciiDecode() + ' : '
                          + limitChars(row.product, 30).asciiDecode()
        }
      });
    },
      formatItem: function(item) {
        return format(item);
      }
    }).result(function(e, item) {
        currentItemID       = item.id;
        currentProductItem  = item.product;
        currentGroupItem    = item.group;
        
        if(currentItemID !='' && processingAJAX == false) {
      
          processingAJAX = true;
          
          var object_id = $(this).attr('id');
          
          // Construct function to call ased on Element
          object_id+'UI'+"('init', null)";
          
          switch (object_id) {
            
            case 'searchAutoComplete':
              searchAutoCompleteUI('init', null);
              break;
              
            case 'productListAutoComplete':
              productListAutoCompleteUI('init', null);
              break;
          }        
          
          
          var dataURL = SITE_URL + 'ajax/products/getProductLink/' + currentItemID;
    
          $.ajax({
            type: 'POST',
            url: dataURL,
            datatype: "json",
            success: function(result) {
              
              switch (object_id) {
            
                case 'searchAutoComplete':
                  searchAutoCompleteUI(null, result);
                  break;
                  
                case 'productListAutoComplete':
                  productListAutoCompleteUI(null, result);
                  break;
              }              
                
            } 
          
        });

    }

  });
   

  
  $("#cse-search-box").submit(function(event) {
    
    if( $("#searchAutoComplete").val() == '' ) return false;

  });




});