$(function() {
  $("li.domain-switcher ul.regions li a").click(function() {
    // ajax request on the url requested
    var href = $(this).attr("href");
    var url = $(this).attr("href") + relative_url;

    var rel_url = "/"+relative_url;
    var region = (href == "http://www.dfs.ie/") ? "Irish" : "UK";

    // google tracking
    _gaq.push(['_trackEvent', 'Domain Dropdown', region, rel_url]);

    $.post("domain-check/", { "url" : url }, function(response) {
      // if the response is okay - we can proceed, otherwise, send the user to the homepage
      if (response != null) {
        if (response.msg == "OK") {
          if (response.reload_url) {
            window.location = base_href + relative_url;
            return false;
          }

          window.location = url;
          return false;
        } else if (response.msg == "ERROR") {
          // redirect to homepage
          window.location = href;
          return false;
        }
      }
    }, "json");

    return false;
  });
});
