var L_CART_TIMER = '';
var L_CART_IS_BUSY = false;
var L_CART_DATA = null;
function buy_item(type, id)
{
	if (G_LOGGED_USER == '')
	{
		show_login_dialog();
		return ;
	}
  if (type && id)
  {
	  var params = {};
	  params.a = 'buy';
	  params.uid = G_LOGGED_USER.uid;
	  params.type = type;
	  params.id   = id;
    $.post(G_SERVICE_URL + 'action.php', params, function (data) {
    	// get_cart_info();
      if (data == "1") {
        
        $('#dlg_continueshopping_item').modal();
      } else if (data == "2") {
        $('#dlg_nobuy_item').modal();
      }else {
        $('#dlg_alreadyshopping_item').modal();
      }
    }, 'text');
  }
}
function show_delete_cart_item(type, id)
{

  if (G_LOGGED_USER !== '')
  {
    L_CART_DATA = {'type': type, 'id': id};
  	$('#dlg_delete_cart_item').modal();
  }
  else
  {
	  show_login_dialog();
  }
}
function delete_cart_item()
{
  var data = L_CART_DATA;
  if (!data) {
    return ;
  }
  
  var type = data.type;
  var id   = data.id;
  if (type && id)
  {
    var params = {};
    params.a = 'del_cart_item';
    params.uid = G_LOGGED_USER.uid;
    params.type = type;
    params.id   = id;
    $.post(G_SERVICE_URL + 'action.php', params, function (data) {
      if (data == "1") {
        if (L_CART_INFO) {
          var data = L_CART_DATA;
          var type = data.type;
          var id   = data.id;
          var item = null;
          for (var i = 0;i < L_CART_INFO.data.length;i++) {
            if (L_CART_INFO.data[i].id == id) {
              item = L_CART_INFO.data[i];
              L_CART_INFO.data.splice(i, 1);
              L_CART_INFO.total_num -= parseInt(item.no);
              L_CART_INFO.total -= parseFloat(item.price * item.no);
              L_CART_INFO.total = Math.round(L_CART_INFO.total * 100) / 100;
              $('#tr_spacer_' + item.id).remove();
              $('#tr_item_' + item.id).remove();
              
              $('#span_cart_total').text(L_CART_INFO.total)
              
              break;
            }
          }
        } else {
          document.location.reload();
        }
      } else {
        alert('Failed');
      }
    	$.modal.close();
    }, 'text');
  }
}
/*


function get_cart_info()
{
  if ($('#cart_items').size() == 0)
  {
	return false;
  }
  if (G_LOGGED_USER)
  {
    if (L_CART_TIMER)
    {
      clearTimeout(L_CART_TIMER);
    }
    if (L_CART_IS_BUSY)
    {
      L_CART_TIMER = setTimeout(get_cart_info, 5000);
      return;
    }
    var params = {};
    params.a = 'get';
    $.post(G_SERVICE_URL + 'cart.php', params, function (data) {
      if (data.status == 'success')
      {
        render_cart(data);	
      }
      else
      {
        //alert(data.reason);
      }
      
      L_CART_TIMER = setTimeout(get_cart_info, 5000);
    }, 'json');
  }
}
function render_piece_cart_item(data, hover)
{
  var html = '';
  html += '<li class="cart_item">';
  if (hover)
  {
	html += '<div class="cart_item_name" style="display:none">';
  }
  else
  {
	html += '<div class="cart_item_name">';
  }
  html += '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
  html += '<tr>';
  html += '<td width="60%" class="gray_4">' + data.name + '</td>';
  html += '<td width="40%" class="gray_4">数量：' + data.no + '</td>';
  html += '</tr>';
  html += '</table>';
  html += '</div>';
  if (hover)
  {
	html += '<div class="cart_item_extra_1">';
  }
  else
  {
	html += '<div class="cart_item_extra_1" style="display:none">';
  }
  html += '<h3>' + data.name + '<a href="javascript: void(0)" onclick=\'show_delete_cart_item("' + data.type + '", ' + data.id + ')\'>关闭</a></h3>';
  html += '<div>';
  html += '<a href="javascript: void(0)" class="proImg"><img alt="image_60" src="' + data.thumb + '" width="60" height="60" onclick="preview_item(\'' + data.type  + '\', ' + data.id + ')" /></a>';
  html += '<ul class="cart_item_extra_btn">';
  html += '<li><strong class="gray_4">价格：</strong> <span class="blue_2">' + data.price + '</span>M币</li>';
  html += '<li><strong class="gray_4">数量：</strong><span class="blue_2">' + data.no + '</span></li>';
  html += '<li><strong class="gray_4"> 有效期：</strong><span class="blue_2">' + data.days + '</span>天</li>';
  html += '</ul>';
  html += '</div>';  
  html += '</div>';
  html += '</li>';
  html += '<div class="line_short"><img width="1" height="1" src="images/spacer.gif"/></div>';
  return html;
}
function render_cart(data)
{
  var data = data.data;
  
	$('#cart_total_num').text(data.total_num);
	$('#cart_total_price').text(data.total);
	$('#cart_items').html('');
	for (var i = 0;i < data.data.length;i++)
	{
	  // add to local cache
	  L_DATA.add(data.data[i].type, data.data[i]);
	  if (i == 0)
	  {
	    $('#cart_items').append(render_piece_cart_item(data.data[i], true));
	  }
	  else
	  {
	    $('#cart_items').append(render_piece_cart_item(data.data[i], false));
	  }
	}
	
	// for shopping cart
	$('#shopping_cart > ul > li').mouseover(function () {
	  $('#cart_items .cart_item_extra_1').hide();
	  $('#cart_items .cart_item_name').show();
	  $(this).find('.cart_item_name').hide();
	  $(this).find('.cart_item_extra_1').show();
	  L_CART_IS_BUSY = true;
	}).mouseout(function () {
	  L_CART_IS_BUSY = false;
	});
	$('#shoppingcar').show();
}
$(document).ready(function () {
  get_cart_info();
});
*/
