// Get an email address for anti-spam.
function emailantispam(email1, email2, email3, email4) {
   emailE = email1 + email2 + '@' + email3 + email4;
   document.write('<a href="mailto:' + emailE + '">' + emailE + '</a>');
};

// Slide the slideshow to next image.
function slideSwitch() {
	var $activeDiv = $('#slideshow div.active');
	var $activeNav = $('#image-navigator span.active');

  if ( $activeDiv.length == 0 ) $activeDiv = $('#slideshow div:last');
	if( $activeNav.length == 0 ) $activeNav = $('#image-navigator span:last');

  var $nextDiv =  $activeDiv.next().length ? $activeDiv.next() : $('#slideshow div:first');
  var $nextNav =  $activeNav.next().length ? $activeNav.next() : $('#image-navigator span:first');
  
  $activeDiv.addClass('last-active');
  $activeNav.addClass('last-active');

  $nextNav.addClass('active');
	$activeNav.removeClass('active last-active');

  $nextDiv.css({opacity: 0.0})
      .addClass('active')
      .animate({opacity: 1.0}, 1500, function() {
          $activeDiv.removeClass('active last-active');
			});
	
	setTimeout("slideSwitch()", 3500);
}

// Horizontal Carousel Scroll Left
function scrollLeft(container, items) {

	if(container && items) {
		var table = container.find('table');
		if(table) {
			var containerWidth = container.width();
			var moveDistance = 0;
			var offset = table.position().left + table.width();
			var finished = 0;
			$($(items).get().reverse()).each(function() {
				if(finished == 0) {
					if(offset <= 0) {
						if(moveDistance + $(this).outerWidth(true) <= containerWidth) {
							moveDistance = moveDistance + $(this).outerWidth(true);
						}
						else {
							finished = 1;
						}
					}
					offset = offset - $(this).outerWidth(true);
				}
			});
									
			var leftPosition = table.position().left + moveDistance;
			var distanceRemaining = table.width() + leftPosition - containerWidth;

			if(leftPosition < 0) {
				$('#scroll-left').fadeIn(1250);
			}
			else {
				$('#scroll-left').fadeOut(1250);
			}
			if(distanceRemaining <= 0) {
				$('#scroll-right').fadeOut(1250);
			}
			else {
				$('#scroll-right').fadeIn(1250);
			}
			
			moveTableLeft(table, leftPosition);
		}
	}
}

// Horizontal Carousel Scroll Right
function scrollRight(container, items) {

	if(container && items) {
		var table = container.find('table');
		if(table) {
			var containerWidth = container.width();
			var moveDistance = 0;
			var offset = table.position().left;
			var finished = 0;
			$(items).each(function() {
				if(finished == 0) {
					if(offset >= 0) {
						if(offset + $(this).outerWidth(true) <= containerWidth) {
							moveDistance = moveDistance + $(this).outerWidth(true);
						}
						else {
							finished = 1;
						}
					}
					offset = offset + $(this).outerWidth(true);
				}
			});
			
			var leftPosition = table.position().left - moveDistance;
			
			var distanceRemaining = table.width() + leftPosition - containerWidth;

			if(leftPosition < 0) {
				$('#scroll-left').fadeIn(1250);
			}
			else {
				$('#scroll-left').fadeOut(1250);
			}
			if(distanceRemaining <= 0) {
				$('#scroll-right').fadeOut(1250);
			}
			else {
				$('#scroll-right').fadeIn(1250);
			}
			
			moveTableLeft(table, leftPosition);
		}
	}
}

