//////////////////////////////////////////////////////////
//		CLASS Scroller
//////////////////////////////////////////////////////////
Scroller = function()
{
	var iIncr,sUnit,iWidth,iHeight;
	var oParent,oContent;
	var oUp,oDown,oPuce,oBarre,oShape,aAdvancePuce;
	var bUseAdvancePuce;
	var oTimerScroll;
	var bDragPuce,bScrollable;
	var bDom,sNav;
	
	this.bDisplay = true;
	this.bScrollable = false;
	this.bDragPuce = false
	this.iIncr=10;
	this.sUnit='px';
	
	this.detectBrowser();
};
//-------------------------------------------------------------------
//	PUBLIC METHOD
//-------------------------------------------------------------------
//--------------[setDimension]--------------
Scroller.prototype.setDimension = function( iWidth,iHeight)
{
	if( iWidth && this.oParent ) {
		this.setWidth( this.oParent , iWidth);
		this.iWidth = iWidth;
	}
	if( iHeight && this.oParent ) {
		this.setHeight( this.oParent , iHeight);
		this.iHeight = iHeight;
	}
	
	this.isScrollable();
	this.refreshPosition();
	this.moveTo(0);
};
//--------------[setShape]--------------
Scroller.prototype.setShape = function( sTagId )
{ 
	if( !this.bDisplay ) return;
	this.oShape = this.getObjet(sTagId);
};
//--------------[setParent]--------------
Scroller.prototype.setParent = function( sTagId )
{
	if( !this.bDisplay ) return;
	this.oParent = this.getObjet(sTagId);
	this.oParent.style.position='absolute';
	this.iWidth  = this.getWidth( this.oParent );
	this.iHeight = this.getHeight( this.oParent );
	this.oParent.oScroll = this;
	this.oParent.onmousewheel = function(){if(! this.oScroll.isScrollable() ) return ;this.oScroll.moveFrom(-event.wheelDelta/120*this.oScroll.iIncr);return false;};

};
//--------------[setContent]--------------
Scroller.prototype.setContent = function( sTagId )
{
	if( !this.bDisplay ) return;
	this.oContent = this.getObjet(sTagId);
	this.oContent.style.position='absolute';
	this.setTop(this.oContent, 0 );
	this.isScrollable();

};
//--------------[setControlUp]--------------
Scroller.prototype.setControlUp = function( sTagId )
{ 
	if( !this.bDisplay ) return;
	this.oUp = this.getObjet(sTagId);
	this.oUp.oScroll = this;
	this.oUp.onmousedown = function(){ if(! this.oScroll.isScrollable() ) return ; clearInterval(this.oScroll.oTimerScroll);this.oScroll.oTimerScroll = setInterval("this.oScroll.moveFrom(-this.oScroll.iIncr);",100);return false; };
	this.oUp.onmouseup = function(){ clearInterval(this.oScroll.oTimerScroll) };
	this.oUp.onmousemove = function(){ return false; }
	this.setPositionUp();
	this.setVisibily(this.oUp,true);
};
//--------------[setControlDown]--------------
Scroller.prototype.setControlDown = function( sTagId ) {
	if( !this.bDisplay ) return;
	this.oDown = this.getObjet(sTagId);
	this.oDown.oScroll = this;
	this.oDown.onmousedown = function(){ if(! this.oScroll.isScrollable() ) return ;clearInterval(this.oScroll.oTimerScroll);this.oScroll.oTimerScroll = setInterval("this.oScroll.moveFrom(this.oScroll.iIncr);",100);return false; };
	this.oDown.onmouseup = function(){ clearInterval(this.oScroll.oTimerScroll) };
	this.oDown.onmousemove = function(){ return false; }
	this.setPositionDown();
	this.setVisibily(this.oDown,true);
};
//--------------[setAdvanceControlPuce]--------------
Scroller.prototype.setAdvanceControlPuce = function( sTagTopId, sTagBotId, sTagBgId, sTagCenterId ) {
	if( !this.bDisplay ) return;
	this.bUseAdvancePuce = true;
	this.aAdvancePuce = new Array();
	this.aAdvancePuce[0] = this.getObjet( sTagTopId );
	this.aAdvancePuce[1] = this.getObjet( sTagBotId );
	this.aAdvancePuce[2] = this.getObjet( sTagBgId );
	if( sTagCenterId != '' ) this.aAdvancePuce[3] = this.getObjet( sTagCenterId );
	
	for(i in this.aAdvancePuce ){
		this.setVisibily(this.aAdvancePuce[i],true);
		this.aAdvancePuce[i].style.position = 'absolute';
		this.aAdvancePuce[i].style.zIndex = 3+i;
		this.aAdvancePuce[i].oScroll = this;
		this.aAdvancePuce[i].onmousedown = function(){ clearInterval(this.oScroll.oTimerScroll);this.oScroll.bDragPuce = true;  };
		this.aAdvancePuce[i].onmouseup = function(){ this.oScroll.bDragPuce = false; };
		this.aAdvancePuce[i].onmousemove = function(oEvent){
			if(!oEvent) oEvent= event;
			if(this.oScroll.bDragPuce)this.oScroll.convertBarrePos(this.oScroll.getMouseTop(oEvent)-this.oScroll.getTop( this.oScroll.oParent ) -this.oScroll.getHeight( this.oScroll.oUp )  );
			return false;
		}
	}
	this.setPositionPuce();
}
//--------------[setControlPuce]--------------
Scroller.prototype.setControlPuce = function( sTagId ) {
	if( !this.bDisplay ) return;
	this.bUseAdvancePuce = false;
	this.oPuce = this.getObjet(sTagId);
	this.oPuce.oScroll = this;
	
	this.oPuce.onmousedown = function(){ clearInterval(this.oScroll.oTimerScroll);this.oScroll.bDragPuce = true;  };
	this.oPuce.onmouseup = function(){ this.oScroll.bDragPuce = false; };
	this.oPuce.onmousemove = function(oEvent){
		if(!oEvent) oEvent= event;
		if(this.oScroll.bDragPuce)this.oScroll.convertBarrePos(this.oScroll.getMouseTop(oEvent)-this.oScroll.getTop( this.oScroll.oParent ) -this.oScroll.getHeight( this.oScroll.oUp )  );
		return false;
	}
	this.oPuce.style.position = 'absolute';
	this.oPuce.style.zIndex = 3;
	this.setTop(this.oPuce, this.getTop( this.oParent )+this.getHeight( this.oUp ) );
	this.setVisibily(this.oPuce,true);
};
//--------------[setControlBarre]--------------
Scroller.prototype.setControlBarre = function( sTagId ) {
	if( !this.bDisplay ) return;
	this.oBarre = this.getObjet(sTagId);
	this.oBarre.oScroll = this;
	this.oBarre.onclick = function(oEvent){ clearInterval(this.oScroll.oTimerScroll); if(!oEvent) oEvent= event; this.oScroll.convertBarrePos(this.oScroll.getMouseTop(oEvent)-this.oScroll.getTop( this)) };
	this.oBarre.ondrag = function(oEvent){ clearInterval(this.oScroll.oTimerScroll); if(!oEvent) oEvent= event; this.oScroll.convertBarrePos(this.oScroll.getMouseTop(oEvent)-this.oScroll.getTop( this)) };
	this.setPositionBarre();
	this.setVisibily(this.oBarre,true);
};
//-------------------------------------------------------------------
//	 EVENT
//-------------------------------------------------------------------
Scroller.prototype.onChangeScrollable = function(bSrollable){};

