/*
 * Brita Common Code
 *
 */

// init the $brita namespace. All functions should be part of this namespace
var $brita = window.$brita || {};  

//
// initRelExternal(): open a link in a new window if it has rel='external' attached to it
//
$brita.initRelExternal = function() {
 	$('a.external').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });
    
    // insert a tooltip span that tells you that you're leaving the site
    // class='warning' can be applied to anything other than a link
    // class='warningLink' should only be applied to <a> tags, as it is needed to keep IE6 happy
    $('.warning, a.warningLink').append("<span>You are now exiting the Brita web site.  The Clorox Company is not responsible for the content or data collection of that independent site.</span>");
    $('.warning span').addClass('externalTooltip');	// only add externalTooltip class to the 'warning' objects (not the 'warningLink' objects)
    												// because IE6 has a hissy fit if the span inside the warningLink object has a class

}
//
// createLink(): Given a jQuery selector and a URL, add an onclick event that 
// lets you click on a div to go to a new page 
//
$brita.createLink = function(cssSelector, url) {
	$(cssSelector).click(function() { 
		if ($(this).hasClass('external')) {
			window.open( url );
        	return false;
		} else {
			location.href=url;
		}
	});
}

// 
// trackAnalyticsPageView(): tracks a pageview for Google Analytics. We track these as
// '/pageType/pageName'
//
$brita.trackAnalyticsPageView = function(pageType, pageName) {

	// standardize our page types. We don't want to litter GA with a bunch of junk, so
	// we check to make sure the pageType in question is on the 'OK' list (and throw a 
	// really obvious alert if it isn't)
	if (pageType != 'promo') {
		alert("GA pageType '" + pageType + "' is not a known type.'");
		return;
	}

	if (pageName == '') {
		alert("pageName was passed blank.'");
		return;
	}

	try {
		pageTracker._trackPageview('/'+pageType+'/'+pageName);
	} catch(err) {}
}

//
// attachClickAnalytics(): attach an onclick handler to any 
// element that has a class of 'classToAttach', and record a pageview
// in Google Analytics as /class/id  (e.g. /promo/filterforgood)
//
$brita.attachClickAnalytics = function(classToAttach) {
	$('.' + classToAttach).each(function() {
		$(this).mousedown(function() {	// use mousedown, not click, because it works with the embedded links
										// and gives us a bit more time to make the tracking call
			$brita.trackAnalyticsPageView(classToAttach, this.id);			
		})
	});
}

//
// addMouseoverClass(): this function attaches a class to an element on mouseover, and
// removes it on mouse out. It is used to handle rollovers in IE6, which doesn't support
// the CSS :hover property. The idea is based on this:
// http://htmldog.com/articles/suckerfish/dropdowns/
// but modified for jquery
//
$brita.addMouseoverClass = function(jQuerySelectorToAttach, className) {
	// fix warning popups in IE6
	$(jQuerySelectorToAttach).mouseover(function() {
		$(this).addClass(className);
	})
	
	$(jQuerySelectorToAttach).mouseout(function() {
		$(this).removeClass(className);
	})
}

// initialize all functions on document ready
$(document).ready( function() {
	$brita.initRelExternal();
	
	if (window.locale === undefined)
		$brita.createLink('#header h1', '/');
	else
		$brita.createLink('#header h1', '/'+locale+'/');		// click on the brita logo to go back home
	
	// preloading the navigation images. 
	if (document.images){
		pic1= new Image(); 
		pic1.src="/images/nav_products.gif"; 
		pic2= new Image(); 
		pic2.src="/images/nav_news.gif"; 
		pic3= new Image(); 
		pic3.src="/images/nav_about.gif"; 
		pic4= new Image(); 
		pic4.src="/images/nav_support.gif"; 
		pic5= new Image(); 
		pic5.src="/images/nav_you.gif";
	}

	$brita.attachClickAnalytics('promo');

	// fix nav in IE 6. The jquery method causes some menu flicker, 
	// so we do it old-school style now (see below)
	// $brita.addMouseoverClass('#mainNav li', 'sfhover');
	$brita.addMouseoverClass('.warning', 'whover');		// fix leave site warning popup in IE 6
	
   // add additional fxns as needed
});

sfHover = function() {
	var sfEls = document.getElementById("mainNav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);