﻿$(document).ready(function(){
  initBehaviour();
});

function initBehaviour()
{
  initStopFlicker();  
  initActAsLinkHover();
  initButtonHover();
  //initSmoothScroll();
  initFormEnhancements();
  initContactPod();
  initSearchBox();
  searchDate();
  initTabs();
  initSubNav();
  $(".sharingWrapper").sharing();
  initNewsPod();
  initPaging();
} // initBehaviour()

function initStopFlicker()
{
  try { document.execCommand("BackgroundImageCache", false, true); } catch(err) {}
} // initStopFlicker()

function initActAsLinkHover()
{
  $(".actAsLink").hover(
    function(){ $(this).css("text-decoration", "none"); }, // over
    function(){ $(this).css("text-decoration", "underline"); } // out
  );
  if($.browser.opera) { $("label.actAsLink").one("click", function(){ $(this).find("input").trigger("click"); }); }
} // initActAsLinkHover()

function initButtonHover()
{
  $(".btnHover").hover(
    function(){ $(this).css("background-position", "left bottom"); }, // over
    function(){ $(this).css("background-position", "left top"); } // out
  );
} // initButtonHover()

function initSmoothScroll()
{
	$("a[href*=#]").click(function(){
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) 
		{
			var target = $(this.hash);
			target = target.length && target || $("[name=" + this.hash.slice(1) +"]");
			if (target.length) 
			{
        var targetOffset = target.offset().top;
        $("html,body") .animate({scrollTop: targetOffset}, "slow");
        return false;
			}
		}
	});
} // initSmoothScroll()

function initFormEnhancements()
{
  if($.browser.msie)
  {
    $("input.txt").focus(function(){ /* Insert Focus State */ }).blur(function(){ /* Remove Focus State */ });
    $("textarea.txt").focus(function(){ /* Insert Focus State */ }).blur(function(){ /* Remove Focus State */ });
    $("select.ddl").focus(function(){ /* Insert Focus State */ }).blur(function(){ /* Insert Focus State */ });
  }
} // initFormEnhancements()

function initContactPod() {  
  var contactErrorLength = $("div.contactUsPodWrapper ul.error").length;
  
  if (contactErrorLength == 0) { $("div.contactUsPodWrapper").hide(); }
  $("div.contactPod a.contactActivate").click(function () {
    $("div.contactUsPodWrapper").slideToggle();
    return false;
  });

  if ($("div.contactPod").find("div.thankYouWrapper").length > 0) { $("div.contactUsPodWrapper").show(); }

} // initNavigation();

function initSearchBox() {
    $("input.txtSearch").focus(function () {
        var value = $(this).val();
        if (value == "Search here" || value == "Enter a search term") { $(this).val(""); }
    });

    $("input.txtSearch").blur(function () {
        var value = $(this).val();
        if (value == "") { $(this).val("Search here"); }
    });
};

function searchDate() {
    $("p#searchDate").text(bab.date);
  } // searchDate()


function initTabs() {
  if ($('.jsTabs').length > 0 && $('.jsTabs').length > 0) {
    // cache in memory
    var $tabs = $('.jsTabs a');
    var $tabContent = $('.tabContent');


    // find out if any of the tabs have been initialized already.  If they have, then we don't want to auto-init the first tabs
    var autoinit = true;
    $('.jsTabs a').each(function () {
      if ($(this).parent().is(".active")) { autoinit = false; }
    });

    if (autoinit) {
      // hide all except first tabbed content area
      $tabContent.not(':first').hide();

      $tabs.filter(':first').parent().addClass('active');
    }

    // bind click events
    $tabs.click(function (event) {

      // alt to return false
      event.preventDefault(); 

      // remove & set correct active tab link
      $tabs.parent().removeClass('active'); 
      $(this).parent().addClass('active');

      // Hide tab content, show correct tab content
      $tabContent.hide();           
      $(this.hash).parent().show();

      // EXTRAS
      // add hash to URL
      // look for hash on load and load the correct tab - for bookmarking
      // MEGA EXTRA
      // History management of tabs - for back button

    });

  }
}

function initSubNav()
{
  // Cache HTML from Footer
  navigationalHTMLSections = {};
  navigationalHTMLSections.aboutUs = $("div.footerWrapper ul.menuItem1102 li ul.secondLevel").html();
  navigationalHTMLSections.markets = $("div.footerWrapper ul.menuItem1109 li ul.secondLevel").html();
  navigationalHTMLSections.capabilities = $("div.footerWrapper ul.menuItem1112 li ul.secondLevel").html();
  navigationalHTMLSections.investors = $("div.footerWrapper ul.menuItem1125 li ul.secondLevel").html();
  navigationalHTMLSections.careers = $("div.footerWrapper ul.menuItem1240 li ul.secondLevel").html();
  // Inject in Nav and remove old links
  $("ul.navigation li.menuItem1102 a").after("<ul class='secondLevel'>" + navigationalHTMLSections.aboutUs + "</ul>");
  $("ul.navigation li.menuItem1109 a").after("<ul class='secondLevel'>" + navigationalHTMLSections.markets + "</ul>");
  $("ul.navigation li.menuItem1112 a").after("<ul class='secondLevel'>" + navigationalHTMLSections.capabilities + "</ul>");
  $("ul.navigation li.menuItem1125 a").after("<ul class='secondLevel'>" + navigationalHTMLSections.investors + "</ul>");
  $("ul.navigation li.menuItem1240 a").after("<ul class='secondLevel'>" + navigationalHTMLSections.careers + "</ul>");
  // Wrap Nav Items
  $("ul.navigation li ul.secondLevel").wrap("<div class='subNavigation clearfix' />");
  $("ul.navigation li div.subNavigation").wrap("<div class='subNavigationContainer' />");
  // Append Arrow
  $("ul.navigation li div.subNavigationContainer").prepend("<p class='littleArrow'></p>")
  // Append titles / title links
  $("ul.navigation li a:first-child").each(function() {
    var liClass = $(this).parent().attr("class");
    var title = $("div.footer ul." + liClass + " a.firstLevel").attr("title");
    $(this).parent().find("div.subNavigation").prepend("<p class='title'>" + title + "</p>"); // titles
    $(this).parent().find("div.subNavigation ul.secondLevel").prepend("<li class='title'><a href='" + $(this).attr("href") + "'>" + $(this).html() + "</a></li>"); // title links
  });
  // Remove title attributes as they intefere with menu
  $("ul.navigation li a.firstLevel").attr("title", "");  
  // Hovering
  $("ul.navigation li").hover(function () {
    if (bab.ie6) { $("select.ddl").css("visibility", "hidden"); } // Hide drop downs ie6
    $(this).find("div.subNavigationContainer").css("visibility", "visible");
  }, function () {
    if (bab.ie6) { $("select.ddl").css("visibility", "visible"); } // Show drop downs ie6
    $(this).find("div.subNavigationContainer").css("visibility", "hidden");
  });
} // initSubNav()

function initNewsPod() 
{
  $("ul.latestNews").bannerPlugin({
    slideType: "upDown",
    delay: 250
  });
}

function initPaging() {
  $("p.numbering label input").each(function () {
    if ($(this).attr("disabled")) { $(this).parent().addClass("active"); }
  });
 
}
