// specialsLib.js
// Create the specials page

// ------ Catalog globals ------
var itemList = new Array();
var itemstart = 0;  // first item to display from inventory
var itemspp = 4;  // items per page


//------ Functions  -----

function pinitem(name1, price1, detail1, regprice1)
{
  //this.image = image;
  this.name = name1;
  this.price = price1;
  this.image = detail1;
  this.regprice = regprice1;
}

function addSpecialpin(name, price, detail, regprice)
{
  var i = itemList.length;

   itemList[i] = new pinitem(name, price, detail, regprice);
}

function init()
{
// load 1st detail image
//setDetail(itemstart);
}


var currentName = "";
var currentPrice = "0";


function OrderPin(pindx)
{
var pin = itemList[pindx];
currentName = pin.name;//getpinName(p);
currentPrice = pin.price;// getpinPrice(p);

  //append Current pin to cookie
  var value = WM_readCookie("pinorder");
  value = value + "/special-" + currentName +"@"+currentPrice;
  WM_setCookie ("pinorder", value);
  location.href="shoppingCart.htm";
}


function nextPage(updn)
{
 var nextstart=itemstart+itemspp*updn;
 if (nextstart < 0) {
   nextstart = 0;
 }
 if (nextstart >= itemList.length) {
    nextstart = itemList.length - itemspp;
 }
 var page=location.pathname;
 location.href= page + "?itemstart="+nextstart;
}



/////////////////////////////////////////////////////////////
// printSpecialsPage
//
/////////////////////////////////////////////////////////////
function printSpecialsPage(height)  
{
//height = ;  // image height

// Get itemstart from URL
var temp=location.search;
var i=temp.indexOf("itemstart=");
var lastitem =0;
var border = true;

if (i >= 0)
{
  temp=temp.substring(i+10);//117
  itemstart=parseInt(temp);
}
if (isNaN(itemstart))
{
   itemstart=0;
}


// Page Constants, based on itemstart
lastitem = itemstart + itemspp-1;   // index of last item displayed this page
if (lastitem >= itemList.length) {
  lastitem = itemList.length-1;
}
// next/prev buttons?
var isnext = true;
var isprev = false;

if (itemstart > 0) {
  isprev = true;
}
if (lastitem >= (itemList.length-1)) {
  isnext = false;
}


  
// ------- generate table of pins----------
  var htmltext = "";

  if (border == false) {
    htmltext += '<table width="100%" border="0" cellspacing="5" cellpadding="0" align="center"  bgcolor="#FFFFFF">';
  } else {
    htmltext += '<table width="100%" border="1" cellspacing="5" cellpadding="0" align="center"  bgcolor="#FFFFFF">';
  }
  htmltext += "<tr>";
  document.write(htmltext);

  
  var i;
  var col = 0;

  // for each pin 
  for (i = 0; i < itemspp; i++)
  {
     htmltext = "";

     var ndx = itemstart+i;
     // if not reached end of inventory
     if (ndx < itemList.length)
     {
       // get the pin from inventory list
       // Display pic, name and price
       var thispin = itemList[ndx];
       
       htmltext += '<td bgcolor="#FFFFFF"  valign="top" align="center"  valign=bottom>';
       htmltext += '<img src="'+thispin.image+ '" border=0 height='+height+'>';
       htmltext += '<br>'+thispin.name+'<br>$'+thispin.price
       if (thispin.regprice != "") {
         htmltext +=' (reg $'+thispin.regprice+')';
       }
       htmltext += '<br><input type="button" onclick="OrderPin('+ndx+')" value=".....Buy This Pin!...."  />';
       htmltext += '<br><a href="shoppingCart.htm">View shopping cart</a>  </td>';
     }
     //else
     //{  // end of inventory, 
       //htmltext='<td width="20%">&nbsp;</td>';
     //}
     document.write(htmltext);
     col++;
     if (col >= 5) {
        document.write("</tr><tr>");
        col=0;
     }
  }
  document.write('</tr></table>');

  //-- next/prev buttons --
  htmltext = '<form  name="bform"><table width=100%>';
  htmltext += '<tr><td align=right>';
  htmltext += "Viewing items "+(itemstart+1) + " through " + (lastitem+1);
  if (isprev) {
    htmltext += '<input type="button"  name="prevbutton" value="<<Prev"  onclick="nextPage(-1)">';
  }
  if (isnext) {
    htmltext += '<input type="button"  name="nextbutton" value="Next>>"  onclick="nextPage(1)">';
  }
  htmltext += '</td></tr></table></form>';
  document.write(htmltext);
  
}


