function MultiProductSelector(jsName, domPrefix, products, activationHandler, deactivationHandler)
{
  this.jsName = jsName;
  this.domPrefix = domPrefix;
  this.products = products;
  this.activationHandler = activationHandler;
  this.deactivationHandler = deactivationHandler;
  this.status = 0;
}

MultiProductSelector.prototype.getSubcatByRRID = function(rrid, subcatId)
{
  for (var i = 0; i < this.products.length; i++)
  {
    var p = this.products[i];
    if (p.isSubcat && (p.resultRuleId == rrid))
    {
      if (p.getSubcatId() == subcatId)
        return p;
    }
  }
  return null;
}

MultiProductSelector.prototype.getSubcatDataByRRID = function(rrid, subcatId)
{
  var p = this.getSubcatByRRID(rrid, subcatId);
  if (p == null)
    return null;
  return p.subcatData;
}

MultiProductSelector.prototype.hasProducts = function()
{
  return (this.products.length > 0);
}

MultiProductSelector.prototype.updateButton = function()
{
  var divName = this.domPrefix + 'ButtonDiv';
  var d = document.getElementById(divName);
  if (!d)
    return;
  if (this.hasProducts())
  {
    var t = '<a href="javascript:' + this.jsName + '.toggleSelector()" border=0><img src="/images/pp/freeitem-on.gif" onMouseOver="swapImageOver(this)" onMouseOut="swapImageOut(this)" onLoad="preloadSwapImage(this)" overImage="/images/pp/freeitem-rollover.gif" width=100 height=20 border=0></a>';
    d.innerHTML = t;
  }
  else
  {
    d.innerHTML = '';
    this.hideWindow();
  }
  return;
}

MultiProductSelector.prototype.toggleSelector = function()
{
  switch (this.status)
  {
  case 0:
    if (!this.hasProducts())
      break;
    if (this.needSelectorWindow())
    {
      this.displaySelectorWindow();
      this.status = 1;
    }
    else
    {
      this.displaySubcat(this.products[0].resultRuleId, this.products[0].getSubcatId());
      this.status = 2;
    }
    break;
    
  case 1:
    this.hideWindow();
    this.status = 0;
    break;
  
  case 2:
    this.hideWindow();
    this.status = 0;
    break;
  }
  return;
}

MultiProductSelector.prototype.goBackToSelector = function()
{
  switch (this.status)
  {
  case 0:
    if (!this.hasProducts())
      break;
    if (this.needSelectorWindow())
    {
      this.displaySelectorWindow();
      this.status = 1;
    }
    break;
    
  case 1:
  case 2:
    if (!this.hasProducts() || !this.needSelectorWindow())
    {
      this.hideWindow();
      this.status = 0;
      break;
    }
    this.displaySelectorWindow();
    this.status = 1;
    break;
  }
  return;
}

MultiProductSelector.prototype.needSelectorWindow = function()
{
  if (!this.hasProducts())
    return false;
  if (this.products.length > 1)
    return true;
  return !this.products[0].isSubcat;
}

MultiProductSelector.prototype.getDiv = function()
{
  return document.getElementById(this.domPrefix + 'MainDiv');
}

MultiProductSelector.prototype.setDivContents = function(contents)
{
  var d = this.getDiv();
  if (!d)
    return;
  var wasHidden = (d.style.visibility == 'hidden');
  if (contents == null)
  {
    d.style.visibility = 'hidden';
    d.innerHTML = '';
    if (!wasHidden && (this.deactivationHandler != null))
      this.deactivationHandler(this);
  }
  else
  {
    if (wasHidden && (this.activationHandler != null))
      this.activationHandler(this);
    d.innerHTML = contents;
    d.style.visibility = 'inherit';
  }
  return;
}

MultiProductSelector.prototype.hideWindow = function()
{
  this.setDivContents(null);
  return;
}

MultiProductSelector.prototype.wrapWindowContents = function(windowContents)
{
  var t = '<div style="border: 1px solid black; background-color: white; color: black; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8pt; padding: 5px">\n'
        + windowContents
        + '</div>';
  return t;
}

