Page = new (function() {
   var _this = this;
   
   _this.constructor = undefined;
   
   _this.updateMinStoraDag = function(amount) {
      Utils.request({
         url: '/backend/update_MinStoraDag.php',
         type: 'GET',
         data: { amount: amount },
         success: function() {
            Cart.update();
         },
         error: function(errno, error) {
            //alert(error);
         }
      });  
   }

   _this.addToCart = function(products) {
      for (var i = 0; i < products.length; i++)
      {
         var p = products[i], price, total;
         price = Lang.price(p.price);
         total = Lang.price(p.total);
         
         if (p.ptype == Config.ITEM_TYPE_DISCOUNT)
         {
            html = '<tr class="cart-row" product-id="' + p.prodid + '">' + 
                   '<td>&nbsp;</td>' +
                   '<td style="color:#ff0000">' + p.descr + '</td>' +
                   '<td colspan="2">&nbsp;</td>' +
                   '<td align="right" class="rprice" style="color:#ff0000">' + total + '</td>' +
                   '<td><div class="cart-icon-delete" onclick="Cart.removeProduct(' + p.prodid + ');">&nbsp;</div></td>' +
                   '</tr>';
            
            $('#_campaign, #_campaignHR').remove();
            
         }         
         else         
         {
            html = '<tr class="cart-row" product-id="' + p.prodid + '">' +
                   '<td>&nbsp;</td>' +
                   '<td>' + p.descr + '</td>' +
                   '<td><input type="text" value="' + p.qty +'" class="txt wT ar"' +
                   'onchange="Cart.updateProduct(' + p.prodid +', \$(this).val());"/></td>' +
                   '<td align="right" class="lprice">' + price + '</td>' +
                   '<td align="right" class="rprice">' + total + '</td>' +
                   '<td><div class="cart-icon-delete" onclick="Cart.removeProduct(' + p.prodid + ');">&nbsp;</div></td>' +
                   '</tr>';
         }
         
         $('#_cartTable tbody').append(html);
      }         
   }
   
   _this.removeFromCart = function(id) {
      $('#_cart tr[product-id=' + id + ']').remove();
      
      if ($('#_campaign').length == 0) {
        html  =  '<tr style="height:40px;" id="_campaign">' +
                 '<td>&nbsp;</td>' +
                 '<td colspan="2">Rabatt-/kampanjkod <span class="tS">- Om du har en ' +
                 'kod skriv in den i rutan till höger och klicka på OK.</span></td>' +
                 '<td colspan="2"><input style="width:100px;margin-right:13px;" class="txt fr"' + 
                 'type="text" value="" id="_campaignBox"/></td>' +
                 '<td><button style="margin-right:5px;" class="fr ui-state-default button"' +
                 'onclick="Cart.addProduct(\'campaign\', $(\'#_campaignBox\').val()); return false;">OK</button></td>' +
                 '</tr>' +
                 '<tr id="_campaignHR">' +
                 '<td colspan="6"><div class="hr"><hr /></div></td>' +
                 '</tr>';
                 
      
         $('#_cartTable tfoot').append(html);

      }
   }
   
   _this.redrawCart = function() {
      var cart = Cart.getData();
      
      $('#_cartTable tbody tr:even').removeClass('cart-odd-row');
      $('#_cartTable tbody tr:odd').addClass('cart-odd-row');

      var x = $('#_deliveryBox td:nth-child(2)');
      if (x.length >= 5) {
         //$(x[0]).html(Lang.price(cart.delivery_new));
         $(x[1]).html(Lang.price(cart.delivery_new));
         $(x[2]).html(Lang.price(cart.delivery_new + Config.codFee));
         $(x[3]).html(Lang.price(cart.delivery_new + Config.expressFee));
         $(x[4]).html(Lang.price(cart.delivery_new + Config.budFee));
      }

      /* Update the _rightBox div. */
      $('#_cart #_rightBox tr:nth-child(1) td:nth-child(2)').html(
            Lang.price(cart.totMoms));
      $('#_cart #_rightBox tr:nth-child(2) td:nth-child(2)').html(
            Lang.price(cart.total));
      
      for (var i = 0, n = 1; i < cart.products.length; i++) {
         var product = cart.products[i];

         var tr = $('#_cart > table tbody tr:nth-child(' + n + ')');

         if (product.ptype == Config.ITEM_TYPE_GENERIC || product.ptype == Config.ITEM_TYPE_EXTENSION)
            continue;
         
         if ((product.ptype == Config.ITEM_TYPE_DISCOUNT) || (product.ptype == Config.ITEM_TYPE_VOUCHER_PAY)) {
            tr.children('td:nth-child(4)').html(Lang.price(product.total));
         } else {
            tr.children('td:nth-child(5)').html(Lang.price(product.total));
            tr.children('td:nth-child(3) input').val(product.qty);
         }
         
         n++;
      }
   }
      
});



