/*
 * 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.
 */

/**
 * Gives a warning if the person navigates from the page.
 */
function confirmExit() {
  if (!confirm('The back button will take you away from shopvale.com, are you sure you want to leave our site?')) {
    return false;
  }
}
function guardFromHittingBack() {
  window.onbeforeunload = confirmExit;
}

/**
 * Returns all the possible type id; created in:
 *
 *   createProducts.php
 */
function getTypes() {
	return TYPES;
}


/*
 * This page is where we create all the content.
 */

function createId(type,i) {
	var n = '_' + type + '_' + i;
  return n;
}

function createPaneId(type,i) {
  return "pane" + createId(type,i);
}

function createImgId(type,index) {
  return "collectionImg" + "_" + type + "_" + index;
}

function createCollectionId(type,i) {
  return "collectionHref" + createId(type,i);
}

/**
 * type  : String type of this collection -- e.g. necklaces2
 * i     : product id (great name!)
 * index : number between 0 and 8, inclusive of the index in this collection and type
 */
function createProductPaneCol(type,i,index) {

	document.write('<td id="' + createPaneId(type,i) + '">');
	link = "javascript:showProduct(" + i + ");";	
	document.write('<a id="' + createCollectionId(type,i) + '" href="' + link + '" ');
	var img = getImageThumb(i);
	document.write('border=0><img style="display:hidden" border="0" id="' + 
                 createImgId(type,index) + '" class="collectionitem" ');
  if (PRELOAD_IMAGES) {
    document.write(' src="' + img + '"');
  }
  //document.write(' alt="'+img+'"');
  var name = getProduct(i).getName();
  document.write(' alt="' + name.replace('"', '\"') + '"');
  document.write(' title="' + name.replace('"', '\"') + '"');
  document.write('></a>');
	document.write('<br/>');
	document.write('</td>');
	document.write("\n");
}

function createProductPaneRow(type,start,stop,arr) {
	if (start>arr.length) return; // todo: probably don't need this
	document.write("<tr>\n");
	for (var i=start; i<=stop && i<arr.length && arr[i]; i++) {
		createProductPaneCol(type,arr[i],i);
	}
	document.write("</tr>\n");
}

function createProductPaneStart(type) {
	document.write('<!-- table for ' + type + ' -->');
	document.write("\n");
	document.write('<div writingMode="tb-rl" style="display:none" class="productsPane" id="productsPane_' + type + '">');
}

function createProductPaneEnd($type) {
	document.write('</div>');
}

function createProductPane(type,arr) {
	createProductPaneStart(type);
	document.write('<table border="0" cellpadding="1" cellspacing="1">');

	createProductPaneRow(type,0,2,arr);
	createProductPaneRow(type,3,5,arr);
	createProductPaneRow(type,6,8,arr);

	document.write('</table>');
	createProductPaneEnd(type);
	document.write("\n");
}

function createProductPanes(type) {
	var arr = getProductIDs(type);
	var N = getNumSections(type);
	for (var i=0; i<N; i++) {
		var page = i+1;
		var newArr = getProductIDs(type,page);
		var newType = type;
		newType += page;
		createProductPane(newType,newArr);
	}
}

function createContent() {
	//
	// this is the box in the top right for 'page 1 2 ... N'
	// it's currently filled in hard-coded in javascript
	// search for @pageHack in effects.js
	//
	document.write('<div id="pagesBox" class="pagesBox"></div>');
	//
	// create all the panes
	//
	for (var type in TYPES2IDS) {
		createProductPanes(type);
	}
	if (false) {
		createProductPanes('necklaces');
		createProductPanes('earrings');
		createProductPanes('cufflinks');
		createProductPanes('macaroon');
		createProductPanes(PREVIEW_ID);
		createProductPanes(UNDER100_ID);
	}
	// 
	// bracelets
	//
	createProductPaneStart('bracelets');
	document.write('coming soon...');
	createProductPaneEnd('bracelets');
	document.write("\n");
}



createContent();