MultiProductSelector.prototype.displaySelectorWindow = function()
{
  var d = this.getDiv();
  if (!d)
    return;
  var t = '<table border=0 cellspacing=0 cellpadding=3 style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8pt">\n'
        + '  <tr>\n'
        + '    <td'
  if (this.products.length > 1)
  {
    var colspan = this.products.length;
    if (colspan > 3)
      colspan = 3;
    t += ' colspan=' + colspan;
  }
  t += '>\n'
     + '      <div style="background-color: #C6C6C6; padding: 3px; font-size: 14pt">\n'
     + '        <span style="clear:both; float:right"><a href="javascript:' + this.jsName + '.toggleSelector()" style="color: white; text-decoration: none; text-decoration: underline">close</a></span>\n'
     + '        <span style="width:100%"><b>SELECT PROMO ITEM</b></span>\n'
     + '      </div>\n'
     + '    </td>\n'
     + '  </tr>\n';
  for (var i = 0; i < this.products.length; i++)
  {
    if ((i % 3) == 0)
      t += '  <tr>\n';
    var p = this.products[i];
    t += '    <td valign="top" align="center" width=250>\n';
    if (p.isSubcat)
    {
      t += '      <a href="javascript:' + this.jsName + '.displaySubcat(' + p.resultRuleId + ', ' + p.getSubcatId() + ')"><img src="' + p.thumbImageUrl + '" width=135 height=135 border=0><p>\n'
         + '      ' + p.subcatData.subcatName + ' (click to select)</a>\n';
    }
    else
    {
      t += '      <a href="javascript:' + this.jsName + '.singleProductAddToCart(' + p.resultRuleId + ', ' + p.productId + ', ' + p.getSubcatId() + ')"><img src="' + p.thumbImageUrl + '" width=135 height=135 border=0><p>\n'
         + '      ' + p.subcatName + ' - ' + p.merchPN + ' (click to add to cart)</a>\n';
    }
    t += '    </td>\n';
    if (((i + 1) % 3) == 0)
      t += '  </tr>\n';
  }
  if (this.products.length > 3)
  {
    var lastColIndex = this.products.length % 3;
    if (lastColIndex != 0)
    {
      var colspan = 3 - lastColIndex;
      t += '    <td';
      if (colspan > 1)
        t += ' colspan=' + colspan;
      t += '></td>\n';
         + '  </tr>\n';
    }
  }
  t += '</table>\n';
  this.setDivContents(this.wrapWindowContents(t));
  return;
}

