// JavaScript Document

$(document).ready(function() {
		// Start. for the menu dropdowns
		$("ul#header-top-nav3 li:not('.dropdown_typs')").hover(function() {
			var index = $(this).parent().children("li").index(this);
			$(this).siblings("div").eq(index).show();
		},
		function(){
			var index = $(this).parent().children("li").index(this);
			$(this).siblings("div").eq(index).hide();
		});

		$("ul#header-top-nav3 div.shop_dropdown").hover(function() {
			$(this).show();
		},
		function(){
			$(this).hide();
		});
		// End. for the menu dropdowns

		// for the search
		/*$("input#header-search-btn").click(function(){
																 window.location = "/search.cfm/kword/" + encodeURIComponent($("input#header-search-box").val() );
																 return false;
																 });*/

// for the scrolling arrow buttons when clicked
		// the previous arrow
		function scrollPrevClick(event){
			if (event.data.currentScroller === "featured"){
				var prodContainer = $(this).siblings("div:visible").children("ul.allScrollingProdContainer");
			}
			var that = $(this);
			
			var location = prodContainer.position().left;
			var newLocation = location + event.data.elementWidth;
			$(this).unbind('click');
			
			// we stop it from going to the previous one if there is no previous
			if (location < 0){
				prodContainer.stop('true', 'true').animate({left: newLocation}, 'slow', '', function(){
					$("#hp-product-scroller-next").removeClass("featured-next-off").addClass("featured-next");
					if (newLocation >= 0){
						that.removeClass("featured-previous").addClass("featured-previous-off");
					}
					that.bind('click', event.data, scrollPrevClick);
					});
			} else {
					that.bind('click', event.data, scrollPrevClick);
			}
			return false;
		}

// the next arrow
		function scrollNextClick(event){
			if (event.data.currentScroller === "featured"){
				var prodContainer = $(this).siblings("div:visible").children("ul.allScrollingProdContainer");
			}
			
			var that = $(this);
			
			var location = prodContainer.position().left;
			var newLocation = location - event.data.elementWidth;
			var numOfChildren = prodContainer.children("li").not(".hp-featured-spacer").size();
			var lengthOfChildren = -((numOfChildren * event.data.elementWidth) - event.data.widthShown);
			$(this).unbind('click');
			
			if (newLocation >= lengthOfChildren){
				prodContainer.animate({left: newLocation}, 'slow', '', function(){
					$("#hp-product-scroller-previous").removeClass("featured-previous-off").addClass("featured-previous");
					if (newLocation <= lengthOfChildren){
						that.removeClass("featured-next").addClass("featured-next-off");
					}
					that.bind('click', event.data, scrollNextClick);
					});
			} else {
					that.bind('click', event.data, scrollNextClick);
			}
			return false;
		}
		
		var featuredConfig = {
			widthShown: 830,
			elementWidth: 166,
			currentScroller: "featured"
		};
		
		$("div a#hp-product-scroller-next").bind('click', featuredConfig, scrollNextClick);
		$("div a#hp-product-scroller-previous").bind('click', featuredConfig, scrollPrevClick);

// for the scrolling arrow buttons when clicked end

 // for the tabs on the homepage
 		function tabClicked(event){
			var index = $("#featured-list li").index(this);
			$(this).siblings("li").removeClass("featured-list-item-on").addClass("featured-list-item-off");
			$(this).removeClass().addClass("featured-list-item-on");

			var curr = $(this).parent().siblings("div.featured-items-holder");
			curr.hide();
			var actualCurr = curr.eq(index);
			actualCurr.show();
			
			// the next few lines are to set the previous and next buttons to their proper state for current div.
			var prodContainer = actualCurr.children("ul.allScrollingProdContainer");
			var location = prodContainer.position().left;
			var newLocation = location - event.data.elementWidth;
			var numOfChildren = prodContainer.children("li").not(".hp-featured-spacer").size();
			var lengthOfChildren = -((numOfChildren * event.data.elementWidth) - event.data.widthShown);
			
			if (newLocation >= lengthOfChildren){
				$("#hp-product-scroller-next").removeClass("featured-next-off").addClass("featured-next");
			} else {
				$("#hp-product-scroller-next").removeClass("featured-next").addClass("featured-next-off");
			}
			
			if (location < 0){
				$("#hp-product-scroller-previous").removeClass("featured-previous-off").addClass("featured-previous");			
			} else {
				$("#hp-product-scroller-previous").removeClass("featured-previous").addClass("featured-previous-off");			
			}
			
		}
		
		$("#featured-list li").bind('click', featuredConfig, tabClicked);
 // end tabs on homepage
 
	 // Blog/News hidden comment section
		$("span#blogComments").toggle(function()
		{
			$("div#hiddenComments").show();	
		},
		function()
		{
			$("div#hiddenComments").hide();
		});
	
	// Product Detail Page Scroll
	// for the scrolling arrow buttons when clicked
	// the previous arrow
	function scrollPrevClick2(event){
		var prodContainer = $("ul.allScrollingProdDetailContainer");
		var that = $(this);
		
		var location = prodContainer.position().left;
		var newLocation = location + event.data.elementWidth;
		$(this).unbind('click');
		
		// we stop it from going to the previous one if there is no previous
		if (location < 0){
			prodContainer.stop('true', 'true').animate({left: newLocation}, 'slow', '', function(){
				$("#hp-product-scroller-next").removeClass("featured-next-off").addClass("featured-next");
				//if (newLocation >= 0){
//					that.removeClass("featured-previous").addClass("featured-previous-off");
//				}
				that.bind('click', event.data, scrollPrevClick2);
				});
		} else {
				that.bind('click', event.data, scrollPrevClick2);
		}
		return false;
	}

// the next arrow
	function scrollNextClick2(event){
		var prodContainer = $("ul.allScrollingProdDetailContainer");
		var that = $(this);
		
		var location = prodContainer.position().left;
		var newLocation = location - event.data.elementWidth;
		var numOfChildren = prodContainer.children("li").not(".hp-featured-spacer").size();
		var lengthOfChildren = -((numOfChildren * event.data.elementWidth) - event.data.widthShown);
		$(this).unbind('click');
		
		if (newLocation >= lengthOfChildren){
			prodContainer.animate({left: newLocation}, 'slow', '', function(){
				$("#hp-product-scroller-previous").removeClass("featured-previous-off").addClass("featured-previous");
				//if (newLocation <= lengthOfChildren){
//					that.removeClass("featured-next").addClass("featured-next-off");
//				}
				that.bind('click', event.data, scrollNextClick2);
				});
		} else {
				that.bind('click', event.data, scrollNextClick2);
		}
		return false;
	}
	
	var featuredConfig2 = {
		widthShown: 930,
		elementWidth: 180,
		currentScroller: "featured"
	};
	
	$("#product-scroller-next").bind('click', featuredConfig2, scrollNextClick2);
	$("#product-scroller-previous").bind('click', featuredConfig2, scrollPrevClick2);

	$("#footer-newsletter a#popup-link").click(function(){
		$("#newsletter-form").show();
	});
	
	$("#close-popup").click(function(){
		$("#newsletter-form").hide();
	});
	
			// for the shipping calculator on the viewcart page
		var optionsShipping = {
			target: '#shippingRates',
			cache: false,
			beforeSubmit: showLoading,
			success: removeLoading
		};
		
		function showLoading(){
			$("#shippingRates").html('<div id="shippingLoading"><img src="/images/ajax-loader.gif" /></div>');
		}
		
		function removeLoading(){
		}
		
		$("#shippingForm").ajaxForm(optionsShipping);
			
		
		// end for the shipping calculator on the viewcart page
		
		
		
		// for the scrolling arrow buttons when clicked
		// the previous arrow
		function scrollPrevClick2(event){
			var prodContainer = $(".allScrollingProdDetailContainer");
			var that = $(this);
			var location = prodContainer.position().left;
			var newLocation = location + event.data.elementWidth;
			$(this).unbind('click');
			
			// we stop it from going to the previous one if there is no previous
			if (location < 0){
				prodContainer.stop('true', 'true').animate({left: newLocation}, 'slow', '', function(){
					//$("#scroll-wrapper").removeClass("left-scroll-off").addClass("left-scroll");
					if (newLocation >= 0){
						//that.removeClass("right-scroll").addClass("right-scroll-off");
					}
					that.bind('click', event.data, scrollPrevClick2);
					});
			} else {
					that.bind('click', event.data, scrollPrevClick2);
			}
			return false;
		}

// the next arrow
	function scrollNextClick2(event){
		var prodContainer = $(".allScrollingProdDetailContainer");
		var that = $(this);
		var location = prodContainer.position().left;
		var newLocation = location - event.data.elementWidth;
		var numOfChildren = prodContainer.children().size();
		var lengthOfChildren = -((numOfChildren * event.data.elementWidth) - event.data.widthShown);
		$(this).unbind('click');
		
		if (newLocation >= lengthOfChildren){
			prodContainer.animate({left: newLocation}, 'slow', '', function(){
				//$("#scroll-wrapper").removeClass("left-scroll-off").addClass("left-scroll");
				if (newLocation <= lengthOfChildren){
					//that.removeClass("right-scroll").addClass("right-scroll-off");
				}
				that.bind('click', event.data, scrollNextClick2);
				});
		} else {
				that.bind('click', event.data, scrollNextClick2);
		}
		return false;
	}
	
	var hpBannerConfig = {
		widthShown: 956,
		elementWidth: 223,
		currentScroller: "product-detail"
	};
	
	$("div a#product-scroller-next").bind('click', hpBannerConfig, scrollNextClick2);
	$("div a#product-scroller-previous").bind('click', hpBannerConfig, scrollPrevClick2);

});

