//Global JavaScript Document

/********************************

	Notes:
		+ I rely on JQuery v1.2.6
		+ Please use long-form jQuery(...) rather than $(...) to prevent conflicts with legacy Prototype usage

********************************/

// Prevent console commands from throwing errors in IE
try { console.log('init console... done'); } catch(e) { console = { log: function() {} } }

// Add classes to form inputs for css use
function addFormClasses() {
	if(jQuery('input').length) {
		jQuery('input').each(function(){
			jQuery(this).addClass(this.type);
		})
	}
}

function closeThickBox () {
	if('function' == typeof tb_remove) {
		tb_remove();
	}	
}

function invokeOnCloseThickBox (functionObj) {	
	jQuery("#TB_window").unload (functionObj) ;
}

// Open external links in a new window
// Exception for links with rel value of "same-window"
function externalLinks() {
	jQuery('a[href^="http"]:not(a[rel*="same-window"])').click(function(){
		window.open(this.href);
		return false;
	})
}

// Recover hidden Flash objects
function recoverFlash(){
	jQuery('object.hidden').removeClass('hidden');	
}

// Show/hide list items
// Parameters:
// - hidingElms = Required - jQuery object containing elements to be hidden
// - triggerElm = Required - jQuery object containing elements which act as triggers for showing/hiding
// - showFirst = Optional - Flag indicating whether first item should remain visible at page load
function listSlider(hidingElms, triggerElm, showFirst) {	
	if (typeof showFirst == 'undefined') {
		hidingElms.hide();
	} else {
		triggerElm.eq(0).toggleClass('expanded');
		hidingElms.slice(1).hide();
	}	
	triggerElm.each(function(){
		jQuery(this).click(function(){
			if (jQuery(this).hasClass('expanded')) {
				jQuery(this).next().slideUp('fast');
				jQuery(this).toggleClass('expanded');
			} else {
				jQuery(this).next().slideDown('fast');
				jQuery(this).toggleClass('expanded');
			}
		})
	})
}

function inputDefault() {
	// Auto-hide default input text
	// add class of "default" to inputs to activate
	jQuery('input[type="text"].default').each(function(){
		var $this = jQuery(this);
		var $def = $this.val();
		// hide default text on focus
		$this.focus(function(){
			if($this.val() === $def) {
				$this.val('');
			}
		})
		// show default text if empty on blur
		.blur(function(){
			if($this.val() === '') {
				$this.val($def);
			}
		});
	});
}

// Showing/hiding of search results
function collapseSearchResults() {
	if (jQuery('body#results-all-categories .search-section-results').length) {
		listSlider(jQuery('.search-section-results'), jQuery('.search-section-header'), 1);	
	}
}

// Function to open PDFs in a new window
function pdfLinks() {
	jQuery('a[href$=".pdf"]').click(function(){
		window.open(this.href);
		return false;
	})
}

// Show/hide list items
// Parameters:
// - hidingElms = Required - jQuery object containing elements to be hidden
// - triggerElm = Required - jQuery object containing elements which act as triggers for showing/hiding
// - showFirst = Optional - Flag indicating whether first item should remain visible at page load
function listSlider(hidingElms, triggerElm, showFirst) {	
	if (typeof showFirst == 'undefined') {
		hidingElms.hide();
	} else {
		triggerElm.eq(0).toggleClass('expanded');
		hidingElms.slice(1).hide();
	}	
	triggerElm.each(function(){
		jQuery(this).click(function(){
			if (jQuery(this).hasClass('expanded')) {
				jQuery(this).next().slideUp('fast');
				jQuery(this).toggleClass('expanded');
			} else {
				jQuery(this).next().slideDown('fast');
				jQuery(this).toggleClass('expanded');
			}
		})
	})
}

/* Popup window closer */
function closePopup() {
	jQuery('p.close-window a, a.close-window').bind('click', function(){
		window.close();
	})
}

// Showing/hiding of FAQ
function collapseFaq() {
	if (jQuery('ul#faq').length) {
		listSlider(jQuery('ul#faq .faq-answer'), jQuery('ul#faq li h3'));
	}
}

