/*
# File: /javascriptlibrary/contentscroller.js
# Copyright, 2005-2009 by Frank Koenen, fkoenen@feweb.net, Finkle Enterprises, LLC. All Rights Reserved. Used by Permission only.
#
# Purpose: horizontal (from left to right) or vertical (from top to bottom) scroller of content. Defines class named ContentScroller
#
# Sample Javascript and HTML:
#  <script src="/jslib/contentscroller.js" language="JavaScript" type="text/javascript"></script>
#  <script>
#    window.myscroller = new ContentScroller({myname:'window.myscroller',containerid:'elementtoscrollcontentof'})
#    var articles = new Array();
#    articles.push({displayhtml:"Welcome to Frank's world 1."});
#    articles.push({displayhtml:"Welcome to Frank's world 2.", id:null});
#    articles.push({displayhtml:"Welcome to Frank's world 3.", id:"testid"});
#    articles.push({displayhtml:"Welcome to Frank's world 4."});
#    window.myscroller.Items = articles;
#    if (window.addEventListener) window.addEventListener ("load",function(event) {window.myscroller.start();},false);
#    else if (window.attachEvent) window.attachEvent ("onload",function(event) {window.myscroller.start();});
#  </script>
#
# <body>
#   <div id="elementtoscrollcontentof"></div>
# </body>
#
# History:
# 07-Feb-09 fhk; Init version 1.0
#--------------------------------------------------
*/




// --------------------------------------------------
// The constructor ...
function ContentScroller(myargs)
{
   // required arguments ...
   this.myname = myargs.myname; // so we can talk to ourself!
   this.containerid = myargs.containerid;  // a host container (DOM) id.

   // public vars ...
   this.scrollerwidth = 350;        // Width of entire scroller.
   this.scrollerheight = 25;        // Height of scroller.
   this.pause = true;               // Pause the scroller on mouseOver, true or false
   this.orientation = "horizontal"; // Orientation of the scroller, one of: horizontal or vertical
   this.Items = new Array();        // Assign any HTML content to scroll here.
   this.smoothfactor = 4;           // Increase this to reduce loop resets to merit a smoother scroller. (2..10)
   this.moveby = 1;                 // The number of Pixels to move by. Lower number merits a smoother, slower scroll. (1..100)
   this.speed = 25;                 // Time in MS to re-trigger movement, eg: scroller speed. (smaller value :== faster scroll)



























   // private vars.
   this.arguments = null;
   this._timer = null;
   this._container = null;
   this._scrollo = null;
   this._isNS4 = (document.layers)?true:false;

   // determine a unique id set for DOM ids.
   {
     var charSet = "0123456789abcdefghijklmnopqrstuvwxyz";
     this._rc = "";
     for (var i = 1; i <= 6; ++i) this._rc += charSet.charAt(Math.floor(Math.random() * (charSet.length - 0)) + 0);
   }
}















window.ContentScroller.prototype.start = function()
{
   if ( ! this.myname || this.myname == "" ) return; // we have no name?!
   if ( ! this.containerid || this.containerid == "" ) return; // no container specified to scroll within.
   this._container = document.getElementById(this.containerid); if ( ! this._container ) return; // no container to scroll within.
   if ( this.Items.length <= 0 ) return; // nothing to scroll.

   // sane settings ...
   if ( this.smoothfactor < 2 || this.smoothfactor > 10 ) this.smoothfactor = 4;
   if ( this.moveby < 1 || this.moveby > 100 ) this.moveby = 1;

   var html = "";

   if(this._isNS4)
   {
     html='<table id="contentscrollertabletop_'+this._rc+'" border="0" cellpadding="0" cellspacing="0" width="'+this.scrollerwidth+' style="border-collapse:collapse"><tr><td>';
     html+='<table border="0" cellpadding="0" cellspacing="0" width="100%" height="'+this.scrollerheight+' style="border-collapse:collapse"><tr><td align="center" nowrap>';
     html+='</td></tr></table></td></tr></table>';
   }
   else
   {
     html='<table border="0" cellpadding="0" cellspacing="0" style="border-collapse:collapse;width:'+this.scrollerwidth+';height:'+this.scrollerheight+'">';
     html+='<tr valign="middle"><td><div id="'+this._rc+'_div" style="';
     if(this.orientation=="vertical")html+='height:'+this.scrollerheight+';';
     html+='width:'+this.scrollerwidth+'; position:relative;overflow:hidden">';
     html+='<div id="contentscroller_'+this._rc+'_div1" style="position:relative; left:0; z-index:1">';
     html+='<table border="0" name="table" cellpadding="0" cellspacing="0" style="border-collapse:collapse" id="contentscroller_'+this._rc+'_table"';
     if(this.orientation=="vertical")html+='style="width:'+this.scrollerwidth+'"';
     html+='><tr>';
     var y=0;
     while (y<this.smoothfactor)
     {
       for (i=0; i<(this.Items.length); i++)
       {
         if(this.orientation=="vertical")html+='<tr>';
         html+='<td ';
         if(this.Items[i].id&&this.Items[i].id!="")html+='id="'+this.Items[i].id+'" ';
         if(this.orientation=="horizontal")html+='nowrap';
         if(this.pause==true){html+=' onMouseOver="'+this.myname+'._stopscroller();" onMouseOut="'+this.myname+'._setpixelsandscroll()"';}
         html+='>';
         html+=this.Items[i].displayhtml;
         html+='</td>';
         if(this.orientation=="vertical")html+='</tr>';
       }
       y++;
     }
     html+='</tr></table></div></div></td></tr></table>';
   }
   this._container.innerHTML = html;

   this._setpixelsandscroll();
}

// for smooth scrolling, ensure the width of the scroller is divisible by 2
window.ContentScroller.prototype._setpixelsandscroll = function()
{
  var tableo=document.getElementById("contentscroller_"+this._rc+"_table");
  this._scrollo=document.getElementById("contentscroller_"+this._rc+"_div1");
  var dd=(this.orientation=="horizontal")?tableo.offsetWidth:tableo.offsetHeight;
  var n = ( (Math.floor(dd/2)) *2)+2;
  this._scrollo.style.width=n;
  this._move(n);
}

// move N pixels to the left
window.ContentScroller.prototype._move = function(n)
{
  var maxLeft = (0-(n/2)+2)/2;
  if(this.orientation=="horizontal")
    this._scrollo.style.left=(parseInt(this._scrollo.style.left) <= maxLeft)?0:parseInt(this._scrollo.style.left)-this.moveby;
  else
  { // vertical scroll
    if(this._scrollo.style.top=="")this._scrollo.style.top=0;
    if (parseInt(this._scrollo.style.top)<(0-(n/2)+6))this._scrollo.style.top = 0;
    else this._scrollo.style.top = parseInt(this._scrollo.style.top)-this.moveby;
  }
  this._stopscroller();
  this._timer = setTimeout (this.myname+"._move("+n+");", this.speed);
}

window.ContentScroller.prototype._stopscroller = function()
{
  if ( this._timer ) clearTimeout(this._timer);
  this._timer = null;
}

