/* @file: shoppingCart.js
 * @description: shopping cart functionality
 * @dependencies: moo.ajax, listings module, custom css
*/

/*
 * @name  removeMsg
 * @description  remove the 'product added' message
 * @author  Michiel van der Blonk
 * @date  May 12, 2006
*/
function removeMsg()
{
//  Element.remove('productAdded');
    var pa = $('productAdded');
    pa.dispose();
}

/*
 * @name  updateSummary
 * @description  update the shopping cart items on page
 * @author  Michiel van der Blonk
 * @date  May 12, 2006
*/
function updateSummary(el) {
  var sc = $('shoppingCart');
  // create a new paragraph
  msg = document.createElement("p");
  msg.setAttribute('id', 'productAdded');
//  $('modules').appendChild(msg);
  el.appendChild(msg);
  // if there has been a change, the product is added
  // so we check the contents of the result with the cart summary
//  sc.innerHTML = request.responseText;
  if ($('cartMessage'))
  {
    msgText = document.createTextNode($('cartMessage').value);
    msg.appendChild(msgText);
  }
  else
  {
    msgText = document.createTextNode("product added");
    msg.appendChild(msgText);
  }
  sc.className = "active tooltip";

  setTimeout("removeMsg()", 3000);
}


/*
 * @name  changeQty
 * @description  inc or dec the quantity field for a product
 * @author  Michiel van der Blonk
 * @date  May 12, 2006
*/
function changeQty()
{
  delta = 1;
  if (isNaN($(this.rel).value))
    return false;
  if (this.className=='minus')
    delta = -1;
  $(this.rel).value = +$(this.rel).value + delta;
  // check minimum value
  if ($(this.rel).value<1)
    $(this.rel).value = 1;
  return false;
}

/*
 * @name  onLoadActions
 * @description  initiate shopping cart effects
 * @author  Michiel van der Blonk
 * @date  May 12, 2006
*/
function onLoadActions()
{

  $$('#contentPane .field_addCart input').each(function(element){
    element.onclick = function(){
      now = new Date();
      url = eval(this.alt) + '&inline=true' + '&time=' + now.getTime();
      var parent = this.parentNode;
      $('shoppingCart').className = "active updating";
	var req = new Request({
	  method: 'get',
	  url: url,
	  onComplete: function(response) 
	  {$('shoppingCart').innerHTML=response;updateSummary(parent);}
	});
	req.send();
      return false;
    };
  });

  $$('#contentPane td.field_quantity a').each(function(element){
    element.onclick = changeQty;
  });
}

