jQuery(document).ready(function() {
  // Stuff to do as soon as the DOM is ready;
  
  var whatClicked;
  
  jQuery(".ShoppingCart_Item").each(function(){
    var amount = jQuery(".itemAmount", this).text();
    addAmount = parseFloat(amount)+1;
    minusAmount = parseFloat(amount)-1;
    jQuery(".addItemAmount", this).attr("value", addAmount)
    jQuery(".minusItemAmount", this).attr("value", minusAmount)
  })
  
  jQuery(".addForm").livequery("submit", function(){
    whatClicked = jQuery(this).parents("ul").attr("id");
    form_values = jQuery(this).serialize();
    	jQuery('img.mainloadingimage').fadeIn('0');
		// Sometimes jQuery returns an object instead of null, using length tells us how many elements are in the object, which is more reliable than comparing the object to null
		jQuery.post( 'index.php?ajax=true', form_values, function(returned_data) {
			eval(returned_data);
			jQuery('img.mainloadingimage').fadeOut('0');
						
			if(jQuery('#fancy_notification') != null) {
				jQuery('#loading_animation').css("display", 'none');
				//jQuery('#fancy_notificationimage').css("display", 'none');
			}
			
		});
    return false;
  })
  
  jQuery(".minusForm").livequery("submit", function(){
    whatClicked = jQuery(this).parents("ul").attr("id");
    form_values = jQuery(this).serialize();
    	jQuery('img.mainloadingimage').fadeIn('0');
		// Sometimes jQuery returns an object instead of null, using length tells us how many elements are in the object, which is more reliable than comparing the object to null
		jQuery.post( 'index.php?ajax=true', form_values, function(returned_data) {
			eval(returned_data);
						jQuery('img.mainloadingimage').fadeOut('0');
			jQuery('div.wpsc_loading_animation').css('visibility', 'hidden');
						
			if(jQuery('#fancy_notification') != null) {
				jQuery('#loading_animation').css("display", 'none');
				//jQuery('#fancy_notificationimage').css("display", 'none');
			}
			
		});
    return false;
  })
  



});

function CartUpdate(foo){
  if(jQuery('#sidebar_right').length == 0){
    jQuery('body').append('<div id="hidden" style="display:none;"></div>')
    var hid = jQuery('#hidden');
    hid.html(foo);
    var items = hid.children('#cart_items_total').text();
    var matches = items.replace(/[a-z]/g, '');  
    jQuery('.top_cart_items_count').html(matches);
    var cost = jQuery('#cart_total').next("small").text();
    jQuery('.top_cart_cost_total').html(cost);
    
  }else{
    
    jQuery('body').append('<div id="hidden" style="display:none;"></div>')
    var hid = jQuery('#hidden');
    hid.html(foo);

    // UPDATE INDIVIDUAL ITEM COUNT
    var newShoppingCart = jQuery("#shoppingcart_holder", hid).html();
    jQuery("#shoppingcart_holder").html(newShoppingCart);
    
    
    // UPDATE ITEM COUNT TOP AND BOTTOM
    var items = jQuery('#cart_items_total', hid).text();
    var matches = items.replace(/[a-z]/g, '');  

    jQuery('.top_cart_items_count').html(matches);
    jQuery('.shopping_cart_item_count').html(items);

      
    // UPDATE COST COUNT TOP AND BOTTOM
    var cost = jQuery('#cart_total', hid).next("small").text();
    var shipping = jQuery('#shipping_total', hid).next("small").text();
    jQuery('.top_cart_cost_total').html(cost);
    jQuery('#cart_total').next("small").html(cost);
    jQuery('#shipping_total').next("small").html(shipping);
    
    Cufon.replace('h4, h3.prodtitles a, h3.producttitle, h3#shoppingcarttitle, a.empty_cart_link, a.goto_checkout',{
  		fontFamily: "Trade Gothic",
  		hover: true
  	});
  	
  	jQuery(".ShoppingCart_Item").each(function(){
      var amount = jQuery(".itemAmount", this).text();
      addAmount = parseFloat(amount)+1;
      minusAmount = parseFloat(amount)-1;
      jQuery(".addItemAmount", this).attr("value", addAmount)
      jQuery(".minusItemAmount", this).attr("value", minusAmount)
    })
  }
  jQuery.prettyLoader.hide();
  return false;
}