// Vertical Carousel Scroll Up
function scrollUp(container, items) {

	if(container && items) {
		var table = container.find('table');
		if(table) {
			var containerHeight = container.height();
			var moveDistance = 0;
			var offset = table.position().top + table.height();
			var finished = 0;
			$($(items).get().reverse()).each(function() {
				if(finished == 0) {
					if(offset <= 0) {
						if(moveDistance + $(this).height() < containerHeight) {
							moveDistance = moveDistance + $(this).height();
						}
						else {
							finished = 1;
						}
					}
					offset = offset - $(this).height();
				}
			});
									
			var topPosition = table.position().top + moveDistance;
			var distanceRemaining = table.height() + topPosition - containerHeight;

			if(topPosition < 0) {
				$('#scroll-up').fadeIn(1250);
			}
			else {
				$('#scroll-up').fadeOut(1250);
			}
			if(distanceRemaining <= 0) {
				$('#scroll-down').fadeOut(1250);
			}
			else {
				$('#scroll-down').fadeIn(1250);
			}
			
			moveTableTop(table, topPosition);
		}
	}
}

// Vartical Carousel Scroll Down
function scrollDown(container, items) {

	if(container && items) {
		var table = container.find('table');
		if(table) {
			var containerHeight = container.height();
			var moveDistance = 0;
			var offset = table.position().top;
			var finished = 0;
			$(items).each(function() {
				if(finished == 0) {
					if(offset >= 0) {
						if(offset + $(this).height() <= containerHeight) {
							moveDistance = moveDistance + $(this).height();
						}
						else {
							finished = 1;
						}
					}
					offset = offset + $(this).outerHeight(true);
				}
			});
			
			var topPosition = table.position().top - moveDistance;
			
			var distanceRemaining = table.height() + topPosition - containerHeight;

			if(topPosition < 0) {
				$('#scroll-up').fadeIn(1250);
			}
			else {
				$('#scroll-up').fadeOut(1250);
			}
			if(distanceRemaining <= 0) {
				$('#scroll-down').fadeOut(1250);
			}
			else {
				$('#scroll-down').fadeIn(1250);
			}
			
			moveTableTop(table, topPosition);
		}
	}
}

// Animate item to new left position
function moveTableLeft(table, leftPosition) {
	
	if(leftPosition * -1 <= table.outerWidth() && leftPosition <= 0) {
		var leftPx = leftPosition + 'px';
		table.animate
		(
			{ left: leftPx }, 
			{
				duration: 1250, 
				easing: 'easeOutQuint'
			}
		);
	}
}

// Animate the item to new top location
function moveTableTop(table, topPosition) {
	
	if(topPosition * -1 <= table.outerHeight() && topPosition <= 0) {
		var topPx = topPosition + 'px';
		table.animate
		(
			{ top: topPx }, 
			{
				duration: 1250, 
				easing: 'easeOutQuint'
			}
		);
	}
}

// Horizontal Carousel Left Click handler
function leftClickHandler(evt) {
	var container = $("#product-carousel-horizontal #inner-container");
	var table = $("#product-carousel-horizontal #inner-container table");
	var items = $("#product-carousel-horizontal td");
	
	if(!table.is(':animated')) {
		scrollLeft(container, items);
		
		var highlighted = $("#product-carousel-horizontal #button-container #scroll-paging span.active");
		highlighted.removeClass('active');
		var prevHighlight = highlighted.prev();
		if(prevHighlight != null)
		{
			prevHighlight.addClass('active');
		}
	}
}

// Horizontal Carousel Right Click handler
function rightClickHandler(evt) {
	var container = $("#product-carousel-horizontal #inner-container");
	var table = $("#product-carousel-horizontal #inner-container table");
	var items = $("#product-carousel-horizontal td");
	
	if(!table.is(':animated')) {
		scrollRight(container, items);

		var highlighted = $("#product-carousel-horizontal #button-container #scroll-paging span.active");
		highlighted.removeClass('active');
		var nextHighlight = highlighted.next();
		if(nextHighlight != null)
		{
			nextHighlight.addClass('active');
		}
	}
}

// Vertical carousel up click handler
function upClickHandler(evt) {
	var container = $("#product-carousel-vertical #inner-container");
	var table = $("#product-carousel-vertical #inner-container table");
	var items = $("#product-carousel-vertical tr");
	
	if(!table.is(':animated')) {
		scrollUp(container, items);
	}
}

