// These vars could be changed, so check them
if ('undefined' == typeof perPAGE || null == perPAGE || parseInt (perPAGE) < 2) {
	perPAGE = 3;
}
if ('undefined' == typeof scrollITEMS || null == scrollITEMS || parseInt (scrollITEMS) < 2) {
	scrollITEMS = perPAGE -1;
}

// These vars will not change unless the structure changes
var colNUM = 2;
var itemFIRST = 1;

// Like getElementById
function getElById (id)
{
  if ('undefined' == typeof id || null == id) {
    return null;
  }
  var el = document.getElementById (id);
  if ('undefined' == typeof el || null == el) {
    return null;
  }
  return el;
}

// ii is the item index, 1..max
// UNDER DEVELOPMENT
function createPR_Row_title (ii)
{
  // Sanity check
  if ('undefined' == typeof ii || null == ii) {
    return null;
  }
  ii = parseInt (ii);
  if (1 > ii || ii > arDatetime.length) {
    return null;
  }

  elTr = document.createElement ('tr');
  
  elTd = document.createElement ('td');
  // contents is text
  elTd.innerHTML = '<b style="color: #0060b0;" >&bull;&nbsp;' + arDatetime [ii -1] + ' </b>';

  elDiv = document.createElement ('div');
  elDiv.style.paddingLeft = '0.9em';
  // elDiv.innerHTML = unescape (arHeadline [ii -1]);
  
  elTxt = document.createTextNode (unescape (arHeadline [ii -1]));
  href = arHref [ii -1];
  if ('undefined' != typeof href && null != href && '' != href) {
    elA = document.createElement ('a');
    elA.setAttribute ('href', href);
    elA.setAttribute ('target', href);
    elA.appendChild (elTxt);
	elDiv.appendChild (elA);
  } else {
	elDiv.appendChild (elTxt);
  }
  
  elTd.appendChild (elDiv);
  elTd.style.paddingBottom = '8px';
  
  elTr.appendChild (elTd);
  
  return elTr;
}

// ii is the item index, 1..max
function createPR_Row (ii)
{
  // Sanity check
  if ('undefined' == typeof ii || null == ii) {
    return null;
  }
  ii = parseInt (ii);
  if (1 > ii || ii > arDatetime.length) {
    return null;
  }

  elTr = document.createElement ('tr');
  elTr.setAttribute ('id', 'scroller_' + ii);

  elTd = document.createElement ('td');
  elTd.setAttribute ('id', 'scroller_' + ii + '_1');
  elTd.setAttribute ('className', 'column_date');
  // mozilla does not propagate classes down to td?
  elTd.style.verticalAlign = 'top';
  elTd.style.paddingTop = '8px';
  elTd.style.paddingRight = '8px';
  elTd.style.textAlign = 'right';
  // contents is text
  elTxt = document.createTextNode (arDatetime [ii -1]);
  elTd.appendChild (elTxt);
  elTr.appendChild (elTd);

  elTd = document.createElement ('td');
  elTd.setAttribute ('id', 'scroller_' + ii + '_2');
  elTd.setAttribute ('className', 'column_headline');
  // mozilla does not propagate classes down to td?
  elTd.style.verticalAlign = 'top';
  elTd.style.paddingTop = '8px';
  // contents is text
  elTxt = document.createTextNode (unescape (arHeadline [ii -1]));
  href = arHref [ii -1];
  if ('undefined' != typeof href && null != href && '' != href) {
    elA = document.createElement ('a');
    elA.setAttribute ('href', href);
    elA.setAttribute ('target', href);
    elA.appendChild (elTxt);
    elTd.appendChild (elA);
  } else {
    elTd.appendChild (elTxt);
  }
  elTr.appendChild (elTd);

  return elTr;
}

