var sqInit = false;
function emptyCart() {
  var req = new Request.JSON({
    'url': cart_doc,
    'method': 'post',
    onComplete: function(resp) {
      cart_count = resp;
      if (!cart_count.length) {
        SqueezeBox.close();
        $$(cart_el).set('html', '');
      } else {
        if (confirm(cart_warn)) {
          var req2 = new Request.JSON({
            'url': cart_doc,
            'method': 'post'
          }).post({
            'action': 'empty'
          });
          $$(cart_el).set('html', '');
        }
      }
    }
  }).post({
    'action': 'count'
  });
  return false;
}
function checkout(evt) {
  $('content').empty();
  var div = new Element('div', {
    'html': evt,
    'id': 'finalForm'
  }).injectInside($('content'), 'top');
  SqueezeBox.close();
  $('paypal').submit();
}
function getOpts() {
  var req = new Request.JSON({
    'url': cart_doc,
    'method': 'post',
    onComplete: function(resp) {
      op_field = resp;
      $$('.' + op_field).set('value', resp.value);
    }
  }).post({
    action: 'item_opts',
    'get': true
  });

}
function cartCount() {
  var req = new Request.JSON({
    'url': cart_doc,
    'method': 'post',
    onComplete: function(resp) {
      cart_count = resp;
      cart_string = '<p><a class="jbox tc_view" title="' + cart_view + '" href="' + cart_doc + '&action=view" rel="{handler:\'iframe\',size:{x:500,y:400}}" onclick="return false;">' + '<span class="tc_cart_view_text">' + cart_view + '</span> <span class="tc_cart_count" class="tc_count">(' + cart_count.length + ')</span></a></p>' + '<p><a class="tc_cart_empty ' + class_empty + '" href="#" title="' + cart_empty + '">' + cart_empty + '</a></p>';
      $$(cart_el).set('html', cart_string);
      $$('a.tc_view').addEvent('click',
      function(e) {

        if (!cart_count.length) {
          emptyCart();
        }
      });
      $$('a.' + class_empty).addEvent('click', emptyCart);
      if ($$('.tc_view')) {
        var tcV = SqueezeBox.assign($$('.tc_view'), {
          parse: 'rel'
        });
      }
    }
  }).post({
    'action': 'count'
  });

  return false;

}
function addToCart(a, b, c) {
  var req = new Request.JSON({
    'url': cart_doc,
    'method': 'post',
    onComplete: function(resp) {
      cartCount();
      $$(cart_el).morph({
        'opacity': [0.2, 1]
      });
      $(a).morph({
        'opacity': [0.0, 1]
      });
    }
  }).post({
    'action': 'add',
    'selector': b,
    'item': c
  });
  return false;
}

function tcConfig(a) {
  cart_name = a.business_name;
  cart_view = a.cart_view;
  cart_empty = a.cart_empty;
  cart_warn = a.cart_warn;
  cart_el = a.cart_el;
  class_add = a.class_add;
  class_empty = a.class_empty;
  $$('.' + class_add).each(function(el) {
    el.addEvents({
      'click': function(e) {
        addToCart(el, el.get('rel'), el.get('name'));
        e.stop();
      }
    });
  });
  cartCount();
}
function paypalForm(a) {

  var req = new Request.JSON({
    'url': cart_doc,
    'method': 'post',
    onComplete: function(resp) {
      getOpts();
    }
  }).post({
    action: 'item_opts',
    'name': a.get('name'),
    'value': a.get('value')
  });
}
window.addEvent('load',
function() {
  var req = new Request.JSON({
    'url': cart_doc,
    'method': 'post',
    onComplete: function(resp) {
      tcConfig(resp);
    }
  }).post({
    'action': 'config'
  });

});