/*
 * Copyright 2003 Knife Edge Technologies, Inc.  All Rights Reserved.
 * 
 * This software is the proprietary information of Knife Edge Technologies, 
 * Inc. provided under license for use by "The Flying Mule."
 * No part of this file may be copied or re-used without prior written 
 * agreement from Knife Edge Technologies, Inc.
 * http://www.knifeedgetech.com   info@knifeedgetech.com
 */
strImgPath          = "/img/prd/";
strThmSuffix        = "_thm.jpg";
strGalSuffix        = "_gal.jpg";
strLrgSuffix        = "_lrg.jpg";
strNAThmName        = strImgPath + "NA_thm.jpg";
strNALrgName        = strImgPath + "NA_lrg.jpg";
strLoadImgURL       = strImgPath + "LOAD_lrg.jpg";
strLoadImgURL       = "/img/LOAD_lrg.jpg";
strSpacerName       = "/img/pge_spc.gif";
astrPreloadImgURL   = new Array( strLoadImgURL );
strLastItemCookie   = "LAST_ITEM";
cxLrg               = 640;
cyLrg               = 425;
cxThm               = 120;
cyThm               = 80;
cxBdr               = 3;
cyBdr               = 3;
cxLast              = cxThm;
cyLast              = cyThm;
cxFtr               = 250;
cyFtr               = 167;
cimgThmLine         = 6;

astrDefaultLastItem = new Array( "CG-US33820"
                               , "CG-US37101"
                               , "CG-AA31931"
                               , "CG-US31309"
                               , "CG-AA34001"
                               , "CG-AA33304"
                               , "FM-B11B205"
                               , "FM-B11B196"
                               );

/* 
 * Initializes globals for displaying products
 */
function prdInit()
{
   // Need initializing?
   if (navigator.aimgCommon == null)
   {
      // Preload common images
      navigator.aimgCommon = utlPreloadImgs( astrPreloadImgURL, null );
   }
}


/*
 * Opens product description table and displays image thumbnails
 */
function prdHeader( strCode, cimg )
{
   var iimg;


   // Any images available for product?
   if (cimg > 0)
   {
      // Find main product image
      imgMain = utlFindImg( strCode );

      // Note this item as last viewed
      prdSetLastItem( strCode );
   }
   else
   {
      // Find "Not Available" image
      imgMain = utlFindImg( strNALrgName );
   }

   // Adjust main image alignment
   imgMain.width         = cxLrg;
   imgMain.height        = cyLrg;
   imgMain.align         = "left";
   imgMain.hspace        = 0;
   imgMain.strCode       = strCode;
   imgMain.iimg          = 1;
   imgMain.cimg          = cimg;
   imgMain.onclick       = prdImgOnClick;
   imgMain.oncontextmenu = prdImgOnContextMenu;


   // Create preload name array
   document.astrLrgName = new Array();

   // Open description table
   document.writeln( "<TABLE cellpadding='0' cellspacing='0' border='0'>" );

   // Multiple images available for product?
   if (cimg > 1)
   {

      // Determning thumbnail size
      cx = ((cxLrg + (2 * cxBdr)) / cimgThmLine) - (2 * cxBdr);
      cy = cx * (cyLrg / cxLrg);

      // Start description row
      document.writeln( "<TR valign='top'>" );
      document.writeln( "<TD width='" + cxLrg + "' >" );

      // Open thumbnail table
      document.writeln( "<TABLE cellpadding='0' cellspacing='0' border='0'>" );
      document.writeln( "<TR valign='top'>" );

      // Add each thumbnail to table
      for (iimg = 1; iimg <= cimg; iimg++)
      {
         // Add thumbnail to table
         document.writeln( "<TD>" );
         prdWriteImg( strCode
                    , iimg
                    , cx
                    , cy
                    , null
                    , null
                    , null
                    , null
                    );
         document.writeln( "</TD>" );
   
         // End of thumbnail row?
         if (iimg % cimgThmLine == 0)
         {
            // Start new thumbnail row
            document.writeln( "</TR>" );
            document.writeln( "<TR valign='top'>" );
         }
      }

      // Close thumbnail table
      document.writeln( "</TR>" );
      document.writeln( "</TABLE>" );

      // End description row
      document.writeln( "</TD>" );
      document.writeln( "</TR>" );
   }

   // Start description row
   document.writeln( "<TR valign='top'>" );
   document.writeln( "<TD width='" + cxLrg + "'><BR>" );
}


