// Continuous ScrollerII (22-April-2009)
// by: Vic Phillips http://www.vicsjavascripts.org.uk

// To provide a continuous Scroll any number of images or HTML messages in a banner of any length
// For both Vertical or Horizontal Applications.
// With event call functions to Stop or Start the scroll
// and to change the direction between scroll left and right.

// ****** Application Notes.


// **** Initialising the Script.
//
// The  script would normally be initialised by BODY or window event call to function
// var S1=new zxcCScrollerOOP('h','tst2','start',1,100,200);
// where:
//  parameter 0 = the mode, for vertical 'v', for horizontal 'h'.                               (string 'v' or 'h')
//  parameter 1 = the unique id name of the scroll container.                                   (string)
//  parameter 2 = (optional) to scroll up/left = negative digit or down/right = positive digit. (digit, default = -1)
//                may also be used to control the scroll speed.
//  parameter 3 = (optional) the scroll speed (milli seconds).                                  (digits, default = 100)
//  parameter 4 = (optional) the hold delay in milli seconds(see Note 1).                       (digits, default = no hold)
//  parameter 5 = (optional) the hold position from the left/top (px).                          (digits, default = 0)
//  parameter 6 = (optional) the initial delay before scrolling (milli seconds).                (digits, default = no auto start)
//
// Note(Important):
// The instance of the script must be  assigned to a global variable to allow subsequent control of the script.
//
// **** Control of the Scroller.
//
// The Stop/Start, direction, speed, hold delay and hold position
// of the Scroller instance by be controlled by an event call to function:
// zxcCScroller(S1,true);
// where:
//  parameter 0 = the global variable referencing the instance of the script.                   (global variable)
//  parameter 1 = (optional boolean) true = run scroll, false = stop scroll.                    (boolean, default = the existing run condition)
//  parameter 1 = (optional string)  toggle run.                                                (string, default = the existing run condition)
//  parameter 2 = (optional digits) to scroll up/left = negative digit or down/right = positive digit. (digit, default = the current direction)
//  parameter 2 = (optional string) toggle the direction.                                       (string, default = the current direction)
//  parameter 3 = (optional) the scroll speed (milli seconds).                                  (digits, default = the current speed)
//  parameter 4 = (optional) the hold delay in milli seconds(see Note 1).                       (digits, default =  the current hold delay)
//  parameter 5 = (optional) the hold position from the left/top (px).                          (digits, default =  the current hold position)
//
// The Scroller instance may be scrolled by an event call to function:
// zxcCSScroll(S1,1,50);
// where:
//  parameter 0 = the global variable referencing the instance of the script.                    (global variable)
//  parameter 1 = to scroll up/left = negative digit or down/right = positive digit(see Note 2). (digit)
//  parameter 2 = (optional) the scroll speed (milli seconds).                                   (digits, default = 10)
//

// **** Integration with other Applications.
//
// There are a minimum of three scroll objects for each instance of the script.
// The current position of these object are stored in an array S1.pos
// The maximum and minimum scroll extremities are stored in an array S1.data
// where field[0] = the maximum and field[1] the minimum.
// where S1 is the global variable referencing the instance of the script.
//

// Note 1
//  The hold delay may only be used if parameter 2 is 1 or -1.
//
// Note 2
//  The scroll may be stopped by using function zxcCScroller(S1,false) or zxcCSScroll(S1,0);.
//


// **** Functional Code(3.42K) - NO NEED to Change.

function zxcCScroller(oop,run,ud,spd,hold,holdpos){
 if (typeof(oop)=='object'&&oop.scroll){
  clearTimeout(oop.to);
  clearTimeout(oop.sto);
  if (typeof(run)=='boolean') oop.run=run;
  else if (typeof(run)=='string') oop.run=!oop.run;
  if (typeof(ud)=='number') oop.ud=ud;
  else if (typeof(ud)=='string') oop.ud=-oop.ud;
  oop.spd=typeof(spd)=='number'?spd:oop.spd;
  oop.hold=typeof(hold)=='number'?hold:oop.hold;
  oop.holdpos=typeof(hold)=='number'?holdpos:oop.holdpos;
  if (oop.run) oop.scroll();
 }
}

