$(document).ready(function (){
	//Make all rel=external links pop in new windows
	$clxcommon.initExternal();
	//Warn users when they are clicking off site
	$clxcommon.initWarning();

	//Add event tracking
    $clxcommon.attachClickAnalytics('clorox-link');
    $clxcommon.attachClickAnalytics('clorox-promo');
    $clxcommon.attachClickAnalytics('clorox-popup');
    $clxcommon.attachClickAnalytics('clorox-download');

	// hide sub nav on initial page load
	$('.uberNavChild').hide();
	// hide the whiteout on initial page load
	$('#whiteout').hide();
	// whiteout absorbs clicks that are off subnav
	$('#whiteout').click( function() {
		$('.uberNavChild').hide();
		//$('#our_products').css( 'background-image', 'url(/images/clear.gif');
		$('#our_products').removeClass('menuDown');
		//$('#your_industry').css( 'background-image', 'url(/images/clear.gif');
		$('#your_industry').removeClass('menuDown');
		$(this).hide();
	});
	// show subnav when click parent
	$('.uberNavParent').click( function() {
		// hide the subnav
		$('.uberNavChild').hide();
		// clear the rollover state
		//$('#our_products').css( 'background-image', 'url(/images/clear.gif');
		$('#our_products').removeClass('menuDown');
		//$('#your_industry').css( 'background-image', 'url(/images/clear.gif');
		$('#your_industry').removeClass('menuDown');
		// show the proper subnav
		var thisId = $(this).attr( 'id' );
		var childName =  thisId + "SubNav";
		$('#'+childName).show();
		$('#whiteout').show();
		// show the proper rollover state
		//$('#' + thisId).css( 'background', 'url(/images/nav_on_' + thisId + '.png) 0 0 no-repeat' );
		$('#' + thisId).addClass('menuDown');

		$clxcommon.trackAnalyticsPageView('clorox-popup', thisId);
	});

	//Add 'active' class to which nav should be lit up
	if(window.location.pathname.match(/^\/products/)) {
		$('#our_products').addClass('active');
	} else if(window.location.pathname.match(/^\/industry/)) {
		$('#your_industry').addClass('active');
	} else if(window.location.pathname.match(/^\/about-us/)) {
		$('#about_us').addClass('active');
	} else if(window.location.pathname == '/') {
		$('#home').addClass('active');
	}

	//product selector, in common/product-selector.tpl
	$(document).ready(function (){
		$('#productSelect .dropdown').selectBox();
		$('#productSelect .dropdown').change(function(event) {
			window.location = this.value;
		});
	});


	/** ************************************************************************
	 * How to buy stuff
	 */
	$('#howToBuyBottom').click( function() {
		$(window).scrollTop(0);
		$('#howToBuyLinkSubNav').show();
		$('#whiteout').show();
		$clxcommon.trackAnalyticsPageView('clorox-popup','how-to-buy-open-bottom');
	});
	$('#howToBuyResultsCloseTop').click(function(event){ 
		event.preventDefault();
		$('#howToBuyLinkSubNav').hide();
		$('#whiteout').hide();
		$clxcommon.trackAnalyticsPageView('clorox-popup','how-to-buy-close-top');
	});
	$('#howToBuyResultsCloseBottom').click(function(event){ 
		event.preventDefault();
		$('#howToBuyLinkSubNav').hide();
		$('#whiteout').hide();
		$clxcommon.trackAnalyticsPageView('clorox-popup','how-to-buy-close-bottom');
	});
	$('.howToBuyForm').submit(function(event){
		event.preventDefault();
		//Get values from form. Reference fields by looking at siblings.
		var industry = $('.howToBuyIndustry', this)[0].value;
		var zip = $('.howToBuyZip', this)[0].value;
		//Get search results via Ajax
		$.ajax({
			url: '/vendor/search/'+industry+'/'+zip+'/',
			success: function(data) {
				$('#howToBuyResultsAjax').html(data);
			}
		});
		//If we didn't get here from the topnav, show the div
		$(window).scrollTop(0);
		$('#howToBuyLinkSubNav').show();
		$('#whiteout').show();
		//Update the form on the popup
		$('#howToBuyZipGlobal')[0].value = zip;
		$('#howToBuyIndustryGlobal')[0].value = industry;
		//Drop a cookie to remember last zip entered
		$.cookie('howToBuyZip', zip, { expires: 365, path: '/' });

		$clxcommon.trackAnalyticsPageView('clorox-popup', 
			'how-to-buy-' + industry + '-' + zip);
	});
	//Fill in all zip locators with cookie
	if($.cookie('howToBuyZip').length) {
		$('.howToBuyZip').each(function() {
			this.value = $.cookie('howToBuyZip');
			});
	}

	//Use selectbox plugin to make industry selector look nice
	$('.howToBuyIndustry').selectBox();

	/** ************************************************************************
	 * Repeated widgets
	 */

	//Activate print buttons to print their parent.
	$('.print-section').click(function(event) {
		event.preventDefault();
		//By default, print the container one level up
		var levelsUp = 1;
		//Look for a class named up-NUMBER
		var m = this.className.match(pattern = /up-([1-9]+)/);
		if(m && m[1]) {
			levelsUp = parseInt(m[1]);
		}
		//Walk up the DOM
		var container = $(this);
		for(var i=0; i < levelsUp; i++) {
			container = container.parent();
		}
		container.printArea();
	});
	//Activate email buttons to email their parent.
	$('.email-section').click(function(event) {
		event.preventDefault();
		//By default, print the container one level up
		var levelsUp = 1;
		//Look for a class named up-NUMBER
		var m = this.className.match(pattern = /up-([1-9]+)/);
		if(m && m[1]) {
			levelsUp = parseInt(m[1]);
		}
		//Walk up the DOM
		var container = $(this);
		for(var i=0; i < levelsUp; i++) {
			container = container.parent();
		}
		//Prefix all the image tags with the host
		var host = window.location.href.match(/http:\/\/[^\/]+\//);
		$('img', container).each(function(i) {
			var src = $(this).attr('src');
			$(this).attr('src', host + src);
		});
		container.mailElement();
	});

	//Close all closed sections and open the open ones.
	$('.section-header ~ div').hide();
	$('.section-toggle').each(function(index, element) {
		var e = $(element);
		if(e.hasClass('open')) {
			e.parent().nextAll().show();
		}
	});
	
	//Activate "view all" buttons
	$('.section-view-all').click(function(event) {
		event.preventDefault();
		$('.section-toggle').each(function(index, element) {
			var e = $(element);
			if(e.hasClass('closed')) {
				e.parent().nextAll().show();
				e.removeClass('closed');
				e.addClass('open');
			}
		});
	});

	//Activate open/close icons for sections
	$('.section-toggle').click(function(event) {
		event.preventDefault();
		event.stopPropagation(); // don't let the onclick happen for the header
		//walk up and get blue headline div
		var e = $(this).parent();
		while(e.length && !e.hasClass('section-header')) {
			e = e.parent();
		}
		if(!e.length) {
			//We never found the section header!
			return;
		}
		//Show all the sibling blocks and toggle the open/close class
		if($(this).hasClass('open')) {
			$(this).removeClass('open');
			$(this).addClass('closed');
			e.nextAll().hide();
		} else {
			$(this).removeClass('closed');
			$(this).addClass('open');
			e.nextAll().show();
		}
	});

	$('.section-header').click(function(event) {
		event.preventDefault();
		$('.section-toggle', this).click();
	});

	//Activate close buttons for section footers
	$('.section-close').click(function(event) {
		event.preventDefault();
		//walk up and get blue headline div
		var e = $(this).parent();
		while(e.length && !e.hasClass('section-header')) {
			e = e.prev();
		}
		if(!e.length) {
			//We never found the section header!
			return;
		}
		//Show all the sibling blocks and toggle the open/close class
		e.find('.section-toggle').removeClass('open');
		e.find('.section-toggle').addClass('closed');
		e.nextAll().hide();
	});

	//Related products should be clickable over the entire area,
	//so grab href inside.
	$('.relProd').click(function(event) {
		event.preventDefault();
		var href = $('a', this)[0].href;
		if($($('a', this)[0]).attr('href') == '#') {
			alert('Product URL is set to "#"!');
			return false;
		}
		window.location = href;
	});
	
	//Use selectbox plugin to make free resources selector look nice
	$('.freeResourcesList').selectBox();

	//Activate the free resource sidebar
	$('.freeResourcesForm').submit(function(event) {
		event.preventDefault();
		window.location = $('.freeResourcesList').val();
	
	});
	
});