//-------------------------------------------------------------------
//	PRIVATE  METHOD
//-------------------------------------------------------------------
//--------------[detectBrowser]--------------
Scroller.prototype.detectBrowser = function()
{
	this.bDom  = ( document.getElementById )?true:false;
	if( ! this.bDom) this.bDisplay = false;
	if( document.layers ) {
		this.sNav = 'N4',
		this.bDisplay = false;
	}
}
//--------------[isScrollable]--------------
Scroller.prototype.isScrollable = function( )
{
	var bOldScrollable = this.bScrollable;
	if(! this.oContent || ! this.oParent ){
		this.bScrollable = false;
		if(bOldScrollable!=this.bScrollable) this.onChangeScrollable( this.bScrollable );
		return this.bScrollable;
	}
	this.bScrollable = ( this.getHeight(this.oContent) > this.getHeight(this.oParent) )?true:false;
	if(bOldScrollable!=this.bScrollable) this.onChangeScrollable( this.bScrollable );
	return this.bScrollable;
};
//--------------[reset]--------------
Scroller.prototype.reset	= function()
{
	this.moveTo(0);
	this.setVisibily(this.oDown,false);this.oDown=null;
	this.setVisibily(this.oUp,false);this.oUp=null;
	this.setVisibily(this.oBarre,false);this.oBarre=null;
	this.setVisibily(this.oPuce,false);this.oPuce=null;
	for(i in this.aAdvancePuce ) this.setVisibily( this.aAdvancePuce[i],false);
	this.aAdvancePuce=new Array();
};
//--------------[refreshPosition]--------------
Scroller.prototype.refreshPosition	= function()
{
	if(this.oDown) this.setPositionDown();
	if(this.oUp) this.setPositionUp();
	if(this.oBarre) this.setPositionBarre();
	if(this.oPuce) this.setPositionPuce();
	if(this.oPuce) this.setPositionPuce();
	if(this.oShape) this.setPositionShape();
};
//--------------[setPositionShape]--------------
Scroller.prototype.setPositionShape = function( ){ this.setHeight( this.oShape,this.getHeight(this.oParent) ); };

