﻿
// Utility functions that require jQuery


// See http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery
String.prototype.startsWith = function (prefix) {
  return (this.substr(0, prefix.length) === prefix);
}
// See http://stackoverflow.com/questions/280634/endswith-in-javascript/2548133#2548133
String.prototype.endsWith = function(suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
};


// ------------------------------------------------------------------------
// This implements target="_blank" for XHTML-Strict for all external URLs.  
// See http://www.badlydrawntoy.com/2009/03/03/replacing-target_blank-for-strict-xhtml-using-jquery-redux/
// Works for <a> and <area> elements.
// ------------------------------------------------------------------------
function _cstmENR_SetExternalUrlClickHandler() {
  $("[href^='http://']:not([href*='" + location.hostname + "'])").click(function () {

    if ($(this).attr("href").startsWith("http://www.youtube.com/watch?v=")) {
      // Display YouTube videos on a fancybox. 
      // Works for links that start with "http://www.youtube.com/watch?v=".
      $.fancybox({
        'padding': 0,
        'autoScale': false,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'title': this.title,
        'width': 598,
        'height': 360,
        'href': this.href.replace(new RegExp("watch\\?v=", "i"), 'v/') + "&amp;autoplay=1",
        'type': 'swf',
        'swf': { 'wmode': 'transparent', 'allowfullscreen': 'true' }
      });
    } else {
      // Open new window/tab for all other external links
      window.open($(this).attr("href"));
    }
    return false;
  });
};


$(function () {

  // ------------------------------------------------------------------------
  // Set class on first element of top-level menu
  // ------------------------------------------------------------------------
  $("#s4-topheader2 .s4-tn .menu-horizontal > ul.root > li.static > ul.static > li:first > a").addClass("first")
  
  // ------------------------------------------------------------------------
  // Social popup (see http://stackoverflow.com/questions/1273566/how-do-i-check-if-the-mouse-is-over-an-element-in-jquery)
  // ------------------------------------------------------------------------
  $(".sociallinks a").hover(
    function () {
      clearTimeout($("#socialpopup").data("timeoutId"));
      cstmShowSocialPopup();
    },
    function () {
      var timeoutId = setTimeout(function () { cstmHideSocialPopup(); }, 300);
      $("#socialpopup").data("timeoutId", timeoutId);
    }
  );

  $("#socialpopup").hover(
    function () {
      clearTimeout($("#socialpopup").data("timeoutId"));
    },
    function () {
      var timeoutId = setTimeout(function () { cstmHideSocialPopup(); }, 300);
      $("#socialpopup").data("timeoutId", timeoutId);
    }
  );

  function cstmShowSocialPopup() {
    if (jQuery.support.opacity) {
      $("#socialpopup").fadeIn(600);
    } else {
      $("#socialpopup").show();
    };
  };

  function cstmHideSocialPopup() {
    if (jQuery.support.opacity) {
      $("#socialpopup").fadeOut(600);
    } else {
      $("#socialpopup").hide();
    };
  };

  // Disable clicks on social links
  $(".sociallinks a").click(function () {
    return false;
  });

  // Adjust position of social popup--if home.js had moved it (by adding a class), then reset.
  $("#masthead .sociallinks a").mouseenter(function () {
    $("#socialpopup").removeClass();
  });

  // ------------------------------------------------------------------------
  // Analytics tracking:
  // ------------------------------------------------------------------------
  if (typeof pageTracker === "object") {
    // When FB/Twitter on social popup from masthead or homepage are clicked, then track it
    $("#socialpopup ul.accounts a").click(function () {
      if ($("#socialpopup").hasClass("homebannersocialpopup")) {
        //pageTracker._trackPageview("Home: " + $(this).attr("title"));
        pageTracker._trackEvent("exit", $(this).attr("href"), "home_follow_us");
      } else {
        //pageTracker._trackPageview("Masthead: " + $(this).attr("title"));
        pageTracker._trackEvent("exit", $(this).attr("href"), "top_follow_us");
      }
    });

    // Event tracking on search buttons    
    $("#searchcontainer a").click(function () {
      pageTracker._trackEvent("search", "searchbox", "");
    });

    // Event tracking on main nav top-level
    $("#s4-topheader2 ul.root > li > ul.static > li.static > a.menu-item").click(function () {
      var itemtext = $(this).find(".menu-item-text").text();
      if ($(this).attr("href").startsWith("http://") && !$(this).attr("href").startsWith("http://" + location.hostname + "/")) {
        // Click sends browser to a different site
        pageTracker._trackEvent("exit", itemtext, "main_nav");
      } else {
        // Top-level menu click
        pageTracker._trackEvent("main_nav", itemtext, "link");
      }
    });

    // Event tracking on main nav submenus
    $("#s4-topheader2 ul.root > li > ul.static > li.static > ul > li > a.menu-item").click(function () {
      var topleveltext = $(this).closest("li.static").children("a.menu-item").find(".menu-item-text").text();
      var itemtext = $(this).find(".menu-item-text").text();
      if ($(this).attr("href").startsWith("http://") && !$(this).attr("href").startsWith("http://" + location.hostname + "/")) {
        // Click sends browser to a different site
        pageTracker._trackEvent("exit", itemtext, "main_nav");
      } else {
        // Submenu click
        pageTracker._trackEvent("main_nav", topleveltext, itemtext);
      }
    });
    
    // Event tracking on battery finder
    $("#utilitynav .powerfinder").click(function () {
      pageTracker._trackEvent("utility_nav", "Power Finder", "link");
    });

    // Event tracking on footer
    $("#footerlinks a").click(function () {
      pageTracker._trackEvent("footer_nav", $(this).text(), "link");
    });
  }


  // ------------------------------------------------------------------------
  // Go to global link
  // ------------------------------------------------------------------------
  $("#gotogloballink").click(function () {
    if ($("#globalWebsiteBox").val() != null && $("#globalWebsiteBox").val() !== "") {
      // Track the event
      if (typeof pageTracker === "object") {
        pageTracker._trackEvent("change_country", "", "");
      }
      // Go to the global site
      location.href = $("#globalWebsiteBox").val();
    }
    return false;
  });

  // Set click handler for external URLs
  _cstmENR_SetExternalUrlClickHandler();


});




