/**
 *  This javascript include file is used for functions used in multiple sections
 */
$(function(){
	if($.CS_RegionalizedValues) {
		options = typeof(options) == 'undefined' ? {} : options;
		translations = $.CS_RegionalizedValues(options);
	}
    loadEmail();
    loadCountryPullDown();
    loadSearchBox();
});
$(window).load(function () {
    loadVideoIcon();
});
/**
 * preLoadImages
 */
(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);
$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
};
/**
 * 
 */
function loadEmail(){
//======================================================================
//   This makes the word "Enter your email" disappear onfocus
//======================================================================
    $('input#uxClarisonicEmail').focus(function(){
        $(this).attr('value', $(this).attr('value').replace(translations.registration.helper_email,'')).css('color', '#565049').css('font-style', 'normal').css('font-family', 'Verdana').css('font-size', '1em');
    });
}
/**
 * Adding an image icon on top of Video thumbnails and PDF files
 */
function loadVideoIcon() {
	$('a[href*=".pdf"]').attr("target","_blank");
	$('a[href*=".swf"]:not([target="_blank"])>img,a[href*="youtube"][href*="v"]:not([target="_blank"])>img').each(function(){
		$(this).wrap('<div class="playButtonWrap" style="position:relative"></div>').parent().css({width:$(this).width()+'px',height:$(this).height()+'px',cursor:'hand'}).append('<img src="http://cdn.clarisonic.com/includes/images/generic_images/buttons/play.png" style="position:absolute;right:2px;bottom:2px;margin:0px" />');
	});
}
/**
 *
 */
function loadSearchBox(){
	var origValue = $('div.searchBox input').eq(0).val();
//======================================================================
//   This makes the word "Search" disappear onfocus
//======================================================================
    $('div.searchBox input').eq(0).focusin(function(){
        if($(this).attr('value') == origValue) {
            var sel = '#toptext input[type=text]';
            $(sel).attr('value','');
            $(this).css('color', '#565049');
            $(this).css('font-style', 'normal');
        }
    });
}

function loadCountryPullDown(){
//=========================================================
//  This loads the functionality for the country pulldown in the footer.
//=========================================================
    if(typeof $('#countrySelect') !== 'undefined'){
            $('#countrySelect li:eq(0)').show();
            $('#countrySelect').mouseenter(function(){
                $('#countrySelect li').show();
                $('#countrySelect').css('height', '100px');
            });
            $('#countrySelect').mouseleave(function(){
                $('#countrySelect li').hide();
                $('#countrySelect li:eq(0)').show();
                $('#countrySelect').css('height', '20px');
            });
    }
}
//=========================================================

