/*
 * Clorox.com Additional Javascript Definitions and Initialization
 * 
 * NOV 9, 2010
 *
 */


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

$(document).ready(function() {

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

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

	// attach a click handler to the link. We act on mousedown, not 'click'
	$('#redcross_preparedness_guide').mousedown(function() {
		$clxcommon.trackAnalyticsPageView('clorox-download','redcross_preparedness_guide');
	});
	
	$('#laundry-made-easy').mousedown(function() {
		$clxcommon.trackAnalyticsPageView('clorox-download','laundry-made-easy-guide');
	});
	
	$('.track_home_whitenyourwhites').mousedown(function() {
		$clxcommon.trackAnalyticsPageView('clorox-promo','home_whitenyourwhites');
	});	

	$('.track_special_offers_whitenyourwhites').mousedown(function() {
		$clxcommon.trackAnalyticsPageView('clorox-promo','special_offers_whitenyourwhites');
	});	

	$('.track_home_eprize_pabf').mousedown(function() {
		$clxcommon.trackAnalyticsPageView('clorox-promo','home_eprize_pabf');
	});	
	
	$('.track_home_facebook_left_right').mousedown(function() {
		$clxcommon.trackAnalyticsPageView('clorox-promo','home_facebook_left_right');
	});	
	
	//sign in button in overlay 
	$('#signInDropdown #btn-signin').mousedown(function() {
		$clxcommon.trackAnalyticsPageView('clorox-virtual','signin_modal_signin_button');
	});	
	
	
$clxcommon.createLink('#homepage-mystain', 				'http://www.clorox2.com/stain-app/');
$clxcommon.createLink('#homepage-odetocommode', 		'http://www.facebook.com/odetothecommode');
$clxcommon.createLink('#homepage-blog', 				'/blog');
$clxcommon.createLink('#homepage-powerfuture',			'http://clorox.promo.eprize.com/brightfuture/?affiliate_id=Inbound5/');
$clxcommon.createLink('#homepage-seasons', 				'http://www.seasonsofwhite.com/');
$clxcommon.createLink('#homepage-newsletter', 			'/register/');
$clxcommon.createLink('#homepage-ingredients',  		 'http://www.thecloroxcompany.com/products/ingredients-inside/en-us/clorox/');

$clxcommon.createLink('#enterNow',  'http://cloroxrecipesforfun.com/register/');
$clxcommon.createLink('#winTenThousand',  'http://cloroxrecipesforfun.com/register/');


	
$clxcommon.createLink('#promoCucFacebook',  'http://www.facebook.com/Clorox?sk=app_155211227838269');
$clxcommon.createLink('#bathroomCleaner', 	'/products/clorox-disinfecting-bathroom-cleaner/');
$clxcommon.createLink('#promoMoneyBack', 	'http://www.clorox2.com/pdfs/c2_moneybackguarantee.pdf');
$clxcommon.createLink('#ctbc', 				'/products/clorox-automatic-toilet-bowl-cleaner/');
$clxcommon.createLink('#promoOdeComode', 	'http://www.facebook.com/odetothecommode#!/odetothecommode?v=app_137045596308251');
$clxcommon.createLink('#enespanol', 		'http://www.cloroxenespanol.com/');

$clxcommon.createLink('#commitment-carousel-checkups', 			'/our-commitment/causes/childrens-health-fund/');
$clxcommon.createLink('#commitment-carousel-bright-future', 	'/our-commitment/causes/power-bright-future/');
$clxcommon.createLink('#commitment-carousel-wounded', 			'http://www.cloroxcsr.com/involved/supporting-veterans');

$clxcommon.createLink('#prodREG .productHero', 	'/products/clorox-regular-bleach/bleach-it-away/');
$clxcommon.createLink('#advisorPromo', 	'/clorox-cleaning-and-laundry-advisor/');
$clxcommon.createLink('#bethannyPromo', 'https://www.facebook.com/Clorox');

$clxcommon.createLink('#bleachablemoment-specialoffer', '/products/clorox-regular-bleach/bleach-it-away/');
$clxcommon.createLink('#recipesForFun-specialoffer', 'http://cloroxrecipesforfun.com/');
$clxcommon.createLink('#recipesForFun-coupons', 'http://cloroxrecipesforfun.com/');
$clxcommon.createLink('#powerBrightFuture-specialoffer', 'http://www.powerabrightfuture.com/');
$clxcommon.createLink('#play2day-specialoffer', 'http://clorox.promo.eprize.com/play2day/:b=firefox6/');
$clxcommon.createLink('#nowSow-specialoffer', 'http://seasonsofwhite.aol.com/?utm_source=Clorox&utm_medium=Website&utm_campaign=SpecialOffers');
$clxcommon.createLink('#myIdea-specialoffer', 'http://cloroxconnects.com/groups/bc6a714e87/summary');
$clxcommon.createLink('#song-specialoffer', '/mp3/MyLittleGirl_Main_MSTR.mp3');
$clxcommon.createLink('#kitchenCentral-specialoffer', 'http://shine.yahoo.com/kitchen-central/');

$clxcommon.createLink('#hpPromoAskDrLaundry', 'http://www.clorox.com/blogs/dr-laundry/');
$clxcommon.createLink('#hpPromoInsideProgramMobile', 'http://itunes.apple.com/us/app/clorox-ingredients/id472845717?mt=8');
 


/*
 * Before loading accordions, looks for a specified query string parameter.
 * This should be an int greater than 0: 1 is the top level, and so on.
 * If this exists, then it's an indicator that one level should be open 
 * by default. This function adds a specified class to the level
 * specified as the value of the query string param. Use this class 
 * as the value for your accordion's "active" property.
 *
 */

$(function() {

	//the query string param
	myVar = 				$clorox.getParameterByName('exp');
	
	//the element clicked to reveal the accordion levels
	accordionHeadElement = 	'.accordionSection ul li h4';
	accordionSubheadElement = '.accordionSection ul li h5';
	
	//"active : '.open'" must be stated in the accordion setup
	accordionOpenClass = 	'open';
	
	if(myVar!=""){
		
		n = $(accordionHeadElement).length;
		var toOpen = myVar.split(',');
		if (toOpen[0]) {
			var oneToOpen = parseInt(toOpen[0]) - 1;
			$(accordionHeadElement).each( function(index){
				if(index == oneToOpen) {
					$(this).addClass(accordionOpenClass);
					if ($.scrollTo && !toOpen[1]) {
						var tmp = $(this);
						setTimeout(function() {
							$.scrollTo(tmp);
						}, 250);
					}
				}	
			});
		}
		
		if (toOpen[1]) {
			var oneToOpen = parseInt(toOpen[1]) - 1;
			$(accordionSubheadElement).each( function(index){
				if(index == oneToOpen){
					var click = $(this);
					setTimeout(function() { click.click(); }, 1);
					if ($.scrollTo) {
						var tmp = $(this);
						setTimeout(function() {
							$.scrollTo(tmp);
						}, 1);
					}
				}	
			});
		}
	}
/*			oneToOpen = myVar -1;
			$(accordionHeadElement).each( function(index){
				if(index == oneToOpen){
					$(this).addClass(accordionOpenClass);
					if ($.scrollTo) {
						//Position seems to not be updated immediately after adding
						//the open class above, so set this to execute on a timeout
						var tmp = $(this);
						setTimeout(function() {
							$.scrollTo(tmp);
						}, 1);
					}
				}	
			});
		}
	}*/
});	
/*
//CUSTOM FORM LOOK AND FEEL
$(function(){ $("select").uniform(); });
*/
$(function() {

// accordion section
	if ($('.accordionSection').length > 0) {
		$('.accordionSection').accordion( {
			header : "h4",
			collapsible : true,
			active : ".open", //this is that class from the above function
			alwaysOpen : false,
			autoHeight : false
		});
	}

});


$(function() {

// accordion section nested in an accordion: 
//clorox-cleaning-and-laundry-advisor/healthier-home-and-family/allergy/

	if ($('.accordionMinor').length > 0) {
		$('.accordionMinor').accordion( {
			header : "h5",
			collapsible : true,
			active : true,
			alwaysOpen : false,
			autoHeight : false
		});
	}

});

$(function() {

// accordion section - nested in top level accordion - as on Regular Bleach How To
	if ($('.accordionSectionNested').length > 0) {
		$('.accordionSectionNested').accordion( {
			header : "h6",
			collapsible : true,
			active : true,
			alwaysOpen : false,
			autoHeight : false
		});
	}

});


$(function() {

// accordion section - nested in top level accordion - as on Regular Bleach How To
	if ($('.accordionSectionPromo').length > 0) {
		$('.accordionSectionPromo').accordion( {
			header : "h6",
			collapsible : true,
			active : true,
			alwaysOpen : false,
			autoHeight : false
		});
	}

});



$(function() {

// accordion section
	if ($('.classroomSignIn').length > 0) {
		$('.classroomSignIn').accordion( {
			header : "h4",
			collapsible : true,
			active : true,
			alwaysOpen : false,
			autoHeight : false
		});
	}

});



// subnav tabs
$(function() {
	var tabContainers = $('div.subTabs > div');
	tabContainers.hide().filter(':first').show();

	$('div.subTabs ul.subTabNavigation a').click(function() {
		tabContainers.hide();
		tabContainers.filter(this.hash).show();
		$('div.subTabs ul.subTabNavigation a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
});



// sign in drop down show on click and hide when clicking any other part of the site.
document.onclick=check;
function check(e){
	var target = (e && e.target) || (event && event.srcElement);
	var obj = document.getElementById('signInHideShow');
	if( !obj ) return;
	
	var obj2 = document.getElementById('sho');
	checkParent(target) ? obj.style.display='none' : null;
		
	var firstShow = false;
	if( target==obj2 ) {
		if( obj.style.display!='block') {
			firstShow = true;
			obj.style.display='block';
		}
	}
	
	// focus on field if it is empty, this is to prevent a focus if user fills in field really quickly
	if( firstShow ) {
		var usernameJq = jQuery('#signInHideShowUserName');
		if( usernameJq.val()=='' ) {
			jQuery('#signInHideShowUserName').focus();
		}	
	}
}

function checkParent(t){
	while(t.parentNode){
		if(t==document.getElementById('signInHideShow')){
			return false;
		}
		t=t.parentNode;
	}
	return true;
} 

	
/*
//PRIMARY NAV HIDING DIVIDER BARS WHEN CSS CAN'T DO IT
    $('#primaryNav li').mouseover(function () {

		$(this).find('span').hide(); 
		$(this).prev('li').find('span').hide();

});
	
$('#primaryNav li').mouseout(function () {
		$(this).find('span').show(); 
		$(this).prev('li').find('span').show();

	});
*/
	
});

//function to look at query strings
$clorox.getParameterByName = function (name)
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return decodeURIComponent(results[1].replace(/\+/g, " "));
}


// Track Analytics for Flash pages, analyticsType & flashID is passed by ExternalInterface.call in the actionscript 
// analyticsType can only be valid Google Analytics pageTypes (e.g. clorox-promo,etc)
// flashID should be as descriptive as possible identifying what is tracked.
function trackFlashAnalytics(analyticsType, flashID){
	$clxcommon.trackAnalyticsPageView(analyticsType, flashID);
}

//Set a cookie
function setCookie(c_name, value, exdays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value = escape(value) + ((exdays==null) ? '' : '; path=/; expires=' + exdate.toUTCString());
	document.cookie=c_name + '=' + c_value;
}

//Get a cookie
function getCookie(c_name) {
	var i,x,y,ARRcookies=document.cookie.split(';');
	for (i=0; i < ARRcookies.length; ++i) {
		x = ARRcookies[i].substr(0, ARRcookies[i].indexOf('='));
		y = ARRcookies[i].substr(ARRcookies[i].indexOf('=') + 1);
		x = x.replace(/^\s+|\s+$/g,'');

		if (x==c_name) {
			return unescape(y);
		}
	}
}

// Get url parameters as an associate array
function getUrlVars() {
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; ++i) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}

