//=======================================================
//===        Copyright 2008 by Shoreline Sports       ===
//===              All rights reserved.               ===
//=======================================================

//=======================================================
//=== LAYOUT VARIABLES
//=======================================================
var nPageCols      = 3;	
var nPageRows      = 8;	
var nCurrentKey    = -1;
var nCurrentOrder  = -1;
var nCurrentPage   = -1;
var sCurrentFilter = "imenu_opts_0";
var nLastPage      = 0;
var nSortLT        = -1;
var nSortGT        = 1;
var nSortElement   = 1;
var nItemsPage     = 0;
var bPageText      = false;
var aShow;

//    0             1               2   3      4           5             6   7       8
// [3432,"Arbor A-Frame Snowboard",85,598.95,598.95,"AB9-100904TAN.jpg",140,196];   "%"

//=======================================================
//=== RENDER VIEW 
//=======================================================
function renderView( nPage, nKey, nOrder, sFilter, bNoAdd ) { var sHTML;
  if (document.forms.navi.loadStatus.value=="") { return false; }
  nPage  = (nPage)  ? nPage  : nCurrentPage;
  nKey   = (nKey)   ? nKey   : nCurrentKey;
  nOrder = (nOrder) ? nOrder : nCurrentOrder;
  if (sFilter) { if (!bNoAdd) { nPage = 1; } }
  else { sFilter = sCurrentFilter; }
  if ((nCurrentKey!=nKey) || (nCurrentOrder!=nOrder)) {
    sortProducts( nKey, nOrder );
    sHTML = buildSortOptions();
    document.getElementById("navi_sort_top" ).innerHTML = sHTML; }
  if (sFilter!="imenu_opts_0" && sFilter!="") {
    var tst = parseInt(document.getElementById(sFilter).value);
    aShow = new Array();
    for (k in aPL) { if (aPL[k][2]==tst) { aShow[aShow.length]=aPL[k]; } } }
  else { aShow = aPL; }
  if (bPageText) { document.forms.navi.viewText.value='Pics';
    sHTML = buildTextLayout(nPage); } 
  else { document.forms.navi.viewText.value='Text';
    sHTML = buildLayout(nPage,nPageRows,nPageCols); }
  document.getElementById("prod_layout").innerHTML = sHTML;
  nCurrentPage   = nPage;
  nCurrentKey    = nKey;
  nCurrentOrder  = nOrder;
  sCurrentFilter = sFilter;
  if (!bNoAdd) { viewCreate(nCurrentPage,nCurrentKey,nCurrentOrder,sCurrentFilter); }
  sHTML = buildPageLinks();
  document.getElementById("navi_page_top" ).innerHTML = sHTML;
  document.getElementById("navi_page_bott").innerHTML = sHTML.replace(/return renderView/g, "scroll(0,0); return renderView");
  return false; }
//=======================================================

//=======================================================
//=== SORT PRODUCTS ARRAY
//=======================================================
function sortProducts( nKey, nOrder ) { 
  nCurrentKey   = nKey;
  nCurrentOrder = nOrder;
  nCurrentPage  = 0;
  nSortGT       = nOrder;
  nSortLT       = -1 * nOrder;
  var aKeys     = new Array(0,1,1,3,8);
  nSortElement  = aKeys[nKey];
 
  if (nKey==1) { 
    if (nOrder==1) { aPL.sort(sortInsensitiveUp2);   }
    else           { aPL.sort(sortInsensitiveDown2); } } 
  else if (nKey==2) { 
    if (nOrder==2) { aPL.sort(sortInsensitiveUp);   }
    else           { aPL.sort(sortInsensitiveDown); } } 
  else             { aPL.sort(sortByElement);       } }

//  if (aKeys[nKey]==1) { aPL.sort(sortInsensitive); }
//  else                { aPL.sort(sortByElement);   } }

function sortByElement(a,b) {
  if (a[nSortElement]<b[nSortElement]) return nSortLT;
  if (a[nSortElement]>b[nSortElement]) return nSortGT;
  return 0; }
function sortInsensitiveUp(a,b) {
  if (a[nSortElement].toLowerCase()<b[nSortElement].toLowerCase()) return nSortLT;
  if (a[nSortElement].toLowerCase()>b[nSortElement].toLowerCase()) return nSortGT;
  return 0; }