//
//
//
function clear_left(sel, i){
//...............................................
//  This function adds a clear-left style after to the "i-th" element
//  selected by sel and removes all other clear lefts
//...............................................
var count = 0;
$(sel).each(function(){
    count++;
    if(count % parseInt(i) == 1) {
        $(this).css('clear', 'left');
    } else {
        $(this).css('clear', 'none');
    }
});
}
//...............................................
//
//
$.fn.extend({
    checkArrows: function(slides, direction){
    //.................................................
    //  This function checks to make sure the arrows are in the appropriate state
    //.................................................
        var left = $(this).css('left').replace('px', '');
        var slider = slides.replace('div#slider_', '');
        var num = parseInt(slider)-1;
        var pint = $('span#image_slide_counter_slider_'+slider).html();
        var maxslides = $(slides + ' img').length/2;
        switch(parseInt(pint)){
            case 1:
                $('div.bfaWrap:eq('+ num + ') td.image_slider_arrow_cell:eq(0) img').attr('src', '/cdn/includes/images/generic_images/buttons/arrow_L_disabled.gif').unbind('mouseenter mouseleave');
                break;
            case 2:
                if(direction=='right'){
                    $('div.bfaWrap:eq('+ num + ') td.image_slider_arrow_cell:eq(0) img').attr('src', '/cdn/includes/images/generic_images/buttons/arrow_L_rest.gif').hover(function(){
                        $('div.bfaWrap:eq('+ num + ') td.image_slider_arrow_cell:eq(0) img').attr('src', '/cdn/includes/images/generic_images/buttons/arrow_L_hover.gif');
                    }, function(){
                        $('div.bfaWrap:eq('+ num + ') td.image_slider_arrow_cell:eq(0) img').attr('src', '/cdn/includes/images/generic_images/buttons/arrow_L_rest.gif');
                    });

                }
                break;
            case maxslides-1:
                $('div.bfaWrap td.image_slider_arrow_cell:eq(1) img').attr('src', '/cdn/includes/images/generic_images/buttons/arrow_R_rest.gif');
                $('div.bfaWrap td.image_slider_arrow_cell:eq(1) img').hover(function(){
                    $('div.bfaWrap td.image_slider_arrow_cell:eq(1) img').attr('src', '/cdn/includes/images/generic_images/buttons/arrow_R_hover.gif');
                }, function(){
                    $('div.bfaWrap td.image_slider_arrow_cell:eq(1) img').attr('src', '/cdn/includes/images/generic_images/buttons/arrow_R_rest.gif');
                });
                break;
            case maxslides:
                    $('div.bfaWrap:eq('+ num + ') td.image_slider_arrow_cell:eq(1) img').attr('src', '/cdn/includes/images/generic_images/buttons/arrow_R_disabled.gif').unbind('mouseenter mouseleave');
                break;
            default:
                break;
        }
    },

    //.................................................
    moveSlide: function(){
    //.................................................
    //
    //  Slide the reel 264px to the left
    //
    //.................................................
        $(this).unbind('click').click(function(){
            return false;
        });
        var direction = ($(this).parent().attr('class').replace('image_slider_arrow_cell ', '').replace('_arrow', ''));
        var index = ($('td.image_slider_arrow_cell a').index($(this)));
        var slider = (parseInt(index/2) + 1);
        var slides = 'div#slider_'+slider;
        var left = parseInt($(slides).css('left').replace('px', ''));
        var lastslide = parseInt($('div#slider_'+slider+ ' img').length/2);
        if ((left == 0 || isNaN(left)) && direction !=='right'){
            $(this).click(function(){
                return $(this).moveSlide();
            });
            return false;

        }
        if((left-264) == (lastslide*-264) && direction =='right'){
            $(this).click(function(){
                return $(this).moveSlide();
            });
            return false;
        }
        if(direction == 'right'){
            var go= '-=264';
        } else {
            var go = '+=264';
        }
        var obj = $(this);
        $(slides).animate({
            left: go
        }, function(){
            $(obj).click(function(){
                $(this).moveSlide();
            });
            var currslide = (parseInt($(slides).css('left').replace('px', ''))/-264)+1;
            $('span#image_slide_counter_slider_'+slider).html(currslide);
            $(slides).checkArrows(slides, direction);
        });

        return false;
    }
});
    //.................................................
$(function(){
	m_names = translations.monthNames;
});
$(function(){
    //
    //  The logic for small before and after image sliders
    //
    if(typeof $('table.imageSlider') !=='undefined'){
        $('td.image_slider_arrow_cell a').click(function(){
            return $(this).moveSlide();
        });
        var rightArrowImg = 'div.bfaWrap td.right_arrow img';
        $(rightArrowImg).attr('src', '/cdn/includes/images/generic_images/buttons/arrow_R_rest.gif');
        $(rightArrowImg).hover(function(){
            $(this).attr('src', '/cdn/includes/images/generic_images/buttons/arrow_R_hover.gif');
        }, function(){
            $(this).attr('src', '/cdn/includes/images/generic_images/buttons/arrow_R_rest.gif');
        });
    }
});
$('span.st_sharethis_hcount').live('mouseover', function(e){
    $('div#stwrapper').css('position', 'absolute');
    $('div#stwrapper').css('top', e.pageY);

});
$(function(){
    $('h2').replaceWith(function() {
      $title = $(this).html();
      $new_title = $title.replace(/\*(\d+)/, "<span class=\"sup\">*$1</span>");
      return '<h2>' + $new_title + '</h2>';
    });
});
