// for site specific functions. either grab js from code-library.js or write your own.


$(document).ready( function() {
	$('body').addClass('js');
	anchors.addBehaviors();
	sfHover();
	txtBoxClear.init();
	productFinder.init();
	$('#contact li').jTooltips();
});



/* =================================================================== */
jQuery.fn.jTooltips = function(options) {
	var settings = jQuery.extend({
    	tooltipContentClass	: 'tooltip'
	}, options); 
	//"this" is the current context; in this case, the list elements we want to have the tooltips applied to
	$this = $(this);
	$(this).mouseenter( function(e) {
		var target = e.target;
		var cntr = 1;
		while (! $(target).is('li') ) {
			target = $(target).parent();
			cntr = cntr + 1;
			if (cntr == 10) break; //just for safety reasons
		}
		$(target).find('.'+settings.tooltipContentClass).show();
	}).mouseleave( function(e) {
		var target = e.target;
		while (! $(target).is('li') ) {
			target = $(target).parent();
		}
		$(target).find('.'+settings.tooltipContentClass).hide();
	});
	
	return this;
};
/* =================================================================== */



/* =================================================================== */
productFinder = {
	navItemId			: 'nav-Products',
	productFinderBoxId	: 'productFinder', 
	
	init : function() {
		this.addNavItem();
		this.positionProductFinderBox();
		this.setUpEvents();
	},
	
	addNavItem : function() {
		$('#navLevelOne').append('<li id="' + this.navItemId + '"><a href="#">Product Finder</a></li>');
	}, 
	
	positionProductFinderBox : function() {
		//$('#'+this.productFinderBoxId).remove().appendTo('#'+this.navItemId);
		$('#'+this.productFinderBoxId).remove().appendTo('#envelope');
	},
	
	setUpEvents : function() {	
		$('#'+this.navItemId).find('a').mouseover( function() {
			$(this).parent('li').addClass('selected');
			$('#'+productFinder.productFinderBoxId).show();
		}).mouseleave( function(e) {
			// if mousing onto the product finder box, keep it visible. 
			// if mousing somewhere else, hide the box
			if ( $(e.relatedTarget).closest('#'+productFinder.productFinderBoxId).length == 0 ) {
				$('#'+productFinder.productFinderBoxId).hide();
				$(this).parent('li').removeClass('selected');
			}
		});
		$('#'+this.productFinderBoxId).mouseleave( function() {
			$(this).hide();
			$('#'+productFinder.navItemId).removeClass('selected');
		});
	}
}
/* =================================================================== */



/* =================================================================== */
// various link functionality - popups, external, onstate
// original script taken from Jeremy Keith
// dependencies: jQuery
var anchors = {
	a: Object,
	addBehaviors : function() {
		$('a').each( function() {
			var $a = $(this);
			// external links
			if ( ($a.attr('rel')=="external") || $a.hasClass('external') || $a.hasClass('pdf') || $a.hasClass('popupFull') ) {
				$a.click( function(e) {
					e.preventDefault();
					return anchors.openWin(this,"");
				});
			}
			// popup
			if ( $a.hasClass('popup') ) {
				$a.click( function() {
					return anchors.openWin(this,"height=550,width=600,scrollbars=yes");
				});
			}
			// onstate
			if ( $a.attr('href') == location.href ) {
				$a.addClass('onstate');
			}
		});
	},
	openWin : function(o,params) {
		window.open(o.href, "newwin","" + params + "");
		return false;
	}
};
/* =================================================================== */


/* =================================================================== */
// use this to automatically clear a text box of it's default value
// if nothing is typed in the box, the script will put the default value back
// useful on sites where search boxes don't have a seperate label
// you can use it for more than one box per page using the txtBoxes array
txtBoxClear = {
	txtBoxes : ['searchField'],
	init: function() {
		for (i=0;i<txtBoxClear.txtBoxes.length;i++) {
			var oCurrentTxtBox = document.getElementById(txtBoxClear.txtBoxes[i]);
			if (!oCurrentTxtBox) { continue; }
			oCurrentTxtBox.defaultVal = oCurrentTxtBox.defaultValue;
			txtBoxClear.clearBox(oCurrentTxtBox);
		}
	},
	clearBox : function(txtBox) {
		txtBox.onfocus = function() {
			if (txtBox.value == txtBox.defaultVal) { txtBox.value = ''; } 
		};
		txtBox.onblur = function() {
			if (txtBox.value == '') { txtBox.value = txtBox.defaultVal; }
		};
	}
};
/* =================================================================== */

/* =================================================================== */
// for suckerfish dhtml menus
// dependencies: jQuery
sfHover = function() {
	$('#navLevelOne li').each( function() {
		$(this).hover( function() {
			$(this).addClass('sfhover');
		}, function() {
			$(this).removeClass('sfhover');
		});
	});
};
/* =================================================================== */

/* =================================================================== */
// old openWin function for opening FLASH demos, bcook 7/20/2009
// dependencies: none
function openWinBC(url,windowName,w,h)
{
var myNewWin = window.open(url,windowName,'toolbar=no,location=no,directories=no,menubar=no,status=yes,scrollbars=no,resizable=no,width='+w+',height='+h);
myNewWin.focus();
}
/* =================================================================== */



