// =-=-=-=-=-=-= initialization and globals

var prodix = 0; // global product table index
var prodmainix = -1; // main product table index
var prodcoord = false;
var prodnamed;
var primeprodsrc = prodfillsrc; // prime product image src for rollover restore


// initialize: determine selected product from ?query on URL tail
var selproduct = "";
var bakproduct = "";
var bakprodix = -1;
var urlqueryix = -1;
var urlqueryishash = false;


// generic URL query parser
function PageQuery(q) {
  if (q.length > 1) this.q = q.substring(1, q.length);
  else this.q = null;
  this.keyValuePairs = new Array();
  if (q) {
    for (var i=0; i < this.q.split("&").length; i++) {
      this.keyValuePairs[i] = this.q.split("&")[i];
    }
  }
  this.getKeyValuePairs = function() { return this.keyValuePairs; }
  this.getValue = function(s) {
    for (var j=0; j < this.keyValuePairs.length; j++) {
     if (this.keyValuePairs[j].split("=")[0] == s)
       return this.keyValuePairs[j].split("=")[1];
    }
    return false;
  }
  this.getParameters = function() {
    var a = new Array(this.getLength());
    for (var j=0; j < this.keyValuePairs.length; j++) {
      a[j] = this.keyValuePairs[j].split("=")[0];
    }
    return a;
  }
  this.getLength = function() { return this.keyValuePairs.length; }
}
// return value v of query ?x=v by key x
function queryString(key) {
  var page = new PageQuery(urlqueryishash ? window.location.hash : window.location.search);
  var val = page.getValue(key);
  return (val) ? unescape(page.getValue(key)) : false;
}

if ((urlqueryix = window.location.href.indexOf('?')) < 0) // or location.query
{
  urlqueryix = window.location.href.indexOf('#'); // or location.hash
  if (urlqueryix >= 0)
    urlqueryishash = true;
}
else urlqueryishash = false;

if (urlqueryix >= 0)
{ // there is a query (search) or anchor (hash) indicator, so let's decipher the selected product info
  if (selproduct = queryString("p")) { // product is selected
    selproduct = selproduct.toLowerCase();
  }
  if (bakproduct = queryString("c")) { // coordinate main back-return is indicated
    bakproduct = bakproduct.toLowerCase();
    prodcoord = true;
  }
  else prodcoord = false;

}
else urlqueryix = window.location.href.length;

// determine the back-return product index, if any
for (prodix = Product.length - 1; prodix >= 0; prodix--) {
   if (bakproduct == Product[prodix].pagetag)
   {
      bakprodix = prodix;
      break;
   }
}
// preset page's product index to the selected product, or the first product if no selection
prodnamed = false;
for (prodix = Product.length - 1; prodix >= 0; prodix--) {
   if ((selproduct == Product[prodix].pagetag)
       ||
       (selproduct == Product[prodix].name.toLowerCase()))
   {
      prodmainix = Product[prodix].mainix;
      prodnamed = true;
      break;
   }
}
if (!prodnamed)
{ // if no product is named (correctly) then find the first main product to display
  for (var j = 0; j < ProductMain.length; j++)
  {
    if (ProductMainIndex[j] >= 0) {
      prodmainix = j;
      prodix = ProductMainIndex[j];
      break;
    }
  }
}

// =-=-=-=-=-=-= page utilities and display

// shift to previous or next main product page
function shiftProductMain(direction) {
   var curprodmainix = prodmainix;
   if (prodmainix >= 0) do {
     if (direction < 0) {
        if (prodmainix-- <= 0)
           prodmainix += ProductMainIndex.length;
     }
     else if (direction > 0) {
        if (++prodmainix >= ProductMainIndex.length)
           prodmainix = 0;
     }
   } while ((ProductMainIndex[prodmainix] < 0) && (prodmainix != curprodmainix));
   // got to new page and force reload
   gotoProduct(ProductMainIndex[prodmainix],false);
}

