// JavaScript Document

var $j = jQuery.noConflict();

$j(document).ready(function() {

	$jvariables = [];

if($j('#slider').length) {
	$j('#slider').cycle({
	fx:    'fade',
	speed:  600,
	timeout:  5000 
	});
}
	
if($j("#banr1").length) { // used for the home page. Setup the scroller
		
		$jvariables.homefeatureIndex = 2; //Since we have 4 elements we are already starting on the first element set the next element to 2.
		$jhomepageScroller();
		$j("#coln1").hover( function() {
			$jhomeBannerOver(1);						   
		}, function() {
			$jvariables.homefeatureIndex = 2;
			$jhomepageScroller();
		});
		$j("#coln2").hover( function() {
			$jhomeBannerOver(2);						   
		}, function() {
			$jvariables.homefeatureIndex = 3;
			$jhomepageScroller();
		});
		$j("#coln3").hover( function() {
			$jhomeBannerOver(3);						   
		}, function() {
			$jvariables.homefeatureIndex = 4;
			$jhomepageScroller();
		});
		$j("#coln4").hover( function() {
			$jhomeBannerOver(4);						   
		}, function() {
			$jvariables.homefeatureIndex = 1;
			$jhomepageScroller();
		});
	}
	
	function $jhomeBannerOver(index) {
		clearInterval($jvariables.homeTimer);
			$j("#banRolls div").removeClass('currentQuadCol');
			$j("#shop-home-banner div").fadeOut("slow", function() {
				$j("#banr"+index).css("top", 0);
			});
			$j("#banr"+index).fadeIn("slow");
			$j("#coln"+index).addClass('currentQuadCol');
	}
	
	
	function $jhomepageScroller() {
		// This function takes the next element index ($jvariables.homefeatureIndex) and hides the current element $jvariables.prevArray
		// and removes it's highlight state. Then fadesIn the next element.
		clearInterval($jvariables.homeTimer);  // clear any current timer we get to remove duplications
		$jvariables.homeTimer = setInterval(function() {		// set new timer function 
		$jvariables.prevArray = ($jvariables.homefeatureIndex == 1) ? 	4 : $jvariables.prevArray = $jvariables.homefeatureIndex-1 ;
		//If the next element is the first column, set the previous element to (4) the fourth column otherwise just next element - 1.
		$j("#banr"+$jvariables.prevArray).fadeOut("slow", function() { 
			$j("#coln"+$jvariables.prevArray).removeClass('currentQuadCol');	
		
		});			
		$j("#banr"+$jvariables.homefeatureIndex).fadeIn("slow", function(){
			$j("#coln"+$jvariables.homefeatureIndex).addClass('currentQuadCol');
			$jvariables.homefeatureIndex++;		
			if($jvariables.homefeatureIndex > 4) {
				$jvariables.homefeatureIndex = 1; // Ensure we're only dealing with values ranging from 1 to 4 (number of columns)
			}
					});
		},8000);	//milliseconds until each change (8 seconds).
	}

if($j(".productDetailView").length) {
	$j("#topNavLinks").hide();
	$j("table.catalogueTable").remove();
	$j(".catalog-information").hide();
}

$j("select.categoryDrop").change(function() {
	if($j(this).val() != null || $j(this).val() != 'null') {
		window.location = $j(this).val();
	}
	//console.log($j(this).val());
})

if($j(".related-products .product-description").length) {
$j(".related-products").find('.product-description').each(function() { 
	$j(this).find('a:eq(0)').remove();
});
} else {
$j(".otherProductList").remove();
}



  $j(".equalHeights").each(function(){
	  var currentTallest = 0;
	  $j(this).children().each(function(i){
		  if ($j(this).height() > currentTallest) { currentTallest = $j(this).height(); }
	  });
	  // for ie6, set height since min-height isn't supported
	  if ($j.browser.msie && $j.browser.version == 6.0) { $j(this).children().css({'height': currentTallest}); }
	  $j(this).children().css({'min-height': currentTallest}); 
  });
  

$j(".accordionHead").click(function() {
		if($j(this).hasClass('expanded'))
			{
				$j(this).parent().find('.accordionBody').slideUp(200);
				$j(this).parent().find('.accordionHead').removeClass('expanded')
			} else {
				$j(this).parent().find('.accordionBody').slideDown();
				$j(this).parent().find('.accordionHead').addClass('expanded')
			}
		
			
		var accordianTotal = $j(this).parent().parent().find('.accordionItem');
		var expandedTotal = $j(this).parent().parent().find('.expanded');
		
		if(accordianTotal.length === expandedTotal.length) {
			$jvariables.allExpanded = true;
			$j("#openAllLink").html('Close all');
		} else {
			$jvariables.allExpanded = false;
			$j("#openAllLink").html('Open all');
		}
		
		return false;

	});
	
	// Open all link functionality
	
	$jvariables.allExpanded = false;
	
	$j("#openAllLink").click(function() {
		if ($jvariables.allExpanded == false)
		{
			$j(this).html('Close all');
			$j(".accordionItem").find('.accordionBody').slideDown();
			$j(".accordionItem").find('.accordionHead').addClass('expanded')
			$jvariables.allExpanded = true;
			return false;
		}
		else
		{
			$j(this).html('Open all');
			$j(".accordionItem").find('.accordionBody').slideUp();
			$j(".accordionItem").find('.accordionHead').removeClass('expanded')
			$jvariables.allExpanded = false;
			return false;
		}
	});

for(t=1;t<=3;t++) {
if($j("#raceResults"+t).attr('href') === "" || $j("#raceResults"+t).attr('href') === "#") {
$j("#raceResults"+t).hide();
}else {
$j("#raceResults"+t).show();
}
}

/* removes blank PDF download links */
$j(function () {
	$j('#results-pdf-downloads a').each(function () {
		if ($j(this).attr('href') == "")
		{
			$j(this).remove();
		}
		});
	});


});


