/*-------------------------------------------------------------------- 
 * JQuery Plugin: "EqualHeights"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
 *
 * Copyright (c) 2008 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Description: Compares the heights or widths of the top-level children of a provided element 
 		and sets their min-height to the tallest height (or width to widest width). Sets in em units 
 		by default if pxToEm() method is available.
 * Dependencies: jQuery library, pxToEm method	(article: 
		http://www.filamentgroup.com/lab/retaining_scalable_interfaces_with_pixel_to_em_conversion/)							  
 * Usage Example: $(element).equalHeights();
  		Optional: to set min-height in px, pass a true argument: $(element).equalHeights(true);
 * Version: 2.0, 08.01.2008
--------------------------------------------------------------------*/

$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		//if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
		$(this).children().css({'min-height': currentTallest}); 
	});
	return this;
};

// make the tabs functional
function initTabs() {
	var sets = document.getElementsByTagName("ul");
	for (var i = 0; i < sets.length; i++) {
		if (sets[i].className.indexOf("tabset") != -1) {
			var tabs = [];
			var links = sets[i].getElementsByTagName("a");
			for (var j = 0; j < links.length; j++) {
				if (links[j].className.indexOf("tab") != -1) {
					tabs.push(links[j]);
					links[j].tabs = tabs;
					var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));

					//reset all tabs on start
					if (c) if (links[j].className.indexOf("active") != -1) c.style.display = "block";
					else c.style.display = "none";

					links[j].onclick = function () {
						var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
						if (c) {
							//reset all tabs before change
							for (var i = 0; i < this.tabs.length; i++) {
								var tab = document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1));
								if (tab) {
									tab.style.display = "none";
								}
								this.tabs[i].className = this.tabs[i].className.replace("active", "");
							}
							this.className += " active";
							c.style.display = "block";
							return false;
						}
					}
				}
			}
		}
	}
}

window.onbeforeprint = function(){
	/* when printing the page in IE the rounded corners (added by the DD Roundies lib) are breaking the print layout. 
	   targetting the styles through print.css to hide them does not work. so the solution is to remove the rounded 
	   corners tags from the HTML page altogether */
	$('ignore').remove();
}

window.onafterprint = function() {
	/* after preppping the page for printing by removing the rounded corner tags on onbeforeprint, we can't put the 
	   rounded corners back in the page dynamically so our only option is to reload the page */
	window.location.reload();
}

// when the DOM has loaded
$(document).ready(function(){
	// let the CSS know that JavaScript is active and add the flash overlay background
	$('body').addClass('wjs').append('<div id="facebox_overlay" class="facebox_hide facebox_overlayBG"></div>');

	// make the global nav interactive
	$('#nav>li').hover(
		function(){
			$(this).addClass('hover');
		},
		function(){
			$(this).removeClass('hover');
		}
	);
	
	// initalize tabs
	initTabs();
	
	// initalize the homepage slideshow
	$('#headerGallery').slideShow({
		slideEl: '#headerGallerySlides img',
		linkNext: 'a.right',
		linkPrev: 'a.left',
		linkPause: 'a.pause',
		numElementLink: '#headerGalleryNav li a',
		duration: 500,
		autoSlideShow: true,
		switchTime: 10000,
		event: 'click',
		currentEl: '#footer span.cur',
		allEl: '#footer span.all'
	});
	
	// activate the facebox links
	$('a[rel*=facebox]').click(function(){
		$('#facebox_overlay').css('opacity', 0.8).show();
		$('#popup').show();
		DD_roundies.addRule('div#popup', '6px', true);
		return false;
	});
	$('#facebox_overlay').click(function(){
		$('#facebox_overlay').hide();
		$('#popup').hide();
	});
	$('#popup a.x').click(function(){
		$('#facebox_overlay').hide();
		$('#popup').hide();
		return false;
	});
	
	// round the corners of boxes
	DD_roundies.addRule('div.boxHighlight', '6px', true);
	DD_roundies.addRule('div.boxDefault', '6px', true);
	DD_roundies.addRule('div.boxCallout.screen', '6px', true);
	DD_roundies.addRule('div.boxTools', '6px', true);
	DD_roundies.addRule('div.main-i', '6px', true);
	DD_roundies.addRule('div#contentWide', '6px', true);
	DD_roundies.addRule('div.box', '6px', true);
	DD_roundies.addRule('div.boxAd', '6px', true);
	DD_roundies.addRule('div.boxEvent', '6px', true);
	DD_roundies.addRule('div.boxFinder', '6px', true);
	DD_roundies.addRule('div.boxTabs', '6px', true);
	DD_roundies.addRule('div.moreInfo div', '6px', true);
	

	// insert rounded corners for the heading (so the roundness doesn't need to be in the image)
	var boxObjs =                  'div.boxHighlight';
	    boxObjs = boxObjs + ', ' + 'div.heading';
	    boxObjs = boxObjs + ', ' + 'div.boxDefault div.heading';
	    boxObjs = boxObjs + ', ' + 'div.boxTools div.heading';
	    boxObjs = boxObjs + ', ' + 'div.boxAd div.heading';
	    boxObjs = boxObjs + ', ' + 'div.boxEvent div.heading';
	    boxObjs = boxObjs + ', ' + 'div.boxEvent div.body';
	    boxObjs = boxObjs + ', ' + 'div.boxFinder div.heading';
	    boxObjs = boxObjs + ', ' + 'div.boxTabs';
	$(boxObjs).append('<span class="left"></span><span class="right"></span>');
	
	// equal height homepage boxes
	$('div.boxes').equalHeights();
	
	// add wrappers for the BG images
	$('#container').wrap('<div id="jsContainerWrapper1"><div id="jsContainerWrapper2"></div></div>');
	
	// interaction for the search form field
	$('#headerSearch input[type=text]')
		.focus(function(){
			if($(this).val() == 'enter keywords'){
				$(this).val('');
			}
		})
		.blur(function(){
			if($(this).val() == ''){
				$(this).val('enter keywords');
			}
		});
		
	// :psuedo class fix
	$('ul>li:first-child').addClass('firstChild');
	$('ul>li:last-child').addClass('lastChild');
	
	//
	var moreInfoTimer;
	$('ul.moreInfo a').hover(
		function(){
			clearTimeout(moreInfoTimer);
			$('div.moreInfo').hide();
			var div = $(this).attr('href');
			var divHeight = $(div).height();
			var liTop = $(this).parent('li').offset().top;
			var liLeft = $(this).parent('li').offset().left;
			var liWidth = parseInt($(this).width()) + parseInt($(this).parent('li').css('padding-left'));
			var liHeight = $(this).parent('li').height() + parseInt($(this).parent('li').css('padding-top'));
			var containerLeft = $('#container').offset().left
			
			$(div)
				.css('top', liTop + (liHeight/2) - (divHeight/2) + 'px')
				.css('left', liLeft + liWidth - containerLeft + 10 + 'px')
				.fadeIn()
				.hover(
					function(){
						clearTimeout(moreInfoTimer);
					},
					function(){
						moreInfoTimer = setTimeout("$('div.moreInfo').fadeOut();", 2000);
					}
				);
		},
		function(){
			moreInfoTimer = setTimeout("$('div.moreInfo').fadeOut();", 2000);
		}
	);
	
	// homepage map signup form field interaction
	$('div.boxSignup input')
		.one('focus', function(){
			$(this).data('default', $(this).val());
		})
		.focus(function(){
			if($(this).val() == $(this).data('default')) {
				$(this).val('');
			}
		})
		.blur(function(){
			if($(this).val() == '') {
				$(this).val($(this).data('default'));
			}
		});
});


