/////////////////////////////////////////////////////////////////////////////////////
///////////////////////////      scroll layer v1.2        ///////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//
// per la creazione:
//     scrollTesto = new scroll('testo', 100, 100, 100, 100)
// poi inserire la chiamata la metodo _scrollOpenLayer prima dell'inizio del testo
//     es. scrollTesto.open()
// al termine dle testo bisogna chiudere i livelli aperti
//   document.write(scrollTesto.layersClosing)
// per lo scroll in alto
//     onMouseDown="scrollTesto.su()" onMouseUp="scrollTesto.stop()" onMouseOut="scrollTesto.stop()"
// per lo scroll in basso
//     onMouseDown="scrollTesto.giu()" onMouseUp="scrollTesto.stop()" onMouseOut="scrollTesto.stop()"



// costruttore oggetto
//  name = name istanza
//  l = left
//  t = top
//  r = right
//  b = bottom

function ScrollInit(name, left, top, right, bottom, direction) {
	this.layerObj = null
				
	this.name    = name;
	this.left    = left;
	this.top     = top;
	this.right   = right;
	this.bottom  = bottom;
	this.direction = direction;

	this.h       = bottom;
    this.w       = right;
	this.hTotal  = null ;
    this.wTotal  = null;
	this.scrollTop = 0;

	this.layerTop = 0;
    this.layerLeft = 0;
	

	this.ratio   = 0.0;
	this.spostamento = 0.05;
	this.timeOut = 120;
        
	this.offsetH  = null
 	this.offsetV  = null

	this.state = false
	this.layersClosing = ''
	
	this.init   = _scrollInit;
	this.open   = _scrollOpenLayer
	this.close  = _scrollCloseLayer
	this.auto   = _scrollAuto;
	this.move  = _scrollMove
	this.moveH  = _scrollMoveV
	this.moveV  = _scrollMoveV
	this.onUp     = _scrollOnUp
	this.onDown    = _scrollOnDown
	this.onLeft     = _scrollOnUp
	this.onRight    = _scrollOnDown
	this.onStop   = _scrollOnStop

	this.obj = name + "scroll"
	eval(this.obj + "=this")
}

function _scrollOpenLayer() {
	if (document.layers) {
		// scrive livello Netscape
		document.write('<layer id="c_' + this.name + '"');
		document.write(' left="' + this.left + '" top="' + this.top + '"');
		document.write(' clip="0,0,' + this.right + ',' + this.bottom + '" z-index="11">');
		document.write('<layer id="t_' + this.name + '" left="0" top="0" width="' + this.right + '" z-index="11">');
		this.layersClosing = '</layer></layer>';
	} else {
		// scrive livello Explorer
		document.write('<div id="c_' + this.name + '" style="position:absolute; ');
		document.write('left:' + this.left + 'px ; top:' + this.top +'px; ');
		document.write('width:' + this.right + 'px ; height:' + this.bottom +'px; ');
		document.write('z-index:11; clip: rect(0px ' + this.right + 'px ' + this.bottom + 'px 0px)">');
		document.write('<div id="t_' + this.name + '" style="position:absolute; left=0px; top=0px;');
		document.write('width=' + this.right + 'px; z-index=11">');
		this.layersClosing = '</div></div>';
	}
}

function _scrollCloseLayer() {
    	document.write(this.layersClosing);
}	

function _scrollInit() {
	    var pag;
	if (document.layers) {
		pag = 'document.c_' + this.name + '.layers';	
		stile = '';
	} else {
		pag = 'c_' + this.name + '.document.all';	
		stile = '.style';
	}

	var evalStringa = '';
	evalStringa += pag + '["t_'+ this.name + '"]' + stile;
	this.layerObj = eval(evalStringa);

	if (document.layers) this.hTotal = this.layerObj.document.height 
	else this.hTotal = eval (pag + '["t_'+ this.name + '"].scrollHeight');
	this.offsetV  = this.hTotal - this.h    

	if (document.layers) this.wTotal = this.layerObj.document.width 
	else this.wTotal = eval (pag + '["t_'+ this.name + '"].scrollWidth');
	this.offsetH  = this.wTotal - this.w
}


function _scrollAuto(speed, delay) {
	if (this.layerObj == null) this.init();
 
	this.scrollTop -= speed;
	if (this.scrollTop<(-this.hTotal)) this.scrollTop = this.h;
	this.layerObj.top = this.scrollTop;
	setTimeout(this.obj + ".auto("+speed+","+delay+")",delay)	
}


function _scrollMove(spostamento) {
	if (this.direction=='h') {
		this.moveH(spostamento);
	} else {
		this.moveV(spostamento);
	}
}

function _scrollMoveV(spostamento) {
	// inizializza le variabile
	// per lo scrolling se non sono gi&ugrave; inizializzate

	if (this.layerObj == null) this.init();
	
	
	if ((this.state) && (this.hTotal > this.h)) {
		this.ratio = this.ratio + spostamento
		if (this.ratio > 1.0) this.ratio = 1.0
		if (this.ratio < 0) this.ratio = 0

		var offsetV = Math.ceil(this.offsetV * this.ratio);
		this.layerObj.top = this.layerTop - offsetV

		if (spostamento <  0) spostamento = spostamento - 0.02
		else spostamento = spostamento + 0.02
		
		setTimeout(this.obj + ".move(" + spostamento + ")",this.timeOut)	
	}
}


function _scrollMoveH(spostamento) {
	// inizializza le variabile
	// per lo scrolling se non sono gi&ugrave; inizializzate
	if (this.layerObj == null) this.init();
	
	if ((this.state) && (this.wTotal > this.w)) {
		this.ratio = this.ratio + spostamento
		if (this.ratio > 1.0) this.ratio = 1.0
		if (this.ratio < 0) this.ratio = 0

		var offsetH = Math.ceil(this.offsetH * this.ratio);
        	this.layerObj.left = this.layerLeft - offsetH
        

		if (spostamento <  0) spostamento = spostamento - 0.02
		else spostamento = spostamento + 0.02
		
		setTimeout(this.obj + ".move(" + spostamento + ")",this.timeOut)	
	}
}


function _scrollOnUp() {
	this.state = true
	this.move(0 - this.spostamento)
}

function _scrollOnDown() {
	this.state = true
	this.move(this.spostamento)
}

function _scrollOnLeft(){
        this.state = true;
        this.move(0 - this.spostamento)
}
function _scrollOnRight(){
        this.state = true;
        this.move(this.spostamento)
}

function _scrollOnStop() {
	this.state = false
}