// Vertical Carousel down click handler
function downClickHandler(evt) {
	var container = $("#product-carousel-vertical #inner-container");
	var table = $("#product-carousel-vertical #inner-container table");
	var items = $("#product-carousel-vertical tr");
	
	if(!table.is(':animated')) {
		scrollDown(container, items);
	}
}


$(function() {

	// Set up the search box functionality
	$(".search-input").focus(function() {
		if($(this).val() == "SEARCH")
		{
			$(this).val("");
		}
	});
			
	$(".search-input").blur(function() {
		if($(this).val() == "")
		{
			$(this).val("SEARCH");
		}
	});
		
	// Setup the pulse images.
	$('span.pulseImage').mouseenter(function() {
		$(this).animate( { 'opacity' : .75 }, 200, function() {
			$(this).animate( { 'opacity' : 1 }, 1000 );
		} );
	});

	// Setup the pulse images.
	$('div.pulseImage').mouseenter(function() {
		$(this).animate( { 'opacity' : .75 }, 200, function() {
			$(this).animate( { 'opacity' : 1 }, 1000 );
		} );
	});

	// Setup the pulse images.
	$('img.pulseImage').mouseenter(function() {
		$(this).animate( { 'opacity' : .75 }, 200, function() {
			$(this).animate( { 'opacity' : 1 }, 1000 );
		} );
	});
	
	// Setup the slideshow
	var slideshowDiv = $('#slideshow > div');
	if(slideshowDiv && slideshowDiv.size() > 1)
	{
		setTimeout('slideSwitch()', 5000);
	}
	
});

$(window).load(function () {
  setupHorizontalCarousel();
  setupVerticalCarousel();
  setupScrollBoxVertical();
  setupProductIndexGridCarousel();
});

function setupHorizontalCarousel()
{
	// Setup horizontal carousel if present
	var horizontalCarousel = $('product-carousel-horizontal');
	if(horizontalCarousel)
	{
		var scrollLeft = $('#scroll-left');
		var scrollRight = $('#scroll-right');	
		var paging = $('#scroll-paging');
		scrollLeft.hide();
		scrollRight.hide();
		paging.hide();

		var container = $("#product-carousel-horizontal #inner-container");
		var table = $("#product-carousel-horizontal #inner-container table");
		if(table.width() > container.width())
		{
			scrollLeft.css('visibility', 'visible');
			scrollRight.css('visibility', 'visible');
			paging.css('visibility', 'visible');
			
			scrollLeft.bind('click', leftClickHandler);
			scrollRight.bind('click', rightClickHandler);
			$('#scroll-paging span:first').addClass('active');
			
			paging.fadeIn(1250);
			scrollRight.fadeIn(1250);
		}
	}
}

function setupVerticalCarousel()
{	
	// Setup vertical carousel if present
	var verticalCarousel = $("#product-carousel-vertical");
	if(verticalCarousel)
	{	
		var scrollUp = $('#scroll-up');
		var scrollDown = $('#scroll-down');	
		scrollUp.hide();
		scrollDown.hide();
		
		var container = $("#product-carousel-vertical #inner-container");
		var table = $("#product-carousel-vertical #inner-container table");
		if(table.height() > container.height())
		{
			scrollUp.css('visibility', 'visible');
			scrollDown.css('visibility', 'visible');
			
			scrollUp.bind('click', upClickHandler);
			scrollDown.bind('click', downClickHandler);
			scrollDown.fadeIn(1250);
		}
	}
}
 
