/* 
 *  jsContentManagers.js version 0.1
 *
 *  copyright 2006 Jim E Sontag
 *  all rights reserved
 *
 *************************************************************/

var IV = 1      // image Viewer
var PV = 2
var DAD = 0
var MOM = 1

// ******************************************************************

function jsImageViewer( imageID, menuID, imgList )
{
  this.type = IV
  this.imageID = imageID
  this.menuID = menuID
  this.idx = 0
  this.img = new Array()
  this.control = this
  this.curItem = null

  var j, k, count

  // check for valid arguments
  if( jsImageViewer.arguments.length < 3 )
  {
    alert( 'Insufficient arguments' )
    return
  }

  k = document.getElementById( imageID )
  if( k == null )
  {
    alert( 'image element ' + imageID + ' does not exist')
    return
  }

  k = document.getElementById( menuID )
  if( k == null )
  {
    alert( 'menu DIV ' + menuID + ' does not exist' )
    return
  }

  // preload the images
  for( j=2, count=0; j < jsImageViewer.arguments.length; j++, count++ )
  {
    this.img[count] = new Image
    this.img[count].src = jsImageViewer.arguments[j]
  }

  function viewIndex( idx )
  {
    getElementObj = null; getElementCount=0
    var p = document.getElementById( this.menuID )
    getElement( p, idx )
    
    if( !getElementObj )
      return;
      
    if( typeof getElementObj.onclick == 'function' )
      getElementObj.onclick()
      
    if( typeof getElementObj.onmouseover == 'function' )
      getElementObj.onmouseover()
  }
  
  this.select = viewIndex
}

// ******************************************************************

function viewImage( element, img )
{
  // if already selected, do nothing
  if( element.style.fontWeight == 'bold' )
    return

  var control = findParent( element, IV, DAD )
  if( !control )
    return

  if( viewImage.arguments.length == 2 )
  {  
    // change the image  
    document.getElementById( control.imageID ).src = img
  }

  // bold the text
  element.style.fontWeight = 'bold'
  
  if( control.curItem != null )
    control.curItem.style.fontWeight = ''
//  else alert('no current element')

  control.curItem = element

  var p = document.getElementById( control.menuID )
  var idx=0, count=0
  getIndex( p, element )
  control.idx = idx
  
  if( viewImage.arguments.length == 1 )
  {
    document.getElementById( control.imageID ).src = control.img[control.idx].src
  }
  
  // this is an embedded function that is called recursively
  function getIndex( parent, element )
  {
    var j, child = parent.childNodes
    for( j = 0; j < child.length; j++ )
    {
      if( child[j].nodeName == 'SPAN' )
      {
        if( child[j] == element )
          idx = count

        count++
      }

      // note - this is a recursive function!
      if( child[j].hasChildNodes() )
        getIndex( child[j], element )
    }
  }
}

// ******************************************************************

function jsPageViewer( boardID, menuID, idx )
{
  this.type = PV
  this.boardID = boardID
  this.menuID = menuID
  this.control = this
  this.curItem = null

  var j, k, x, count 

  // check for valid arguments
  k = document.getElementById( boardID )
  if( k == null )
  {
    alert( 'jsPageViewer DIV ' + boardID + ' does not exist')
    return
  }

  var k = document.getElementById( menuID )
  if( k == null )
  {
    alert( 'menu DIV ' + menuID + ' does not exist' )
    return
  }

  // scan through the division looking for span elements
  // bold the one that is selected by idx
  if( typeof idx != 'undefined' )
  {
    this.idx = idx
    selectFunc( this.idx )
    this.curItem = getElementObj

    // now walk through the jsPageViewer divs and toggle the class
    var child = document.getElementById( this.boardID ).childNodes
    switchClass( this.ptr, child, this.idx )
  }

  else
  {
    this.idx = 0
  }

  function selectFunc( idx )
  {
    getElementObj=null; getElementCount=0
    var p = document.getElementById( menuID )
    getElement( p, idx )
    
    var val = getElementObj.style.fontWeight
    getElementObj.style.fontWeight = (val == 'bold') ? '' : 'bold'
  }

  function viewIndex( idx )
  {
    getElementObj=null; getElementCount=0
    var p = document.getElementById( menuID )
    
    getElement( p, idx )
    if( !getElementObj )
    {
      getElementObj=null; getElementCount=0
      getElement( p, 0 )
      if( !getElementObj )
      {
        alert( 'No element found')
        return
      }
    }
    
    if( typeof getElementObj.onclick == 'function' )
      getElementObj.onclick()
      
    if( typeof getElementObj.onmouseover == 'function' )
      getElementObj.onmouseover()
  }

  this.select = viewIndex
}

