/*
 * Brita Common Code
 *
 */

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

// initialize all functions on document ready

$(document).ready(function() {

	$clxcommon.initExternal();
    $clxcommon.initWarning();
    

//show sitemap
$("#btn_sitemap").click(function() {
    $('#siteMap').slideToggle(500, 'swing');
});


//change hover state on mouseover
$('#btn_sitemap').hover(function () {
    $(this).css('backgroundPosition', '0 -23px');
}, function() {
    $(this).css('backgroundPosition', '0 0');
});    	
    
//move up primary nav and legal bar above site map
$( "#btn_sitemap").toggle(
    function() {
        $("#toggleUp").animate( {
            bottom: '278px'
        }, 500, 'swing');
        $('#bottomFixedFooter').addClass('siteMapVisible');
    },
    function() {
        $("#toggleUp").animate( {
            bottom: '0'
        }, 500, 'swing');
        $('#bottomFixedFooter').removeClass('siteMapVisible');
    }
);

//hide slash between login and register when background pill graphic is visible 
$('.btn_pill').hover(function () {
        $("li.slash").hide();
        $(this).css('marginLeft', '7px');
	}, function() {
        $("li.slash").show();
		$(this).css('marginLeft', '0');
    });   
    
    
//every third item will not have a right margin on product cat. pages
$("#three-col .productGrid li:nth-child(3n+3)").css ( {
	'marginRight': '0'
})


//tooltips


$(".faucet-filtration #pdColors img").tooltip({
    tipClass : "tooltipColorsLong"
});

$(".water-pitchers #pdColors img").tooltip({
    tipClass : "tooltipColorsLong"
});



$(".water-dispensers #pdIcons img, .water-pitchers #pdIcons img, .faucet-filtration #pdIcons img").tooltip({
    tipClass : "tooltipTall"
});

$("#pdColors img").tooltip({
    tipClass : "tooltipColorsLong"
});

$("#pdIcons img").tooltip({
 	tipClass : "tooltipIcons"
});

/*overlay, but exclude the buy - button link*/
$("a[rel]").not('a[rel=external]').not('.sourceUrl').overlay({
	mask: "null",
	zIndex: 8000,
    effect: "default"
});

//if viewport resized to 960px wide or less, hide plants on top and bottom. 
//IE doesn't get a fade b/c of ugly black outline in PNGs
    
$(window).resize(function() {
if ($('body').width() < 960) {
	if( (jQuery.browser.msie) ) {		
		$(".ornament").hide();
	}
	else { 
		$(".ornament").fadeOut("slow"); 
	}
}
else {
	if( (jQuery.browser.msie) ) {		
		$(".ornament").show();
	}
	else { 
		$(".ornament").fadeIn("slow");
	}
}
});
    
/*HIDE ADDITIONAL INFORMATION INITIALLY ON PAGES
e.g. /why-brita/
*/
$(".container").hide();

    /*show them on click*/
    $("#item1").click(function() {
        $(".container").hide();
        $(".item1Container").show();
    });

    $("#item2").click(function() {
        $(".container").hide();
        $(".item2Container").show();
    });
  //Pointing these two items to actual pages instead of down below
    $("#item3").click(function() {
        $(".container").hide();
        $(".item3Container").show();
    });

    $("#item4").click(function() {
        $(".container").hide();
        $(".item4Container").show();
    });


$('.accordionIndiv h3').click(function() {
	// Update the clicked item's status
    if( !$(this).next().is(':visible') ) {
            // It is being opened now, so change the text to 'Close'
            $(this).children('.accordStatus').text("Close");
            $(this).children('.accordStatus').addClass("expanded");

        } else {
            // It is being closed now, so change the text to 'Open'
            $(this).children('.accordStatus').text("Open");
            $(this).children('.accordStatus').removeClass("expanded");

        }

        // Toggle the visibility of the div underneath this H3
        $(this).next().slideToggle(500, 'swing');
        
        //keeps browser from jumping when accordion opens
        return false;


    }).next().hide();

    //Open initial div based on anchor
    if (window.location.hash && window.location.hash.length > 1 && window.location.hash.charAt(1) != '&') {
    	//For accordion divs, have to click the parent h3 element
    	var tmp = $(window.location.hash);
    	if (tmp && tmp.parent()) {
    		tmp.parent().click();
    	}
    	
    	//For why-brita, have to click on the actual link
    	switch (window.location.hash) {
    	case '#how-filters-work':
    	case '#reduces-common-pollutants':
    		tmp = $('#item2');
    		if (tmp) {
    			tmp.click();
    		}
    		break;
    	}
    }
     
    //AKQA tags
    $('#login-modal-window').click(function() {
    	var axel = Math.random() + "";
    	var a = axel * 10000000000000;
    	var ifrm = document.createElement("iframe"); 
    	ifrm.setAttribute('src', 'https://fls.doubleclick.net/activityi;src=3326219;type=regis396;cat=sitef105;ord=' + a + '?');
    	document.body.appendChild(ifrm);
    });
    $('#nav_better_you').click(function() {
    	var axel = Math.random() + "";
    	var a = axel * 10000000000000;
    	var ifrm = document.createElement("iframe");
    	ifrm.setAttribute('src', 'https://fls.doubleclick.net/activityi;src=3326219;type=homep191;cat=brita026;ord=' + a + '?');
    	document.body.appendChild(ifrm);
    });
    $('#nav_world').click(function() {
    	var axel = Math.random() + "";
    	var a = axel * 10000000000000;
    	var ifrm = document.createElement("iframe");
    	ifrm.setAttribute('src', 'https://fls.doubleclick.net/activityi;src=3326219;type=homep191;cat=brita264;ord=' + a + '?');
    	document.body.appendChild(ifrm);
    });
    
    $('#forgotPasswordLinkInOverlay').click(function(event) {
        event.preventDefault();
        $('#loginOverlayContent').hide();
        $('#forgotPasswordOverlayContent').show();
    });

	$clxcommon.createLink('h2#logo', '/');
    $clxcommon.createLink('#GA_productFaucet', 			'/products/faucet-filtration/');
    $clxcommon.createLink('#GA_productBottles', 		'/products/filtering-bottle/brita-bottle/');
    $clxcommon.createLink('#GA_productPitchers',		'/products/water-pitchers/');
    $clxcommon.createLink('#GA_productDispensers',  	'/products/water-dispensers/');
    $clxcommon.createLink('#GA_productFilters',     	'/products/replacement-filters/');
    $clxcommon.createLink('#GA_your_reminders',    		'/your-brita/filter-reminders/');
    $clxcommon.createLink('#GA_your_pitch',     		'/your-brita/recycle-your-filter/');
    $clxcommon.createLink('#GA_your_using',     		'/your-brita/using-your-brita/');
    $clxcommon.createLink('#GA_biggestLoser',     		'http://www.nbc.com/the-biggest-loser/exclusives/sponsors/brita/');
	$clxcommon.createLink('.btnWTB',     				'/where-to-buy/#find-stores/');
    $clxcommon.createLink('#GA_productFaucet', 			'/products/faucet-filtration/');
	$clxcommon.createLink('#pageHero', 					'#');
	$clxcommon.createLink('#CTAOurPartners', 			 '/filter-for-good/our-partners/');
	$clxcommon.createLink('#CTAgetFacts', 				'/filter-for-good/say-no-to-bottled-water/');
	$clxcommon.createLink('#CTAreadStory', 				'http://www.facebook.com/media/set/?set=a.10150539967637608.402735.6985452607&type=1#!/media/set/?set=a.10150539967637608.402735.6985452607&type=3');
	$clxcommon.createLink('#CTAregisterNow', 			'/user/register/?from=ffg');
	$clxcommon.createLink('#facebookShare',     		'https://www.facebook.com/BritaFilterForGood');
	$clxcommon.createLink('#twitterShare',     			'https://twitter.com/filterforgood');
	$clxcommon.createLink('#youtubeShare',     			'http://www.youtube.com/britafilterforgood');
	$clxcommon.createLink('#createAccount',     		'/user/register/');
	$clxcommon.createLink('#pledgeLeftPromo',     		'/user/take-pledge/');

    $clxcommon.attachClickAnalytics('clorox-promo');
    $clxcommon.attachClickAnalytics('clorox-link');
    $clxcommon.attachClickAnalytics('clorox-download');
    $clxcommon.attachClickAnalytics('clorox-popup');
	$clxcommon.attachClickAnalytics('clorox-video');
	$clxcommon.attachClickAnalytics('clorox-virtual');
});

//Function to attach AKQA analytics
function attachAKQAAnalytics(selector, action) {
	$(selector).click(function() {
		var az_p=location.protocol=='https:'?'https:':'http:';
		var img = new Image();
		img.src= az_p + '//s0.srtk.net/www/delivery/ti.php?trackerid=1375&cb=709224688&_sr_amount=1&_sr_actionid='+action;
	});
}

//Function to unescape entities in a string
String.prototype.unescapeHtml = function () {
    var temp = document.createElement("div");
    temp.innerHTML = this;
    var result = temp.childNodes[0].nodeValue;
    temp.removeChild(temp.firstChild);
    return result;
}

