/* CONFIG */
var EMPTY_CART_MSG = 'Sorry, your cart is empty.';
var xOffset    = -10;
var yOffset    = -150;
var moreLeft   = 119;
var moreBottom = 3;
// these variables determine popup's distance from the cursor
// you might want to adjust to get the right result
/* END CONFIG */

$(document).ready(function()
{
   $('body').click(function(e)
   {
      var $tgt = $(e.target);
      if($tgt.parents('div').filter('div#mini_cart_top_div').length<=0)
      {
         closeMiniCart();
      }
   });

   $('#mini_cart').bind('click',function(e)
   {
      showMiniCart(e);
   });
});

function showMiniCart(e)
{

   var top_pos  = $('#cart').position().top + $('#cart').height()+moreBottom;
   var left_pos = $('#cart').position().left-moreLeft;

   $.ajax({
   type    : "POST",
   url     : "/cart.php",
   data    : 'action=show_mini_cart',
   dataType:"json",
   success: function(response)
            {
	            $("body").append("<div id='mini_cart_top_div'>"+ response.content+"</div>");
	            //$("#mini_cart_top_div").css({left: (xcoord + yOffset), top: (ycoord - xOffset)});
	            $("#mini_cart_top_div").css({left: left_pos, top: top_pos});
	            $("#mini_cart_top_div").fadeIn("fast",function() {

	               if(!response.is_empty_cart)
	               {
	                  if(!pagination)
                     {
                        $('#mini_cart_items_footer').children('td:first').find('a').remove();
                     }
                     paginateCartItems();
                  }


	            });


            }
   });
}

function deleteCartItem(itemId,productKey,rowObj)
{
   var params = 'action=delete_mini_cart_item&cart_items_id='+itemId+
                '&product_key='+productKey+'&current_page='+displayPage;
   $.ajax({
   type    : "POST",
   url     : "/cart.php",
   data    : params,
   dataType:"json",
   success: function(response)
            {
	            if(response.status)
	            {
	               $(rowObj).parents('table').next('h5').remove();
	               $(rowObj).parents('table').remove();
	               if(response.is_empty_cart)
	               {
	                  $('#mini_cart_msg_container').text(EMPTY_CART_MSG);
	                  $('#mini_cart_nav').empty();
                     $('.mini_cart_btn_container2').empty();
	                  $('.mini_cart_btn_container').empty();

	                  //Changing header cart info
	                  $('#shopping_cart_items').text(0);
	                  $('#shopping_cart_sub_total').text('$'+0);
                     if($("#chk_out").length > 0)
                     {
                        $("#chk_out").remove();
                        $("#span_view_cart").append('<a href="/checkout.php?action=show_empty_check_out_screen" id="new_chk_out"><img src="/theme/default/images/checkout.png"/></a>');
                     }
                     else if($("#new_chk_out").length > 0)
                     {
                        $("#new_chk_out").remove();
                        $("#span_view_cart").append('<a href="/checkout.php?action=show_empty_check_out_screen" id="chk_out"><img src="/theme/default/images/checkout.png"/></a>');
                     }
	                  return;
	               }
	               $('#sub_total').html('Subtotal: <strong>'+response.sub_total+'</strong>');
	               $('#total_items').html('Items:<strong>'+response.total_cart_items+'</strong>');

	               //Changing header cart info
	               $('#shopping_cart_items').text(response.total_cart_items);
	               $('#shopping_cart_sub_total').text('$'+response.sub_total);

                  //new change
                  displayPage = response.display_page;
	               total_pages = response.total_pages;
	               $('#page').html('Page <strong>'+displayPage+'</strong>');
	               //new change
	               start       = displayPage;
	               paginateCartItems();
	            }
            }
   });
}

function clearCart()
{
   var params = 'action=clear_cart&product_ids='+$.toJSON(productIds)+'&product_keys='+$.toJSON(productKeys);
   $.ajax({
   type    : "POST",
   url     : "/cart.php",
   data    : params,
   dataType:"json",
   success: function(response)
            {
	            if(response==1)
	            {
	               $('#mini_cart_items_container').empty();
	               $('#mini_cart_nav').empty();
	               $('.mini_cart_btn_container2').empty();
	               $('.mini_cart_btn_container').empty();
	               $('#mini_cart_msg_container').text(EMPTY_CART_MSG);

	               //Changing header cart info
	               $('#shopping_cart_items').text(0);
	               $('#shopping_cart_sub_total').text('$'+0);
                  if($("#chk_out").length > 0)
                  {
                     $("#chk_out").remove();
                     $("#span_view_cart").append('<a href="/checkout.php?action=show_empty_check_out_screen" id="new_chk_out"><img src="/theme/default/images/checkout.png"/></a>');
                  }
                  else if($("#new_chk_out").length > 0)
                  {
                     $("#new_chk_out").remove();
                     $("#span_view_cart").append('<a href="/checkout.php?action=show_empty_check_out_screen" id="chk_out"><img src="/theme/default/images/checkout.png"/></a>');
                  }

	            }
	         }

   });
}

function showNextPage()
{
   displayPage++;
   start = displayPage;
   if(!displayPage < total_pages)
   {
      $('#next').hide();
   }
   $('#page').html('<strong>Page'+displayPage+'</strong>');
   paginateCartItems();
}

function showPrevPage()
{
   displayPage--;
   start = displayPage;
   if(!(displayPage > 1))
   {
      $('#prev').hide();
   }
   $('#page').html('<strong>Page'+displayPage+'</strong>');
   paginateCartItems();
}

function paginateCartItems()
{
   start   = (start-1)*miniCartItemsPerPage;
   var end = (start+miniCartItemsPerPage) -1;

   if(displayPage < total_pages)
   {
      $('#next').show();
   }
   else
   {
      $('#next').hide();
   }
   if(displayPage > 1)
   {
      $('#prev').show();
   }
   else
   {
      $('#prev').hide();
   }

   $('#mini_cart_items_container table').hide();

   for(var i=start,tables =$('#mini_cart_items_container table'), size= tables.length; i <= end; i++)
   {
      if(tables[i])
      $(tables[i]).show();
   }

}

function closeMiniCart()
{
   $("#mini_cart_top_div").remove();
}