MultiProductSelector.prototype.displaySubcat = function(rrid, subcatId)
{
  var d = this.getDiv();
  if (!d)
    return;
  var p = this.getSubcatByRRID(rrid, subcatId);
  if (p == null)
  {
    this.goBackToSelector();
    return;
  }
  var t = '<table border=0 cellspacing=0 cellpadding=0 style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 7pt">\n'
        + '  <tr>\n'
        + '    <td colspan=2>\n'
        + '      <div style="background-color: #C6C6C6; padding: 3px; font-size: 9pt">\n'
        + '        <span style="clear:both; float:right"><a href="javascript:' + this.jsName + '.goBackToSelector()" style="color: white; text-decoration: none">close <img src="/images/floatCart/float-cart-close.gif" width=9 height=9 border=0></a></span>\n'
        + '        <span style="width:100%"><b>';
  if (p.subcatData.secProdAltTitle.length > 0)
    t += p.subcatData.secProdAltTitle;
  else
    t += p.subcatData.subcatName;
  t += '</b>';
  if (p.subcatData.secProdExtTitle.length > 0)
    t += '<br>\n<font color="red"><b>' + p.subcatData.secProdExtTitle + '</b></font>';
  t += '</span>\n'
        + '      </div>\n'
        + '    </td>\n'
        + '  </tr>\n'
        + '  <tr>\n'
        + '    <td valign="top" width=230>\n'
        + '      <table border=0 cellspacing=0 cellspacing=2 cellpadding=0 class="abouttext">\n';
  if (p.selectorHtml.length > 0)
    t += '        <tr>\n'
       + '          <td valign="top" width=225>\n'
       + p.selectorHtml
       + '          </td>\n'
       + '        </tr>\n';
  t += '        <tr>\n'
     + '          <td valign="top" width=225>\n'
     + p.imageCellContent
     + '          </td>\n'
     + '        </tr>\n'
     + '        <tr>\n'
     + '          <td valign="top" width=225>\n';
  if (!p.subcatData.swatchImage.isEmptyImage())
    t += '            <div id="' + p.subcatData.getDomName('Swatch') + '" align="center" style="margin-top:4px"></div>\n';
  t += '            <table border=0 cellspacing=0 cellpadding=5 class="abouttext">\n'
     + '              <tr>\n'
     + p.buttonRowContent
     + '              </tr>\n'
     + '            </table>\n';
     + '          </td>\n'
     + '        </tr>\n';
  if (p.personalizationProfileArgs != null)
  {
    t += '        <tr>\n'
       + '          <td valign="top" width=225>\n';
       + '            <div id="' + p.subcatData.getDomName('PersonalizationFlashDiv') + '"></div>\n'
       + '          </td>\n'
       + '        </tr>\n';
  }
  t += '      </table>\n'
     + '    </td>\n'
     + '    <td valign="top">\n'
     + p.selectorContent;
     + '    </td>\n'
     + '  </tr>\n'
     + '</table>';
  this.setDivContents(this.wrapWindowContents(t));
  this.status = 2;
  if (p.subcatData.mainImageIsFlash)
    p.subcatData.createFlashMainImage();
  if (p.subcatData.personalizationProfile != null)
  {
    p.subcatData.personalizationProfile.createFlashObject();
    p.subcatData.personalizationProfile.display();
  }
  p.subcatData.initDisplay();
  return;
}

MultiProductSelector.prototype.singleProductAddToCart = function(rrid, productId, subcatId)
{
  floatCart.addToCart(productId, 1, false, null, subcatId);
  return;
}

function SubcatProductData(resultRuleId, ecouponName, ecouponDscr, selectorHtml, subcatData, thumbImageUrl, productTourInfoArgs, personalizationProfileArgs, imageCellContent, buttonRowContent, selectorContent)
{
  this.resultRuleId = resultRuleId;
  this.ecouponNamme = ecouponName;
  this.ecouponDscr = ecouponDscr;
  this.selectorHtml = selectorHtml;
  this.subcatData = subcatData;
  this.thumbImageUrl = thumbImageUrl;
  this.productTourInfoArgs = productTourInfoArgs;
  this.personalizationProfileArgs = personalizationProfileArgs;
  this.imageCellContent = imageCellContent;
  this.buttonRowContent = buttonRowContent;
  this.selectorContent = selectorContent;
  this.isSubcat = true;

  if (productTourInfoArgs != null)
    subcatData.setProductTourInfo.apply(subcatData, productTourInfoArgs);
  if (personalizationProfileArgs != null)
    subcatData.setPersonalizationProfile.apply(subcatData, personalizationProfileArgs);
}

SubcatProductData.prototype.getSubcatId = function()
{
  return this.subcatData.subcatId;
}

function SingleProductData(resultRuleId, ecouponName, ecouponDscr, selectorHtml, subcatId, productId, merchPN, subcatName, priceCents, srpCents, stat, availMsg, noStock, thumbImageUrl)
{
  this.resultRuleId = resultRuleId;
  this.ecouponName = ecouponName;
  this.ecouponDscr = ecouponDscr;
  this.selectorHtml = selectorHtml;
  this.subcatId = subcatId;
  this.productId = productId;
  this.merchPN = merchPN;
  this.subcatName = subcatName;
  this.priceCents = priceCents;
  this.srpCents = srpCents;
  this.stat = stat;
  this.availMsg = availMsg;
  this.noStock = noStock;
  this.thumbImageUrl = thumbImageUrl;
  this.isSubcat = false;
}

SingleProductData.prototype.getSubcatId = function()
{
  return this.subcatId;
}
