/*
 * Copyright 2006, Jeffrey Palm.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/** Number of products in a section */
var SECTION_SIZE = 9;
var IDS2PRODUCTS = new Array();

for (var i=0; i<PRODUCTS.length; i++) {
  var p = PRODUCTS[i];
  //
  // Deleted products will show up here
  //
  if (!p) continue;
  IDS2PRODUCTS[p.getId()] = p;
 }


// set the max product id
var MAX_PRODUCT_ID;

/**
 * Returns the highest numbered product.
 */
function getMaxProductID() {
  if (MAX_PRODUCT_ID == undefined) {
    MAX_PRODUCT_ID=-1;
    for (type in TYPES2IDS) {
      var ids = TYPES2IDS[type];
      for (var i=0; i<ids.length; i++) {
        if (ids[i]>MAX_PRODUCT_ID) MAX_PRODUCT_ID=ids[i];
      }
    }
  }
  return MAX_PRODUCT_ID;
}

/**
 * Returns true if id in in TYPES2IDS
 */
function isOnDisplay(id) {
  for (type in TYPES2IDS) {
    var ids = TYPES2IDS[type];
    for (var i=0; i<ids.length; i++) {
      if (ids[i] == id) return true;
    }
  }
  return false;
}

function getTypeFromProductID(id) {
  var typeAndSection = getTypeAndSectionFromProductID(id);
  return typeAndSection ? typeAndSection[0] : 0;
}

function getSectionFromProductID(id) {
  var typeAndSection = getTypeAndSectionFromProductID(id);
  return typeAndSection ? typeAndSection[1] : 0;
}

function getTypeAndSectionFromProductID(id,theType,theSection) {
  if (theType && theSection) {
    return new Array(theType,theSection);
  }
  function f(type) {
    var ids = TYPES2IDS[type];
    for (var i=0; i<ids.length; i++) {
      if (ids[i] == id) {
        var section = Math.round((i/SECTION_SIZE)+.5);
        return new Array(type,section);
      }
    }
    return 0;
  }
  //
  // see [under100-note]
  //
  var res = null;
  if (theType) res = f(theType);
  if (currentCollectionType == PREVIEW_ID) {
    res = f(PREVIEW_ID);
  } else if (currentCollectionType == UNDER100_ID) {
    res = f(UNDER100_ID);
  }
  if (res) return res;
  for (type in TYPES2IDS) {
    res = f(type);
    if (res) return res;
  }
  return 0;
}

/**
 * String[typeID] -> String[type name]
 */
function getProductTypeNameFromType(type) {
  //
  // [under100-note]
  // We only do the special thing for the preview node, if we're
  // coming from the preview node. This is a semi-hack due to the fact
  // that, with the introduction of the under 100 listing we can have
  // products in *three* categories, not just two.
  //
  if (currentCollectionType == PREVIEW_ID) {
    //
    // For the preview node
    //
    if (type == PREVIEW_ID) {
      return PREVIEW_TITLE;
    }
  } else {
    if (type == UNDER100_ID) {
      return UNDER100_TITLE;
    }
  }
  var name = type.replace(/\d+/,'');
  //
  // hack to replace 'Preview' with ' preview'
  //
  var newName = name.replace(/P/, ' p');

  //HACK: (again!)
  if (newName == 'cufflinks') {
    newName = 'rings, bracelets & cufflinks';
  }
  
  return newName
    }

/**
 * Returns the number of products.
 *
 * @return the number of products.
 */
function getNumProducts(type) {
  var lst = getProductIDs(type);
  if (!lst) return 0;
  return lst.length;
}

/**
 * Returns the array of products ids for this <code>type</code>
 * and section <code>n</code>.
 *
 * @param type String type of the product
 * @param n    section 1..getNumProducts(type)/SECTION_SIZE-1 or -1 for all
 */
function getProductIDs(type,n) {
  var ids = TYPES2IDS[type];
  if (n) { //n != undefined) {
    var start = (n-1)*SECTION_SIZE;
    var res = new Array();
    for (var i=0; i<SECTION_SIZE; i++) {
      var j =i+start;
      if (!ids[j]) break;
      res[i] = ids[j];
    }
    return res;
  } else {
    return ids;
  }
}

function getNumSections(type) {
  var f = Math.floor(getNumProducts(type)/SECTION_SIZE);
  if (f == getNumProducts(type)/SECTION_SIZE) return f;
  return f+1;
}

/**
 * Returns the n<sup>th</sup> product.
 *
 * @return the n<sup>th</sup> product.
 */
function getProduct(n) {
  return IDS2PRODUCTS[n];
}