//--------------[setPositionUp]--------------
Scroller.prototype.setPositionUp = function(  ){
	this.oUp.style.position = 'absolute';
	this.oUp.style.zIndex = 1;
	this.setTop(this.oUp, this.getTop( this.oParent ) );
}
//--------------[setPositionDown]--------------
Scroller.prototype.setPositionDown = function(  ){
	this.oDown.style.position = 'absolute';
	this.oDown.style.zIndex = 2;
	this.setTop(this.oDown, this.getTop( this.oParent )+this.getHeight( this.oParent )-this.getHeight( this.oDown ) );
}
//--------------[setPositionBarre]--------------
Scroller.prototype.setPositionBarre = function(  ){
	this.oBarre.style.position = 'absolute';
	this.oBarre.style.zIndex = 0;
	this.setTop(this.oBarre, this.getTop( this.oParent ) + this.getHeight( this.oUp ) );
	this.setHeight(this.oBarre, this.getHeight( this.oParent ) - this.getHeight( this.oDown ) - this.getHeight( this.oUp ) );
}
//--------------[convertBarrePos]--------------
Scroller.prototype.convertBarrePos = function( iPixel ) {
	var iDelta = this.getHeight( this.oParent ) - this.getHeight( this.oDown )-this.getHeight( this.oUp );
	var iRatio = iPixel/iDelta;
	var iMax = this.getHeight( this.oParent )-this.getHeight( this.oContent )
	this.moveTo(iRatio*iMax)
}
//--------------[getHeightAdvancePuce]--------------
Scroller.prototype.getHeightAdvancePuce = function()
{ 
	if( ! this.bUseAdvancePuce ) return;
	
	var iRap = this.getHeight( this.oContent ) / this.getHeight( this.oParent );
	var iMax = this.getHeight( this.oParent ) - this.getHeight( this.oDown )-this.getHeight( this.oUp );
	var iMin = this.getHeight( this.aAdvancePuce[0] ) + this.getHeight( this.aAdvancePuce[1] );
	iMin = ( this.aAdvancePuce.length==4)?iMin+this.getHeight( this.aAdvancePuce[3] ):iMin;
	if(iRap<1)iRap=1
	var iHeight = iMin+((iMax-iMin)/iRap);
	return iHeight

};
//--------------[setPositionPuce]--------------
Scroller.prototype.setPositionPuce = function()
{ 
	if( ! this.bUseAdvancePuce ){
		var iMin = this.getTop( this.oParent )+this.getHeight( this.oUp );
		var iDelta = this.getHeight( this.oParent ) - this.getHeight( this.oDown )-this.getHeight( this.oUp )-this.getHeight( this.oPuce );
		var iPos =  parseInt( this.oContent.style.top )
		var iRatio = iPos/( this.getHeight( this.oParent )-this.getHeight( this.oContent ) );
		
		this.setTop(this.oPuce,iMin + iRatio*iDelta);
		//this.setLeft(this.oPuce,this.getLeft(this.oParent) + this.getWidth(this.oParent) + 4);
	}else{
		var iMin = this.getTop( this.oParent )+this.getHeight( this.oUp );
		var iDelta = this.getHeight( this.oParent ) - this.getHeight( this.oDown )-this.getHeight( this.oUp )-this.getHeightAdvancePuce();
		var iPos =  parseInt( this.oContent.style.top )
		var iRatio = iPos/( this.getHeight( this.oParent )-this.getHeight( this.oContent ) );
		
		this.setTop( this.aAdvancePuce[0] , iMin + iRatio*iDelta );
		this.setTop( this.aAdvancePuce[1] , this.getTop( this.aAdvancePuce[0] ) +this.getHeightAdvancePuce() - this.getHeight( this.aAdvancePuce[1] ) );
		this.setTop( this.aAdvancePuce[2] , this.getTop( this.aAdvancePuce[0] ) +this.getHeight( this.aAdvancePuce[0] )  );
		this.setHeight( this.aAdvancePuce[2] , this.getHeightAdvancePuce() -this.getHeight( this.aAdvancePuce[0] )  - this.getHeight( this.aAdvancePuce[1] ) );
		if( this.aAdvancePuce.length == 4 )  this.setTop( this.aAdvancePuce[3] , this.getTop( this.aAdvancePuce[0] ) +this.getHeight( this.aAdvancePuce[0] ) -this.getHeight( this.aAdvancePuce[3] )/2+ this.getHeightAdvancePuce()/2);
	}
};
//--------------[moveFrom]--------------
Scroller.prototype.moveFrom = function( iPixel )
{ 
	var iTop = parseInt( this.oContent.style.top ) - iPixel;
	if( iTop > 0 ) iTop=0;
	if( iTop < this.getHeight( this.oParent )-this.getHeight( this.oContent ) ) iTop=this.getHeight( this.oParent )-this.getHeight( this.oContent );
	
	this.setTop(this.oContent,iTop);
	this.setPositionPuce();
};
//--------------[moveTo]--------------

