/**
* Stuff for the cart, incl. the top box.
**/

function head_cart_redraw() {
	if (cart_info.cart_size == 0 && cart_info.wishlist_size == 0) {
		$('#head-cart').hide();
		$('#head-cart-message').hide();
		return;
	}
	
	$('#head-cart-num-cart').html(cart_info.cart_size + ' Item' + (cart_info.cart_size != 1 ? 's' : ''));
	$('#head-cart-num-wishlist').html(cart_info.wishlist_size + ' Item' + (cart_info.wishlist_size != 1 ? 's' : ''));
	
	if (cart_info.cart_size == 0) {
		$('#head-cart-purchase').hide();
	} else {
		$('#head-cart-purchase').fadeIn();
	}
}

function head_cart_add_anim() {
	$('#head-cart').fadeIn(function() {
		$('#head-cart-message').stop(true, true);
		$('#head-cart-message').css('opacity', 0.0);
		$('#head-cart-message').css('top', 80);
		$('#head-cart-message').show();
		$('#head-cart-message').animate({'opacity': 0.9}, 600);
		$('#head-cart-message').animate({'opacity': 0.7}, 500);
		$('#head-cart-message').animate({'opacity': 0.8}, 300);
		$('#head-cart-message').animate({'opacity': 0.7}, 300);
		$('#head-cart-message').animate({'opacity': 0.8}, 300);
		$('#head-cart-message').animate({'opacity': 0.7}, 300);
		$('#head-cart-message').animate({'top': 28}, 1000);
	});
}


/**
* Add cart event handler
**/
$(document).bind('add-cart', function(event, product_id) {
	var post = {};
	post.id = product_id;
	
	$.post(ROOT + 'cart/add_action', post, function(resp) {
		if (resp.success == 0) {
			report_error(resp.message);
			return;
		}
		
		$('#head-cart-message').hide();
		$('#head-cart-message').find('p').text('Item added to cart');
		
		cart_info.cart_size += 1;
		
		head_cart_redraw();
		head_cart_add_anim();
		
	}, 'json');
});


/**
* Add wishlist event handler
**/
$(document).bind('add-wishlist', function(event, product_id) {
	var post = {};
	post.id = product_id;
	
	$.post(ROOT + 'wishlist/add_action', post, function(resp) {
		if (resp.success == 0) {
			report_error(resp.message);
			return;
		}
		
		$('#head-cart-message').hide();
		$('#head-cart-message').find('p').text('Item added to wishlist');
		
		cart_info.wishlist_size += 1;
		
		head_cart_redraw();
		head_cart_add_anim();
		
	}, 'json');
});


/**
* Prep the page
**/
$(document).ready(function() {
	$('#product-overlay').hide();
	
	$('#results .results-thumb').mouseover(function() {
		var $thumb = $(this);
		
		window.setTimeout(function() {
			$.get(ROOT + 'product/hover_data/' + $thumb.attr('data-id'), function(html) {
				$('#product-overlay').html($thumb.html() + html);
				$('#product-overlay').css('top', $thumb.position().top-1);
				$('#product-overlay').css('left', $thumb.position().left);
				$('#product-overlay').show();
			});
			
		}, 500);
	});
	
	var overlay_timeout = 0;
	$('#product-overlay').mouseover(function() {
		window.clearTimeout(overlay_timeout);
	});
	
	$('#product-overlay').mouseout(function() {
		overlay_timeout = window.setTimeout(function() {
			$('#product-overlay').hide();
		}, 500);
	});
	
	head_cart_redraw();
	
	// Tabs for products
	$('#product-spec div.desc').hide();
	$('#tabs li').click(function() {
		$('#product-spec div.specs').hide();
		$('#product-spec div.desc').hide();
		$('#product-spec ' + $(this).attr('data-for')).show();
		$('#tabs li').removeClass('on');
		$(this).addClass('on');
	});
});