/*
 * Closes product description table
 */
function prdFooter( strCat )
{
   // Note product category
   document.strCat = strCat;

   // Close thumbnail/description table
   document.writeln( "</TD>" );
   document.writeln( "</TR>" );
   document.writeln( "</TABLE>" );
}


/*
 * Adds an item to end of last item list
 */
function prdSetLastItem( strCode )
{
   // Need a last item list?
   if (navigator.astrLastItem == null)
   {
      // Load from cookie
      prdLoadLastItems();
   }

   // Remove any previous instances of this item from list
   istrChk = 0;
   strNew  = strCode;

   while (istrChk < navigator.astrLastItem.length)
   {
      // Replace this item
      strOld                          = navigator.astrLastItem[istrChk];
      navigator.astrLastItem[istrChk] = strNew;
      strNew                          = strOld;

      // Was this the same as the new item
      if (strOld == strCode)
      {
         // Done shuffling
         istrChk = navigator.astrLastItem.length;
      }
      else
      {
         // Shuffle it down
         istrChk++;
      }
   }

   // Save away for future sessions
   prdSaveLastItems();
}


/*
 * Returns an item from last item list
 */
function prdGetLastItem( iItem )
{
   // Got last items?
   if (navigator.astrLastItem == null)
   {
      // Load from cookie
      prdLoadLastItems();
   }

   return (navigator.astrLastItem[iItem]);
}


/*
 * Saves last item list as cookie
 */
function prdSaveLastItems()
{
   // Create a comma-separated string of items
   strLastItems = navigator.astrLastItem[0];

   for (iItem = 1; iItem < navigator.astrLastItem.length; iItem++)
   {
      strLastItems += "," + navigator.astrLastItem[iItem];
   }

   // Write last items as a cookie
   dtExpire = new Date();
   dtExpire.setTime( dtExpire.getTime() + 1000*60*60*24*365 );
   utlSetCookie( strLastItemCookie, strLastItems, "/", dtExpire );
}


/*
 * Loads last item list from cookie
 */
function prdLoadLastItems()
{
   // Initialize with defaults
   navigator.astrLastItem = astrDefaultLastItem;

   // Look for items in cookie
   strLastItems = utlGetCookie( strLastItemCookie );

   // Found?
   if (strLastItems != null)
   {
      iItem    = 0;
      ichStart = 0;

      // Add each item to list
      while (ichStart < strLastItems.length)
      {
         // Look for next item
         ichEnd = strLastItems.indexOf( ",", ichStart );

         // Last item?
         if (ichEnd == -1)
         {
            ichEnd = strLastItems.length;
         }

         // Add to list
         navigator.astrLastItem[iItem] = strLastItems.substring( ichStart, ichEnd );

         // Next item
         iItem++
         ichStart = ichEnd + 1;
      }
   }
}



/*
 * Writes an item thumbnail from last item list
 */
function prdShowItem( iItem, strBaseURL )
{
   strCode   = prdGetLastItem( iItem );
   strPrdURL = prdGetProductURL( strCode );

   prdWriteImg( strCode
              , 1
              , cxLast
              , cyLast
              , strBaseURL
              , strPrdURL
              , strCode
              , null
              );
}



/*
 * Writes a product image link
 */