Scroller.prototype.moveTo = function( iPixel )
{ 
	if( iPixel > 0 ) iPixel=0;
	if(this.isScrollable() )if( iPixel < this.getHeight( this.oParent )-this.getHeight( this.oContent ) ) iPixel=this.getHeight( this.oParent )-this.getHeight( this.oContent );
	this.setTop(this.oContent,iPixel);
	this.setPositionPuce();
};
//-------------------------------------------------------------------
//	PRIVATE GET/SET 
//-------------------------------------------------------------------

//--------------[getObjet]--------------
Scroller.prototype.getObjet = function( sTagId )
{ 
	if( this.bDom ) return document.getElementById(sTagId); 
	if( this.sNav=='N4') return document.layers[sTagId]; 
};
//--------------[getWidth]--------------
Scroller.prototype.getWidth = function( oTag )
{ 
	if( ! oTag )return 0;
	if( this.sNav=='N4') return 0; 
	return oTag.offsetWidth;
};
//--------------[getHeight]--------------
Scroller.prototype.getHeight = function( oTag )
{ 
	if( ! oTag )return 0;
	if( this.sNav=='N4') return 0; 
	return oTag.offsetHeight;
};
//--------------[getTop]--------------
Scroller.prototype.getTop = function( oTag ){ if( ! oTag )return 0;if(oTag.offsetParent)return (oTag.offsetTop + this.getTop(oTag.offsetParent)); else return (oTag.offsetTop); }
//--------------[getLeft]--------------
Scroller.prototype.getLeft = function( oTag ){ if( ! oTag )return 0;if(oTag.offsetParent)return (oTag.offsetLeft + this.getLeft(oTag.offsetParent)); else return (oTag.offsetLeft); }
//--------------[setLeft]--------------
Scroller.prototype.setLeft = function( oTag ,iPixel ){ if( ! oTag )return;oTag.style.left = iPixel+this.sUnit; }
//--------------[setTop]--------------
Scroller.prototype.setTop = function( oTag ,iPixel ){ if( ! oTag )return;if(isNaN(iPixel))return;;oTag.style.top = iPixel+this.sUnit; }
//--------------[setWidth]--------------
Scroller.prototype.setWidth = function( oTag ,iPixel ){ oTag.style.width = iPixel+this.sUnit; }
//--------------[setHeight]--------------
Scroller.prototype.setHeight = function( oTag ,iPixel ){ if( ! oTag )return;oTag.style.height = iPixel+this.sUnit; }
//--------------[setVisibily]--------------
Scroller.prototype.setVisibily = function( oTag ,bVisible ){ if( ! oTag )return;oTag.style.visibility = (bVisible)?'visible':'hidden'; }
//--------------[getMouseTop]--------------
Scroller.prototype.getMouseTop = function(oEvent){ if(oEvent.y) return oEvent.y; if(oEvent.pageY) return oEvent.pageY; }