function do_scroll (howMUCH)
{
  // Sanity check
  if (perPAGE < 1) {
    return false;
  }

  itemFIRST += howMUCH;
  if (itemFIRST < 1) {
    itemFIRST = 1;
  }
  if (itemFIRST > arDatetime.length) {
    itemFIRST -= howMUCH;
  }

  elTBody = document.getElementById ('scroller_content');

  // Clean up the old items
  while (elTBody.lastChild) {
    elTBody.removeChild (elTBody.lastChild);
  }
  
  // Fill in with new items  
  itemCOUNT = 1;
  for (ii = itemFIRST; ii <= arDatetime.length; ++ii, ++itemCOUNT) {

    if (itemCOUNT > perPAGE) {
      break;
    }

    elTr = createPR_Row (ii);
    elTBody.appendChild (elTr);
  }

  // Proper labeling
  el = document.getElementById ('scroller_recent');
  if ('undefined' != typeof el && null != el) {
    if (itemFIRST <= 1) {
      el.innerHTML = 'most recent items';
    } else {
      el.innerHTML = 'click for more recent items';
    }
  }

  el = document.getElementById ('scroller_older');
  if ('undefined' != typeof el && null != el) {
    if (arDatetime.length - itemFIRST  >= perPAGE) {
      el.innerHTML = 'click for older items';
    } else {
      el.innerHTML = 'oldest items';
    }
  }

  // Move the active listing item.
  pageX = 1+ (itemFIRST -1) / scrollITEMS;
  pageX = Math.ceil (pageX);
  numPAGES = 1.+ (arDatetime.length - perPAGE) / scrollITEMS;
  numPAGES = Math.ceil (numPAGES);

  for (ii = 1; ii <= numPAGES; ++ii ) {
    el = document.getElementById ('listing_' + ii );
    if ('undefined' == el || null == el ) {
      continue;
    }

    if ( ii == pageX ) {
      el.className = 'current';
    } else {
      el.className = 'active';
    }
    
  }

  return true;
}

function go_listing (pageX)
{
  // Trace the mouse
  el = document.getElementById ('listing_' + pageX);

  // Calculate the page number
  itemToFirst = (pageX -1) * scrollITEMS + 1;
  if (itemToFirst < 1) {
    itemToFirst = 1;
  }
  if (itemToFirst > arDatetime.length - perPAGE +1) {
    itemToFirst = arDatetime.length - perPAGE +1;
  }

  do_scroll (itemToFirst - itemFIRST);
  
  return true;
}

/**
 * Optional arguments.
 * [0] is a string that describes the presentation style.
 * 'title' is used on the title page.
 * (anything else) insie the site.
 * [1] is the total number of news items to show.
 * Undefined if negative.
 */
function build_scroller ()
{
  var newsStyle;
  if (0 < arguments.length) {
	newsStyle = arguments [0];
  } else {
	newsStyle = 'paged'; // default
  }

  // BUILD THE CONTENTS STRUCTURE
  elTBody = document.getElementById ('scroller_content');

  if ('undefined' == typeof elTBody || null == elTBody) {
    return false;
  }

  // clean up the table
  while (null != elTBody.lastChild) {
    elTBody.removeChild (elTBody.lastChild);
  }

  // now build the table
	for (ii = 1; ii <= perPAGE; ++ii ) {
		if (ii > arDatetime.length) {
			break;
		}
		
		switch (newsStyle) {
		case 'title':
			elTr = createPR_Row_title (ii);
			break;

		default:
			elTr = createPR_Row (ii);
			break;
		}
		
		elTBody.appendChild (elTr);
	}

	// BUILD THE LISTING
	elL = document.getElementById ('scroller_listing');
	elL.innerHTML = '';

	if ('title' != newsStyle) {

		if (scrollITEMS < 1) {
			alert ('***ERROR\nscrollITEMS is ' + scrollITEMS);
			return false;
		}
		numPAGES = 1.+ (arDatetime.length - perPAGE) / scrollITEMS;
		numPAGES = Math.ceil (numPAGES);

		elL.innerHTML += '<nobr><span class="pale_label" >pages</span>';
		for (ii = 1; ii <= numPAGES; ++ii) {
			elL.innerHTML += '<span class="'
			+ ((1 == ii) ? 'current' : 'active')
			+ '" id="listing_' + ii
			+ '" onclick="return go_listing (' + ii + ');" >' + ii + '</span>';
		}
		elL.innerHTML += '</nobr>';
	
	}

  return true;
}