function sortInsensitiveDown(a,b) {
  if (a[nSortElement].toLowerCase()<b[nSortElement].toLowerCase()) return nSortLT;
  if (a[nSortElement].toLowerCase()>b[nSortElement].toLowerCase()) return nSortGT;
  return 0; }
function sortInsensitiveUp2(a,b) {
  if (a[9]+a[nSortElement].toLowerCase()<b[9]+b[nSortElement].toLowerCase()) return nSortLT;
  if (a[9]+a[nSortElement].toLowerCase()>b[9]+b[nSortElement].toLowerCase()) return nSortGT;
  return 0; }
function sortInsensitiveDown2(a,b) {
  if (a[10]+a[nSortElement].toLowerCase()<b[10]+b[nSortElement].toLowerCase()) return nSortLT;
  if (a[10]+a[nSortElement].toLowerCase()>b[10]+b[nSortElement].toLowerCase()) return nSortGT;
  return 0; }
//=======================================================

//=======================================================
//=== BUILD NAVIGATION CONTROLS
//=======================================================
function buildPageLinks() {
  var nPage;
  sPages = "Page:&nbsp;&nbsp;&nbsp;";
  if (nCurrentPage!=1) {
    nPage   = nCurrentPage - 1;
    sPages += "<span onmouseover='this.className=\"ctrl_over\"' onmouseout='this.className=\"ctrl_out\"' onclick='return ";
    sPages += "renderView(" + nPage + "," + nCurrentKey + "," + nCurrentOrder + ",false);'>PREV</span>&nbsp;"; }
  else { sPages += "<span style='color:gray;'>PREV</span>&nbsp;"; }
  for (i=1; i<=nLastPage; i++) {
    if (i==nCurrentPage) {sPages += "&nbsp;<span style='padding-left:2px; padding-right:2px; color:white; background:#202020;'>" + i + "</span>"; }
    else{ sPages += "&nbsp;<a href='home.php?cat=" + nCategoryID;
      if (i!=1) { sPages += "&page=" + i; }
      sPages += "' onclick='return renderView(" + i + "," + nCurrentKey + "," + nCurrentOrder + ",false); return false;'>" + i + "</a>";
      if ((i%15==0) && (i<nLastPage)) sPages += "<br />"; } }
  if (nCurrentPage!=nLastPage) { 
    nPage   = nCurrentPage + 1;
    sPages += "&nbsp;&nbsp;<span onmouseover='this.className=\"ctrl_over\"' onmouseout='this.className=\"ctrl_out\"' onclick='return ";
    sPages += "renderView(" + nPage + "," + nCurrentKey + "," + nCurrentOrder + ",false);'>NEXT</span>"; }
  else { sPages += "&nbsp;&nbsp;<span style='color:gray;'>NEXT</span>"; }
  sPages += "&nbsp;&nbsp;";
  return sPages; }
function buildSortOptions() {
  var aSorts  = new Array( "", "Catalog", "Title", "Price", "Discount" ), tmp;
  var aOrders = new Array( 0, 1, 1, 1, -1 );
  var sSort = "Sort:&nbsp;";
  for (i=1; i<aSorts.length; i++) {
    if (aOrders[i]!=0) {
      if (i==nCurrentKey) {
        tmp    = (nCurrentOrder==1) ? 'ctrlUp' : 'ctrlDown';
        sSort += "&nbsp;<span class='" + tmp + "_out' onmouseover='this.className=\"" + tmp + "_over\"' onmouseout='this.className=\"" + tmp + "_out\"'";
        sSort += " onclick='renderView(1," + i + ",";
        sSort += (nCurrentOrder==1) ? -1 : 1;
        sSort += ",false);'>" + aSorts[i] + "</span>&nbsp;"; }
      else {
        sSort += "&nbsp;<span onmouseover='this.className=\"ctrl_over\"' onmouseout='this.className=\"ctrl_out\"'";
        sSort += " onclick='renderView(1," + i + "," + aOrders[i] + ",false);'>";
        sSort += aSorts[i] + "</span>&nbsp;"; } } }
  return sSort + "&nbsp;&nbsp;"; }
//=======================================================

