var Store = function() {
  
  var _success_url  = 'http://www.brownsbrewing.com/store';
  var _cancel_url   = 'http://www.brownsbrewing.com/store';
  var _paypal_email = 'garry@brownsbrewing.com';
  var _page_style   = 'Primary';
  
  var _products = {};
  
  
  var add_to_cart = function(product_id) {
    var product = _products[product_id];
    if(!product) return false;
    
    var quantity = $('store-quantity-'+product_id).value;
    quantity = Math.round(parseInt(quantity));
    if(typeof quantity != 'number' || quantity <= 0) return false;
    
    var size = $('store-size-'+product_id) ? $('store-size-'+product_id).value.strip() : null;
    if(size && product['sizes'].indexOf(size) == -1) return false;
    
    var color = $('store-color-'+product_id) ? $('store-color-'+product_id).value.strip() : null;
    if(color && product['colors'].indexOf(color) == -1) return false;
    
    var price = quantity * product['price'];
    price = Math.round(100*price) / 100;
    
    var name = '(' + quantity + ') ';
    if(size) name += size + ' ';
    if(color) name += color + ' ';
    name += product['name'];
    
    var weight = product['weight'] || 0;
    
    var url = 'https://www.paypal.com/cgi-bin/webscr?';
    url += 'add=1&cmd=_cart&business='+encodeURIComponent(_paypal_email)+'&page_style='+encodeURIComponent(_page_style)+'&no_shipping=2&return='+encodeURIComponent(_success_url)+'&cancel_return='+encodeURIComponent(_cancel_url)+'&no_note=1&currency_code=USD&weight_unit=lbs&lc=US&bn=PP-ShopCartBF';
    url += '&item_name='+encodeURIComponent(name)+'&item_number=00'+product_id+'&amount='+encodeURIComponent(price)+'&weight='+weight;

    window.open(url, 'paypal');
  };
  
  
  var add_gift_certificate_to_cart = function() {
    var amount = parseFloat($('gift-certificate-amount').value);
    if(!amount || amount < 0) {$('gift-certificate-amount').value = '0'; return;}
    var url = 'https://www.paypal.com/cgi-bin/webscr?';
    url += 'add=1&cmd=_cart&business='+encodeURIComponent(_paypal_email)+'&page_style='+encodeURIComponent(_page_style)+'&no_shipping=2&return='+encodeURIComponent(_success_url)+'&cancel_return='+encodeURIComponent(_cancel_url)+'&no_note=1&currency_code=USD&weight_unit=lbs&lc=US&bn=PP-ShopCartBF';
    url += '&item_name=Gift%20Certificate&item_number=0999&amount='+encodeURIComponent(amount);
    url += '&on0=To&os0='+encodeURIComponent($('gift-certificate-to').value)+'&on1=From&os1='+encodeURIComponent($('gift-certificate-from').value);
    window.open(url, 'paypal');
  };


  var add_club_browns_to_cart = function() {
    var amount = 50.00;
    var url = 'https://www.paypal.com/cgi-bin/webscr?';
    url += 'add=1&cmd=_cart&business='+encodeURIComponent(_paypal_email)+'&page_style='+encodeURIComponent(_page_style)+'&no_shipping=2&return='+encodeURIComponent(_success_url)+'&cancel_return='+encodeURIComponent(_cancel_url)+'&no_note=1&currency_code=USD&weight_unit=lbs&lc=US&bn=PP-ShopCartBF';
    url += '&item_name=Club%20Browns&item_number=1000&amount='+encodeURIComponent(amount);
    window.open(url, 'paypal');
  };
  
  
  var view_cart = function() {
    var url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_cart&business='+encodeURIComponent(_paypal_email)+'&display=1&page_style='+encodeURIComponent(_page_style);
    window.open(url, 'paypal');
  };
  
  
  var checkout = function() {
    var url = 'https://www.paypal.com/us/cgi-bin/webscr?cmd=_cart&business='+encodeURIComponent(_paypal_email)+'&checkout=1';
    window.open(url, 'paypal');
  };
  
  
  var add_products = function(products) {
    _products = products;
  };
  
  
  return {
    add_products:add_products,
    add_to_cart:add_to_cart,
    view_cart:view_cart,
    checkout:checkout,
    add_gift_certificate_to_cart:add_gift_certificate_to_cart,
    add_club_browns_to_cart:add_club_browns_to_cart
  }
}();