// Base Scroller
// Scrolls an object wrapped within another

// objectID works like this: Pass "mystuff"
// The scroller will assume that the actual IDs are then:
//  "mystuffc" (for the content to scroll)
//  "mystuffs" (for the scroll wrapper)

var scrollObject;     // current target object
var scrollTimer;      // current timer (used to abort interval)
var scrollVelocity;   // current speed of the scroller

var objectPos;        // current target position
var objectPosX;       // current target position (x-axis)
var scrollSpace;      // current space to be scrolled (one direction)
var scrollSpaceX;     // current space to be scrolled (one direction, x-axis)
var scrollDirection;  // current direction of the scroll


// Function: baseScroll()
// Checks if an object exists and calculates the delay (velocity)

function baseScroll(objectID, setAction, setSpeed) {

  // Check if the target object exists
  var objectExists  = document.getElementById(objectID + 'c');
      objectExists  = (objectExists != undefined) ? true : false;

  if( objectExists == true ) {

    // Calculate the velocity based on speed input
    // This should be a number from 1 - 10, 1 is slowest and 10 is fastest
    // If no speed is specified, it will be set to the middle (average)
    setSpeed        = (setSpeed == null) ?   8.5 : setSpeed;
    setSpeed        = (setSpeed  > 10  ) ?  10 : setSpeed;
    setSpeed        = (setSpeed  < 1   ) ?   1 : setSpeed;
    scrollVelocity  = Math.round(101 - (setSpeed * 10));

    // Set the direction of the current scroll
    scrollDirection = setAction;

    // Initial work is done
    // Check if the scroller can be started
    switch(setAction) {
    case 'up':   setBaseScroll(objectID); break;
    case 'down': setBaseScroll(objectID); break;
    case 'right': setBaseScroll(objectID); break;
    case 'left': setBaseScroll(objectID); break;
    default:
    // when no action is specified, stop the scroller
      clearInterval(scrollTimer);
    } // ! action?

  // ! does the object exist?
  } else {
    // If nothing is passed, stop the scroller
    clearInterval(scrollTimer);
  }

} // ! baseScroll()




// Function: setBaseScroll()
// Calculates the data of the objects involved in the scrolling
// and sets their values so doBaseScroll() can use them.

function setBaseScroll(objectID) {

  // Load the objects current position/data
  scrollObject  = document.getElementById(objectID + 'c');
  objectPos     = scrollObject.style.top;
  objectPos     = (objectPos != undefined) ? objectPos : '0';
  objectPos     = objectPos.replace(/px/g, "");
  objectPos     = objectPos * 1;
  objectHeight  = scrollObject.clientHeight;
  objectHeight  = (objectHeight == 0) ? scrollObject.offsetHeight : objectHeight;

  // Also allow for the scroller to move on the X-axis
  objectPosX    = scrollObject.style.left;
  objectPosX    = (objectPosX != undefined) ? objectPosX : '0';
  objectPosX    = objectPosX.replace(/px/g, "");
  objectPosX    = objectPosX * 1;
  objectWidth   = scrollObject.clientWidth; // ?
  objectWidth   = (objectWidth == 0) ? scrollObject.offsetWidth : objectWidth; // ?

  // Get the data of the scroll wrapper
  wrapObject    = document.getElementById(objectID + 's');
  wrapHeight    = wrapObject.clientHeight;
  wrapHeight    = (wrapHeight == 0) ? wrapObject.offsetHeight : wrapHeight;
  wrapWidth     = wrapObject.clientWidth; // X-axis
  wrapWidth     = (wrapWidth == 0) ? wrapObject.offsetWidth : wrapWidth; // X-axis

  // Scroll space left to scroll (if any)
  // Take into account already scrolled area
  scrollSpace   = (objectHeight + (objectPos * 1)) - wrapHeight;
  scrollSpaceX  = (objectWidth + (objectPosX * 1)) - wrapWidth;

  // Start the scroller until it is cancelled
  // This can be done by calling baseScroll() with no arguments
  // or by using the function clearInterval(scrollTimer)
  scrollTimer   = setInterval("doBaseScroll()", scrollVelocity);

} // ! setBaseScroll()



// Function: doBaseScroll()
// Scrolls the content until the maximum boundaries are reached
// or until the scroll is aborted manually (e.g. by mouseout).

function doBaseScroll() {

  switch(scrollDirection) {
  case 'down':
    if( scrollSpace <= 0 ) {
      clearInterval(scrollTimer);
    } else {
    // Scroll the content downwards
      objectPos--;
      scrollSpace--;
      scrollObject.style.top  = objectPos + 'px';
    }
  break;
  case 'up':
    if( objectPos >= 0 ) {
      clearInterval(scrollTimer);
    } else {
    // Scroll the content back up
      objectPos++;
      scrollObject.style.top  = objectPos + 'px';
    }
  break;
  case 'right':
    if( scrollSpaceX <= 0 ) {
      clearInterval(scrollTimer);
    } else {
    // Scroll the content right
      objectPosX--;
      scrollSpaceX--;
      scrollObject.style.left  = objectPosX + 'px';
    }
  break;
  case 'left':
    if( objectPosX >= 0 ) {
      clearInterval(scrollTimer);
    } else {
    // Scroll the content back up
      objectPosX++;
      scrollObject.style.left  = objectPosX + 'px';
    }
  break;
  } // ! switch (direction)

} // ! doBaseScroll()


///////


// Function: adjustBaseScroll()
// Adjusts the scrollers depending on the headline above them

var adjustScrollers = new Array();

function adjustBaseScroll() {

  adjustCount   = adjustScrollers.length;

  if( adjustCount > 0 ) {

    // Cycle each adjustment
    for(scroller = 1; scroller < adjustCount; scroller++) {

      headObject   = document.getElementById('featuretitle' + adjustScrollers[scroller]);
      areaObject   = document.getElementById('featbox' + adjustScrollers[scroller] + 's');

      // Get the height of the headline
      headHeight   = headObject.clientHeight;
      headHeight   = (headHeight == 0 || headHeight == undefined) ? headObject.innerHeight : headHeight;

      if( headHeight < 21 ) {
        // If the area needs to be adjusted
        areaObject.style.height = 120 + 'px';
        areaObject.style.top = 260 + 'px';

      } else if( headHeight < 33 ) {
        areaObject.style.height = 105 + 'px';
        areaObject.style.top = 275 + 'px';
      }

      baseScroll('featbox' + scroller, 'up');
      if( scrollSpace > 0 ) {
        document.getElementById('arrowbox' + scroller).style.visibility = 'visible';
      }
      baseScroll();

    } // ! for loop

  } // ! anything to adjust?

} // ! adjustBaseScroll()