function prdWriteProductLink( strCode, iimg, cx, cy )
{
   // Make image name stub
   strImgName = strImgPath + strCode + iimg;

   // Size specified?
   if ((cx != 0) || (cy != 0))
   {
      // Use large image
      strImgName += strLrgSuffix;
   }
   else
   {
      // Use thumbnail
      strImgName += strThmSuffix;
      cx         = cxThm;
      cy         = cyThm;
   }

   document.writeln( "<A href='" + prdGetProductURL( strCode ) + "'>" );
   document.write( "<IMG src='" + strImgName );

   if (cx != 0)
   {
      document.write( "width='" + cx + "' " );
   }

   if (cy != 0)
   {
      document.write( "height='" + cy + "' " );
   }

   document.write( "border='0' " );
   document.writeln( "></A>" );
}


/*
 * Returns the thumbnail URL for the specified product
 */
function prdGetThmImgURL( strCode, iimg, strBase )
{
   return (prdGetImgURL( strCode, iimg, strBase, strThmSuffix));
}


/*
 * Returns the large image URL for the specified product
 */
function prdGetLrgImgURL( strCode, iimg, strBase )
{
   return (prdGetImgURL( strCode, iimg, strBase, strLrgSuffix));
}


/*
 * Returns the image URL for the specified product
 */
function prdGetImgURL( strCode, iimg, strBase, strSuffix )
{
   if (strBase == null)
   {
      strBase = "";
   }

   if (strImgPath == null)
   {
      strImgPath = "";
   }

   if (iimg < 1)
      iimg = 1;

   strImgURL = strBase + strImgPath + strCode + "_";

   if (iimg < 10)
   {
      strImgURL += "0"; 
   }

   strImgURL += iimg + strSuffix;


   return (strImgURL);
}





/*
 * Returns a featured category matching a specified prefix
 */
function prdGetFeatureIndex( astrFtr, strCatPrefix )
{
   // Initialize 
   istrFtrFound = -1;
   istrFtrLast  = astrFtr.length - 5;

   // Count number of matching featured products
   cstrFtrMatch = 0;
   for (istrFtrCheck = 0; istrFtrCheck <= istrFtrLast; istrFtrCheck += 5)
   {
      if (astrFtr[istrFtrCheck].indexOf( strCatPrefix ) == 0)
      {
         cstrFtrMatch++;
      }
   }
  
   // Found any?
   if (cstrFtrMatch > 0)
   {
      // Pick a random product from matching set
      cstrFtrSkip = Math.floor(Math.random() * cstrFtrMatch);
   
      // Find matching product
      fSearch      = true;
      istrFtrCheck = 0;
      while (fSearch)
      {
         if (astrFtr[istrFtrCheck].indexOf( strCatPrefix ) == 0)
         {
            if (cstrFtrSkip == 0)
            {
               fSearch      = false;
               istrFtrFound = istrFtrCheck;
            }
            else
            {
               cstrFtrSkip--;
            }
         }

         istrFtrCheck += 5;
   
         if (istrFtrCheck > istrFtrLast)
         {
            fSearch = false;
         }
      }
   }
  
   return (istrFtrFound);
}


/*
 * Writes out a featured product
 */
function prdWriteFeatured( strCatPrefix, strAlign, fFull )
{
   var  istrFtr;


   // Write description by default
   if (fFull == null)
   {
      fFull = true;
   }

   // Find product category
   astrFtr = navigator.astrFtr;
   istrFtr = prdGetFeatureIndex( astrFtr, strCatPrefix );

   // Found?
   if (istrFtr != -1)
   {
      prdWriteImg( astrFtr[istrFtr + 1]
                 , 1
                 , cxFtr
                 , cyFtr
                 , null
                 , prdGetProductURL( astrFtr[istrFtr + 1] )
                 , astrFtr[istrFtr + 2]
                 , strAlign
                 );

      // Writing description?
      if (fFull)
      {
         document.write( "<P><H2>Featured " );
         stoGetCat( astrFtr[istrFtr + 0] ).writeName( null, true );
         document.writeln( " product:</H2>" );
     
         document.write( "<P><B>" );
         document.write( astrFtr[istrFtr + 2] );
         document.write( "</B> - " );
         document.write( astrFtr[istrFtr + 4] );
         document.writeln( "<BR>" );
     
         document.writeln( astrFtr[istrFtr + 3] + ".." );
   
         document.write( "<A href='" );
         document.write( prdGetProductURL( astrFtr[istrFtr + 1] ) );
         document.writeln( "'>" );
     
         document.writeln( "READ MORE</A>.</P>" );
      }
   }
}