function showProductPrimePic(prodselix) {
 document.write( "<img id=\"catalogpic\" class=\"primeprodpic\" "
                 +    "onError=\"this.src=\'" + prodfillsrc + "\';\" "
                 +    "alt=\"" + Product[prodselix].name + "\" "
                 +    "src=\"products/" + Product[prodselix].name + ".jpg\">" );
}

function showProductDescription(prodselix) {
 // determine which, if any, green indicia to show
 if (Product[prodselix].actsym.indexOf('G') >= 0)
   leafcode = "graphics/leaflite18.gif" ;
 else if (Product[prodselix].actsym.indexOf('g') >= 0)
   leafcode = "graphics/leafdark18.gif" ;
 else leafcode = "graphics/leafnone18.gif" ;
 leafcode = "&nbsp;&nbsp;<img style=\"height: 18px; vertical-align:middle; border-style=none;\" src=\"" + leafcode + "\">" ;

 document.write( "<B>" + Product[prodselix].name + "</B>"
               + ((Product[prodselix].newprodflag)? " - NEW!": "") 
               + leafcode + "<br>"
               + "#" + Product[prodselix].catnum + "<br>"
               + Product[prodselix].colorways + " Colorways" + "<br>" 
               + Product[prodselix].material + "<br>"
               + Product[prodselix].width + " Width" + "<br>"
               + ((Product[prodselix].repeat == "") ?
                  "" :
                  (Product[prodselix].repeat + " Repeat"))
                 + "<br>" );

}

function showProductStandards(prodselix) {
 var stdtext = Product[prodselix].standards;
 if (stdtext != "") {
   stdtext = stdtext.replace(/([^,])( )/g,"$1&nbsp;");
 }
 if (Product[prodselix].dblrubs != "") {
   stdtext = (Product[prodselix].dblrubs + " Double Rubs" + ((stdtext != "") ? ", " : ""))
           + stdtext;
 } 
 document.write( "<span>"
               + stdtext
               + "<br>" + "</span>" );
}

function showProductACTSymbols(prodselix) {
 if (Product[prodselix].actsym.indexOf('A') >= 0)
   document.write( "<img class=\"actsym\" alt=\"ACT Abrasion: HDU\" src=\"graphics/act_abrasion_hdu20.gif\">");
 if (Product[prodselix].actsym.indexOf('a') >= 0)
   document.write( "<img class=\"actsym\" alt=\"ACT Abrasion: GCU\" src=\"graphics/act_abrasion_gcu20.gif\">");
 if (Product[prodselix].actsym.indexOf('F') >= 0)
   document.write( "<img class=\"actsym\" alt=\"ACT Flammability\" src=\"graphics/act_flammability20.gif\">");
 if (Product[prodselix].actsym.indexOf('C') >= 0)
   document.write( "<img class=\"actsym\" alt=\"ACT Wet &amp; Dry Crocking\" src=\"graphics/act_crocking20.gif\">");
 if (Product[prodselix].actsym.indexOf('L') >= 0)
   document.write( "<img class=\"actsym\" alt=\"ACT Colorfastness to Light\" src=\"graphics/act_colorfastness20.gif\">");
 if (Product[prodselix].actsym.indexOf('P') >= 0)
   document.write( "<img class=\"actsym\" alt=\"ACT Physical Properties\" src=\"graphics/act_physical20.gif\">");
}

function showProductColorsShown(prodselix) {
 if (Product[prodselix].colorsshown != "")
   document.write("colors shown: " + Product[prodselix].colorsshown);
}

