// vcat
//   catalog inventory items (from vDesc and vPics tables)
//   adapted from vpg.js, 31 august 2007
//   copyright 2007 by wray cummings, all rights reserved

//
// special so we can use n.toFixed()
//
if ( !Number.prototype.toFixed ) {
  Number.prototype.toFixed = function( decimals ) {
    var decDigits = ( isNaN(decimals) ) ? 2 : decimals;    // default to two decimal digits
    var k         = Math.pow( 10, decDigits );
    var fixedNum  = Math.round( parseFloat(this) * k ) / k;
    var sFixedNum = new String( fixedNum );
    var aFixedNum = sFixedNum.split( "." );
    var i         = ( aFixedNum[1] ) ? aFixedNum[1].length : 0;
    if ( (i == 0) && (decDigits) ) { sFixedNum += "."; }   // append decimal point if needed
    while ( i < decDigits ) {                              // append zeros if needed
      sFixedNum += "0";
      i++;
      }
    return sFixedNum;
  }
}

function _fmtAmt( s ) {      // int if possible, otherwise two decimal digits
  var n = new Number( s );
  var i = parseInt( s,10 );
  return ( ( n == i ) ? i : n.toFixed( 2 ) );
}


//
// ==================== initialization


var goBigPic = null;
var goPicDiv = null;
var goPicTb  = null;
var gaPicTd  = null;


function initCmd() {
/*
  goPicDiv = getObj( 'pics' );
  goPicTb  = goPicDiv.getElementsByTagName( 'table' ).item(0);
  gaPicTd  = goPicDiv.getElementsByTagName( 'td' );
  goBigPic = goPicDiv.getElementsByTagName( 'img' ).item(0);
  if ( gaPicTd.length > 0 ) { initPics(); }      // no pics, no need ...
  if ( gaPicTd.length < 2 ) {                    // if 0 or 1 pic, no need ...
    _undisplay( goPicTb );
    }
  setTimeout( '_display( goPicDiv );',200 );
*/
}

var giTr        = 0;         // track current tr for prev/next
var goShowInvTr = null;

function showInvItem( el ) {
  if ( goShowInvTr ) {
    _showInvUnclik( goShowInvTr );
    goShowInvTr = null;
    }
  var s  = el.getAttribute( 'id' );
//  alert( 'at showInvItem( el ), s is ' + s );
  var a  = gxoVPics[ s ];
  $( '#bigPicTitle' ).text( a[ 'qtitle' ] );
  $( '#bigPicPrice' ).text( '$' + a[ 'price' ] );
  var an = a[ 'inv' ] + '.htm';
  $( '#bigPicA' ).attr( { href:an } );
  $( '#viewImg' ).attr( { src:a[ 'picUri' ],alt:a[ 'alt' ],title:a[ 'alt' ] } );
  $( '#viewCap' ).empty();
  var jP = $( '<p></p>' ).text( a[ 'cap' ] );
  $( '#viewCap' ).append( jP );
  $( '#bigPicH3A' ).attr( { href:an } );
  
  _showInvClik( el );
  goShowInvTr = el;
  
  $.cookie( gsCatCookie,a[ 'inv' ] );         // save for re-entry
  var i = parseInt( el.getAttribute( 'i' ),10 );
  giTr  = i;                                     // save current Tr for prev/next
}

function _showInvClik( el ) {
  $( el ).removeClass( 'ttOver' );
  $( el ).addClass( 'ttClik' );
  $( el ).unbind();
  goViewPicTd = el;
}

function _showInvUnclik( el ) {
  $( el ).hover( function() {
    $( this ).addClass( 'ttOver' ).fadeTo( 'fast',1 );
  }, function() {
    $( this ).removeClass( 'ttOver' ).fadeTo( 'fast',.6 );
  } );
  $( el ).click( function() {
    showInvItem( this );
    } );
  $( el ).removeClass( 'ttOver ttClik' );
  $( el ).fadeTo( 'fast',.6 );
}

var rePageNo = /group-(\d+)/;
function nextInvItem( el ) {
  var s     = $( gjTr[ giTr ] ).attr( 'class' );           // class of current item TR
  var curPg = rePageNo.exec( s )[ 1 ];                     // extract pageNo
  giTr++;
  giTr      = ( giTr == gjTr.length ) ? 0 : giTr;
  s         = $( gjTr[ giTr ] ).attr( 'class' );           // class of new item TR
  var newPg = rePageNo.exec( s )[ 1 ];                     // extract pageNo
  if ( curPg != newPg ) { clikIndexPage( newPg - 1 ); }    // click index page if changed, rel 0
  $( gjTr[ giTr ] ).click();                               // click selected item TR

}

function prevInvItem( el ) {
  var s     = $( gjTr[ giTr ] ).attr( 'class' );           // class of current item TR
  var curPg = rePageNo.exec( s )[ 1 ];                     // extract pageNo
  giTr      = ( giTr == 0 ) ? gjTr.length: giTr;
  giTr--;
  s         = $( gjTr[ giTr ] ).attr( 'class' );           // class of new item TR
  var newPg = rePageNo.exec( s )[ 1 ];                     // extract pageNo
  if ( curPg != newPg ) { clikIndexPage( newPg - 1 ); }    // click index page if changed, rel 0
  $( gjTr[ giTr ] ).click();
}


var goXXX      = {};
var gaMvN      = [];
var gjPaginate = null;

var gxPrefix   = {};

function initIndexPages( o ) {
  ax = $( '#titleTBody tr' );          // build list of catalog items

  var i      = -1;           // c) setup to build master list
  var iPage  = 1;
  var iMax   = giPageMax;
  var jOl    = $( '<ol></ol>' );
  var svLi   = null;
  $.each( ax, function() {   // d) build master list from sorted list
    if ( ++i >= iMax ) {               // test for page overflow
      iPage++;
      i = 0;
      }
    var jTr = this;
    var s   = 'group-' + iPage + ' ' + $( jTr ).attr( 'class' );
    $( jTr ).attr( { 'class':s } );      // group must be first class name used !!
    } );

  $( 'tr', '#titleTBody' ).hide();               // e) hide all list elements
  $( 'tr.group-1', '#titleTBody' ).show();       // f) show first group
  var iCount = ax.length;
  doPagination( iCount );                        // g) build page navigation
  $( '#showSpinner' ).hide();
  
  gjPaginate = $( '.pagination' )[ 0 ];          // save paginator object
}

function clikIndexPage( sPg ) {
  iPg = parseInt( sPg,10 );
  gjPaginate.autoSelectPage( iPg);
}

var giPageMax = 10;
var giCurPage = 0;
function showPage( pageId ) {
//  alert( 'at showPage( pageId,jq ), pageId is '+pageId+'\njq is '+jq );
  var sClass = 'tr.group-' + ( pageId + 1 );
  var iStart = ( parseInt( pageId,10 ) * giPageMax ) + 1;
  $( 'tr:first','#titleTBody' ).attr( 'start',iStart );
  $( 'tr','#titleTBody' ).hide();
  $( sClass,'#titleTBody' ).show();
  giCurPage = pageId;                            // save active page id
  $.cookie( gsPageCookie,pageId );               // save for re-entry

  return false
}

function doPagination( i ) {
  $(".pagination").pagination(i, {
      items_per_page:giPageMax,
      num_edge_entries: 0,
      num_display_entries: 5,
      callback:showPage
		});
}


