function assembleSearchURL(psid, sc) {
  var searchURL = "http://shop.mywebgrocer.com/shop.aspx?strid=";
  searchURL += psid;
  searchURL += "&sc=" + sc + "&shopurl=search.aspx&k=";
  return searchURL;
}

function assembleGoogleSearchURL(searchTerms, domain) {
  var searchURL = "http://www.google.com/search?hl=en&q=";
  searchURL += searchTerms;
  searchURL += " " + domain;
  return searchURL;
}

function ClickSearch(psid, sc) {
  var searchTerms = document.getElementById("txtSearch").value;
  if (searchTerms != "" && searchTerms != "Search" && searchTerms.length >= 3) {
    if (psid != "") {
      var searchLocation = assembleSearchURL(psid, sc);
      searchLocation += searchTerms;
      window.top.location = searchLocation;
    }
  }
}

function assembleBuyURL(psid, pid, sc) {
  var searchURL = "http://shop.mywebgrocer.com/shop.aspx?&strid=";
  searchURL += psid;
  searchURL += "&shopurl=ProductDetail.aspx&pid=";
  searchURL += pid;
  searchURL += "&clk_src=seopd&sc=";
  searchURL += sc;
  return searchURL;
}

function ClickBuy(psid, pid, sc) {
  var searchLocation = assembleBuyURL(psid, pid, sc);
  //Redirect user
  window.top.location = searchLocation;
}

function makeAjaxCall(targetURL, targetElement, parameterToPost) {

  var myRequest = new ajaxObject(targetURL);

  myRequest.callback = function(responseText) {
    if (responseText.length > 0) {
      document.getElementById(targetElement).innerHTML = responseText;
    }
    else {
      document.getElementById(targetElement).style.visibility = 'hidden';
      document.getElementById(targetElement).innerHTML = '';
    }
  }

  myRequest.update(parameterToPost);
}

function loadZipStores(sku, chainid, psid, brand, product, size) {
  var param = 's=' + document.getElementById('txtZipCode').value + '&sku=' + sku + '&psid=' + psid + '&chainid=' + chainid;
  param += '&brand=' + brand + '&product=' + product + '&size=' + size;
  makeAjaxCall('/pd/ajax/AvailableStores.aspx', 'otherStoresList', param);
}

function merchCarouselItemLoadCallback(carousel, state) {
  for (var i = carousel.first; i <= carousel.last; i++) {
    if (carousel.has(i)) {
      continue;
    }
    if (i > merchCarouselItems.length) {
      break;
    }
    carousel.add(i, merchCarouselGetItemHTML(merchCarouselItems[i - 1]));
  }
};

$(document).ready(function ClickSearchText() {
  //round corners
  DD_roundies.addRule('.allRound_10px', '10px', true);

  //round corners for product tabs in other browsers
  if (!$.browser.msie) {
    DD_roundies.addRule('#productTabs li a', '5px 5px 0 0', true);
    DD_roundies.addRule('.tab', '0 5px 5px 5px', true);
  }

  // activate Buy Now button
  $(".shopButton").button();

  //show details tab
  $('#productDetailsTab').show();

  //product tab click
  $('#productTabs li a').click(
        function() {
          $('#productTabs li a').removeClass('activeBtn');
          $(this).addClass('activeBtn');
          var tab = $(this).attr('rel');
          $('.tab').hide();
          $(tab).show();
        }
    )

  //hide loading
  $('#merchCarouselLoading').hide();
  
  //init jcarousel
    jQuery('#merchCarousel').jcarousel({
    scroll: 1,
      size: merchCarouselItemsLength,
      itemLoadCallback: { onBeforeAnimation: merchCarouselItemLoadCallback }
    });

  //global vars
  var searchBoxDefault = $('#txtSearch').val();

  //Searchbox show/hide default text if needed
  $('#txtSearch').focus(function() {
    if ($('#txtSearch').attr("value") == searchBoxDefault) $('#txtSearch').attr("value", "");
  });
  $('#txtSearch').blur(function() {
    if ($('#txtSearch').attr("value") == "") $('#txtSearch').attr("value", searchBoxDefault);
  });

  //performe search when pressing enter on header search box
  $('#txtSearch').keypress(
  function(e) {
    if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
      $('#btnSearch').click();
      return false;
    } else {
      return true;
    }
  });


});