//=======================================================
//=== BUILD PAGE LAYOUT
//=======================================================
function buildTextLayout(nThisPage) {
  nItemsPage     = 100;
  var nPages     = Math.ceil(aShow.length/nItemsPage);
  nLastPage      = nPages;
  var nFirstItem = (nThisPage-1) * nItemsPage;
  var nLastItem  = nFirstItem + nItemsPage - 1;
  var nLastItem  = Math.min( nLastItem, aShow.length-1 );
  var sLayout    = "<table border='0' cellpadding='2' cellspacing='0' width='100%'>";
  sLayout       += "<tr><td align='left'>Description</td><td align='right'>Retail</td><td align='right'>Our Price</td><td align='right'>Save</td></tr>";
  var sBack      = 'prod_text_2';
  var sCell      = "";
  for (p=nFirstItem; p<=nLastItem; p+=1) {
    sBack = (sBack=='prod_text_2') ? 'prod_text_1' : 'prod_text_2';
    sCell  = "<tr width='100%' class='" + sBack + "'>";
    sCell += "<td width='295' align='left' class='" + sBack + "'>";
    sCell += "<a href='product.php?productid=" + aShow[p][0] + "' title='"+aShow[p][1]+" "+aShow[p][3]+"'>";
    sCell += aShow[p][1] + "</a></td>";
    sCell += "<td width='80' class='prod_text_low' align='right'>$" + buildPrice(aShow[p][4]) + "</td>";
    sCell += "<td width='80' align='right'>$" + buildPrice(aShow[p][3]) + "</td>";
    if (aShow[p][8]>0) { sCell += "<td width='40' class='prod_text_high' align='right'>" + aShow[p][8] + "%</td>"; } 
    else {               sCell += "<td width='40'>&nbsp;</td>"; }
    sCell += "</tr>";
    sLayout += sCell; }
  return sLayout + "</table>"; }
function buildLayout(nThisPage, nRowsHigh, nCellsWide) {
  nItemsPage     = nCellsWide * nRowsHigh;
  var nPages     = Math.ceil(aShow.length/nItemsPage);
  var nFirstItem = (nThisPage-1) * nItemsPage;
  var nLastItem  = nFirstItem + nItemsPage - 1;
  var nLastItem  = Math.min( nLastItem, aShow.length-1 );
  var sLayout    = "<table border='0' cellpadding='4' cellspacing='8' width='100%'>";
  var nCurRow    = 0;
      nLastPage  = nPages;
  for (p=nFirstItem; p<=nLastItem; p+=nCellsWide) {
    sLayout += buildRow(p, nCurRow, nCellsWide);
    nCurRow++;}
  return sLayout + "</table>"; }
function buildRow(nFirstCell, nThisRow, nCellsWide) { 
  var nCellWidth = Math.floor(100/nCellsWide);
  var nThisCol = 0;
  var sRow = "<tr class='product_row'>"
  for (i=0; i<nCellsWide; i++) {
    if (aShow[nFirstCell+i]) {
      sRow += buildCell(aShow[nFirstCell+i], nThisRow, nThisCol, nCellWidth);
      nThisCol++; } }
  while (nThisCol<nCellsWide) {
      sRow += "<td height='200' width='" + nCellWidth + "%' id='PC" + "_" + nThisRow + "_" + nThisCol + "' class='product_cell'>&nbsp;</td>";
      nThisCol++; }
  return sRow + "</tr>"; }
function buildCell(aItem, nThisRow, nThisCol, nCellWidth) {
  var sCell = '';
  sCell  = "<td height='200' width='" + nCellWidth + "%' id='PC_" + nThisRow + "_" + nThisCol;
  sCell += "' class='product_cell' onmouseover='this.className=\"product_over\";' onmouseout='this.className=\"product_cell\";' align='center'>";
  sCell += "<a href='product.php?productid=" + aItem[0] + "' title='"+aItem[1]+" $"+aItem[3]+"'>";
  sCell += "<img height='" +aItem[7]+ "' width='" +aItem[6]+ "' valign='top' src='images/T/";
  sCell += aItem[5]+"' alt='"+aItem[1]+" $"+aItem[3]+"' border='0'><br>"
  sCell += aItem[1]+"</a><br>";
  if (aItem[3]>0) {
    if (isNaN(aItem[8]) || (aItem[8]==0)) { sCell += "<b>$" + buildPrice(aItem[3]) + "</b></td>"; } 
    else { sCell += "$<s>" +buildPrice(aItem[4])+ "</s> <b>$" +buildPrice(aItem[3])+ "</b> " +aItem[8]+ "% Off</a></td>"; } }
  return sCell; }
