var currentTitle = 1;
entriesPerPage = 5;
_lang_missing_entry = "The select entry is missing. Return to beginning of list";

function init() {
  displayEntry(currentTitle);
}

function getArrows(ev) {
  if (!currentTitle) currentTitle = 1;

  flag = 0;
  oldTitle = currentTitle;

  arrows=((ev.which)||(ev.keyCode));

  switch(arrows) {
    case 33: // page-up
      currentTitle = (currentTitle*1) - entriesPerPage;  // *1 to handle cases where IE treats it as a string
      if (currentTitle < 1) currentTitle = 1;
      flag = 1;
      break;
    case 34: // page-down
      currentTitle = (currentTitle*1) + entriesPerPage; // *1 to handle cases where IE treats it as a string
      if (currentTitle > maxTitles) currentTitle = maxTitles;
      flag = 1;
      break;
    case 35: // end
      currentTitle = maxTitles;
      flag = 1;
      break;
    case 36: // home
      currentTitle = 1;
      flag = 1;
      break;
    case 37: // left (unused at the moment)
      break;
    case 38:  // up
      currentTitle = (currentTitle*1) - 1;  // *1 to handle cases where IE treats it as a string
      if (currentTitle < 1) currentTitle = 1;
      flag = 1;
      break;
    case 39: // right (unused at the moment)
      break;
    case 40:  // down
      currentTitle = (currentTitle*1) + 1; // *1 to handle cases where IE treats it as a string
      if (currentTitle > maxTitles) currentTitle = maxTitles;
      flag = 1;
      break;
  }
  if (flag) {
      flag = 0;
      if (oldTitle != currentTitle) {
        hideEntry(oldTitle);
        displayEntry(currentTitle);
      }
      ev.returnValue = '';  // prevent IE from scrolling the main window after keypress
      if(navigator.userAgent.indexOf("Firefox")!=-1) {
        ev.preventDefault();  // prevent NS from scrolling the main window after keypress
      }
  }
//alert (arrows);
}

function keyNumber(ev) {
  // only used in testing
  ev.which?alert(ev.which):alert(ev.keyCode);
}

// when an title is clicked on, unactivate the current element and activate the selected title
function activate(title) {
  if (currentTitle != title.id.substr(5)) {
    hideEntry(currentTitle);
    currentTitle = title.id.substr(5);
    displayEntry(currentTitle);
  }
}

// when the "up" scrollbar is clicked in the title section
function scroll_up() {
  oldTitle = currentTitle;
  currentTitle = (currentTitle*1) - 1;  // *1 to handle cases where IE treats it as a string
  if (currentTitle < 1) currentTitle = 1;
  if (oldTitle != currentTitle) {
    hideEntry(oldTitle);
    displayEntry(currentTitle);
  }
}

// when the "down" scrollbar is clicked in the title section
function scroll_down() {
  oldTitle = currentTitle;
  currentTitle = (currentTitle*1) + 1; // *1 to handle cases where IE treats it as a string
  if (currentTitle > maxTitles) currentTitle = maxTitles;
  if (oldTitle != currentTitle) {
    hideEntry(oldTitle);
    displayEntry(currentTitle);
  }
}

// when the "up" scrollbar is clicked in the entry section
function escroll_up() {
//  document.getElementById('podcast').up(7);
}

// when the "down" scrollbar is clicked in the entry section
function escroll_down() {
  el = document.getElementById('podcast');
//alert (el.offsetHeight);
//alert (el.style.top);
  el.style.top = "100px";
  if(el.y>-el.scrollHeight+objContainer.clipHeight){ 
    this.MoveArea(0,this.y-move) 
    if(loop) setTimeout(this.obj+".down("+move+")",speed) 
  }
}

function hideEntry(entry) {
  elt =  document.getElementById('title' + entry);
  ele =  document.getElementById('podcast');
  if (ele) {
    elt.className = '';
    ele.innerHTML = '<h1>Loading<blink>...</blink></h1>';
  }    
}

function displayEntry(entry) {
  pc = document.getElementById('podcast');

/*
  alert (maxTitles);
  if (maxTitles == 0) {
    pc.innerHTML = "<h1>No entries for this category.</h1>";
    return;
  }
  alert ("cool");
*/
  elt = document.getElementById('title' + entry);  // the title
  ele = document.getElementById('title' + entry).title.substr(5);  // the entry
  if (pc) { 
    elt.className = 'active';
    getData('entry' + ele + '.html','podcast')
    pc.style.top = 0;
    elt.scrollIntoView(0);
  } /* else {
    //alert (_lang_missing_entry);
    currentTitle = 1;
    displayEntry(1);
  } */
  // see if we need to display scroll bars for this entry
    document.getElementById('escroller').style.display = 'block';
/*
  if (pc.offsetHeight > document.getElementById('podcast').offsetHeight) {
    document.getElementById('escroller').style.display = 'block';
  } else {
    document.getElementById('escroller').style.display = 'none';
  }
*/
}

function highlight(el) {
  // only highlight if it's not the currently active entry
  if (el.id.substr(5) != currentTitle) {
    el.style.cursor='pointer';
    // not going to use this, but leaving functionality available for later
    // el.className = 'hover';
  }
}

function unhighlight(el) {
  // if we've hovered over the currently active entry, don't blank out the classname.
  if (el.id.substr(5) != currentTitle) {
    el.className = '';
  } else {
    el.className = 'active';
  }
}

