//-------------------------------------------------------------
// FILE: slider.js
// PURPOSE: 
// PARAM: id - id of the element we're sliding
// PARAM: localRef - name of the local (page level) reference
//        to this Slider object.  Required for use of setTimeout
// EXAMPLE: var oSlider = new Slider('slider_div', 'oSlider')
//-------------------------------------------------------------

function Slider(id, localRef){

  // Make sure we've been passed a valid element id and get our handle
  try{
    this.handle = document.getElementById(id);
    if(localRef == null || localRef == '') { return false; }
  }catch(e){
    return false;
  }  

  // Properties
  this.isVisible = false;  // default to hiding the slider
  this.direction = 1; // default to 1 to match default hidden state
  this.origin    = 0; // default to top
  this.min       = 0;
  this.max       = 200;
  this.rate      = 8;
  this.slideAmt  = 3;
  this.localRef  = localRef;
  this.isSliding = false;
  this.observers = new Array();

  // Public Methods
  this.Slide           = Slider_Slide;
  this.AddObserver     = Slider_AddObserver;

  // Private Methods
  this.Animate         = Slider_Animate;
  this.SetDirection    = Slider_SetDirection;
  this.SetIsSliding    = Slider_SetIsSliding;
  
  // Events
  this.onOpen  = Slider_onOpen;
  this.onClose = Slider_onClose;
}

function Slider_Slide(){
  // Check to see if we're already sliding
  if(!this.isSliding){
    // If not, set our "Hey we're sliding" bit
    this.SetIsSliding();

    // Before we slide, let's also make sure we're sliding in the right direction
    this.SetDirection();

    if(this.isVisible){
      //Slide back in from our current coordinate which should be the max value
      this.Animate(this.max);
    }else{
      //Slide out from our current coordinate which should be the min value
      this.Animate(this.min);
    }
  }
}

function Slider_Animate(current){
  var newHeight = current + (this.slideAmt * this.direction);
  
  if (newHeight > this.max) {
    this.handle.style.height = this.max + "px";
    this.isVisible = true;
    this.SetIsSliding(); //done sliding
    this.onOpen();  // fire onOpen event
  }
  else if (newHeight < this.min) {
    this.handle.style.height = this.min + "px";
    this.handle.style.display = 'none';
    this.isVisible = false;
    this.SetIsSliding(); //done sliding
    this.onClose();  // fire onClose event
  }else{
    this.handle.style.display = 'block';
    this.handle.style.height = newHeight + "px";  
    var pause = setTimeout(this.localRef +".Animate(" + newHeight + ");",this.rate);	
  }
}

function Slider_SetDirection(){
  this.direction = (this.isVisible ? -1 : 1)
}

function Slider_SetIsSliding(){
  // Toggle the sliding state
  this.isSliding = (this.isSliding ? false : true)
}

function Slider_AddObserver(obj){
  if( (obj != null) && (typeof(obj) != 'undefined') )
    this.observers[this.observers.length] = obj;
}

function Slider_onOpen(){
  for (var i=0; i < this.observers.length; i++){
    try{
      this.observers[i].onOpen();
    }catch(e){
      // DEBUG:
        alert(e.description);
    }
  }
}

function Slider_onClose(){
  for (var i=0; i < this.observers.length; i++){
    try{
      this.observers[i].onClose();
    }catch(e){
      // DEBUG:
        alert(e.description);
    }
  }
}
   
//---------------------------
// EOF
//---------------------------
