
// 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,speed,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.speed=typeof(speed)=='number'?speed:oop.speed;
		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,speed)
{
	if(typeof(oop)=='object'&&oop.scroll&&typeof(ud)=='number') {
		zxcCScroller(oop,false);
		if(ud==0) return;
		speed=speed||10;
		oop.scroll(ud*123);
		//  oop.sto=setTimeout(function(){ zxcCSScroll(oop,ud,speed); },speed);
	}
}

function zxcCScrollerInit(mode,id,ud,speed,hold,holdpos,srt)
{
	var p=document.getElementById(id);
	mode=(typeof(mode)=='string'?(mode+' '):' ').charAt(0).toLowerCase();
	if((mode!='v'&&mode!='h')||!p) return;
	if(!p[mode+'scroll']) return p[mode+'scroll']=new zxcCScrollerOOP(mode,id,ud,speed,hold,holdpos,srt);
	var oop=p[mode+'scroll']
	clearTimeout(p.to);
	oop.speed=speed||oop.speed;
	oop.ud=ud||-oop.ud;
	return oop;
}

function zxcCScrollerOOP(mode,id,ud,speed,hold,holdpos,srt)
{
	var p=document.getElementById(id);
	p.style.overflow='hidden';
	this.p=p;
	this.mode=mode;
	this.vh=mode=='v'?'top':'left';
	var os=mode=='v'?['offsetHeight','offsetTop','height']:['offsetWidth','offsetLeft','width'];
	var c=p.getElementsByTagName('div')[0];
	var clds=c.childNodes;
	for(var i=0;i<clds.length;i++) {
		if(clds[i].nodeType==1 && clds[i].innerHTML!='') { // clds[i].innerHTML!='' added by BF 2010-01-21
			this.wh=clds[i][os[0]]+clds[i][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 && clds[z2a].innerHTML!='') this.ary[z2][2].push(clds[z2a][os[1]]); // clds[z2a].innerHTML!='' added by BF 2010-01-21
		}
	}
	this.ud=ud||-1;
	this.speed=speed||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 speed=this.speed,ud=stop||this.ud;
	for(var i=0;i<this.ary.length;i++) {
		if(((ud<0)&&this.ary[i][1]<=this.data[1])||(ud>0&&this.ary[i][1]>this.data[0])) this.ary[i][1]=this.data[(ud<0)?0:1]+ud;
		this.ary[i][1]+=stop||ud;
		this.ary[i][0].style[this.vh]=this.ary[i][1]+this.holdpos+'px'
		if(!stop&&this.hold&&Math.abs(ud)==1) {
			for(var j=0;j<this.ary[i][2].length;j++) {
				if(parseInt(this.ary[i][0].style[this.vh])+this.ary[i][2][j]*ud==this.holdpos) speed=this.hold;
			}
		}
	}
	this.posary();
	if(!stop) this.to=setTimeout(function(oop){return function(){oop.scroll();}}(this),speed);
}

zxcCScrollerOOP.prototype.posary=function()
{
	for(var i=0;i<this.ary.length;i++){
		this.pos[i]=this.ary[i][1]
	}
}