// JavaScript Document
// Fix background image flickering in IE
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) { }

// Run jQuery in no conflict mode so that it will work in the same page as Prototype
// See jQuery docs for more info: http://docs.jquery.com/Core/jQuery.noConflict
jQuery.noConflict();

// To restore "normal usage" (not running in no conflict mode), simply delete the noConflict()
// line above and replace 'jQuery(' with '$(' in this file and any templates. Do not change any
// other JavaScript files as they are already built to support "normal" and no conflict mode
// correctly.

// Hide content with JS so that users w/out JS can still see it.
document.write('<style type="text/css">ul.expandableContent div.content { display: none; }</style>');

var ExpandableContent = {
  expandAllLinks: null,
  collapseAllLinks: null,
  titleLinks: null,
  init: function() {
    var ep = this;
    this.titleLinks = jQuery('ul.expandableContent > li > a.title').click(
      function() {
        if (jQuery(this).is('.open'))
          ep.close(jQuery(this));
        else
          ep.open(jQuery(this));
        return false;
      }
    );
    this.expandAllLinks = jQuery('ul.expandableContentControls a.expandAll').click( ep.openAll );
    this.collapseAllLinks = jQuery('ul.expandableContentControls a.collapseAll').click( ep.closeAll );
    ExpandableContent.checkState();
  },
  open: function($item) {
    if ($item.is('.open'))
      return;
    $item.blur().addClass('open').next().show();
    ExpandableContent.checkState();
    return false;
  },
  close: function($item) {
    if (! $item.is('.open'))
      return;
    $item.blur().removeClass('open').next().hide();
    ExpandableContent.checkState();
    return false;
  },
  openAll: function() {
    if (jQuery(this).blur().is('.disabled'))
      return false;
    ExpandableContent.titleLinks.each(
      function() { ExpandableContent.open(jQuery(this)); }
    );
    return false;
  },
  closeAll: function() {
    if (jQuery(this).blur().is('.disabled'))
      return false;
    ExpandableContent.titleLinks.each(
      function() { ExpandableContent.close(jQuery(this)); }
    );
    return false;
  },
  checkState: function() {
    if (this.titleLinks.filter( function() { return ! jQuery(this).is('.open'); }).length == 0) {
      // All Open
      this.expandAllLinks.addClass('disabled');
    } else if (this.titleLinks.filter('.open').length == 0) {
      // All Closed
      this.collapseAllLinks.addClass('disabled');
    } else {
      // Some open, some closed
      this.expandAllLinks.removeClass('disabled');
      this.collapseAllLinks.removeClass('disabled');
    }
  }
};
jQuery(function() { ExpandableContent.init(); });

var LocationPopup = {
  open: false,
  mapSrc: null,
  init: function() {
    var lp = this;
    lp.mapSrc = jQuery('#LocationPopupMap').attr('src');
    jQuery('#LocationPopup .header ul').click(
      function() {
        if (! lp.open)
        {
          jQuery('div.wrapper', jQuery('#LocationPopup').addClass('lpActive')).slideDown(200);
          lp.open = true;
        }
        return false;
      }
    );
    jQuery('#LocationPopup a.close').click(
      function() {
        if (lp.open)
        {
          jQuery('div.wrapper', jQuery('#LocationPopup').removeClass('lpActive')).slideUp(200);
          lp.open = false;
        }
        return false;
      }
    );
  },
  hilite: function(region) {
    var $map = jQuery('#LocationPopupMap');
    if ($map)
      $map.attr('src', $map.attr('src').replace(/map_short[\-a-z]*\.gif/g,"map_short-"+region+".gif"));
  },
  unhilite: function(region) {
    var $map = jQuery('#LocationPopupMap');
    if ($map)
      $map.attr('src', LocationPopup.mapSrc);
  },
  hiliteLinks: function(id) {
    jQuery('ul.countryList ul').removeClass('hilited');
    jQuery('#'+id).addClass('hilited');
    return false;
  }
};
jQuery(function() { LocationPopup.init(); });

/* Special search behavior */
var Search = {
  prompt: "Search", // default value - should get real value from markup
  init: function() { this.prompt = jQuery('#search').focus(this.focus).blur(this.blur).val(); },
  focus: function() { if (this.value == Search.prompt) { this.value = ''; } },
  blur: function() { if (this.value.length == 0) { this.value = Search.prompt; } }
};