function showProductCoordinate(prodselix, coordix) {
 var coord_name = Product[prodselix].coordname[coordix];
 var coord_color = Product[prodselix].coordclr[coordix];
 var coord_prodix = Product[prodselix].coordprodix[coordix];
 var leftpos = coordix * 130;

 if (!prodcoord && coord_name != "")
 {
   document.write( "<div id=\"coordinate" + (coordix+1) + "\" class=\"coord\" style=\"left: " + leftpos + "px;\">");

   document.write( "<a class=\"imglink\" onClick=\"gotoProduct(" + coord_prodix + ",true); return false;\""
                 + " href=\"" + catalogURL(coord_prodix,true) + "\">");
   document.write( "<img class=\"coordpic\"" 
                   +  " onMouseOver=\"primeprodsrc=document.images['catalogpic'].src; "
                   +  "document.images['catalogpic'].src='products/"
                      + coord_name + ".jpg';\""
                   +    " onError=\"this.src=\'" + prodfillsrc + "\';\" "
                   +  " onMouseOut=\"document.images['catalogpic'].src=primeprodsrc;\""
                   +  " alt=\"" + coord_name + "\" src=\"products/coord_"
                      + coord_name + " - " + coord_color + ".jpg\""
                   + ">" );
   document.write( "</a>");

   document.write( "<a class=\"coord\" onClick=\"gotoProduct(" + coord_prodix + ",true); return false;\""
                 + " href=\"" + catalogURL(coord_prodix,true) + "\">");
   document.write( "<div class=\"coordcaption footnote\">" + coord_name + ((coord_color != "") ? (", " + coord_color) : "") + "</div>");
   document.write( "</a>");

   document.write( "</div>");
 }
}

function showProductBackTo() {
 var bakprod_name = Product[bakprodix].name;
 var leftpos = 130;

 if (prodcoord)
 {
   document.write( "<div id=\"bakproduct\" class=\"coord\" style=\"left: " + leftpos + "px;\">");

   document.write( "<a class=\"imglink\" onClick=\"gotoProduct(" + bakprodix + ",false); return false;\""
                 + "href=\"" + catalogURL(bakprodix,false) + "\">");
   document.write( "<img class=\"bakprodpic\"" 
                   +  " onError=\"this.src=\'" + prodfillsrc + "\';\" "
                   +  " alt=\"" + bakprod_name + "\" src=\"products/thumb60_"
                      + bakprod_name + ".jpg\""
                   + ">" );
   document.write( "</a>");

   document.write( "<a class=\"coord\" onClick=\"gotoProduct(" + bakprodix + ",false); return false;\""
                 + "href=\"" + catalogURL(bakprodix,false) + "\">");
   document.write( "<div class=\"coordcaption footnote\">" + bakprod_name + "</div>");
   document.write( "</a>");

   document.write( "</div>");
 }
}

function showProductNavBar() {
  // initialize ProductMain's index array
  var leftpos = 0;
  // temporary limit of 12!
  var n_baricons = 12;
  if (n_baricons > ProductMain.length) n_baricons = ProductMain.length;

  for (var j = 0; j < n_baricons; j++)
  {
    var prodselix = ProductMainIndex[j];
    if (prodselix >= 0)
    {
      var prodname = ProductMain[j];
      document.write( "<div class=\"prodnavsel\" style=\"left: "
                    + leftpos + "px;" 
                    + "\">");

      document.write( "<a class=\"imglink\" onClick=\"gotoProduct(" + prodselix + ",false); return false;\""
                    + "href=\"" + catalogURL(prodselix,false) + "\">");
      document.write( "<img class=\"prodnavselpic\"" 
                   +    "onError=\"this.src=\'" + prodfillsrc + "\';\" "
                   +  " alt=\"" + prodname + "\" src=\"products/thumb60_"
                      + prodname + ".jpg\""
                   + ">" );
      document.write( "</a>");

      document.write( "<a class=\"prodnavlink\" onClick=\"gotoProduct(" + prodselix + ",false); return false;\""
                    + "href=\"" + catalogURL(prodselix,false) + "\">");
      document.write( "<div class=\"prodnavselcaption"
                   + (((!prodcoord && (prodselix == prodix)) || (prodcoord && (prodselix == bakprodix))) ?
                      " prodnavselcaptionactive" : "")
                   + "\">" + prodname + "</div>");
      document.write( "</a>");
      document.write( "</div>");

      leftpos += 66 + ((((j+1) % 3) == 0) ? ((j == 5) ? 5:3) : 0);

    }
  }
}

