var BBFWHCart = new Object();
BBFWHCart.parentId = 'fwh_view_cart';
BBFWHCart.childId = 'fwh_cart_display';
BBFWHCart.childContentId = 'fwh_cart_content';
BBFWHCart.childLargerCartId = 'fwh_cart_larger';
BBFWHCart.childEmptyCartId = 'fwh_cart_empty';

BBFWHCart.showerAId = 'fwh_cart_shower';
BBFWHCart.hiderAId = 'fwh_cart_hider';

BBFWHCart.cartQtySpanId = 'cart_summary_unit_count';
BBFWHCart.cartGrandTotalSpanId = 'cart_summary_grand_total';

BBFWHCart.state = 'idle';
BBFWHCart.updateInterval = 48;
BBFWHCart.updateTimeoutId = null;

BBFWHCart.moveByY = 32;
BBFWHCart.childTgtHeight = 200;
BBFWHCart.childMaxHeight = 200;
BBFWHCart.childPaddingBottom = 48;
BBFWHCart.childCurrentHeight = 0;

BBFWHCart.roller = null;

BBFWHCart.cartModStr = null;

BBFWHCart.getCartORSC = function(req)
{
  if (req.readyState == 4)
  {
    BBFWHCart.refreshCart(req);
    BBFWHCart.state = "cart_retrieved";
    BBFWHCart.update();
  }
}

BBFWHCart.refreshCartORSC = function(req)
{
  if (req.readyState == 4)
  {
    BBFWHCart.refreshCart(req);
    BBFWHCart.state = "open";
  }
}

BBFWHCart.refreshCart = function(req)
{
  var itemCount, rgs, el;
  document.getElementById(BBFWHCart.childContentId).innerHTML = req.responseText;

  rgs = req.responseText.match(/%unit_count:([0-9]+)%/);
  if (rgs)
  {
    if (el = document.getElementById(BBFWHCart.cartQtySpanId))
    {
      el.innerHTML = rgs[1];
    }
    if (parseInt(rgs[1]) > 0)
    {
      document.getElementById(BBFWHCart.childLargerCartId).style.display = "block";
      document.getElementById(BBFWHCart.childEmptyCartId).style.display = "none";
    }
    else
    {
      document.getElementById(BBFWHCart.childLargerCartId).style.display = "none";
      document.getElementById(BBFWHCart.childEmptyCartId).style.display = "block";
    } 
  }

  rgs = req.responseText.match(/%grand_total:([0-9\.]+)%/);
  if (rgs)
  {
    if (el = document.getElementById(BBFWHCart.cartGrandTotalSpanId))
    {
      el.innerHTML = rgs[1];
    }
  }
}

BBFWHCart.timeout = function()
{
	if (BBFWHCart.state == "open")
	{
		BBFWHCart.onChildClose();
	}
}

