// jQuery Actions for Whacky
var BASE = "/ajax/?_action=";


$(document).ready(function(){
	// handle toggle of right nav items
//        $("div#rightBarStudios").hide("fast");
		// change image
/*
	$("a.navCategory").click(function(){
//		$("div#rightBarCategories").slideToggle(2000);
//		$("div#rightBarStudios").hide("fast");
	      if ($("div#rightBarCategories").is(":hidden")) {
	        $('div#rightBarCategories').css({'visibility':'visible'});
		$("div#rightBarStudios").css({'visibility':'hidden'});
	      }
		// change image
	});
	$("a.navStudio").click(function(){
	      if ($("div#rightBarStudios").is(":hidden")) {
	        $('div#rightBarStudios').css({'visibility':'visible'});
		$("div#rightBarCategories").css({'visibility':'hidden'});
//	      } else {
//		$("div#rightBarStudios").slideToggle(1000);
	      }
//		$("div#rightBarCategories").hide("fast");
		// change image
	});
*/
        $("input[@type=checkbox].orderOpen").click(function(){
	  if ($("div#placeOrderDiv").is(":hidden")) {
	    $('div#placeOrderDiv').css({'visibility':'visible'}); 
	    $("div#placeOrderDiv").show();
	  } else {
	    $("div#placeOrderDiv").hide();
	  }
	});

        // handle PF compliance div
	$("a.complianceOpen").click(function(){
	  if ($("div#complianceDiv").is(":hidden")) {
	    $('div#complianceDiv').css({'visibility':'visible'}); 
	    $("div#complianceDiv").slideDown("fast");
	    $("div#complianceCover").hide();
	  } else {
	    $("div#complianceDiv").slideUp("fast");
	  }
		// change image
	});

	$("a.complianceClose").click(function(){
		$("div#complianceDiv").slideUp("fast");
		// change image
	});

	$("a.agreementOpen").click(function(){
	  $('div#VA_popUp_full').css({'visibility':'visible'}); 
	  $('div#VA_popUp_small').css({'visibility':'hidden'}); 
		// change image
	});
  
	$("a.agreementClose").click(function(){
	  $('div#VA_popUp_full').css({'visibility':'hidden'}); 
	  $('div#VA_popUp_small').css({'visibility':'visible'}); 
		// change image
	});

    	// handle adding items to cart
	$(".addCartItem").click(function(){
		$(this).effect("transfer", { to: "span#tabCart" }, 500);
		$.ajax({
			url: BASE + "addItem&productId=" + $(this).attr("id"),
			cache: false,
			success: function(xml) {
				//refreshTotal(xml);
			}
		});
	}); // end click
	
	// handle cart item removal
	$("#remove_selected").click(function(){
		var delItems = "";
		var delRows = new Array();
		var i = 0;
		$(".remove_items").each(function(){
			if ($(this).attr("checked")){
				var delItem = $(this).attr("value") + "_row";
				// remove the row
				delRows[i++] = "#" + delItem;
				// add to the ajax call
				delItems = delItems + $(this).attr("value") + ";"
			}; // end if
		}); // end each
		if (delItems.length>0) {
			// ui blocking
			$("div#cartUI").block({  
		        	message: '<h1>Processing</h1>',  
	        		css: { border:'3px solid #a00', width:'500px', backgroundColor:'#cfd4e6' }  
	        	}); 		
			// update on back-end
			$.ajax({
				url: BASE + "removeItem&productId=" + delItems,
				cache: false,
				success: function(xml) {
					refreshTable(xml,delRows);
				}
			});
		}
	}); // end click

	// handle cart quantity changes	
//	$(".cartItemQty").keyup(function(){
		// adjust qty of this item
		// get the item
		// ui blocking
//		$("div#cartUI").block({  
//	        	message: '<h1>Processing</h1>',  
//        		css: { border:'3px solid #a00', width:'500px', backgroundColor:'#cfd4e6' }  
//        	}); 		
//		$.ajax({
//			url: BASE + "updateQty&productId=" + $(this).attr("id") + "&newQuantity=" + $(this).attr("value"),
//			cache: false,
//			success: function(xml) {
//				refreshTable(xml);
//			}
//		});
//	}); // end blur
});

// refresh cart total display
function refreshTotal(xml){
	$(xml).find("errorMessage").each(function(){
		$("#cartTotal").html($(this).text());
	});
	$(xml).find("total").each(function(){
		$("#cartTotal").html($(this).text());
	});
}

// refresh cart table
function refreshTable(xml,delRows) {
	if (null!=delRows) {
		// remove rows
		for (r in delRows) {
			$(delRows[r]).remove();
		}
	} else {
		// update quantity
		$(xml).find("item").each(function(){
			var id = $(this).find("id").text();
			var q  = $(this).find("qty").text();
			$("#" + id).val(q);
		});
	}
	// total
	var total = $(xml).find("total").text();
	$("#cart_total").html(total);
	$("#cartSubTotalInput").val(total);
	var numItems = $(xml).find("totalQty").text();
	$("#cartNumItemsInput").val(numItems);
	if (numItems < 1) {
	  var buttonTd = $("#cartCheckoutButtonTd");
	  buttonTd.empty();
	  var newHtml = "<IMG src='"+buttonTd.attr("disableButton")+"'/>";
	  buttonTd.html(newHtml);
	}

	$("div#cartUI").unblock(); 		
}

function clearFreeShipping() {
  var messageTd = $("div#shippingEstimates");
  messageTd.hide();
}

/*
function showRBStudios() {
  var RBStudiosDiv = $("div#rightBarStudios");
  var RBCategoriesDiv = $("div#rightBarCategories");
  RBStudiosDiv.show();
  RBCategoriesDiv.hide();
}

function showRBCategories() {
  var RBStudiosDiv2 = $("div#rightBarStudios");
  var RBCategoriesDiv2 = $("div#rightBarCategories");
  RBStudiosDiv2.hide();
  RBCategoriesDiv2.show();
}

function close2257popUp() {
  var compliancePopup = $("div#complianceDiv");
  compliancePopup.hide();
}
*/


