var officialState = 'visible';

/**
 * Finds an object in the DOM.
 **/
function findObj ( name ) {
  if( document.getElementById ) {
    return document.getElementById( name );
  }
  else if( document.all ) {
    return document.all[name];
  }
  else if( document.layers ) {
    return document.layers[name];
  }
  else return false;
}

/**
 * Toggles table rows from hidden to visible and vice-versa.
 **/
function toggleRows ( tableId ) {
  // look up the table object
  t = findObj( tableId );

  if ( t ) {
    if( t.getElementsByTagName ) {

      // get all of the tr elements in the table
      var rows = t.getElementsByTagName( 'tr' );

      for( var i = 0; i < rows.length; i ++ ) {

        // get the class names for the tr element
        rowClasses = rows[i].className.split( ' ' );

        for( var j = 0; j < rowClasses.length; j ++ ) {

          // toggle the visible and hidden class names
          if( rowClasses[j] == 'hidden' ) {
            rowClasses[j] = 'visible';
          }
          else if( rowClasses[j] == 'visible' ) {
            rowClasses[j] = 'hidden';
          }
        }

        // set the class name of the tr to the new list
        rows[i].className = rowClasses.join( ' ' );
      }

      moreControl = findObj( 'morecontrol_' + tableId );
      if( moreControl ) {
        moreSrc = moreControl.src;
        if ( moreSrc.indexOf( 'more.gif' ) >= 0 ) {
          moreSrc = moreSrc.replace( 'more.gif', 'collapse.gif' );
        }
        else if ( moreSrc.indexOf( 'collapse.gif' ) >= 0 ) {
          moreSrc = moreSrc.replace( 'collapse.gif', 'more.gif' );
        }
        moreControl.src = moreSrc;
      }
    }
  }
}

/**
 * Registers a table with mouseover and mouseout events on the rows for
 * highlighting.
 **/
function registerTable ( tableId ) {
  // look up the table object
  t = findObj( tableId );

  if ( t ) {
    if( t.getElementsByTagName ) {

      // get all of the tr elements in the table
      var rows = t.getElementsByTagName( 'tr' );

      for( var i = 0; i < rows.length; i ++ ) {

        // attach mouse over event to the row
        rows[i].onmouseover = function ( ) {
          // get the class names for the tr element
          rowClasses = this.className.split( ' ' );

          for( var j = 0; j < rowClasses.length; j ++ ) {

            // toggle the visible and hidden class names
            if( rowClasses[j] == 'rowoff' ) {
              rowClasses[j] = 'rowon';
            }

          }
          // set the class name of the tr to the new list
          this.className = rowClasses.join( ' ' );
        }

        // attach mouse out event to the row
        rows[i].onmouseout = function ( ) {
          // get the class names for the tr element
          rowClasses = this.className.split( ' ' );

          for( var j = 0; j < rowClasses.length; j ++ ) {

            // toggle the visible and hidden class names
            if( rowClasses[j] == 'rowon' ) {
              rowClasses[j] = 'rowoff';
            }

          }
          // set the class name of the tr to the new list
          this.className = rowClasses.join( ' ' );
        }
      }
    }
  }
}

/**
 * Toggles the display of the official game entry on the page.
 **/
function toggleOfficial ( ) {
  // look up the objects
  cornerImg = findObj( 'officialcorner' );
  contentCell = findObj( 'officialcell' );

  if ( contentCell && cornerImg ) {

    // toggle the visible and hidden class names
    if( officialState == 'visible' ) {
      contentCell.style.display = 'none';
      cornerImg.src = '/images/lotro/official_corner_tr_off.gif';
      officialState = 'hidden';
    }
    else if( officialState == 'hidden' ) {
      contentCell.style.display = '';
      cornerImg.src = '/images/lotro/official_corner_tr_on.gif';
      officialState = 'visible';
    }
  }
}

/**
 * Registers an Advancedsearch results table with mouseover and mouseout events on the rows for
 * highlighting; this is different from the regular register table function because the rows
 * themselves have two alternating basic (non-selected) styles.
 **/
function registerAdvancedsearchTable( tableId ) {
  // look up the table object
  t = findObj( tableId );

  if ( t ) {
    if( t.getElementsByTagName ) {

      // get all of the tr elements in the table
      var rows = t.getElementsByTagName( 'tr' );

      for( var i = 0; i < rows.length; i ++ ) {

        // attach mouse over event to the row
        rows[i].onmouseover = function ( ) {
          // get the class names for the tr element
          rowClasses = this.className.split( ' ' );

          for( var j = 0; j < rowClasses.length; j ++ ) {
          
            // toggle the visible and hidden class names
            if( rowClasses[j] == 'row1' ) {
              rowClasses[j] = 'row1on';
            }

            // toggle the visible and hidden class names
            else if( rowClasses[j] == 'row2' ) {
              rowClasses[j] = 'row2on';
            }

          }
          // set the class name of the tr to the new list
          this.className = rowClasses.join( ' ' );
        }

        // attach mouse out event to the row
        rows[i].onmouseout = function ( ) {
          // get the class names for the tr element
          rowClasses = this.className.split( ' ' );

          for( var j = 0; j < rowClasses.length; j ++ ) {

            // toggle the visible and hidden class names
            if( rowClasses[j] == 'row1on' ) {
              rowClasses[j] = 'row1';
            }
            else if( rowClasses[j] == 'row2on' ) {
              rowClasses[j] = 'row2';
            }

          }
          // set the class name of the tr to the new list
          this.className = rowClasses.join( ' ' );
        }
      }
    }
  }
}
