/*
  Name:     Slider
  Descrip:  DHTML Slider using Browser Scroll as anchor
  Features: Crossbrowser compatible in post mid 5 browsers (IE5+, Nav7, Mozilla)

  How to Use:

    <div id="test" style="position:absolute;">STUFF</div>
    <sc_ript>
      if(window.Slider04) slider1 = new Slider04('slider1','test')
    </sc_ript>
*/

window.Slider04 = 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.Slider04'; //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.makeSliderDefault){
          rObj = makeSliderDefault(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 = 99999999               // 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 relative 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 = -500; //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 relative to scroll
      */

      //if(!confirm(document.body)) exit;
      if(!document.body) this.killTimer()
  
      var iCH = 0; //document.body.clientHeight;
      var iST = document.body.scrollTop + 20;
      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 = -500; //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;
  
    }

  //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.makeSliderDefault = function(mInnerHTML){
  if(!this.makeSliderDefaultRun) makeSliderDefaultRun=-1
  var msg = mInnerHTML || 'This is an example using <i>Slider04</i>.'
  var mId = 'ivanDiv'+(++makeSliderDefaultRun)
  var str = ''
  +'<table align="center" width="100%"'
  +'  id="'+mId+'"'
  +'  style="'
  +'    background-color: transparent;'
  +'    position:absolute;'
  +'    top:0px;'
  +'    left:0px;'
  +'    _isibility:hidden;'
  +'  "'
  +'><tr><td align="center" nowrap="1" style="background-color:transparent;">'
  +'<table'
  +'  cellpadding="20"'
  +'  style="'
  +'    border:3px #ffffff outset;'
  +'    background-color: #eeeeee;'
  +'    width: 350px;'
  +'    height: 175px;'
  +'  "'
  +'><tr><td'
  +'  style="'
  +'    font-family: verdana, sans-serif;'
  +'    font-size: 8pt;'
  +'    font-weight: bold;'
  +'    color: #000000;'
  +'  "'
  +'  align="center"'
  +'>'
  + msg
  +'    </td>'
  +'  </tr>'
  +'</table>'
  +'    </td>'
  +'  </tr>'
  +'</table>'

  document.write(str)  
  return mId
}