function prdGetProductURL( strCode )
{
   return ( "http://www.flyingmule.com/products/"
          + strCode
          );
}


function prdImgOnClick( event )
{
   prdImgSelect( this, 0, +1 );

   return (false);
}


function prdImgOnContextMenu( event )
{
   prdImgSelect( this, 0, -1 );

   return (false);
}


function prdImgSelect( img, iimg, dimg )
{
   // Set image index
   if (iimg > 0)
      img.iimg = iimg;

   // Offset image index
   img.iimg += dimg;

   // Range check image index
   if (img.iimg > img.cimg)
      img.iimg = 1;

   if (img.iimg < 1)
      img.iimg = img.cimg;

   // Update image
   img.src = prdGetLrgImgURL( img.strCode, img.iimg, "" );
}


function prdImgMouseOver( cell )
{
   cell.style.borderColor="#000000";

   return (false);
}


function prdImgMouseOut( cell )
{
   cell.style.borderColor="";

   return (false);
}

function prdImgClick( img )
{
   // Thumbnail?
   if (img.srcLrg != null)
   {
      // Switch main image
      img.imgMain.src = img.srcLrg;

   }

   // Link?
   if(img.parentNode.tagName.toUpperCase() == "A")
   {
      // Navigate to link
      window.location.href = img.parentNode.href;
   }

   return (false);
}


function prdWriteImg( strCode, iimg, cx, cy, strBaseURL, strLinkURL, strAlt, strAlign )
{
   // Which image size should we use?
   if (cx > cxThm)
   {
      strImgURL = prdGetLrgImgURL( strCode, iimg, strBaseURL );
   }
   else
   {
      strImgURL = prdGetThmImgURL( strCode, iimg, strBaseURL );
   }

   document.write( "<TABLE cellpadding='0' cellspacing='0' border='2' style='border-style: inset'" );
   utlWriteAtt( "background", strLoadImgURL );
   utlWriteAtt( "align",      strAlign );
   document.write( ">" );
   document.write( "<TR>" );
   document.write( "<TD " );
   utlWriteAtt( "onMouseOver", "return(prdImgMouseOver(this));" );
   utlWriteAtt( "onMouseOut",  "return(prdImgMouseOut(this));" );
   document.write( ">" );

   // Link?
   if (strLinkURL != null)
   {
      document.write( "<A " );
      utlWriteAtt( "href", strLinkURL );
      utlWriteAtt( "alt",  strAlt );
      document.write( ">" );
   }

   document.write( "<IMG " );
   utlWriteAtt( "src",           strImgURL );
   utlWriteAtt( "width",         cx );
   utlWriteAtt( "height",        cy );
   utlWriteAtt( "name",          "thm" + iimg );
   utlWriteAtt( "onClick",       "return(prdImgClick(this));" );
   utlWriteAtt( "onContextMenu", "return(prdImgClick(this));" );
   utlWriteAtt( "border",        "0" );
   document.write( ">" );
            
   // Link?
   if (strLinkURL != null)
   {
      document.write( "</A>" );
   }

   document.write( "</TD>" );
   document.write( "</TR>" );
   document.write( "</TABLE>" );
   document.writeln();

   // Thumbnail?
   if (strLinkURL == null)
   {
      // Store image names with thumbnail
      document.images["thm" + iimg].imgMain = imgMain;
      document.images["thm" + iimg].srcThm  = prdGetThmImgURL( strCode, iimg, strBaseURL );
      document.images["thm" + iimg].srcLrg  = prdGetLrgImgURL( strCode, iimg, strBaseURL );
   }
}
