//Javascript Document
//Author: Jeff Mulder

$(document).ready(function(){

	
	//----------------------------------WARRANTY SLIDERS----------------------------------------------//
	
	$("ul.warranty li.active").find(".click_me").text("Collapse");
	$("ul.warranty li:not(.active) .warranty_content").hide();
	
	
	//active
	$(".warranty_head").bind({
		click: function() {	
			currentCat = $(this).parent("li");	
				if (currentCat.hasClass("active")) {
					currentCat.find(".warranty_content").slideUp("fast");
					currentCat.removeClass("active");
					
					currentCat.find(".click_me").text("View");
					return false;
				} else { 
					currentCat.siblings("li").find(".warranty_content").slideUp("fast");
					currentCat.find(".warranty_content").slideDown("fast");
					currentCat.addClass("active").find(".click_me").text("Collapse");

					currentCat.siblings("li").removeClass("active").find(".click_me").text("View");
					return false;
				}
			
		},
		mouseenter: function() {
			currentCat = $(this).parent("li");
			currentCat.siblings("li").removeClass("rollover");
			currentCat.addClass("rollover");
		},
		mouseleave: function() {
			currentCat = $(this).parent("li");
			currentCat.removeClass("rollover");	
		}
	});
	
	
	
	
	
						   
	//----------------------------------Top Nav----------------------------------------------//					
	$('ul.sf-menu ul').hide();
	$("ul.sf-menu li").hover(function() {	
		$(this).find("a.expandable").addClass("active");
		thisLink = $(this).find("> a");
		thisLink.addClass("hover");
		//animation
		$('> ul', this).animate({opacity: "show"}, "fast");
	}, function() {
		$(this).find("a.expandable").removeClass("active");
		thisLink = $(this).find("> a");
		thisLink.removeClass("hover");
		//animation
		$('> ul', this).animate({opacity: "hide"}, "fast");
	});
	
	$("ul.sf-menu").hover(function() {
		//.sf-menu rollover			
	}, function() {
		//.sf-menu rollout - reset all to inactive
		$(".active").removeClass("active");
	});	
	
	
	
	//----------------------------------Home Banner------------------------------------------------//
	
	var imgWidth = jQuery("ul#home_banner_images li").width();
	var imgNumber = jQuery("#home_banner_images li").length;	
	var bannerWidth = imgWidth * imgNumber;
	jQuery("ul#home_banner_images").css({width: bannerWidth});
	
	$("#home_banner_thumbs li").bind({
		click: function() {		
			var listIndex = $(this).index('#home_banner_thumbs li');
			var newLeftValue = (-(listIndex) * imgWidth);
			$("#home_banner_images").stop().animate({left: newLeftValue + "px"}, 1000);

			$(this).addClass('active');
			$(this).siblings('li').removeClass('active');
		},
		mouseenter: function() {
			
		},
		mouseleave: function() {
			
		}
	});
	
	
	
	//----------------------------------IMAGES GALLERY----------------------------------------------//
	
	//if only one thumb, hide
	thumbLength = $("#thumb_images li").length;
	if(thumbLength<=1) $("#thumb_images").css({display: 'none'});
	
	$("#thumb_images li:first").addClass("active");
	$("#thumb_images li.active img").css({opacity: '.75'});
	
	$("#thumb_images li").hover(function() {
										
		$(this).not(".active").find("img").stop().animate({
			width: '80px',
			height: '80',
			top: '-15px',
			left: '-15px',
			
			
		}, 250);									 
	}, function() {
		$(this).not(".active").find("img").stop().animate({
			width: '50px', 
			height: '50px', 
			top: '0px',
			left: '0px',	

			
		}, 500);
	});
										 
	
	$("#thumb_images li").click(function() {		
		$(this).addClass("active").siblings().removeClass("active");
		$("#thumb_images li img").css({opacity: '1'});
		$(this).find("img").css({
			opacity: '.75',
			width: '50px', 
			height: '50px', 
			top: '0px',
			left: '0px',	
		});
		
		//get src from the image clicked
		imageSRC = $(this).find("a").attr("href");
		//fade out
		$("#large_image img").hide();
		$("#tall_banner .loader").show();
		//$("#largeImage img").animate({opacity: "hide"}, "fast");
		//change src
		$("#large_image img").attr("src", imageSRC).load(function () {
			//fade in
			$("#large_image img").animate({opacity: "show"}, "slow");
			$("#tall_banner .loader").hide();
		});
			
		return false;
			
	});
	
	
	
	
	
	//----------------------------------APPLE COMPATIBILITY----------------------------------------------//
	
	$("#apple_list").css({opacity: '0', right: '100px'});
	
	//rollover
	$("#apple_img, #apple_list").hover(function() {
		$("#apple_list").show().stop().animate({
			opacity: '1',
			right: '160px'
		}, 250);									 
	}, function() {
		$("#apple_list").stop().animate({
			opacity: '0',
			right: '100px'
		}, 500);
	});
	
	 //----------------------------------Sort By Button----------------------------------------------//
	  
	  //moves ('.sort_button ul") top 24px up/down based on $('.sort_button li#currentpage')'s position in list
	  	var listItem = $('.sort_button #current_sort');
		var listIndex = $('.sort_button li').index(listItem);
		var newTopValue = -10 - (listIndex * 24);
		$('.sort_button ul').css('top', newTopValue);
		
		
		//add class .hover
	  	$('.sort_button').bind({
			"click hover": function() {	
				$(this).addClass("hover");
			}
	 	});



	  

	  
	  //----------------------------------Product List Rollover----------------------------------------------//
	  $('.product_list_item').find('.additional_info').css({display: 'none'});
	  	/*$('.product_list_item').find('.additional_info').css({opacity: '0'});
		
	  	$('.product_list_item').hover(function() {
			$(this).find('.additional_info').stop().animate({
				opacity: '1'
			}, 250);									 
		}, function() {
			$(this).find('.additional_info').stop().animate({
				opacity: '0'
			}, 500, function(){
				$(this).find('.additional_info').css({position: 'absolute'});
			});
		
	 	});*/
	

//----------------------------------Home Timer------------------------------------------------//
	
	
	$("#home_banner").hover(function() {
		$("#home_banner").stopTime();
	}, function () {
		activateBannerTimer();
	});

	//activatBannerTimer(); is being called from the index.php page.  It was causeing errors here.  
  		
	
});



function activateBannerTimer() {
		imgWidth = jQuery("ul#home_banner_images li").width();
		$('#home_banner').everyTime("5s", function() {
			var listIndex = $('#home_banner_thumbs li.active').next('li').index('#home_banner_thumbs li');
			if(listIndex<=0)
				listIndex = 0;
			var newLeftValue =  (-listIndex * imgWidth);									   
			$('#home_banner_images').stop().animate({left: newLeftValue + "px"}, 1000);
			$('#home_banner_thumbs li').removeClass('active');
			$('#home_banner_thumbs li').eq(listIndex).addClass('active');
		});
	}
