Ajax.Responders.register({
  // Show progress indicator and hide 
  // all other notices 
  onLoading: function() {
    document.body.addClassName('loading');
  },
  
  onComplete: function() {
    document.body.removeClassName('loading');
  }    
});


Shopify.onError = function(error) {    
  alert('The desired quantity is not currently available for the selected item.\n\nPlease try a lesser quantity or check back soon for additional product!');
}
  
Shopify.onCartUpdate = function(cart) {
  if (!$('cart').visible()) {
    Showroom.showCart();      
  }
  
  $('cart-total').innerHTML = Shopify.formatMoney(cart.total_price);
  $('cart-quantity').innerHTML = cart.item_count;

  $A($('cart-items').childNodes).each(function(node) { $('cart-items').removeChild(node); });
  
  if(cart.items.length > 0) {
    
    cart.items.each(function(item) {
    
      itemDiv           = document.createElement('div');
    	itemDiv.className = 'cart-item';
    	itemDiv.id        = 'item-'+item.variant_id;

    	itemDiv.innerHTML = '<div class=\"cart-item-image\">'+
                       '<a href=\"'+item.url+'\" onclick=\"return Showroom.showProduct(\''+ item.handle +'\');\"><img src=\"' + Shopify.resizeImage(item.image, 'thumb') + '\" alt=\"' + item.title + '\" /></a>' +
    						       '</div>' +
     						       item.quantity + 'x<br /><small><a href=\"'+item.url+'\" onclick=\"return Showroom.showProduct(\''+ item.handle +'\');\">' + item.title.truncate(18) + '</a><br />' +
     						       '<span class="dark-link"><a href=\"/cart/change/'+item.variant_id+'?quantity=0\" onclick=\"Shopify.removeItem(' + item.variant_id + ');return false\">remove</a></span></small>';      
    
      $('cart-items').appendChild(itemDiv);
    });
  
  } else {
    Showroom.hideCart();
  }
}  
  
Shopify.onItemAdded = function(line_item) { 
  Showroom.closeLightbox();

  Shopify.getCart(function(cart){
    Shopify.onCartUpdate(cart);
    
    if($('item-'+line_item.variant_id)) {
      new Effect.Pulsate($('item-' + line_item.variant_id).firstChild, { duration: 1.0, pulses: 3 });    
    }

  });
}

Shopify.onProduct = function(product) {
  
  var content = $("product-lightbox");

  $('product-lightbox-image').update('<img src="'+Shopify.resizeImage(product.featured_image, 'large')+'" alt="" />');
  $('product-lightbox-title').update(product.title);
  $('product-lightbox-body').update(product.description);

  $('product-lightbox-image-previews').update('');
  
  product.images.each(function(image) {
    previewDiv = document.createElement('div');
    
    link = document.createElement('a');
    link.href = '#';
    link.onclick = function(t) { $('product-lightbox-image').firstChild.src = Shopify.resizeImage(image, 'large'); return false; };
  	
  	img = document.createElement('img');
  	img.src = Shopify.resizeImage(image, 'thumb');
  	img.alt = '';
  	
  	link.appendChild(img);
  	previewDiv.appendChild(link);
  	$('product-lightbox-image-previews').appendChild(previewDiv);          
  });

  $('variant-id').update('');
  
  product.variants.each(function(variant) {
    option           = document.createElement('option');
    option.value     = variant.id;
    option.title     = variant.available ? Shopify.formatMoney(variant.price) : "sold out!";
    option.innerHTML = variant.title;
    $('variant-id').appendChild(option);
  });
  
  if (product.available) {
    $('product-lightbox-price').update(($('variant-id').options[$('variant-id').selectedIndex]).title)
    $('lightbox-submit').disabled = false;
  } else {
    $('product-lightbox-price').update("sold out!");
    $('lightbox-submit').disabled = true;
  }
  
  content.show();
}

var Showroom = { 

  toggleCart: function() {
    if (!$('cart').visible()) {
      this.showCart();
    } else {
      this.hideCart();
    }
  },
  
  hideCart: function() {    
    new Effect.BlindUp($('cart'));
    $('page').removeClassName('show-cart');
  },
  
  showCart: function() {
    new Effect.BlindDown($('cart'));
    $('page').addClassName('show-cart');
  },

  showProduct: function(product) {
    $('lightbox-overlay').show();
    Shopify.getProduct(product);
    return false;
  },
  
  closeLightbox: function() {
    $("product-lightbox").hide();
    $('lightbox-overlay').hide();
  },
  
  onVariantChange: function(select) {
    $('product-lightbox-price').update((select.options[select.selectedIndex]).title);
  }
}
