﻿$(document).ready(function () {
  $(".jsHide").hide();
  $("a[href*='action=quickAdd']").each(function () {
    $(this).parent().removeClass("pinkArrow");
    $(this).replaceWith("<a class=\"buyBtnInline\" href=\"" + $(this).attr("href") + "\"><div class=\"buttonBg\"><div class=\"buttonLeft\"></div><span><nobr>" + $(this).text() + "</nobr></span></div></a>");
  });
  $("#countrySelect").change(function () {
    if ($(this).val() != "UK") {
      $("#deliveryMessage").show();
    } else {
      $("#deliveryMessage").hide();
    }
  });
  ClickableProducts();
  //Do the same for producers listings, but not on product pages
  $("#producersWrapper .producerListing").mouseenter(function () {
    $(this).addClass("highlighted");
    $(this).css("cursor", "pointer");
  });
  $("#producersWrapper .producerListing").mouseleave(function () {
    $(this).removeClass("highlighted");
    $(this).css("cursor", "auto");
  });
  $("#producersWrapper .producerListing").click(function () {
    window.location.href = $(this).find(".producerMoreLink a")[0].href;
    return false;
  });

  //Make tabs work on product pages
  $("#productTabs").find("li").find("a").click(function () {
    $(".productTab").addClass("hidden");
    var tabId = $(this).attr("id");
    var contentId = tabId.substring(0, tabId.indexOf("Tab"));
    $("#" + contentId + "Content").removeClass("hidden");
    $(this).parent().parent().find("li").removeClass("selected");
    $(this).parent().addClass("selected")
    return false;
  });

  $("#productTabs").find("li.selected").find("a").click();

  //Make price type switcher work
  $(".switchPriceTypeLink").find("a").click(function () {
    $("#productPrice").find(".productPriceType").removeClass("hidden");
    $(this).parent().parent().addClass("hidden");
    //update the buy form
    var selectedPriceType = $(this).parent().parent().attr("id");
    if (selectedPriceType == "productDutyPrice") {
      $('input[name=priceType]').val("duty");
    } else {
      $('input[name=priceType]').val("bond");
    }
    return false;
  });

  // Need to have at least 1 wine selected to request from reserves
  $("#reservesList .button").click(function () {
    if ($("#reservesTable .reservesCheckbox:checked").length == 0) {
      alert("Please select 1 or more wines to include in your request");
      return false;
    }
  });

  $(".jsShow").show();
  $(".jsHide").hide();
});

function ClickableProducts() {
    //Change wine search results to highlight on mouseovers and work like links
    $(".wineSearchResult").mouseenter(function () {
        $(this).addClass("highlighted");
        $(this).css("cursor", "pointer");
    });
    $(".wineSearchResult").mouseleave(function () {
        $(this).removeClass("highlighted");
        $(this).css("cursor", "auto");
    });
    $(".searchResultBuy").click(function (e) {
        e.stopPropagation();
        return false;
    });
    $(".wineSearchResult").click(function () {
        window.location.href = $(this).find(".searchResultTitle a.viewWineDetails")[0].href;
        //return false;
    });
}

function AddProduct(id, qty) {
  window.location.href = "/checkout.aspx?section=orderOverview&action=quickAdd&productId=" + id + "&qty=" + qty;
  return false;
}

