//jQuery.noConflict();
jQuery(document).ready(function($) {
  
  $('.productAddToCart').live('click', function () {
      var nodeId = $(this).attr('nodeid');   
      /* Add orderline is called using the Tea Commerce javascript API */
      TeaCommerce.addOrderLine(nodeId, 1, true, successfn, errorfn);     
      function successfn () {
        //alert('added!');
      }
      function errorfn () {
        //alert('error');
      }
      return false;
  });
  
  $('a.empty').live('click',function() {
    TeaCommerce.removeAllOrderLines(true, successfn, errorfn);
    function successfn () {
      //alert('added!');
    }    
    function errorfn () {
      //alert('error');
    }
    return false;
  });
  $('a.remove').live('click',function(){    
      var nodeId = $(this).attr('nodeid');
      TeaCommerce.removeOrderLine(nodeId, true, successfn, errorfn);
      function successfn () {
        //alert('added!');
      }    
      function errorfn () {
        //alert('error');
      }
      return false;
  });
  
  /* The confirm order step of the cart */
  $('a#confirmOrder').live("click", function () {
    var personalInformation = $("#personalInformation");
  
    /* We fetch the information from the form and creates
      an object that can be sent to the server as a form
      POST submit */
    var formObj = {
      "company": personalInformation.find("#company").val(),
      "firstName": personalInformation.find("#firstName").val(),
      "lastName": personalInformation.find("#lastName").val(),
      "email": personalInformation.find("#email").val(),
      "streetAddress": personalInformation.find("#streetAddress").val(),
      "zipCode": personalInformation.find("#zipCode").val(),
      "city": personalInformation.find("#city").val()
    };
  
    /* The properties is sent to the server with a syncronous call
      This way we lock the UI and can redirect the user.*/
    TeaCommerce.updateOrderProperties(formObj, false);
    TeaCommerce.setPaymentMethod(2, false);
    TeaCommerce.goToPayment();
    return false;
  });
  
  
      var updateCounter = 0,
      submitTimer = null;
 
      /*
    We hook on to the onCartUpdated event.
    This way we can change the UI everytime the Cart/order has been changed
    */
    TeaCommerce.subscribeToOnCartUpdated(function (data) {
      //updateCarts(data);
      var mainCart = jQuery('#mainCart'),
          miniCart = jQuery('#miniCart'),
          nodeID = document.body.id;
      
      var mainCartJqHtml = jQuery(TeaCommerce.invokeXSLT('MainCart.xslt', nodeID, false)),
          miniCartJqHtml = jQuery(TeaCommerce.invokeXSLT('MiniCart.xslt', nodeID, false));
      //console.log(cartJqHtml);
      miniCart.after(miniCartJqHtml);
      miniCart.remove();
      mainCart.after(mainCartJqHtml);
      mainCart.remove();
      
    });
    
    /*
    We hook on to the onCartUpdating event.
    */
    TeaCommerce.subscribeToOnCartUpdating(function () {
      /*
      Set the start time of the submitTimer if it has not yet been set.
      This way we only set it on the first call in a series off calls
      */
      if (submitTimer === null) {
      submitTimer = new Date();
      }
    
      //Set the cart to updating mode
      jQuery('#mainCart table').addClass('updating').parent().append('<span class="prog"></span>');
      jQuery('#miniCart > div').addClass('updating').parent().append('<span class="prog"></span>');
    
      //Increment the updatecounter
      updateCounter++;
    });
    
    /*
    We hook on to the onCartUpdateError event.
    */
    TeaCommerce.subscribeToOnCartUpdateError(function (error) {
      /*
      The errortext is thrown right at the user in the
      ugliest possible way.
      It is only called when something went very wrong
      and the server through an exception. This should
      never happen as the system is perfect ;)
      */
      alert("Cart Error: \n" + error.responseText);
    
      //The update counter is decremented as this call has returned.
      updateCounter--;
    });



});
