function loadCartModal()
{
        var statusAppend='';
      
        var url = getDomain();
	var totalCost = 0;
	var html = '';
	$.getJSON(url+ "viewCartAjax.do?format=json&jsoncallback=?",
    function(data){
      if(data.items.length > 0)
      {
         
      
      $.each(data.items, function(i,item){
        if(item!=undefined){
      	var totalPrice = parseFloat(item.q) * parseFloat(item.pe);
      	html += '<tr id="'+item.id+'"><td class="title"><a href="http://www.bocajava.com/showProductDetail.do?productId='+item.id+'&catalogId=1">'+item.title+'</a>';
      	if(item.grind) html+='<div class="hidden">'+item.grind+'</div>';
      	if(item.customizable) html += '<div class="sub_msg">'+item.customizable+'</div>';
      	html += '</td>';
      	html += '<td>'+item.q+'</td>';
      	html += '<td class="price">$'+formatCurrency(totalPrice)+'<div class="sub_msg">$'+formatCurrency(item.pe)+' each</div></td></tr>';
      	totalCost+= totalPrice;
        }
      });
      html+='<tr class="totalRow"><td colspan="2" class="totalText">Total: <span class="cartTotal">$' + formatCurrency(totalCost)+'</span></td><td><a href="/checkout.do'+statusAppend+'" class="right gbtn sm"><span><span>checkout</span></span></a></td></tr>';
    //  html+='<tr class="totalRow"><td colspan="3"><a href="/viewCart.do" class="button_b right"><span class="b"><span><span class="b"><span>my account</span></span></span></span></a></td></tr>';
      $("#viewcart_div table").html(html);
      $("#viewcart_div table tr:even").addClass('cartEven');
      $("#viewcart_div table select option").each(function(){
      		if($(this).is(':selected'))
      		{
      			var value = $(this).html();
      			$(this).parent().parent().after('<div class="sub_msg">' + value +'</div>');
      		}
      });




		$("#cartCount").html(data.items.length);
		$("#cartCost").html('$'+formatCurrency(totalCost));

      }
    });
}

function viewCartModal(options)
	{
		//console.log(viewPath);

		loadCartModal();

                $("div.topnav_content").pngfix();
		$("div.topnav_bottom").pngfix();

		var hideDelayTimer = null;
		var hideDelay = 50;
                var anchor = $("#cart_contents div");
		$("#cart_contents div").mouseover(function(){
                    
                                $(this).addClass('on');
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
				var tcOffset = $(this).offset();
				var t = tcOffset.top;
				var l = tcOffset.left;
                                
				var h = 25;
				var w = 368;
				var fWidth = Math.floor(w);
				var vcTop = t+h-6;
                //console.log(t+':'+h);
				var lf=l-368+131;
                                if ($.browser.msie && parseInt($.browser.version)<=6) lf=lf+1;
				//if (jQuery.browser.mozilla)lf -= 2;

				$("#viewcart_div")
				.css({top:vcTop+'px',left:lf+'px',width:fWidth+'px'}).show()
				.mouseover(function(){if (hideDelayTimer) clearTimeout(hideDelayTimer);})
				.mouseout(function () {

		    	  if (hideDelayTimer) clearTimeout(hideDelayTimer);

			      // store the timer so that it can be cleared in the mouseover if required
			      hideDelayTimer = setTimeout(function () {
			        hideDelayTimer = null;
			       $("#viewcart_div").hide();
                               anchor.removeClass('on');
			      }, hideDelay);
		    });


		}).mouseout(function(){
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
                       
	      // store the timer so that it can be cleared in the mouseover if required
	      hideDelayTimer = setTimeout(function () {
	        hideDelayTimer = null;
	       $("#viewcart_div").hide();
               anchor.removeClass('on');
	      }, hideDelay);
		});
	}


/** main view cart page ajax **/
$(function(){
    update_viewcart();
})


function update_viewcart()
    {
        var d = getDomain();
        $("#table_viewcart input").inputUpdater({
           errorMessage:'Please enter a number',
           submitPath:d+'updateCartAjaxAction.do',
           loadingImagePath:'/resources/images/2009/loading_small.gif',
           errorImagePath:'/resources/images/2009/indicator_error.gif',
           successImagePath:'/resources/images/2009/indicator_success.gif',
           actionType:'update',
           parent: 'tr',
           presubmitCallback: function(el,parent,options){
                var $this = $(el);

               // return validate_selection_count(parent);

                return true;
                //always return true or false so it can be submitted
           },
           submitCallback: function(el,parent,options){
               var $this = $(el);
               var priceEach = parent.children('td.td_price').children('div.sub_msg').html();
               priceEach = priceEach.replace(/\seach/gi,'');
               priceEach = priceEach.substring(1);
              // debug(priceEach)
               var priceTotal = parseFloat(priceEach) * $this.val();
               parent.children('td.td_price').children('span').html('$'+formatCurrency(priceTotal));

                viewcart_ordertotal(parent);
                var q = parent.children('td.td_quantity').children('input[name="quantity"]').val();
                var pName = parent.children('td').children('span.c_productName').children('a').html();

                var action = 'View Cart Update Quantity';
                if(q>0)
                  {
                        addNotice('You have updated the quantity of <b>'+pName+'</b> to <b>'+q+'</b>');
                  }
                else
                    {
                        addNotice('You have removed <b>'+pName+'</b> from your cart');
                        action = 'View Cart Remove Product';
                    }

                  loadCartModal();
                 var category = 'AJAX';
                 gaPagetracker(category,action,pName);
           }
       });


       $("#table_viewcart select").inputUpdater({
           valueType: 'alpha',
           submitPath:d+'updateCartAjaxAction.do',
           loadingImagePath:'/resources/images/2009/loading_small.gif',
           errorImagePath:'/resources/images/2009/indicator_error.gif',
           successImagePath:'/resources/images/2009/indicator_success.gif',
           actionType:'update',
           parent:'tr',
           submitCallback: function(el,parent,options) {
               var pName = parent.children('td').children('span.c_productName').children('a').html();
               var grind = $(el).children('option:selected').text();
               addNotice('You have updated the grind of <b>'+pName+'</b> to <b>'+grind+'</b>');
               loadCartModal();
                var action = 'View Cart Change Grind';
                var category = 'AJAX';
                gaPagetracker(category,action,pName,grind);
           }
       });

    }

    function viewcart_ordertotal(parent)
    {
             var ot = 0;
            var savings = 0;
               $("#table_viewcart td.td_price span").each(function(){
                  
                      var val = $(this).html();

                      val = val.replace(/\$/,'');
                      val = val.replace(/\,/gi,'');
                      val = parseFloat(val);


                      ot+= parseFloat(val);
                  
               });  //update order total
             //  console.log(ot)
               $("#cs_ot").html('$'+formatCurrency(ot)).effect("highlight",{},2000);
    }