BBFWHCart.update = function()
{
  var xy, parentEl, childEl, childContentEl, r, h;
  switch (BBFWHCart.state)
  {
    case "idle":
      return;
      break;

    case "open_clicked":
      h = new Object();
      h.onreadystatechange = BBFWHCart.getCartORSC;
      r = new XHReq(h);
      if (BBFWHCart.cartModStr)
      {
        r.url = "cart-ajax.php?mod_cart="+BBFWHCart.cartModStr;
        BBFWHCart.cartModStr = null;
      }
      else
      {
        r.url = "cart-ajax.php";
      }
      r.request();
      return;
      break;
    case "cart_retrieved":
      parentEl = document.getElementById(BBFWHCart.parentId);
      childEl = document.getElementById(BBFWHCart.childId);
      childContentEl = document.getElementById(BBFWHCart.childContentId);
      xy = BBUtil.getXYOffset(parentEl);
      xy["y"] = xy["y"] + parentEl.offsetHeight;
/* Haxxor - hack - not needed anymore
			alert(xy["x"]+","+xy["y"]);
      xy["x"] = parentEl.offsetLeft - parentEl.offsetWidth - 24;
      xy["y"] = parentEl.offsetTop + parentEl.offsetHeight + 5;
Haxxor - hack */      
      
      childEl.style.display = "block";
      childEl.style.left = "-1000px";
      childEl.style.top = "-1000px";
      childContentEl.style.height = "auto";
      childContentEl.style.overflow = "hidden";
      if (childContentEl.offsetHeight > BBFWHCart.childMaxHeight)
      {
        childContentEl.style.height = BBFWHCart.childMaxHeight+"px";
      }
      BBFWHCart.childTgtHeight = childContentEl.offsetHeight + BBFWHCart.childPaddingBottom;

      childEl.style.left = xy["x"]+"px";
      childEl.style.top = xy["y"]+"px";

      childEl.style.height = "0px";
      BBFWHCart.childCurrentHeight = 0;
      
      BBFWHCart.state = "opening";
      break;

    case "opening":
      BBFWHCart.childCurrentHeight += BBFWHCart.moveByY;
      childEl = document.getElementById(BBFWHCart.childId);
      if (BBFWHCart.childCurrentHeight >= BBFWHCart.childTgtHeight)
      {
        BBFWHCart.childCurrentHeight = BBFWHCart.childTgtHeight;
        childEl.style.height = BBFWHCart.childCurrentHeight+"px";
        BBFWHCart.state = "open";
        childContentEl = document.getElementById(BBFWHCart.childContentId);
        childContentEl.style.overflow = "auto";
        childContentEl.style.borderTop = "1px solid transparent";
        setTimeout("BBFWHCart.timeout();", 4500);
        return;
      }
      else
      {
        childEl.style.height = BBFWHCart.childCurrentHeight+"px";
      }
      break;

    case "close_clicked":
      document.getElementById(BBFWHCart.childContentId).style.overflow = "hidden";
      BBFWHCart.state = "closing";
    case "closing":
      childEl = document.getElementById(BBFWHCart.childId);
      BBFWHCart.childCurrentHeight -= BBFWHCart.moveByY;
      if (BBFWHCart.childCurrentHeight <= 0)
      {
        childEl.style.height = "0px";
        childEl.style.display = "none";
        BBFWHCart.state = "idle";
        return;
      }
      else
      {
        childEl.style.height = BBFWHCart.childCurrentHeight+"px";
      }
      break;
  }
  BBFWHCart.updateTimeoutId = setTimeout("BBFWHCart.update();", BBFWHCart.updateInterval); 
}

BBFWHCart.onCartClick = function(itemQty, itemId, action)
{
  var h = new Object();
  itemQty = parseInt(itemQty);
  switch (action)
  {
    case 'inc':
      itemQty = parseInt(itemQty) + 1;
      break;
    case 'dec':
      itemQty = parseInt(itemQty) - 1;
      if (itemQty < 0)
      {
        itemQty = 0;
      }
      break;
    case 'del':
      itemQty = 0;
      break;
  }
  h.onreadystatechange = BBFWHCart.refreshCartORSC;
  r = new XHReq(h);
  r.url = "cart-ajax.php?mod_cart="+itemId+":"+itemQty+";";
  r.request();
}

BBFWHCart.buyItClick = function(itemGroupId, itemId)
{
  BBFWHCart.cartModStr =
    itemId+":"+
    document.getElementById("item_group_"+itemGroupId+"_cart_qty").value+";";
  BBFWHCart.onParentClick();
}

BBFWHCart.onParentClick = function()
{
  document.getElementById(BBFWHCart.showerAId).style.display = "none";
  document.getElementById(BBFWHCart.hiderAId).style.display = "inline";

  if (BBFWHCart.updateTimeoutId)
  {
    clearTimeout(BBFWHCart.updateTimeoutId);
  }

  BBUtil.closeAllDHTMLPops("BBFWHCart");

  BBFWHCart.state = "open_clicked";
  BBFWHCart.update();
}

BBFWHCart.onChildClose = function()
{
  var childEl = document.getElementById(BBFWHCart.childId);

  if (BBFWHCart.state != "open")
  {
    return;
  }

  document.getElementById(BBFWHCart.showerAId).style.display = "inline";
  document.getElementById(BBFWHCart.hiderAId).style.display = "none";

  if (BBFWHCart.updateTimeoutId)
  {
    clearTimeout(BBFWHCart.updateTimeoutId);
  }

  BBFWHCart.state = "close_clicked";
  BBFWHCart.update();
}