function buildPrice(nPrice) { if (isNaN(nPrice)) { return "?"; }
  sTst = nPrice.toString(); nLoc = sTst.indexOf('.');
  if (nLoc<0) {	return sTst + '.00'; } 
  else { sTst += '00'; return sTst.substr(0,nLoc+3); } }
//=======================================================

//=======================================================
//=== VIEW HISTORY 
//=======================================================
function viewLoader() {
  if (typeof(aPL)=="object") { 
    for (i=0; i<aPL.length; i++) {
      aPL[i][3]  = (isNaN(aPL[i][3])) ? 0 : aPL[i][3];
      aPL[i][4]  = (isNaN(aPL[i][4])) ? 0 : aPL[i][4];
      aPL[i][8]  = ((aPL[i][3]<aPL[i][4]) && (aPL[i][4]>0)) ? Math.round((aPL[i][4]-aPL[i][3])*100/aPL[i][4]) : 0; 
      aPL[i][9]  = (aPL[i][8]==0) ? "a" : "b";
      aPL[i][10] = (aPL[i][8]==0) ? "b" : "a"; } }
  if (document.forms.navi) { 
    var tmp = document.forms.navi.loadStatus.value;
    if (tmp=="") {
      document.forms.navi.reset();
      if (document.forms.navi.navisele) document.forms.navi.navisele.blur();
      document.forms.navi.loadStatus.value="0"; }
    var aViewHistory = document.forms.navi.loadStatus.value.split(",");
    if (aViewHistory.length>1) {
      if (aViewHistory.length>parseInt(aViewHistory[0])) { viewGo(0); } 
      else { tmp=""; } }

    if (tmp=="") { 
      // viewCreate(1, 1, 1, sCurrentFilter);		// OLD Log only
      renderView( 1, 1, 1, sCurrentFilter, false );	// NEW Render & Log
      viewButtons("viewBack", "gray", true); } 

    document.getElementById("navi_wait").style.visibility = "hidden";
    if (document.forms.navi.navisele) { document.forms.navi.navisele.disabled = false; }
    setOpacity("navi_top_fade",99);
    document.body.focus(); } }
function viewGo(n) {
  var aViewHistory = document.forms.navi.loadStatus.value.split(",");
  if (aViewHistory.length>0) aViewHistory[0] = parseInt(aViewHistory[0]);
  if ( (aViewHistory[0]+n > 0) && aViewHistory[aViewHistory[0]+n] ) {
    aViewHistory[0] = aViewHistory[0] + n;
    if (aViewHistory[0]>aViewHistory.length-2) { viewButtons("viewFwd", "gray", true); }
    else { viewButtons("viewFwd", "black", false); }
    if (aViewHistory[0]>1) { viewButtons("viewBack", "black", false); }
    else { viewButtons("viewBack", "gray", true); }
    document.getElementById("viewFwd").blur();
    document.getElementById("viewBack").blur();
    var aH = aViewHistory[aViewHistory[0]].split(".");
    if (document.forms.navi.navisele) { document.getElementById(aH[3]).selected = true; }
    bPageText = (aH[4]=="0") ? false : true;
    renderView( parseInt(aH[0]), parseInt(aH[1]), parseInt(aH[2]), aH[3], true );
    document.forms.navi.loadStatus.value = aViewHistory.join(","); } }
function viewCreate(nPg,nKy,nOr,sFi) {
  viewButtons("viewBack", "black", false);
  viewButtons("viewFwd", "gray", true);
  tmp = (bPageText) ? "1" : "0";
  tmp = nPg+"."+nKy+"."+nOr+"."+sFi+"."+tmp;
  var aViewHistory = document.forms.navi.loadStatus.value.split(",");
  if (aViewHistory.length>0) aViewHistory[0] = parseInt(aViewHistory[0]);
  aViewHistory[0] = aViewHistory[0] + 1;
  aViewHistory[ aViewHistory[0] ] = tmp; 
  aViewHistory.length = aViewHistory[0] + 1; 
  document.forms.navi.loadStatus.value = aViewHistory.join(","); }
function viewButtons(sButtonId,sColor,bDisabled) {
  var o=document.getElementById(sButtonId)
  o.style.color = sColor;
  o.disabled    = bDisabled; }
//=======================================================