jQuery(function() {
  Search.init();
});

jQuery(function() {
  jQuery('li.hasFlyout', jQuery('#NavigationSecondary')).hover(
    function() {
      jQuery(this).addClass('hover').children('a').addClass('hover');
    },
    function() {
      jQuery(this).removeClass('hover').children('a').removeClass('hover');
    }
  );
});

jQuery(function() {
  jQuery('ul.tabs li').hover( function() { jQuery(this).addClass('hover'); }, function() { jQuery(this).removeClass('hover'); });
});

/* Supporting JS for the print functionality. */
/*jQuery(function() {
  an_postPrintHandler = function() {
  	// Add headers to the tabbed content
  	jQuery('ul.tabs a').each( function() {
  	  $t = jQuery(this);
  	  href = $t.attr('href');
  	  $c = jQuery(href);
  	  $c.prepend('<h3>' + $t.html() + '</h3>')
  		.show(); // Since we're printing, we want all content shown
  	});
  	// "Unmark" the tabs as tabs so that the tabs JS doesn't hide the content.
  	jQuery('ul.tabs').removeClass('tabs').hide();

    ExpandableContent.openAll();

		if (window.print && jQuery.getURLParam("print")==1) {
      setTimeout("window.print()", 500); // dialog on demand - delayed so that page loads before dialog appears
		}
  };
  jQuery('#PrintLink').popupwindow(); 
  jQuery('#ContentChannel a').not(jQuery('ul.tabs a'))
	                    .not(jQuery('.ecommerce a.addToCart'))
	                    .not(jQuery('#Breadcrumbs a'))
	                    .not(jQuery('ul.expandableContent > li > a.title'))
	                    .not(jQuery('div.linksWithDates a.more'))
	                    .not(jQuery('div.linksWithDates a.prev'))
	                    .not(jQuery('div.searchBox a'))
	                    .not(jQuery('div.ecommerce a'))
	                    .not(jQuery('div.paginated a'))
	                    .printPage({ // Footnote the content
                               postPrintHandler: an_postPrintHandler
                          }); 
});*/

/* Audience Bar */
jQuery(function() {
  jQuery('.audienceBar > li').hover(
    function() { jQuery(this).addClass('hover').children('a').addClass('hover'); },
     function() { jQuery(this).removeClass('hover').children('a').removeClass('hover'); }
   ).click(
     function(e) {
      // Prevent event delegation from stepping on links clicked in overlay.
      $t = jQuery(e.target);
    if ($t.is('a') && $t.parents('.content').length > 0)
      return true;
	  //allow select clicks
	  if ($t.is('select') && $t.parents('.content').length > 0)
      return true;
	  if ($t.is('span') && $t.parents('.content').length > 0)
      return true;
	  if ($t.is('input') && $t.parents('.content').length > 0)
	  	return true;

       var $$ = jQuery(this);
       jQuery('li div.channel', $$.parent()).hide().next().hide();
       jQuery('div.channel', $$).show().next().show();
      
			//if(!jQuery('.home_cta') {
			e.preventDefault();
			
			//}
    }
  );
  jQuery('.audienceBar > li a.close').click(
    function(e) {
      jQuery(this).parents('.channel').hide().next().hide();
      e.stopPropagation();
      e.preventDefault();
    }
  );
  // override prevent default for action button -- HS.
	jQuery('.audienceBar .home_cta').click(
    function(e) {
      e.stopPropagation();
    }
  );
  jQuery('.audienceBar > li div.tab').click(
    function(e) {
      jQuery(this).hide().prev().hide();
      e.stopPropagation();
      e.preventDefault();
    }
  );
  // Show on load for development
  // jQuery('.audienceBar:eq(1) > li:eq(1)').click();
});

