//////////
//Global Pause function
var objPause = new PauseManager();

//Pauses for a set number of milliseconds
//  after which it calls a specified function in the context of the Object passed
function pauseObjectMethod(objObject, objFunction, intMilliSec) {
  var id = objPause.StoreObject(objObject, objFunction);
  window.setTimeout('objPause.Finish(' + id + ');', intMilliSec);
}

//Pause Manager Constructor
function PauseManager() {
  //methods
  this.StoreObject = mtdStoreObject;
  this.Finish = mtdFinish;
  //locals
  var arrObjects = new Array;
  var arrFunctions = new Array;
  
  //stores pause details
  function mtdStoreObject(objObject, objFunction) {
    //save objects to array
    //we start at the beginning and reuuse first null array
    var next = arrObjects.length;
    OutLoop:
    for (i=0; i<arrObjects.length; i++) {
      if (arrObjects[i] == null) {
        next = i;
        break OutLoop;
      }
    }
    //window.status = 'TEST arrObjects[next] ' + next + '       ' + next;
    arrObjects[next] = objObject;
    arrFunctions[next] = objFunction;
    return next;
  }

  //finishes pause
  function mtdFinish(id) {
    var fn = arrFunctions[id];
    var obj = arrObjects[id];
    //wipe values, but don't delete them or it'll screw up the indexes
    arrFunctions[id] = null;
    arrObjects[id] = null;
    //call function
    fn.apply(obj);
  }

}

//Carousel class
function Carousel() {
  //methods
  this.AddObject = mtdAddObject;
  this.AddNav = mtdAddNav;
  this.Scroll = mtdScroll;
  this.Start = mtdStart;
  this.Stop = mtdStop;
  this.CheckUpdate = mtdCheckUpdate;

  //properties
  this.Objects = new Array;
  this.Pages = 1;
  this.CurrentPage = 1;
  this.TotalScroll = 100;
  this.ScrollSpeed = 5;
  this.ScrollStep = 10;
  
  //private
  var blnRunning = false;
  var blnMoving = false;
  
  //Methods
  

  //Adds an object to the stored list
  // id corresponds to an object id in the DOm
  function mtdAddObject(id)  {
    var obj;
    obj = document.getElementById(id);
    if (obj) {
      this.Objects[this.Objects.length] = obj;
    }
  }
  
  //Adds the nav objects
  function mtdAddNav(idPrev, idNext, intPages, intPageWidth) {
    this.Prev = document.getElementById(idPrev)
    this.Next = document.getElementById(idNext);
    this.Pages = intPages;
    if (intPageWidth) { this.TotalScroll = intPageWidth; }
    checkPages(this);
  }
  
  //Fades up an object
  // id corresponds to an object id in the DOm that's already been added
  // direction is a boolean, left is true
  function mtdScroll(id, direction) {
    var obj;
    if (!blnMoving) {
      for (var i=0; i<this.Objects.length; i++) {
        obj = this.Objects[i];
        obj.icoScroll = direction;
        if (direction) {
          this.CurrentPage--;
          obj.targetScroll = getScroll(obj) + this.TotalScroll;
        } else {
          //alert(this.TotalScroll);
          this.CurrentPage++;
          obj.targetScroll = getScroll(obj) - this.TotalScroll;
        }
        checkPages(this);
      }
      this.CheckUpdate();
    }
  }
  
  //Initializes everything
  function mtdStart() {
    blnRunning = true;
    pauseObjectMethod(this, this.CheckUpdate, this.FadeSpeed);    
  }

  //Stops all fading etc
  function mtdStop() {
    blnRunning = false;
  }
  
  //Continues to update the scrolls
  function mtdCheckUpdate() {
    var blnChanged = false;
    if (blnRunning) {
      var o;
      for (var i=0; i<this.Objects.length; i++) {
        obj = this.Objects[i];
        o = getScroll(obj);
        if (obj.icoScroll) {      //Going Left
          if (o<obj.targetScroll) {
            setScroll(obj, o+this.ScrollStep);
            blnChanged = true;
          }
        } else {      //Going Right
          if (o>obj.targetScroll) {
            setScroll(obj, o-this.ScrollStep);
            blnChanged = true;
          }
        }
      }
      if (blnChanged) {
        pauseObjectMethod(this, this.CheckUpdate, this.ScrollSpeed);  //keep calling to continue checking
      }
    }
    blnMoving = blnChanged;
  }
  
  //Private functions
  
  function checkPages(context) {
    context.Next.style.visibility = (context.CurrentPage < context.Pages) ? 'visible' : 'hidden';
    context.Prev.style.visibility = (context.CurrentPage > 1) ? 'visible' : 'hidden';
  }
    
  //Sets scroll position of an object to the integer value
  function setScroll(obj, value) {
    obj.style.left = value + 'px';
  }
  
  //Returns the scroll of an object as an integer
  function getScroll(obj) {
    var s = obj.style.left;
    if (isNaN(parseInt(s))) {
      return 0;
    } else {
      return parseInt(s);
    }
  }
    
  //Returns the width of an object as an integer
  function getWidth(obj) {
    var s = obj.offsetWidth;
    alert(obj.id+"\n"+s);
    if (isNaN(parseInt(s))) {
      return 0;
    } else {
      return parseInt(s);
    }
  }

}