/*
 * This script instantiates sitewide jQuery methods.
 * These methods could bind events to elements, for example.
 */

// Turns on CSS background image caching in IE. May even work in FF.
//try{ document.execCommand("BackgroundImageCache", false, true); } catch(e){}; //breaks in IE6, hmm.

// Pre-load selected images. Adds images to DOM, so the cache should persist.
// Most of the nav bar images are CSS backgrounds, so this will have little effect on FF3.
// FF3 "cleans-up" images that aren't added to the DOM, btw. Don't know what IE does.
var images = [
	'/images/backgrounds/bkgd-search-area.png', 
	'/images/backgrounds/bkgd-top-search.png', 
	'/images/sprites/sprite_nav-utility-icons.png', 
	'/images/sprites/sprite_horiz_bg.png', 
	'/images/sprites/sprite_main_nav_divider.png', 
	'/images/spacer.gif', 
	'/images/header/nav-rollover-bg.png', 
	'/images/sprites/sprite_horiz_bg.png',
	'/images/minicart/bkgd-minicart-closed.png', 
	'/images/minicart/bkgd-minicart-opened.png', 
	'/images/minicart/bkgd-minicart-bottom-opened.png', 
	'/images/minicart/bkgd-minicart-fill-opened.png', 
	'/images/header/nav-account.gif', 
	'/images/header/nav-community.gif', 
	'/images/header/nav-learn.gif', 
	'/images/header/nav-shop.gif', 
	'/images/header/nav-support.gif', 
	'/images/buttons/btn-orange-sm-l.png', 
	'/images/buttons/btn-orange-sm-r.png', 
	'/images/buttons/btn-orange-sm-l-o.png', 
	'/images/buttons/btn-orange-sm-r-o.png', 
	'/images/buttons/btn-orange-med-l.png', 
	'/images/buttons/btn-orange-med-r.png', 
	'/images/buttons/btn-orange-med-l-o.png', 
	'/images/buttons/btn-orange-med-r-o.png' 
];
 
jQuery.each(images, function(i) {
  images[i] = new Image();
  images[i].src = this;
});

$(function(){
	/* PNG 24 fun */
	
	// Fixes transparent PNG bug just for IE6 or less.
	// Don't apply to all elements, as other non-transparent images may break.
	// Here we're appling the hack to all dynamic buttons and button elements.
	$('.btn, button, .item-compare').supersleight();

	// Here we're appling the hack to all "PNG24" images.
	$('.PNG24').supersleight({backgrounds: false});

	// Here we're appling the hack to all "tabs".
	$("ul.tabsNavMenu a span, ul.tabsNavMenu a em").supersleight();

	// Converts all support links to toggles
	$('.support-faq-question-link').click(function() { this.parentNode.onclick(); return false; });


	/* New "target" window for XHTML */
	
	//js alternative to target="_blank"
	$('a.new-win').click(function(){window.open(this.href); return false;});


	// Remove default value from input.
	$('.clear-this').focus(function(){
		$(this).val('');
	});
	
	// Let's not submit the search form if nothing's been entered
	var input_def_value = $('#search-for').val();
	$('#search-site').submit(function(){
		var input_value = $('#search-for').val();
		if (input_value == input_def_value || input_value == "") return false;
		// Add iframe for search results
		$('#content-inner, #bodycontent').empty().append('<iframe name="search-results" id="search-results" frameborder="0" border="0" marginwidth="0" marginheight="0"></iframe>');
		
		// We need to adjust the height of the iframe to accomodate its content
		$('iframe').load(function(){
			// reset height, or else
			$(this).height(0);
			var iframeRef = document.getElementById('"'+this.id+'"');
			var frameHeight = $(iframeRef).find('html').outerHeight(true);
			$(this).height(frameHeight);
		});

		// We need to set a width of 960px if the col-wrapper element is at 100%.
		if($('#col-wrapper').width() > 960) $('#search-results').addClass('set-width');
		return true;
	});
});