/* Login Box */
var LoginBox = {
  loginLink: null,
  loginBox: null,
  closeLink: null,
  passwordCleared: false,
  usernamePrompt: 'User Id',
  usernameField: null,
  passwordField: null,
  init: function() {
    var LB = this;
    LB.loginLink = jQuery('#Toolbar a.login');
    LB.loginBox = jQuery('#Login');
    LB.usernameField = jQuery('input:eq(0)', LB.loginBox);
    LB.usernamePrompt = LB.usernameField.val();
    LB.passwordField = jQuery('input:eq(1)', LB.loginBox);
    LB.closeLink = jQuery('a.close', LB.loginBox).click( LB.close );
    LB.loginLink.click( LB.open );
    LB.usernameField.focus(
      function(e) {
        var $$ = jQuery(this);
        if ($$.val() == LB.usernamePrompt)
          $$.val("");
      }
    ).blur(
      function(e) {
        var $$ = jQuery(this);
        if ($$.val() == "")
          $$.val(LB.usernamePrompt);
      }
    );
    LB.passwordField.focus(
      function(e) {
        if (! LB.passwordCleared)
        {
          LB.passwordField.val("");
          LB.passwordCleared = true;
        }
      }
    );
  },
  close: function(e) {
    var LB = LoginBox;
    LB.loginBox.slideUp();
    LB.loginLink.removeClass('hover');
    e.preventDefault();
  },
  open: function(e) {
    var LB = LoginBox;
    // Act like a toggle
    if (LB.loginBox.is(':visible'))
      return LB.close(e);

    LB.loginBox.slideDown();
    LB.loginLink.addClass('hover');
    e.preventDefault();
  }
};
jQuery(function() { LoginBox.init(); });

/* Fix PNGs with alpha transparencies to display correctly in IE6 */
jQuery(function() {
  if (jQuery.fn.pngfix)
  {
    jQuery('ul.audienceBar div.content').pngfix().find('a.close').pngfix().end().find('img').pngfix();
    jQuery('ul.audienceBar div.tab').pngfix();
  }
});

/* TODO: Can this be combined with ExpandingContent above? */ 
var ExpandingList = {
  init: function() {
    var $ul = jQuery('ul.expandingList');
    jQuery('div.channel', $ul).removeClass('defaultHide').hide();
    $ul.click(
      function(e) {
        $t = jQuery(e.target);
        if ($t.attr("tagName").toUpperCase() == "A")
        {
          // Ignore links in paragraphs as they're content
          if ($t.parent("p").length > 0) {
            
          } else {
            e.preventDefault();
            jQuery('div.channel:visible', $ul).each(function() {
              // // Prevent acting on the item that was clicked
              if ($t.parents('li:eq(0)').find('div.channel').get(0) == this)
                return;
              jQuery(this).hide().parents('li:eq(0)').removeClass('open');
            });
            var $c = $t.parents('li:eq(0)').find('div.channel');
            if ($c.is(':visible')) {
              $c.hide().parents('li:eq(0)').removeClass('open');
            } else {
              $c.show().parents('li:eq(0)').addClass('open');
            }            
          }
        }
      }
    );
  }
};
jQuery(function() { ExpandingList.init(); });

/*jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
    jQuery("<img>").attr("src", arguments[i]);
};*/

//handling imageswap for flyout
	jQuery(document).ready(function() {
   jQuery("li.hasFlyout").hover( addBg, noBg)
 });
	function addBg(){  jQuery(this).children('a').css('background-image', 'url(/pics/shared/leftnav/has-flyout-bg.png)');}
	function noBg(){   jQuery(this).children('a').css('background-image', 'url(/pics/shared/leftnav/has-no-flyout-bg.png)');}
//end imageswap for product flyout

//handles flyout on ie
	jQuery(document).ready(function() {
	 if(jQuery.browser.msie && jQuery.browser.version == '7.0'){
	    jQuery('#NavigationSecondary > ul > li > ul > li > ul').children('div').css('zoom','1.0');
	 }
   jQuery('li.hasFlyout', jQuery('#NavigationSecondary')).hover(
    function() {       	
    	if(jQuery.browser.msie){
    	jQuery(this).children('a').css('background-image', 'url(/pics/shared/leftnav/has-flyout-bg_ie.png)');
      }else{
      jQuery(this).children('a').css('background-image', 'url(/pics/shared/leftnav/has-flyout-bg.png)');	
      }
      jQuery(this).addClass('hover').children('a').addClass('hover');      
  		var  div_height= jQuery(this).children('ul').children('div').attr("clientHeight");
      jQuery(this).children('.flyoutList').css("height", div_height);
     },
     function() {
    	jQuery(this).children('a').removeClass('prodFlyout');
      jQuery(this).children('a').css('background-image', 'url(/pics/shared/leftnav/has-no-flyout-bg.png)');
      jQuery(this).removeClass('hover').children('a').removeClass('hover');
		 });
   });
	//End handles flyout on ie7
	
