/*********************************************************
class Scroll

	Description:
		DHTML-scroll.

	Variables:
		sObject				= Object name.
		sLayer				= Content-layer name.
		iRealX				= input x-coordinate.
		iRealY				= input y-coordinate.
		iWidth				= Width of scroll (excluding the scrollbar).
		iHeight				= Height of the scroll.
		iArrowWidth			= Width of the arrow-buttons.
		iArrowHeight		= Height of the arrow-buttons.
		iBoxHeight			= Height of the scroll-box (width is the same as iArrowWidth).
		iStartSpeed			= Startspeed when arrows are pressed.
		iScrollSpeed		= Scrollspeed when arrows are held.
		sArrowUp			= Arrow-up image.
		sArrowDown			= Arrow-down image.
		sScrollBox			= Scroll-box image.
		sScrollBackground	= Scroll-background image.
		sScrollPix			= Transparent pixel-image.
		iX					= x-pos of scroll.
		iY					= y-pos of scroll.
		sAlign				= Alignment of scroll (default left)
		iDocumentWidth		= Width of "fake" document (used if alignment is center).
		bVisible			= Is the scroll visible?
		oTimer				= Timer when the arrows are held.
		iContentHeight		= Total height of the content-layer.
		iScrollY			= NOF pixels of the content that are scrolled.

	Public methods:
		bSetAlign			= Set alignment "center","left" or "right". Default "left".
		bSetImages			= Set scrollbar-images and dimensions.
		bSetFollowLayer		= Set the scroll to follow a relative layer.

	History:
		2002-04-02	Daniel Önnerby	Init version.

*********************************************************/
function Scroll(sObject,sLayer,iX,iY,iWidth,iHeight,iArrowX,iArrowY){

	this.sObject		= sObject;
	this.sLayer			= sLayer;
	this.iRealX			= iX;
	this.iRealY			= iY;
	this.iWidth			= iWidth;
	this.iHeight		= iHeight;
	this.iArrowX		= iArrowX;
	this.iArrowY		= iArrowY;

	/********* Default dimensions **********/
	this.iArrowWidth	= 16;
	this.iArrowHeight	= 16;
	this.iBoxHeight		= 14;

	/****** Arrow button scroll speed ******/
	this.iStartSpeed	= 10;
	this.iScrollSpeed	= 6;

	/*********** Default images ************/
	this.sArrowUp			= "../images/scroll_arrow_up.gif";
	this.sArrowDown			= "../images/scroll_arrow_down.gif";
	this.sScrollBox			= "../images/scroll_box.gif";
	this.sScrollBackground	= "../images/scroll_bg.gif";
	this.sScrollPix			= "../images/pix.gif";

	/************ Private Variables ********/
	this.iX				= iX;
	this.iY				= iY;
	this.sAlign			= "left";
	this.iDocumentWidth	= 0;
	this.bVisible		= false;
	this.oTimer			= null;

	this.iContentHeight	= dom==1?document.layers[this.sLayer].clip.height:dom==2?document.all[this.sLayer].offsetHeight:dom==3?document.getElementById(this.sLayer).offsetHeight:0;
	this.iScrollY		= 0;
	this.sFollowLayer	= null;

	/*********** Scroll Methods ************/
	this.bSetAlign		= s_bSetAlign;
	this.bSetImages		= s_bSetImages;

	this.bCreate		= s_bCreate;
	this.bShow			= s_bShow;
	this.bMove			= s_bMove;
	this.bResize		= s_bResize;

	this.bStopScroll	= s_bStopScroll;
	this.bStartScroll	= s_bStartScroll;
	this.bArrowMove		= s_bArrowMove;

	this.bScrollFrontDown	= s_bScrollFrontDown;
	this.bScrollFrontMove	= s_bScrollFrontMove;
	this.bMouseWheel		= s_bMouseWheel;
	this.bSetFollowLayer	= s_bSetFollowLayer;

	aScrolls[aScrolls.length]=this;
}

	/*****************************************************
	function bSetAlign

		Description:
			Align the scroll different. Used when sAlign is not "left".

		Input:
			sAlign			=	"center","left" or "right"
			iDocumentWidth	=	used when sAlign="center". iX is then the iX pixels from centered document with iDocumentWidth.

		History:
			2002-04-02	Daniel Önnerby	Init version.

	*****************************************************/
	function s_bSetAlign( sAlign,iDocumentWidth ){
		var iScreenWidth=(dom==1?self.innerWidth:dom==2?document.body.clientWidth:dom==3?self.innerWidth:0);
		if(sAlign){
			this.sAlign=sAlign;
			this.iDocumentWidth=iDocumentWidth;
		}

		switch(this.sAlign){
			case "center":
				if(iDocumentWidth>=iScreenWidth){
					this.iX=this.iRealX;
				}else{
					this.iX=this.iRealX+(iScreenWidth-this.iDocumentWidth)/2;
				}
				break;
			case "right":
				this.iX=iScreenWidth-this.iRealX-this.iWidth-this.iArrowWidth;
				break;
			case "follow":
				this.iX=iGetLayerX(this.sFollowLayer)+this.iRealX;
				this.iY=iGetLayerY(this.sFollowLayer)+this.iRealY;
				break;
			default:
				this.iX=this.iRealX;
		}
	}

	/*****************************************************
	function bCreate

		Description:
			Creates the scroll and shows/hides it.

		Input:
			bShow	=	Show or Hide the scroll?

		History:
			2002-04-02	Daniel Önnerby	Init version.

	*****************************************************/
	function s_bCreate( bShow ){
		/************ Create scrollbar background ************/
		var sTemp="<table border=0 cellpadding=0 cellspacing=0> \n";
		sTemp+="<tr><td><a href=\"Javascript:void(null);\" onmousedown=\"return "+this.sObject+".bStartScroll(true);\" onmouseup=\""+this.sObject+".bStopScroll(true);\" onmouseout=\""+this.sObject+".bStopScroll(true);\"><img src=\""+this.sArrowUp+"\" width="+this.iArrowWidth+" height="+this.iArrowHeight+" border=0></a></td></tr> \n";
//		sTemp+="<tr><td background=\""+this.sScrollBackground+"\"><img src=\""+this.sScrollPix+"\" width="+this.iArrowWidth+" height="+(this.iHeight-this.iArrowHeight*2)+" border=0></td></tr> \n";
		sTemp+="<tr><td><a href=\"Javascript:void(null);\" onmousedown=\"return "+this.sObject+".bStartScroll(false);\" onmouseup=\""+this.sObject+".bStopScroll(false);\" onmouseout=\""+this.sObject+".bStopScroll(false);\"><img src=\""+this.sArrowDown+"\" width="+this.iArrowWidth+" height="+this.iArrowHeight+" border=0></a></td></tr> \n";
		sTemp+="</table> ";
		sPostCreateLayer(this.sLayer+"barbg",this.iX+this.iWidth,this.iY,sTemp,this.iArrowWidth);

		/************ Create scrollbar Box ************/
//		sTemp="<img src=\""+this.sScrollBox+"\" width="+this.iArrowWidth+" height="+this.iBoxHeight+">";
//		sPostCreateLayer(this.sLayer+"barbox",0,0,sTemp,this.iArrowWidth);

		/************ Create scrollbar Top ************/
//		sTemp="<img src=\""+this.sScrollPix+"\" width="+this.iArrowWidth+" height="+(this.iHeight-this.iArrowHeight*2)+" border=0>";
//		sPostCreateLayer(this.sLayer+"barfront",0,0,sTemp,this.iArrowWidth,this.sObject+".bScrollFrontDown");

		this.bShow(bShow,true);
		if(dom==2){
			document.all[this.sLayer].onmousewheel=sglobal_bMouseWheel;
		}
	}

	/*****************************************************
	function bShow

		Description:
			Shows/hides the scroll.

		Input:
			bShow		=	Show or Hide?
			bFirstTime	=	[internal] True if bShow is run from bCreate.

		History:
			2002-04-02	Daniel Önnerby	Init version.

	*****************************************************/
	function s_bShow( bShow,bFirstTime ){
		this.bVisible=bShow;

		bMoveLayer(this.sLayer,this.iX,this.iY);
		bMoveLayer(this.sLayer+"barbg",this.iArrowX,this.iArrowY);
//		bMoveLayer(this.sLayer+"barfront",this.iX+this.iWidth,this.iY+this.iArrowHeight);

		if(this.iContentHeight>=this.iHeight){
			if(bFirstTime){
				this.bMove(this.iScrollY);
			}
//			bShowLayer(this.sLayer+"barbox",true);
			bShowLayer(this.sLayer+"barbg",true);
//			bShowLayer(this.sLayer+"barfront",true);
		}
		bShowLayer(this.sLayer,true);
	}

	/*****************************************************
	function bMove

		Description:
			Scroll the scroll :)

		Input:
			iScrollY	=	pixels to move the content in the scroll up.

		History:
			2002-04-02	Daniel Önnerby	Init version.

	*****************************************************/
	function s_bMove( iScrollY ){
		this.iScrollY=iScrollY>this.iContentHeight-this.iHeight?this.iContentHeight-this.iHeight:iScrollY<0?0:iScrollY;

//		bMoveLayer(this.sLayer+"barbox",this.iX+this.iWidth,this.iY+this.iArrowHeight+(this.iHeight-this.iArrowHeight*2-this.iBoxHeight)*this.iScrollY/(this.iContentHeight-this.iHeight));
		bMoveClipLayer(this.sLayer,this.iX,this.iY,this.iWidth,this.iHeight,0,this.iScrollY);
	}

	/*****************************************************
	function bSetImages

		Description:
			Change the images and scrollbar-dimensions.

		Input:
			sArrowUp			=	self explaining.
			sArrowDown			=	self explaining.
			sScrollBox			=	self explaining.
			sScrollBackground	=	self explaining.
			iArrowWidth			=	self explaining.
			iArrowHeight		=	self explaining.
			iBoxHeight			=	self explaining.

		History:
			2002-04-02	Daniel Önnerby	Init version.

	*****************************************************/
	function s_bSetImages( sArrowUp,sArrowDown,sScrollBox,sScrollBackground,iArrowWidth,iArrowHeight,iBoxHeight ){

		this.iArrowWidth=iArrowWidth;
		this.iArrowHeight=iArrowHeight;
		this.iBoxHeight=iBoxHeight;

		this.sArrowUp=sArrowUp;
		this.sArrowDown=sArrowDown;
		this.sScrollBox=sScrollBox;
		this.sScrollBackground=sScrollBackground;

	}


	/*****************************************************
	function bStopScroll

		Description:
			Stop scrolling. (release of the arrow-buttons)

		Input:
			bUp		=	Scrolling up?

		History:
			2002-04-02	Daniel Önnerby	Init version.

	*****************************************************/
	function s_bStopScroll( bUp ){
		clearTimeout(this.oTimer);
		this.bResize();
	}

	/*****************************************************
	function bStartScroll

		Description:
			Start scrolling. (run from arrow-buttons)

		Input:
			bUp		=	Scrolling up?

		History:
			2002-04-02	Daniel Önnerby	Init version.

	*****************************************************/
	function s_bStartScroll( bUp ){
		this.bMove(this.iScrollY+(bUp?-this.iStartSpeed:this.iStartSpeed));
		this.oTimer=setTimeout(this.sObject+".bArrowMove("+bUp+");",100);
		return false;
	}

	/*****************************************************
	function bArrowMove

		Description:
			Loop when mousebutton is held in on the arrow-buttons.

		Input:
			bUp		=	Scrolling up?

		History:
			2002-04-02	Daniel Önnerby	Init version.

	*****************************************************/
	function s_bArrowMove( bUp ){
		this.bMove(this.iScrollY+(bUp?-this.iScrollSpeed:this.iScrollSpeed));
		this.oTimer=setTimeout(this.sObject+".bArrowMove("+bUp+");",20);
	}

	/*****************************************************
	function bResize

		Description:
			Run when window is resized.

		History:
			2002-04-02	Daniel Önnerby	Init version.

	*****************************************************/
	function s_bResize( ){
		this.bSetAlign( this.sAlign,this.iDocumentWidth );
		this.bShow(this.bVisible,true);
	}


	/*****************************************************
	function bScrollFrontDown

		Description:
			Mouse is pressed on the scrollbar.

		Input:
			oEvent	=	event-object (Mozilla, Netscape)

		History:
			2002-04-02	Daniel Önnerby	Init version.

	*****************************************************/
	function s_bScrollFrontDown( oEvent ){
		oEvent=dom==2?window.event:oEvent;
		oActiveScroll=this;

		switch(dom){
			case 1:
				window.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
				window.onmouseup	= sglobal_bScrollFrontUp;
				window.onmousemove	= sglobal_bScrollFrontMove;
				break;
			case 2:
				document.onmousemove	= sglobal_bScrollFrontMove;
				document.onmouseup		= sglobal_bScrollFrontUp;
				break;
			case 3:
				document.addEventListener('mousemove', sglobal_bScrollFrontMove, true);
				document.addEventListener('mouseup', sglobal_bScrollFrontUp, true);
				break;
		}
		return this.bScrollFrontMove( oEvent );
	}


	/*****************************************************
	function bScrollFrontMove

		Description:
			When mouse is moved after holding down on the scrollbar.

		Input:
			oEvent	=	event-object (Mozilla, Netscape)

		History:
			2002-04-02	Daniel Önnerby	Init version.

	*****************************************************/
	function sglobal_bScrollFrontMove( oEvent ){
		oActiveScroll.bScrollFrontMove(dom==2?window.event:oEvent);
		return false;
	}

	function s_bScrollFrontMove( oEvent ){
		var iY=(dom==1?oEvent.pageY-4:dom==2?oEvent.clientY-4+document.body.scrollTop:oEvent.pageY-4)-(this.iY+this.iArrowHeight);
		var iHeight=this.iHeight-this.iArrowHeight*2-this.iBoxHeight;
		iY=iY<0?0:iY<iHeight?iY:iHeight;

		this.bMove((this.iContentHeight-this.iHeight)*iY/iHeight);
		return false;
	}

	/*****************************************************
	function bScrollFrontUp

		Description:
			Mouse is released after holding down on the scrollbar.

		History:
			2002-04-02	Daniel Önnerby	Init version.

	*****************************************************/
	function sglobal_bScrollFrontUp( ){
		switch(dom){
			case 1:
				window.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
				window.onmouseup=null;
				window.onmousemove=null;
				break;
			case 2:
				document.onmouseup=null;
				document.onmousemove=null;
				break;
			case 3:
				document.removeEventListener('mousemove', sglobal_bScrollFrontMove, true);
				document.removeEventListener('mouseup', sglobal_bScrollFrontUp, true);
				break;
		}
		oActiveScroll.bResize();
	}

	/*****************************************************
	function bMouseWheel

		Description:
			Event-function, run when mousewheel is used. Works only in IE6.

		History:
			2002-04-02	Daniel Önnerby	Init version.

	*****************************************************/
	function sglobal_bMouseWheel( ){
		oEvent=window.event;
		for(var i=0;i<aScrolls.length;i++){
			if(this.id==aScrolls[i].sLayer){
				aScrolls[i].bMouseWheel(oEvent.wheelDelta);
			}
		}
		return false;
	}
	function s_bMouseWheel( iDY ){
		if(this.iContentHeight>=this.iHeight){
			this.bMove(this.iScrollY-iDY);
		}
	}

	/*****************************************************
	function bSetFollowLayer

		Description:
			Set the scroll to follow a relative layer.

		Input:
			sFollowLayer	= Name of layer to follow.

		History:
			2002-04-03	Daniel Önnerby	Init version.

	*****************************************************/
	function s_bSetFollowLayer( sFollowLayer ){
		this.sAlign="follow";
		this.sFollowLayer=sFollowLayer;
		this.bSetAlign();
	}
	
