jQuery(document).ready( function($)
{
  function fRefreshCart()
  {
    var aEntries = $('#cart tr:not(#all,#empty)'),
        pid_name = '',
        pid = '',
        qty = 0,
        dsc = '',
        prc = 0,
        tot = 0,
        entry = '',
        order = '[',
        total = 0;

    $.each( aEntries,function( i,obj )
    {
      pid_name = $(obj).find('input[name$="_dsc"]').attr('name');
      pid = pid_name.substr( 0,(pid_name.length-4) ); // '_dsc' entfernen
      qty = parseFloat( $(obj).find('.quantity').val() );
      dsc = $(obj).find('input[name$="_dsc"]').val();
      prc = parseFloat( $(obj).find('input[name$="_prc"]').val() );
      tot = parseFloat( $(obj).find('.subtotal').val() );
      entry = '{\"qty\":\"'+qty+'\",\"pid\":\"'+pid+'\",\"dsc\":\"'+dsc+'\",\"prc\":\"'+prc.toFixed(2)+'\",\"tot\":\"'+tot.toFixed(2)+'\"},';
      order += entry;
      total += tot;
    });

    $('.total').val( total.toFixed(2) );
    
    if( total > 0 ) {
      order += '{\"total\":\"'+total.toFixed(2)+'\"}]';
      $('input[name="order"]').val( order );
    }
    else fResetCart();
  }
  
  function fResetCart()
  {
    $('#cart tr:not(#all,#empty)').remove();
    $('#all').replaceWith('<tr id="empty"><td class="dsc">Der Warenkorb ist leer</td></tr>');
    $('input[name="order"]').val( '' );    
  }

  function fRemoveItem( obj )
  {
    if( $(obj).closest('#all').length == 1 ) // Klick auf 'Warenkorb löschen' bei 'Total'
      fResetCart();
    else
      $(obj).closest('tr').remove();
  }


  function fRecalcItem( obj, adjust )
  {
    var entry = $(obj).closest('tr');
    var qty = parseInt( $(entry).find('.quantity').val() );
        qty += adjust;

    var prc = parseFloat( $(entry).find('input[name$="_prc"]').val() );
    var tot = parseFloat( qty * prc );

    if( qty > 0 ) {
      $(entry).find('.quantity').val( qty );
      $(entry).find('.subtotal').val( tot.toFixed(2) );
    }
    else fRemoveItem( obj )
  }


  $('.subtotal, .quantity, .total')
    .livequery( 'focus',function(event) {
      $(this).blur();
  });


  $('.to_cart').mouseup( function()
  {
    var prod_id = $(this).attr('title'),                 // <tr title="produkt_id" .. , ohne Leerschläge
        prod_dsc = $(this).parent().attr('title'),       // <a title="Produkt Beschreibung" .. ,
        prod_prc = $(this).prev('.price').attr('title'); // <span title="Produktpreis" .. , 2 Stellen nach dem Komma

    var entry = '<tr id="'+prod_id+'">'
      +'<td class="qty">'
      +  '<input type="text" class="quantity" name="'+prod_id+'_qty" value="1" /> Ex. ' // value 1. Eintrag: 1
      +  '<a href="javascript:void(0)" class="minus">-</a>'
      +  '<a href="javascript:void(0)" class="plus">+</a>'
      +'</td>'      
      +'<td class="dsc">'+prod_dsc
      +  '<input type="hidden" name="'+prod_id+'_dsc" value="'+prod_dsc+'" />'
      +  '</td>'
      +'<td>'
      +  '<input type="text" class="price" name="'+prod_id+'_prc" value="'+prod_prc+'" />'
      +'</td>'
      +'<td>'
      +  '<input type="text" class="subtotal" name="'+prod_id+'_tot" value="'+prod_prc+'" /> ' // value 1. Eintrag: 1 * Preis
      +  '<a href="javascript:void(0)" class="remove">x</a>'
      +'</td>'
    +'</tr>';

    if( $('#cart #empty').length == 1 ) {
      $('#empty').replaceWith( entry );
      $('#cart').append( '<tr id="all"><td colspan="3">Total:</td><td><input type="text" class="total" name="total" value="0" /> <a href="javascript:void(0)" class="remove">x</a></td></tr>' );
    }
    else if( $('#cart').find('#'+prod_id).length == 1 )
    {
      var qty = parseInt( $('#'+prod_id+' .quantity').val() );
          qty++;
      var tot = parseFloat( qty * prod_prc );

      $( '#'+prod_id+' .quantity' ).val( qty );
      $( '#'+prod_id+' .subtotal' ).val( tot.toFixed(2) );
    }
    else $('#all').before( entry );
	
    $(this).css( { 'background-color':'#FBE6EF', 'color':'#4E525B' } );
    fRefreshCart();
  }); 


  $('#cart a.plus')
    .livequery( 'mouseup',function(event) {
      fRecalcItem( $(this), 1 );
      fRefreshCart();
  });


  $('#cart a.minus')
    .livequery( 'mouseup',function(event) {
      fRecalcItem( $(this), -1 );
      fRefreshCart();
  });


  $('#cart a.remove')
    .livequery( 'mouseup',function(event) {
      fRemoveItem( $(this) );
      fRefreshCart();
  });


  $('input[name=reset]').mouseup( function() {
    $('#cart tr:not(#all,#empty)').remove();
    $('.errors').remove();
    $('input[name="order"]').val( '' );
    $('#all').replaceWith('<tr id="empty"><td class="dsc">Der Warenkorb ist leer</td></tr>');
  });
  
  fRefreshCart();

});




