var timer = {
	current: null,
	tabIsActive: false
}
$(document).ready(function() {
	// Start main navigation functionality
	$("#hd .main_nav li").mouseenter(function() {	// handles mousing over the main navigation tabs
		timer.current = this;	// store which one we're hovering over
		if($(timer.current).children("a").hasClass("active")) {
			timer.tabIsActive = true;
		} else {
			timer.tabIsActive = false
		}
		$(timer.current).oneTime(150, function() {	// you must hover over the tab for a short time before the dropdown will display
			canceldelay(timer.current);		// cancel the timer attached to the hovered tab
			dropdownaction(timer.current);	// display the dropdown
		});
	}).mouseleave(function() {
		canceldelay(timer.current);		// cancels the delay timer so that the dropdown will not display if you don't hover over the tab for 150 milliseconds
		closedelay(timer.current, timer.tabIsActive);		// delay closing the dropdown for a short time
	});
	
	$("#hd .dropdowns").children($(timer.current).attr("class")).mouseenter(function() {	// handles mousing over the dropdowns that display
		canceldelay(timer.current);		// cancel the timer attached to the hovered tab
	}).mouseleave(function() {
		closedelay(timer.current, timer.tabIsActive);		// delay closing the dropdown for a short time
	});
	// End main navigation functionality
	
	initCartMonitorMouseOver();
	initQuickCartMouseOver();
	
	// Start Just Added functionality
	initJustAddedMouseOver();
	initJustAddedClose();
	// End Just Added functionality
});

initJustAddedShowHide = function(){
	if($("body").scrollTop() !== 0) {	// if the browser is IE6, IE7, or FF, use the body element
		$('body').animate({		// scroll the page to the top based on a factor of the distance the page is already scrolled
			scrollTop: 0
		}, ($("body").scrollTop() * 1.2), 'swing', function() {
			$(".just_added_wrapper").show().oneTime(15000, function() {		// close the Just Added popup after a 15 second delay if no user interaction
				hidepopup(this);
			});
		});
	} else if($("html").scrollTop() !== 0) {		// if the browser is Safari, use the html element
		$('html').animate({		// scroll the page to the top based on a factor of the distance the page is already scrolled
			scrollTop: 0
		}, ($("html").scrollTop() * 1.2), 'swing', function() {
			$(".just_added_wrapper").show().oneTime(15000, function() {		// close the Just Added popup after a 15 second delay if no user interaction
				hidepopup(this);
			});
		});
	} else {	// if scrollTop is zero, show the Just Added popup immediately
		$(".just_added_wrapper").show().oneTime(15000, function() {		// close the Just Added popup after a 15 second delay if no user interaction
			hidepopup(this);
		});
	}
}

function dropdownaction(elem) {		// display the dropdown pertaining to the hovered tab
	//show menu
	$(elem).children("a").addClass("active");	// make sure the tab stays active while you're hovering over the dropdown
	$("#hd .dropdowns ." + $(elem).attr("class")).show();
}

function closedelay(elem, tabIsActive) {		// delay closing the dropdown
	//set timeout
	$(elem).oneTime(100, function() {
		closedropdown(elem, tabIsActive);
	});
}
function canceldelay(elem) {	// cancel any timers attached to elem parameter
	//clear timeout
	$(elem).stopTime();
}

function closedropdown(elem, tabIsActive) {		// fade out the dropdown
	//hide menu
	$("#hd .dropdowns ." + $(elem).attr("class")).fadeOut(100);
	$(elem).children("a").removeClass("active");	// remove the hover state from the corresponding tab if it wasn't already active before hovering over it
}

function popupaction(elem) {	// show the specified popup
	if(typeof(elem) === 'string'){
		$(elem).show();
	}else{

		var classname = '.'+elem.id;
		//alert('classname: '+classname);
		dropdownaction(classname);
		$(classname).show();
	}
	//show popup
	
}

function hidedelay(elem) {	// delay closing the popup
	// set timeout
	$(elem).oneTime(100, function() {
		hidepopup(elem);
	});
}

function hidepopup(elem) {	// fadeout the specified popup
	//hide popup
	$(elem).fadeOut(100);
}

initCartMonitorMouseOver = function(){
	// Start quick cart functionality (the comments from above are analogous to this section)
	$("#hd .cart_monitor .in_cart").mouseenter(function() {
		timer.current = ".quick_cart_wrapper";
		$(timer.current).oneTime(300, function() {
			canceldelay(timer.current);
			popupaction(timer.current);
		});
	}).mouseleave(function() {
		canceldelay(timer.current);
		hidedelay(timer.current);
	});
}
initQuickCartMouseOver = function(){
	$(".quick_cart_wrapper").mouseenter(function() {
		canceldelay(timer.current);
	}).mouseleave(function() {
		hidedelay(timer.current);
	});
	// End quick cart functionality
}
initJustAddedMouseOver = function(){
$(".just_added_wrapper").mouseenter(function() {	// if user interaction, treat like a dropdown
		timer.current = this;
		canceldelay(timer.current);
		popupaction(timer.current);
	}).mouseleave(function() {
		hidedelay(timer.current);
	});
}

initJustAddedClose = function(){
	$(".just_added_content .close").click(function() {		// if the close button within the popup is clicked
		$(".just_added_wrapper").fadeOut(100);		// fade out the popup quickly
	});
}
