// JavaScript Document

$(document).ready(function() {
	  // for the see price in cart there is another close button
 	$('#quickViewTop img').live("click", function() {
 		tb_remove();
 	});
 
// for the menu dropdown
		function menuHoverOver(){
				$(this).children("div:has(ul)").show(5);
		}
		
		function menuHoverOut(){
				$(this).children("div:has(ul)").hide(5);
		}
		var config = {    
				 sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
				 interval: 300, // number = milliseconds for onMouseOver polling interval    
				 over: menuHoverOver, // function = onMouseOver callback (REQUIRED)    
				 timeout: 200, // number = milliseconds delay before onMouseOut    
				 out: menuHoverOut // function = onMouseOut callback (REQUIRED)    
		};
		$("#menuBar li:has(div)").hoverIntent(config);
		// menu dropdown end

		if ($("#slider-range").length !== 0)	{
			$("#slider-range").slider({
				range: true,
				min: 0,
				max: 150,
				values: [1, 125],
				step: 5,
				change: function(event,ui) {
				 			$("#lnForm").ajaxSubmit(options);							
					},
				slide: function(event, ui) {
					$("#amount").val('$' + ui.values[0] + ' - $' + ui.values[1]);
				}
			});
			$("#amount").val('$' + $("#slider-range").slider("values", 0) + ' - $' + $("#slider-range").slider("values", 1));
		}
													 
		// for the daily deals arrow buttons when clicked
		// the previous arrow
		function scrollPrevClick(event){
			var prodContainer = $(this).parent().siblings("div:visible").children("div.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(){
					that.bind('click', event.data, scrollPrevClick);
					});
			} else {
					that.bind('click', event.data, scrollPrevClick);
			}
			return false;
		}

// the next arrow
		function scrollNextClick(event){
			var prodContainer = $(this).parent().siblings("div:visible").children("div.allScrollingProdContainer");
			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(){
					that.bind('click', event.data, scrollNextClick);
					});
			} else {
					that.bind('click', event.data, scrollNextClick);
			}
			return false;
		}
		
		var dropdownConfig = {
			widthShown: 150,
			elementWidth: 150
		};
		
		$("div a.scrollNext").bind('click', dropdownConfig, scrollNextClick);
		$("div a.scrollPrev").bind('click', dropdownConfig, scrollPrevClick);

		var featuredConfig = {
			widthShown: 546,
			elementWidth: 182
		};
		
		$("#featuredScrollNext").bind('click', featuredConfig, scrollNextClick);
		$("#featuredScrollPrev").bind('click', featuredConfig, scrollPrevClick);

		var mediaConfig = {
			widthShown: 532,
			elementWidth: 133
		};
		
		$("div a.mediaScrollNext").bind('click', mediaConfig, scrollNextClick);
		$("div a.mediaScrollPrev").bind('click', mediaConfig, scrollPrevClick);
		

// for the daily deals arrow buttons when clicked end

 // for the tabs on the homepage
		$("#hpFeaturedTabs li").click(function (){
			var index = $("#hpFeaturedTabs li").index(this);
			$(this).siblings("li").removeClass();
			$(this).removeClass().addClass("hpFeaturedOn");

			var curr = $(this).parent().siblings("div").children(".hpFeaturedBackground");
			curr.hide();
			curr.eq(index).show();
		});
 // end tabs on homepage



// for the dynamic search (Left Nav)
		var options = {
			target: '#mainRight',
			cache: false,
			success: loadColors
		};
		
		// on completion of loading
		function loadColors(){
				var random_num = Math.floor(Math.random()*101)
				var complete_url="/includes/sideNavColors.cfm?id=" + random_num;
				
				$("#lnColorsHolder").load(complete_url);
		}
		
		$("#leftNav ul li").live("click", function(){
			if ($(this).children("input").attr("type") === "radio") // check if it is  a radio box or check box. if its a radio box they cant deselect they can only select a different one.
			{
				if (!$(this).children("input").attr("checked")){ // if the radio is unchecked than we check it.
					 $(this).children("input").attr("checked", "checked");
					 $(this).siblings().removeClass();
					 $(this).addClass("lnSelected");
					 $("#lnForm").ajaxSubmit(options);
				}
			}
			else { // it is not a radio box
				if ($(this).children("input").attr("checked")){ // if the checkbox is checked
					 $(this).children("input").attr("checked", "");
					 $("#lnForm").ajaxSubmit(options);
				}
				else{ // if the checkbox is unchecked
					 $(this).children("input").attr("checked", "checked");
					 $("#lnForm").ajaxSubmit(options);
				}
			}
		});
