$(function() {
	 
	 
	 // closing the popup box
	$(".closePopup").live("click",function() {
		$("#cartItems").click();
	});
	 
	$("#cartItems").click(function() {
		
		// close help box if its open
		if(	$(".helpBox").css("bottom") == "43px") {
			$(".helpBox").animate({
				bottom : $(".helpBox").height()* -1,
			});
		}
		
		// close popup if its open
		if(	$(".popupBox").css("bottom") == "43px") {
			$(".popupBox").animate({
				bottom : $(".popupBox").height()* -1,
			});
			
		// open popup if its closed	
		} else {
			$(".popupBox").css({
				"bottom" : $(".popupBox").height()* -1,
				"visibility" : "visible"
			});
			$(".popupBox").animate({
				bottom : 43,
			});
		}
	});
	
	$("#instructions").click(function() {	
		if(	$(".popupBox").css("bottom") == "43px") {
			$(".popupBox").animate({
				bottom : $(".popupBox").height()* -1,
			});
		}
		
		if(	$(".helpBox").css("bottom") == "43px") {
			$(".helpBox").animate({
				bottom : $(".helpBox").height()* -1,
			});
		} else {
			$(".helpBox").css({
				"bottom" : $(".helpBox").height()* -1,
				"visibility" : "visible"
			});
			$(".helpBox").animate({
				bottom : 43,
			});
		}
	});
	
	$(".checkoutButton").live("click",function() {
		$me = $(this);
		$.post("login/checkLogin",function(data) {
			if(!data) {
				useKeys = false;
				$("#floatingLogin").center().show();
				$(".pageBlocker").clickAway(function() {
					useKeys = true;
				});
				$(".pageBlocker").fadeTo("fast",1);
			} else {
				$("#checkoutForm").submit();
			}
		});
	});
});

function refreshCart() {
	$.post("store/getCart",function(data) {
			
		// start with fresh cart
		$(".popupBody").html("");
		
		// clear all product checkboxes
		$(".checkedProduct").each(function() {
			var price = $(this).parent().attr("price");
			$(this).replaceWith("<a href='#'>Add To Cart - $"+price+"</a>");
		});
		
		// loop through tutorials
		var i = 0;
		var discount = (data.html.discount) ? data.html.discount : "Discount Code";
		for(tid in data.html.tutorials) {
			i++;
			var tutorial = data.html.tutorials[tid];
			// form the product code
			var productCode = tutorial.type+"-"+tid+"-"+0;
			//var productCode = tutorial.type+tid;
			// create a line item
			$item = $("\
				<div class='cartItem' tid='"+tid+"'>\
					<div class='cartItemName'>"+tutorial.title+"</div>\
					<div class='cartItemDelete'>X</div>\
					<div class='cartItemPrice'>$"+tutorial.price+"</div>\
				</div>"
			);
			// add this item to the cart body
			$(".popupBody").append($item);
			
			// check this product on the store front
			$("[tid="+tid+"]").find("a").replaceWith("<div class='checkedProduct'></div>");
		}
		
		// add the footer with total and button
		if($(".cartItem").length > 0) {
			$(".popupBody").append("\
				<div style='overflow:hidden;height:auto' >\
					<div style='float:left;margin-top:5px'><div class='checkoutButton'>Check Out Now</div></div>\
					<div id='discountHolder'>\
						<input style='width:100px;float:left' type='text' name='code' value='"+discount+"' class='discountCode'/>\
					</div>\
					<div class='yellowCheck'>Apply</div>\
					<div style='float:right;margin-top:8px'>Total: $"+data.html.total+"</div>\
				</div>"
			);
			
			$(".discountCode").focus(function() {
				useKeys = false;
			}).blur(function() {
				useKeys = true;
			});
		} else {
			$(".popupBody").html("<div class='noItems'>No Items in Your Cart</div><div></div><div></div>");
		}
		
		// discount apply
		$(".yellowCheck").click(function() {
			var code = $(".discountCode").val();
			$.post("store/getDiscount",{code:code},function(total) {
				refreshCart();
			});
		});
		
		// prevent seleting items in the cart
		$(".cartItem").mousedown(function() {
			return false;
		});
		
		// fill in the number of items and cost display
		$("#nItems").html($(".cartItem").length);
		$("#total").html(data.html.total);
		
		if( $(".cartItem").length > 0 ) $(".checkoutButton").show();

	},"json");
	
	// delete a cart item, then refresh the box
	$(".cartItemDelete").live("click",function() {
		$.post("store/removeFromCart",{
			tid : $(this).parent().attr("tid")
		}, function(data) {
			if(!data.error) {
				refreshCart();
			} else message(data.error,ERROR);
		},"json");
	});
	
	// clear the cart
	$(".clearCart").click(function() {
		$.post("store/clearCart",function() {
			refreshCart();
		});
	});
}

function addToCart(tid,title,type,callback) {
	$.post("store/addToCart",{
		tid : tid,
		title : title,
		type : type
	}, function(data) {
		if(!data.error) {
			// show the checkout button
			$(".checkoutButton").show();
			refreshCart();
			if(callback && typeof callback == "function") callback();
		} else message(data.error,ERROR);
	},"json");
}

function removeFromCart(tid,callback) {
	$.post("store/removeFromCart",{
		tid : tid
	}, function(data) {
		if(!data.error) {
			refreshCart();
			if(callback) callback();
		} else message(data.error,ERROR);
	},"json");
};
