
/*
  Name:     ivanSlider04  (ivanSlider revised in 2004)
  Descrip:  DHTML Slider using Browser Scroll as anchor
  Features: Crossbrowser compatible in post mid 5 browsers (IE5+, Nav7, Mozilla)
  Author:   Thomas Ballard  http://thomas.ballard.ws
  Rights:   © 2002-2004 E.S.Q. Software  http://www.ESQsoft.com
  Terms:    http://esqsoft.com/terms/

  How to Use:

    <div id="test_tag" style="position:absolute;">STUFF</div>
    <sc_ript>
      if(window.ivanSlider04) slider1 = new ivanSlider04('slider1','test_tag')
    </sc_ript>

  rev. 04 - 
    added slide methods and support to pass a slide method at instantiation
    attached to window object instead of document object (makes for shorter calls and was a better place to live)
    interval delay can be passed in
*/





window.ivanSlider04 = function(
    sObj,         // this object on the DOM
    rObj,         // a string representation of the id for the target slide object
    slideMethod,  // which style of sliding you want to use (defaults if not passed)
    hideOnDie,    // if true, when the slider processing expires, the slider object will be hidden
    minY,         // where the "top" should gravitate to
    topOnDie,     // integer value for the "top" property to be set when the slider processing expires
    intDelay,     // how many milliseconds to wait between refreshes
    mInnerHTML    // optional HTML if rObj not passed (this creates a default container)
  ){
  if(!sObj) sObj='window.ivanSlider04'; //if not used as a separate object point at this instance

  ////
  /// INITIALIZE OBJECT
  //
  if(!this.init){
    if(!document.getElementById){
      alert('error: browser DOM doesn\'t appear to support getElementById')
      return;
    }

    //attach to the HTML object on the DOM
      if(!rObj){
        if(window.makeIvanSliderDefault){
          rObj = makeIvanSliderDefault(mInnerHTML)
        }
      }
      this.rObj = document.getElementById(rObj);
      if(!this.rObj) return; //couldn't set up an object reference


    this.mY = 0                   // actual "top" position of slider
    this.mYY = 20                 // acceleration vector for Y 
    this.mTimer = null
    this.delay = intDelay || 100  // milliseconds between process calls
    this.ttl = 10               // time to live (slider stops after this decrements to zero)
    this.init = true
    this.minY = minY || 0         // dont allow "top" to go above this ceiling in the document
    this.topOnDie = topOnDie || 0 // move "top" to here on die
    this.hideOnDie = hideOnDie || 0
    this.slideStyle = slideMethod || 0
  }





  //start: define slide styles
    this.doNoSlide = function(){
      /*
        immediately jump to the correct position absolute to scroll
      */

      var undefined;  //declare for ie5
      if(!document.body || 
          document.body.scrollTop === undefined
        ){
          alert('error: doNoSlide method is not supported by this browser')
          this.killTimer()
          return
      }

      this.mY = document.body.scrollTop + 20;
      this.rObj.style.top = this.mY;

      if(--this.ttl<0){
        if(this.hideOnDie) this.mY = 0; //move off the top of the screen
        else this.mY = this.topOnDie; //move slider to specified location on ttl expire
        //if(!confirm(this.mY + '--'+this.mYY)) exit;
        this.killTimer()
      }
    }





    this.doSlide1 = function(){
      /*
        intertial slide to correct position absolute to scroll
      */

      if(!document.body) this.killTimer()
  
      var iCH = 0; //document.body.clientHeight;
      var iST = 20
      if(window.pageYOffset){ iST += window.pageYOffset; }
      else if(document.body && document.body.scrollTop){ iST += document.body.scrollTop; }
      else if(document.documentElement && document.documentElement.scrollTop){ iST += document.documentElement.scrollTop; }

      if(iST < this.minY) iST=this.minY;
      //slide to new spot slowly
      if(this.mY != iST && this.mY > this.minY){
        var diff = Math.abs(this.mY - iST);
        if(diff>0){
          this.mYY = diff / 2;
        }
  
        if(this.mY < iST){
          this.mY += this.mYY
        }
        else{
          this.mY -= this.mYY
        }
      }
      else{
        this.mY = iST;
      }
  
      //if(!confirm(this.ttl)) exit;
      //if(--this.ttl<0){
       // if(this.hideOnDie) this.mY = 0; //move off the top of the screen
        //else this.mY = this.topOnDie; //move slider to specified location on ttl expire
        //if(!confirm(this.mY + '--'+this.mYY)) exit;
       // this.killTimer()
     // }

      this.rObj.style.top = this.mY + 'px';
  
    }

  //end: define slide styles





  this.killTimer = function(){
    if(this.mTimer) clearInterval(this.mTimer);
    this.mTimer = null;
  }





  this.toggleHide = function(mVal){
    /*
      hide or unhide the attached slider object

      -1st call will unhide the object
      -if you specify an allowed value in mVal argument, the visibility is set to that value
    */

    if(!this.rObj || !this.rObj.style){
      alert('error: toggleHide interface not supported on this DOM')
    }

    var v = 'visible'
    if(this.rObj.style.visibility == 'visible') v = 'hidden'
    if(mVal && mVal.match(/visible|hidden/)) v = mVal

    this.rObj.style.visibility = v
  }






  //resolve the passed slide method to a local function
    var sStyle = this.slideStyle || 'doSlide1'
    if(!eval('this.'+sStyle)){
      alert('error: unrecognized slide method passed')
      return 0
    }


  ////
  /// SET UP RETURN VISIT
  //

  this.mTimer = eval('setInterval("'+sObj+'.'+sStyle+'();",this.delay)')
  return;
}




window.makeIvanSliderDefault = function(mInnerHTML){
  if(!this.makeIvanSliderDefaultRun) makeIvanSliderDefaultRun=-1
  var msg = mInnerHTML || 'This is an example using <i>ivanSlider04</i>.'
  var mId = 'ivanDiv'+(++makeIvanSliderDefaultRun)
  var str = ''
  +'<ul id="'+mId+'" style="position:absolute;">'
  + msg
  +'</ul>'

  document.write(str)  
  return mId
}