// for the dynamic search (Left Nav) end

// For the tool tip popup
		var timeout = null;

		function toolTipHoverOver(){
				var that = $(this); // set a variable to use this inside the inner function
				timeout = setTimeout(function(){ // set timeout to a function so that while this is timed out the value of timeout is the function,  so that when he mouses out we can check the value of timeout if the value of timeout is a function we can just stop the timeout function so the event will not happen.
									timeout = null;
									that.siblings("div.allPagesToolTip").show(5);
									}, 300);
		}
		
		function toolTipHoverOut(){
			if (timeout === null){
				var that = $(this);
				setTimeout(function(){
					that.siblings("div.allPagesToolTip").hide(5);
				},100);
			}
			else{
				clearTimeout(timeout);
			}
		}

		$(".allPagesPopup").live("mouseover", toolTipHoverOver);
		$(".allPagesPopup").live("mouseout", toolTipHoverOut);
// For the tool tip popup End

});

/* updates highlighted box in current hover-me object */
function SetActive(el) {
	$($(el).parent().parent().find("a")).each(function(i) {
		if (i != 0 && i <= $.hoverMe.pageSize) {
			$(this).removeClass("page-box-active");
			if (i==$(el).attr("index")) {
				$(this).addClass("page-box-active");	
			}
		}
	});
	$(el).addClass("page-box-active");	
}

//$(document).ready(function() {
//	//hover-me configuration
//	$.hoverMe = {
//		distance: 950,
//		pageSize: 3,
//		index: 1
//	};
//	//on-load add an index attribute to represent
//	//the current index for each hover-me object
//	$(".hover-me").each(function(i) {
//		$(this).attr("index", $.hoverMe.index);
//	});
//	
//	//moves the hover-me object one index position to the left
//	$(".move-left").click(function() {
//		var obj = $(this).parent().parent().parent();
//		if (obj.attr("index") > 1) { 
//			var ctr = $(this).parent().parent().parent().parent().find(".products-container").children();
//			//decrement index for selected hover-me object
//			var idx = obj.attr("index");
//			if (idx > 1) {
//				obj.attr("index", --idx);
//				//animate the div control
//				ctr.animate({ left: '+=' + $.hoverMe.distance }, 500); 
//			}
//		}
//		SetActive(this);	
//		return false;
//	});
//	
//	//moves the hover-me object one index position to the right
//	$(".move-right").click(function() {		
//		var obj = $(this).parent().parent().parent();
//		var distance = obj.find(".products-container").outerWidth(true) + 2;
//		if (obj.attr("index") < $.hoverMe.pageSize) { 
//			var ctr = $(this).parent().parent().parent().parent().find(".products-container").children();
//			//increment index for selected hover-me object
//			var idx = obj.attr("index");
//			if (idx < $.hoverMe.pageSize) {
//				obj.attr("index", ++idx);
//				//animate the div control
//				ctr.animate({ left: '-=' + $.hoverMe.distance }, 500); 
//			}
//		}
//		SetActive(this);
//		return false;
//	});
//	
//	//disable clicking on active box
//	$(".best-scroll-holder a.page-box-active").live("click", function() { return false; });
//	
//	//for every anchor[box] that is active(selected)
//	//move the hover-me object to that item's index
//	//the item index is stored in it's title attribute
//	$(".best-scroll-holder a:not(.page-box-active)").live("click", function() {
//		var ctr = $(this).parent().parent().parent().parent().find(".products-container").children();	
//		thisIdx = $(this).attr("title");
//		var obj = $(this).parent().parent().parent();
//		if (thisIdx < obj.attr("index")) { //$.hoverMe.index
//			var diff = obj.attr("index") - thisIdx;
//			obj.attr("index", thisIdx); 
//			ctr.animate({ left: '+=' + (diff*$.hoverMe.distance) }, 500); 
//		}
//		else if (thisIdx > obj.attr("index")) { //$.hoverMe.index
//			var diff = thisIdx - obj.attr("index");
//			obj.attr("index", thisIdx); 
//			ctr.animate({ left: '-=' + (diff*$.hoverMe.distance) }, 500);
//		}
//		SetActive(this);
//		return false;
//	});
//	
//	
//});

 // used for magic zoom so the alt image will be displayed in the magic zoom
function changeZoomImage() {
	// Step 1
	// Stop Magic Zoom
	MagicZoom_stopZooms();
	// Step 2
	// Re-initialize Magic Zoom
	MagicZoom_findZooms();
}

