function mini_cart_collapse() {
	$("#tbl_cart_items").find("thead, tfoot").add("#mini_cart_btns, #tbl_mini_cart_details").hide();
}

function mini_cart_expand() {
	$("#tbl_cart_items").find("thead, tfoot").add("#mini_cart_btns, #tbl_mini_cart_details").show();
}

$(function() {
//	var view_cntrlr = "/mgmt/controllers/cust_cart_cntrlr.php";
	var view_cntrlr = "/cart/";
	
	$(".dlg_title #close_crt").click(function() {
		$("#side_col_fixed").fadeOut();
	});
	
	$("#check_out").click(function(){
		window.location = "https://www.thermosoft.com/cart/checkout/";
	});

	$("#content .btn_cart_add").live("click", function(e) {
//		alert('yo');
		var items = {};
		var flag = false;
		var $rows = $("table.price_list tbody tr.selected, ul.price_list li.selected");

		$rows.each(function(){
			var id = this.id;
			var qty = $(".qty", this).val();
			if (qty > 0) {
				items[id] = qty;
				flag = true;
			}
		});
		
		if (flag && typeof(guided) == 'undefined') {
			$.post(
				view_cntrlr + 'add_products',
				{
					items: JSON.stringify(items)
				},
				function() {
					mini_cart_expand();
					$("#dlg_mini_cart").css("background-color", "#8ADF7E").animate({backgroundColor: '#EFE7DA' }, 1000);
//					$("#dlg_mini_cart, #store_features").fadeIn("slow");
					$(".qty", $rows).val(0).keyup();
	//					$("#cart_items.empty").animate({"left": "1018px"}, "medium").removeClass("empty");
	//					wheelQty($("#tbl_cart_items"), true);
	//					tblQtyCalc($("#tbl_cart_items .qty:eq(0)").get(0));
				}
			);
		}
		else if (flag && guided) {
			$.post(
				'/estimate/get_rooms/',
				{},
				function(data) {
					$("#room_list").html(data);
					show_room_picker();
				}
			);
		}
		return false;
	});

	$("#tbl_cart_items input.qty, #tbl_cart_details input.qty").live("keydown", function(){	// must optimize
		var oldVal = this.value;
		$(this).keyup(function(){
			if (this.value != oldVal)
				$("#btn_cart_upd").css("background-color", "yellow");
			$(this).unbind();
		});
	});
	
	$('.chk_del_itm').live("change", function() {
		$(this).parent().parent().toggleClass("remove");
	});
	
	$("#btn_cart_upd").live("click", function(){
		var items = {};
		var $tbl = $("#tbl_cart_items, #tbl_cart_details");
		$('.chk_del_itm:checked').each(function() {
			$(this).parent().parent().find('.qty').each(function() {
				$(this).val(0);
			})
		})
		$("tbody tr", $tbl).each(function(){
			if (this.id != "") {
				items[this.id] = {
					qty: $(".qty", this).val()
				};
			};
		});
		$.post(						// copied from context handler for now
			view_cntrlr + 'update_items',
			{
				items: JSON.stringify(items)
			},
			function(){
				if ($("#tbl_cart_items .qty").length == 0)	// dirty
					mini_cart_collapse();
				$("#btn_cart_upd").css("background-color", "");
			}
		);
	});

	$("#btn_empty_cart").live("click", function(e){
		$.post(view_cntrlr + "empty_cart", mini_cart_collapse);
	});

	$("#pymnt_meth li").live("click", function(){
		$(this).addClass("selected").siblings().removeClass("selected");
		if ($(this).hasClass("credit"))
			$("#cc_pymnt").siblings().hide().end().show();
		else if ($(this).hasClass("paypal"))
			$("#pp_pymnt").siblings().hide().end().show();
	});

	if (typeof(IE6) == "undefined") {
		// standards side column scroll
		var fixed = false,
			$el = $("#side_col_fixed");

		$(window).scroll(function(e) {
			var top = $(this).scrollTop();

			if (top > 400 && !fixed) {
				$el.css("top", "0").css("position", "fixed");
				fixed = true;
			}
			
			else if (top < 400 && fixed) {
				$el.css("top", "300px").css("position", "absolute");
				fixed = false;
			}
		});
	}

});