/*
 * 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() {
	//Attach GA tags
	//$('.arrow.back a').addClass('clorox-virtual').attr('id', 'homepage-back');
	//$('.arrow.forward a').addClass('clorox-virtual').attr('id', 'homepage-forward');
	
	
	var hovering;
    var current;
$('.slider').anythingSlider({
	startPanel               : 3,
	hashTags            : true,      
	buildArrows         : true,      // If true, builds the forwards and backwards buttons
	resizeContents	    : false,	 //set to false to resize :\
	startStopped		: false,		
	infiniteSlides	    : true,
	stopAtEnd           : false,     
	autoPlay            : false,      // This turns off the entire slideshow FUNCTIONALY, not just if it starts running or not
	delay               : 20000,      // How long between slideshow transitions in AutoPlay mode (in milliseconds)
	animationTime       : 300,       // How long the slideshow transition takes (in milliseconds)
	easing              : "easeInOutSine",    // Anything other than "linear" or "swing" requires the easing plugin
    //AT 08/05/11
	//Add slideshow option for GA tracking between the different slideshows
	slideshow           : 'homepage',
onSlideComplete : function(slider){ 
	
	//set current for the sprite positioning index
	current = slider.currentPage - 1;
	
	//get the hover state and possibly update the text when the slide changes 
	switch(hovering){
	case 'div.anythingSlider .back a':
	    moveSprite('back');
	break;
	case 'div.anythingSlider .forward a':
	    moveSprite('forward');
	break;
	default:
	    // or hide the sprite
	    $('div.anythingSlider a').css('backgroundPosition', '200px 0');
	  }
   }
});

	
	$('div.anythingSlider .back a').hover(function () {
	    //state
	    hovering = 'div.anythingSlider .back a';
		//get the current slide # (zero-based) and then move the sprite
		current = $('.slider').data('AnythingSlider').currentPage - 1; // returns page #
        moveSprite('back');
	 
	},  function() { //hover state off
	    hovering = false;
    	$(this).css('backgroundPosition', '200px 0');
	});
	
	$('div.anythingSlider .forward a').hover(function () {
	    //the state
        hovering = 'div.anythingSlider .forward a';
        //get the current slide # (zero-based) and then move the sprite
		current = $('.slider').data('AnythingSlider').currentPage - 1; // returns page #
        moveSprite('forward');
        
	},  function() { //hover state off
	    hovering = false;
    	$(this).css('backgroundPosition', '200px 0');
	});

					

    /**
     * function moveSprite() uses current slide and direction to  
     * move the sprite that holds text info
     *
     * @param arrow String = reference to the forward or back 
     * arrows that are being hovered over
     */    	
    function moveSprite(arrow){
    
        if(arrow=='back'){
            direction = current-1;
        }else{
            direction = current+1;
        }
        //calculate the slide offset
    	// Because we are taking the modulo of a negative number at times, we use a roundabout way of calculating the modulo
    	// to work around a Javascript bug with modulo of negative numbers.
        slideIndex = ((((direction)%5)+5)%5);
        slideOffset = slideIndex*180;
        spriteMove = '-'+slideOffset+'px 80px';
        
        //position the sprite
        if(arrow=='back'){
            $('div.anythingSlider .back a').css('backgroundPosition', spriteMove);
        }else{
            $('div.anythingSlider .forward a').css('backgroundPosition', spriteMove);
        }
        
    }


(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)
  
jQuery.preLoadImages("/img/hp/slides/slide_products.png","/img/hp/slides/slide_why.png", "/img/hp/slides/slide_hydrate.png", "/img/hp/slides/slide_better_world.png","/img/hp/slides/slide_your_brita.png");


	$clxcommon.createLink('#GA-HP-Slide1-Bubble-Products',     		'/products/');
    $clxcommon.createLink('#GA-HP-Slide1-Feature-Pitchers',     	'/products/filtering-bottle/brita-bottle/');
    $clxcommon.createLink('#GA-HP-Slide2-Bubble-WhyBrita',     		'/why-brita/');
    $clxcommon.createLink('#GA-HP-Slide2-Feature-WhyBrita',     	'/why-brita/');
    $clxcommon.createLink('#GA-HP-Slide3-Bubble-BetterYou',     	'/a-better-you/');
    $clxcommon.createLink('#GA-HP-Slide3-Feature-WaterWeight',		'/a-better-you/exercise/#water-and-your-weight');
    $clxcommon.createLink('#GA-HP-Slide3-Feature-BritaChallenge',	'http://challenge.brita.com/the-brita-challenge/');
    $clxcommon.createLink('#GA-HP-Slide4-Button-Visit',				'/filter-for-good/');
    $clxcommon.createLink('#GA-HP-Slide5-Bubble-YourBrita',			'/your-brita/');
    $clxcommon.createLink('#GA-HP-Slide5-Feature-YourBrita',		'/your-brita/using-your-brita/');
    $clxcommon.createLink('#hpPromo-Reminders',						'/your-brita/filter-reminders/');
	$clxcommon.createLink('#hpPromo-NewColors',						'/products/water-pitchers/');
    $clxcommon.createLink('#hpPromo-WhereToBuy',					'/where-to-buy/');
   	$clxcommon.createLink('#hpPromo-Facebook',						'https://www.facebook.com/BritaFilterForGood');
    

}); 