// ******************************************************************

function viewPage( element )
{
  // if already selected, do nothing
  if( element.style.fontWeight == 'bold' )
    return
  
  var control = findParent( element, PV, DAD )
  
  if( !control )
    return
    
  // note used 'parent' here instead of p, IE did not like this
  var p = document.getElementById( control.menuID )
  var idx=0, count=0
  getIndex( p, element )
  control.idx = idx

  p = document.getElementById( control.boardID ).childNodes
  switchClass( control, p, control.idx )

  // bold the text of the selected menu item  
  element.style.fontWeight = 'bold'
  if( control.curItem != null )
    control.curItem.style.fontWeight = ''
  control.curItem = element

  // this is an embedded function that is called recursively
  function getIndex( parent, element )
  {
    var j, child = parent.childNodes
    for( j = 0; j < child.length; j++ )
    {
      if( child[j].nodeName == 'SPAN' )
      {
        if( child[j] == element )
          idx = count

        count++
      }

      // note - this is a recursive function!
      if( child[j].hasChildNodes() )
        getIndex( child[j], element )
    }
  }
}

// ******************************************************************

function switchClass( control, child, idx )
{
  var j, count = 0
  for( j=0; j < child.length; j++ )
  {
    if( child[j].nodeName == "DIV" )
    {
      child[j].className = (count == idx) ? 'opened' : 'closed'
      count++
    }
  }
}

// ******************************************************************

var getElementObj, getElementCount  
function getElement( parent, idx )
{
  // this function refers to global vars getElementObj, getElementCount
  var j, child = parent.childNodes
  for( j = 0; j < child.length; j++ )
  {
    if( child[j].nodeName == 'SPAN' )
    {
      if( getElementCount == idx )
      {
        getElementObj = child[j]
        return
      }
      getElementCount++
    }

    // note - this is a recursive function!
    if( child[j].hasChildNodes() )
      getElement( child[j], idx )

    if( getElementObj != null )
      return
  }
}

// ******************************************************************

function findParent( element, vtype, ptype )
{
  // find the control for this element
  // we look thru successive parents for a container
  // element that has a menuID or boardID in one of our controls

  var j, control, mother = element.parentNode 
  while( mother != null && control == null )
  {
    for( j=0; j < jsContentManagers.length; j++ )
    {
      if( vtype == PV )
      { 
        if( ptype == MOM )
        {
          if( jsContentManagers[j].type == PV )
          {
            if( mother.id == jsContentManagers[j].boardID )
            {
              control = jsContentManagers[j].control
              break
            }
          }
        }
        else
        {
          // ptype == DAD
          if( jsContentManagers[j].type == PV )
          {
            if( mother.id == jsContentManagers[j].menuID )
            {
              control = jsContentManagers[j].control
              break
            }
          }
        }
      }
        
      if( vtype == IV )
      {
        if( ptype == MOM )
        {
          if( jsContentManagers[j].type == IV )
          {
            if( mother.id == jsContentManagers[j].boardID )
            {
              control = jsContentManagers[j].control
              break
            }
          }
        }
        else
        {
          // ptype == DAD
          if( jsContentManagers[j].type == IV )
          {
            if( mother.id == jsContentManagers[j].menuID )
            {
              control = jsContentManagers[j].control
              break
            }
          }
        }
      }
    }
    
    if( !control )
      mother = mother.parentNode
    else
      break
  }

  if( !control )
  {
    alert( 'did not find a control' )
    return null
  }
  
  return control
} 

// *************** jsPageViewer controls ******************************* 

function nextPage( element, ctrl )
{
  var control

  if( nextPage.arguments.length == 2 )
    control = ctrl.control
  else
    control = findParent( element, PV, MOM )

  x = control.idx + 1
  control.select( x )
}
 
function prevPage( element, ctrl )
{
  var control

  if( prevPage.arguments.length == 2 )
    control = ctrl.control
  else
    control = findParent( element, PV, MOM )

  x = control.idx - 1
  x = Math.max( x, 0 )
  control.select( x )
}

function getFrameIndex( idx )
{
  var j
  for( j = 0; j < frameSequence.length; j++ )
  {
    if( frameSequence[j] == idx )
      return j
  }
  return null
}
  