function zxcCSScroll(oop,ud,spd){
 if (typeof(oop)=='object'&&oop.scroll&&typeof(ud)=='number'){
  zxcCScroller(oop,false);
  if (ud==0) return;
  spd=spd||10;
  oop.scroll(ud*123);
//  oop.sto=setTimeout(function(){ zxcCSScroll(oop,ud,spd); },spd);
 }
}

function zxcCScrollerInit(mde,id,ud,spd,hold,holdpos,srt){
 var p=document.getElementById(id);
 mde=(typeof(mde)=='string'?(mde+' '):' ').charAt(0).toLowerCase();
 if ((mde!='v'&&mde!='h')||!p) return;
 if (!p[mde+'scroll']) return p[mde+'scroll']=new zxcCScrollerOOP(mde,id,ud,spd,hold,holdpos,srt);
 var oop=p[mde+'scroll']
 clearTimeout(p.to);
 oop.spd=spd||oop.spd;
 oop.ud=ud||-oop.ud;
 return oop;
}

function zxcCScrollerOOP(mde,id,ud,spd,hold,holdpos,srt){
 var p=document.getElementById(id);
 p.style.overflow='hidden';
 this.p=p;
 this.mde=mde;
 this.vh=mde=='v'?'top':'left';
 var os=mde=='v'?['offsetHeight','offsetTop','height']:['offsetWidth','offsetLeft','width'];
 var c=p.getElementsByTagName('UL')[0];
 var clds=c.childNodes;
 for (var z0=0;z0<clds.length;z0++){
  if (clds[z0].nodeType==1){
   this.wh=clds[z0][os[0]]+clds[z0][os[1]];
  }
 }
 holdpos=(typeof(holdpos)=='number'?holdpos:0)-this.wh;
 c.style.position='absolute';
 c.style[this.vh]=holdpos+'px';
 c.style[os[2]]=this.wh+'px';
 var max=(c[os[0]]+p[os[0]]);
 var pos=0;
 this.ary=[[c,0,[]]];
 while (pos<max){
  var z1=this.ary.length;
  this.ary[z1]=[c.cloneNode(true),pos+=this.wh,[]];
  this.ary[z1][0].style[this.vh]=this.ary[z1][1]+holdpos+'px';
  p.appendChild(this.ary[z1][0]);
 }
 for (var clds,z2=0;z2<this.ary.length;z2++){
  clds=this.ary[z2][0].childNodes;
  for (var z2a=0;z2a<clds.length;z2a++){
   if (clds[z2a].nodeType==1) this.ary[z2][2].push(clds[z2a][os[1]]);
  }
 }
 this.ud=ud||-1;
 this.spd=spd||100;
 this.hold=typeof(hold)=='number'?hold:false;
 this.holdpos=typeof(hold)=='number'?holdpos:0;
 this.to=null;
 this.sto=null;
 this.data=[pos,-this.wh];
 this.run=false;
 this.pos=[];
 this.posary();
 if (typeof(srt)=='number'){
  this.run=true;
  this.to=setTimeout(function(oop){return function(){oop.scroll();}}(this),srt);
 }
}

zxcCScrollerOOP.prototype.scroll=function(stop){
 var spd=this.spd,ud=stop||this.ud;
 for (var z0=0;z0<this.ary.length;z0++){
  if (((ud<0)&&this.ary[z0][1]<=this.data[1])||(ud>0&&this.ary[z0][1]>this.data[0])) this.ary[z0][1]=this.data[(ud<0)?0:1]+ud;
  this.ary[z0][1]+=stop||ud;
  this.ary[z0][0].style[this.vh]=this.ary[z0][1]+this.holdpos+'px'
  if (!stop&&this.hold&&Math.abs(ud)==1){
   for (var z0a=0;z0a<this.ary[z0][2].length;z0a++){
    if (parseInt(this.ary[z0][0].style[this.vh])+this.ary[z0][2][z0a]*ud==this.holdpos) spd=this.hold;
   }
  }
 }
 this.posary();
 if (!stop) this.to=setTimeout(function(oop){return function(){oop.scroll();}}(this),spd);
}

zxcCScrollerOOP.prototype.posary=function(){
 for (var z0=0;z0<this.ary.length;z0++){
  this.pos[z0]=this.ary[z0][1]
 }
}