function setFaq(){
	function hashFaq(hashName) {
		jQuery('div.faq-answer').each(function(){
			if(this.id.toLowerCase() == hashName.replace('#', '')) {
				jQuery(this).show();
				jQuery(this).prevAll('h3').addClass('expanded');
				var scrollY = jQuery(this).parent('li').offset().top;
				jQuery(document).scrollTop(scrollY);
			}
		});
	}
	
	// Check for FAQ list
	if(jQuery('ul#faq').length) {
		jQuery('div.faq-answer a').bind('click', function(){
			var thisHref = jQuery(this).attr('href').toLowerCase();
			var thisPath = (window.location.pathname).toLowerCase();
			if((thisHref.indexOf('#') != -1) && (thisHref.indexOf(thisPath) != -1))	{
				hashFaq(thisHref.substr((thisHref.indexOf('#') + 1)));
				return false;
			}		
		})
		
		if(document.location.hash) {
			jQuery('div.faq-answer').each(function(){
				if(this.id == document.location.hash.replace('#', '')) {
					jQuery(this).show();
					jQuery(this).prevAll('h3').addClass('expanded');
					var scrollY = jQuery(this).parent('li').offset().top;
					jQuery(document).scrollTop(scrollY);
				}
			});	
		}

		if(jQuery.url.param("searchTerm") != null) {
			var searchTerm = jQuery.url.param("searchTerm").toLowerCase();
			var terms = searchTerm.split(' ');
			var re = new RegExp("^and$|^the$|^with$|^this$|^that$");
			function arrRemove(){
				jQuery.each(terms, function(item){
					if(this.length < 3 || re.test(this)){
						terms.splice(item, 1);
						arrRemove();
					}
				})
			}
			arrRemove();

			console.log('filtered terms:', terms);
			var scrollY = "";

			jQuery.each(terms, function(i){
				jQuery('div.faq-answer').each(function(){
					if (jQuery(this).text().toLowerCase().indexOf(terms[i]) != -1) {
						jQuery(this).show();
						jQuery(this).prevAll('h3').addClass('expanded');
						if (scrollY == "") {
							scrollY = jQuery(this).parent('li').offset().top;
						}					
					}
				});
			})

			jQuery(document).scrollTop(scrollY);
		}	
	}	
}


swfobject.embedSWF("/flash/swf/faucet-coach.swf", "faucet-coach", "177", "219", "9.0.0", "expressInstall.swf", null, {wmode: "transparent", menu: "false"});
swfobject.embedSWF("/flash/swf/faucet-coach-faucetcoachpage.swf", "faucet-coach-large", "294", "373", "9.0.0", "expressInstall.swf", null, {wmode: "transparent", menu: "false"});
swfobject.embedSWF("/flash/swf/homepage-flash.swf", "homepage-flash", "660", "190", "9.0.0", "expressInstall.swf", null, {wmode: "transparent", menu: "false"});
swfobject.embedSWF( "/flash/swf/faucet-coach-homepage.swf", "faucet-coach-homepage", "252", "249", "9.0.0", "/flash/expressinstall.swf", null, {wmode: "transparent", menu: "false"});
swfobject.embedSWF( "/flash/swf/faucet-installation.swf", "faucet-installation", "560", "330", "9.0.0", "/flash/expressinstall.swf", null, {wmode: "transparent", menu: "false"});
swfobject.embedSWF( "/flash/swf/faucet-deinstallation.swf", "faucet-deinstallation", "560", "330", "9.0.0", "/flash/expressinstall.swf", null, {wmode: "transparent", menu: "false"});
swfobject.embedSWF( "/flash/video_loader.swf", "true-confessions", "176", "215", "9.0.0", "/flash/expressinstall.swf", {xmlfile: "/xml/videos.xml"}, {wmode: "transparent", menu: "false"});


// Initialize JS functions
jQuery(document).ready(function(){
	externalLinks();
	inputDefault();
	pdfLinks();
	collapseFaq();
	setFaq();
	closePopup();
	collapseSearchResults();
})