function setupScrollBoxVertical()
{	
	// Setup any vertical scroll boxes if present
	var verticalScrollBoxes = $("div.scroll-box-outer-container");
	if(verticalScrollBoxes)
	{
		jQuery.each(verticalScrollBoxes, function() {
			var scrollUp = $(this).find("div.scroll-up");	
			var scrollDown = $(this).find("div.scroll-down");
			var innerContainer = $(this).find("div.scroll-box-inner-container");
			var contentContainer = $(this).find("div.content-container");
			var buttonContainer = $(this).find("div.button-container");
			
			if(innerContainer.height() > contentContainer.height()) {
				innerContainer.width(innerContainer.width() + buttonContainer.width());
			}
			else {
				scrollDown.hide();
				scrollUp.hide();
				scrollDown.css('visibility', 'visible');
				scrollUp.css('visibility', 'visible');
				scrollDown.fadeIn(1250);
				
				scrollDown.bind('click', function() {
					
					if(!contentContainer.is(":animated")) {
						var top = contentContainer.position().top;
						var heightToMove = innerContainer.height() / 10 * 9;
						top = top - heightToMove;
						var topPx = top + 'px';
						contentContainer.animate
						(
							{ top: topPx }, 
							{
								duration: 1250, 
								easing: 'easeOutQuint'
							}
						);
						
						if(top < 0 && !scrollUp.is(":visible")) {
							scrollUp.fadeIn(1250);
						}
						if(top + contentContainer.height() - innerContainer.height() < 0) {
							scrollDown.fadeOut(1250);
						};
					}
				});
				
				scrollUp.bind('click', function() {
					
					if(!contentContainer.is(":animated")) {
						var top = contentContainer.position().top;
						var heightToMove = innerContainer.height() / 10 * 9;
						top = top + heightToMove;
						if(top > 0) top = 0;
						var topPx = top + 'px';
						contentContainer.animate
						(
							{ top: topPx }, 
							{
								duration: 1250, 
								easing: 'easeOutQuint'
							}
						);
						
						if(top == 0 && scrollUp.is(":visible")) {
							scrollUp.fadeOut(1250);
						}
						if(top + contentContainer.height() - innerContainer.height() > 0 && !scrollDown.is(":visible")) {
							scrollDown.fadeIn(1250);
						};
					}
				});
				
				// Setup an Anchor clicks
				$('a.jump-to-item').click(function(event) {
					event.preventDefault();

					if(!contentContainer.is(":animated")) {
						var fullUrl = this.href;
		        //split the url by # and get the anchor target name - home in mysitecom/index.htm#home
						var parts = fullUrl.split("#");
						var target = parts[1];
		        //get the top offset of the target anchor
						var top = $("#"+target).position().top * -1;

						var topPx = top + 'px';
						contentContainer.animate
						(
							{ top: topPx }, 
							{
								duration: 1250, 
								easing: 'easeOutQuint'
							}
						);
						
						if(top == 0 && scrollUp.is(":visible")) {
							scrollUp.fadeOut(1250);
						}
						else if(top < 0 && !scrollUp.is(":visible")) {
							scrollUp.fadeIn(1250);
						}
						
						if(top + contentContainer.height() - innerContainer.height() > 0 && !scrollDown.is(":visible")) {
							scrollDown.fadeIn(1250);
						}
						else if(top + contentContainer.height() - innerContainer.height() <= 0 && scrollDown.is(":visible")) {
							scrollDown.fadeOut(1250);
						}
					}
					return false;
				});
			}
		});		
	}
}

// Vertical carousel up click handler
function scrollUpHandler(evt) {
	
	var container = $("#product-carousel-vertical #inner-container");
	var table = $("#product-carousel-vertical #inner-container table");
	var items = $("#product-carousel-vertical tr");
	
	if(!table.is(':animated')) {
		scrollUp(container, items);
	}
}


