/*
 * Site Common Javascript
 *
 * May 10, 2010
 *
 */

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

$(document).ready(function() {
	
	// Set up accordions
	
	// default accordion functionality
	$('.accordion h4').click(function() {
		$('.accordionContent').slideUp(100);
		$('.accordion h4').removeClass("active");
		//IF THE NEXT SLIDE WASN'T OPEN THEN OPEN IT
		if($(this).next().is(':hidden') == true) {
			
			//ADD THE active CLASS TO THE BUTTON
			$(this).addClass('active');
			  
			//OPEN THE SLIDE
			$(this).next().slideDown(100);
		 }
		 return false;
	});
	
	// Close all accordions on page load
    $('.accordion .accordionContent').hide();
    
    //check if hash tag exists in the URL
    if(window.location.hash) {
         
        //set the value as a variable, and remove the #
        var hash_value = window.location.hash.replace('#', '');
        
        // test to make sure the hashtag actually matches something in an accordion
        if($('.accordion #'+hash_value).length > 0){
            // trigger an open accordion based on the hash tag value
            $('.accordion #'+hash_value).trigger('click');
         }else{
            // or just open whichever one was set as default
            $(".accordion .open").trigger('click');
         }
         
    }else{
    
        // or just open whichever one was set as default
        $(".accordion .open").trigger('click');
    }
    
    // if a link on the same page as an accordion 
    // has a class matching the id of one of the accordion 
    // section + "_open" (e.g. class="product-supply_open" ) it will 
    // open that corresponding accordion section
    
    if($('.accordion .accordionContent').length > 0){
    
        $('.accordion h4').each(function(){
            var theId = $(this).attr('id');
            
            $('a.'+theId+'_open').click(function(){
                
                $('#'+theId).trigger('click');
                console.log($(this));
            });
        });
    }

	
    $clxcommon.initExternal();
    $clxcommon.initWarning();
    $clxcommon.initWarningFrench();
    $clxcommon.initWarningSpanish();
    $clxcommon.attachClickAnalytics('clorox-promo');
    $clxcommon.attachClickAnalytics('clorox-video');
    $clxcommon.attachClickAnalytics('clorox-download');
    	
 	//LINKED DIVS
 	//$clxcommon.createLink('#someDiv', '/some-path/');

	$clxcommon.createLink('#home #specialOffers', '/products/special-offers/');
	$clxcommon.createLink('#home #annualReport', 'http://2011annualreport.thecloroxcompany.com/');
	$clxcommon.createLink('#home #supplyChainsAct', '/downloads/Clorox_CA_Supply_Chains_Act_Disclosure.pdf');

 	$clxcommon.createLink('#performance #promo6', '#');
  	$clxcommon.createLink('#performance #promo4', 'http://'+INVESTORS_DOMAIN);
  	
  	$clxcommon.createLink('#people #promo8', '/corporate-responsibility/people/workplace-safety/');
 	$clxcommon.createLink('#people #promo9', '/corporate-responsibility/people/diversity/');
 	
 	$clxcommon.createLink('#products #promo10', '/products/ingredients-inside/');
 	$clxcommon.createLink('#products #promo11', '/corporate-responsibility/products/product-sustainability/');
 	
 	$clxcommon.createLink('#careers #promo1', '/careers/working-at-clorox/');
 	$clxcommon.createLink('#careers #promo3', '/careers/who-we-are/');
 	
 	$clxcommon.createLink('#company #promo1', '/company/mission-and-values/');
 	$clxcommon.createLink('#company #promo2', '/company/executive-team/');
 	$clxcommon.createLink('#company #promo3', '/company/heritage/bottle-guide/');
	
 	$clxcommon.createLink('#products #promo1', '/products/our-brands');
 	$clxcommon.createLink('#products #promo2', '/products/special-offers');
 	$clxcommon.createLink('#products #promo3', 'http://cloroxconnects.com');
	
	$clxcommon.createLink('#innovation #promo1', 'http://blog.cloroxconnects.com/');
	$clxcommon.createLink('#innovation #promo2', 'http://cloroxconnects.com/pages/0835748c9f');
	
	$clxcommon.createLink('#heritage #promo1', '/company/heritage/timeline');
	$clxcommon.createLink('#heritage #promo2', '/company/heritage/bottle-guide');
	
 	$clxcommon.createLink('#corporate-governance #promo1', '/company/executive-team');	
 	$clxcommon.createLink('#corporate-governance #promo2', '/corporate-responsibility/performance/corporate-governance/board-of-directors/');
 	
 	 	

	// Set up Sliders
	// **************
	if( $('#slider1').length > 0)
	{

		$('#slider1').anythingSlider({
			resizeContents      : true, // If true, solitary images/objects in the panel will expand to fit the viewport
			navigationSize      : 11,     // Set this to the maximum number of visible navigation tabs; false to disable
			navigationFormatter : function(index, panel){ // Format navigation labels with text
				return ['1910s', '1920s', '1930s', '1940s', '1950s', '1960s', '1970s', '1980s', '1990s', '2000s', '2010s'][index - 1];
			},
			onSlideComplete: function(slider) {
				// keep the current navigation tab in view
				slider.navWindow( slider.currentPage );
			}
		});
		$('#slider1').css('visibility','visible');
	};
	
	// Set up modals
	// Load dialog on click
	// this first case is particular to modals with flowplayer
	// to prevent IE crashes when flash elements don't unload 
	if( $('.modalWithUnload').length ){
	   $('.modalWithUnload').click(function (e) {
			e.preventDefault();
			var aux = $(this).attr('id');
        	aux = aux.replace(/[A-z]+/,'modalContent');
			$('#' + aux).modal({
				overlayClose: true,
				// this is the important part to this 
				// particular version
				onClose: function (dialog) {
            	   $f().unload(function(){
            	       $.modal.close();
            	   });
            	}
			});

		});
	   
	//standard case opening/closing modal
	}else if( $('.modal').length ){
		$('.modal').click(function (e) {
			e.preventDefault();
			var aux = $(this).attr('id');
        	aux = aux.replace(/[A-z]+/,'modalContent');
			$('#' + aux).modal({
				overlayClose: true
			});

		});
	};
	
	//open simplemodal on cr page from a particular hashed link
	if(window.location.hash.indexOf('ingredients-inside-video') != -1){
	   $('#modalContent2').modal();

	}



	   
});



//hide or show the career area div on select.

var lastDiv = "";
function showDiv(divName) {
	// hide last div
	if (lastDiv) {
		document.getElementById(lastDiv).className = "hiddenDiv";
	}
	//if value of the box is not nothing and an object with that name exists, then change the class
	if (divName && document.getElementById(divName)) {
		document.getElementById(divName).className = "visibleDiv";
		lastDiv = divName;
	}
}

