$(document).ready(function() {
	
	$(window).resize(function() {
		$('#wrap').css('min-height', $(window).height() - $('#footer').height());
	}).resize();
	
	$('div.anchor-link').click(function() {
		$('html,body').animate({scrollTop: 0});
	});
	
});



/* CART HOVER TOOL */

$(document).ready(function() {
	$('#header').after('<div id="cart-hover" class="hover-box"></div>');
	$('#cart-hover').html('<div class="head"><h2><a href="' + ROOT + 'cart/view">View my cart</a></h2><div class="content"></div></div><div class="foot"></div>');
	
	
	var timeout = 0;
	var hasdata = false;
	var $hover = $('#cart-hover');
	var $content = $('#cart-hover  .content');
	
	
	// Hover over the tab, hover out
	$('#tab-cart').mouseover(function() {
		if (hasdata) {
			$hover.fadeIn();
			
		} else {
			$content.html('<p>Loading...</p>');
			$hover.fadeIn(function() {
				$content.load(ROOT + 'cart/hover_data');
				hasdata = true;
			});
		}
	});
	
	$('#tab-cart,#cart-hover').mouseout(function() {
		timeout = window.setTimeout(function() { $hover.fadeOut(); }, 1000);
	});
	
	$('#tab-cart,#cart-hover').mouseover(function() {
		window.clearTimeout(timeout);
	});
	
	
	// Cart add form submit
	$('.cart-add').submit(function() {
		var $form = $(this);
		
		$.post(ROOT + 'cart/add_action', $form.serialize(), function (json) {
			if (json.success == 0) {
				alert(json.message);
				return;
			}
			
			if (json.freefreight != 0) {
				$('.freefreight').html('Purchase ' + json.freefreight + ' more bottle' + (json.freefreight > 1 ? 's' : '') + ' for free freight');
			} else {
				$('.freefreight').html('This order qualifies for free freight.');
			}
			
			$('.cart-add-1').fadeOut(function() {
				$('.cart-add-2').fadeIn();
			});
			
			$hover.fadeIn();
			$content.load(ROOT + 'cart/hover_data', function() {
				var $msg = $content.find('.hover-product-item[data-cart-item-id=' + json.cart_item_id + '] .msg');
				
				$msg.html('Just added to cart').fadeIn();
				$msg.animate({opacity: 0.7}, 400).animate({opacity: 1.0}, 400);
				$msg.animate({opacity: 0.7}, 400).animate({opacity: 1.0}, 400);
				$msg.animate({opacity: 0.7}, 400).animate({opacity: 1.0}, 400);
				$msg.delay(2000, function() {
					$msg.hide();
					$hover.fadeOut();
				});
				
				$(document).trigger('cart.item-added', [$form, json])
			});
			
		}, 'json');
		
		return false;
	});
	
});