function setupProductIndexGridCarousel() {
	// Setup vertical carousel if present
	var container = $("#grid-container");
	if(container)
	{
		var scrollPrevious = $('#product-index-grid-previous');
		var scrollNext = $('#product-index-grid-next');
		var paging = $('#product-index-grid-paging');
		
		scrollNext.hide();
		scrollPrevious.hide();
		paging.hide();
		scrollNext.css('visibility', 'visible');
		scrollPrevious.css('visibility', 'visible');
		paging.css('visibility', 'visible');
		
		var table = $("#grid-container table");
		if(table.width() > container.width() + 12)
		{
			scrollPrevious.bind('click', productIndexGridScrollLeftHandler);
			scrollNext.bind('click', productIndexGridScrollRightHandler);
			paging.fadeIn(1250);
	
			// Find if we need to scroll
			var highlighted = $("#product-index-grid-paging span.active");
			var left = 0;
			if(highlighted != null)
			{
				var pageNeeded = highlighted.index();
				if(pageNeeded > -1) {
					left = -648 * pageNeeded;
					moveTableLeft(table, left);
				}
			}
			
			if(left < 0) {
				scrollPrevious.fadeIn(1250);
			}
			if(left - 648 > table.width() * -1) {
				scrollNext.fadeIn(1250);
			}
		}
		setupProductIndexGridSwapContent();
	}
}

// Horizontal product index left click handler
function productIndexGridScrollLeftHandler(evt) {
	
	var table = $("#grid-container table");	
	if(!table.is(':animated')) {
		
		var left = table.position().left;
		left = left + 648;
		
		moveTableLeft(table, left);
		
		if(left == 0) {
			$('#product-index-grid-previous').fadeOut(1250);
		}
		
		if(table.width() > 648) {
			$('#product-index-grid-next').fadeIn(1250);
		}
		
		var highlighted = $("#product-index-grid-paging span.active");
		highlighted.removeClass('active');
		var prevHighlight = highlighted.prev();
		if(prevHighlight != null)
		{
			prevHighlight.addClass('active');
		}

	}
}

// Horizontal product index right click handler
function productIndexGridScrollRightHandler(evt) {
	
	var table = $("#grid-container table");	
	if(!table.is(':animated')) {
		
		var left = table.position().left;
		left = left - 648;
		
		moveTableLeft(table, left);
		
		if(left - 648 == table.width() * -1) {
			$('#product-index-grid-next').fadeOut(1250);
		}
		
		if(left < 0) {
			$('#product-index-grid-previous').fadeIn(1250);
		}
		
		var highlighted = $("#product-index-grid-paging span.active");
		highlighted.removeClass('active');
		var nextHighlight = highlighted.next();
		if(nextHighlight != null)
		{
			nextHighlight.addClass('active');
		}

	}
}

function setupProductIndexGridSwapContent() {
	$('div.product-index-grid-page').each(function() {
	
		var mainImage = $(this).find('div.product.large');
		var mainImageDiv = mainImage.find('div.grid-content');
		var mainImageHeading = mainImage.find('div.headline');
		var mainImagePicture = mainImage.find('div.image-span');
		
		$(this).find('div.product.small').each(function() {
		
			$(this).click(function() {
				var smallImage = $(this);
				var smallImageDiv = smallImage.find('div.grid-content');
				var smallImageHeading = smallImage.find('div.body-text-a-regular');
				var smallImagePicture = smallImage.find('div.image-span');
				
				var newSmallImageUrl = mainImagePicture.css('background-image').replace('.jpg', '-thumb.jpg');
				var newSmallHeading = mainImageHeading.text();
				var newLargeImageUrl = smallImagePicture.css('background-image').replace('-thumb.jpg', '.jpg');
				var newLargeHeading = smallImageHeading.text();
				
				mainImageDiv.fadeOut(500);
				smallImageDiv.fadeOut(500, function() {
					mainImageHeading.text(newLargeHeading);
					mainImagePicture.css('background-image', newLargeImageUrl);
					mainImageDiv.fadeIn(500);					
					smallImageHeading.text(newSmallHeading);
					smallImagePicture.css('background-image', newSmallImageUrl);
					smallImageDiv.fadeIn(500);					
				});
				
			});
		});
	
	});
}
