/*
Optimize before using this file in final version

	History:
		
		2000-12-15		Daniel Önnerby		Init version
		2000-12-18		Daniel Önnerby		Updated the calculation and positioning
											Added the old scrollscript functions
		2000-12-19		Daniel Önnerby		Updated the initScroll-function
		2000-12-19		Daniel Önnerby		Updated the updateScrolls-script
											the sContentLayerName pointed at wrong layer
		2000-12-20		Daniel Önnerby		Updated the script so the scroll captures the move-event on the whole document
		2001-01-05		Mattias Ohlson		Fixed a bug in the updateScrollfunction (layers not turned on/off properly)
		2001-01-05		Mattias Ohlson		Added variable 'bgPix' to make it easier to point to background pixel (pix.gif)
		2001-01-09		Daniel Önnerby		Added the showContent and showScroll variables and updated the updateScrolls,
											findScroll and scrollVisibility functions to use them.
		2001-01-09		Daniel Önnerby		Removed the old function iniScroll.
		2001-01-09		Daniel Önnerby		Added forceScrollBarHide-function.
		2001-01-09		Daniel Önnerby		Fixed the calculation of the imagesize of buttons
		2001-01-09		Daniel Önnerby		Updated scrollCreateLayer for NS. captureEvent is no longer used.
		2001-01-09		Daniel Önnerby		Added the optional arguments margin and width to the addScroll-function
		2001-01-31		Daniel Önnerby		Moved the onmousemoveevent for IE to the runScrollDown function
		2001-02-16		Daniel Önnerby		Added support for Mozilla-browser (NS6)
		2001-02-20		Daniel Önnerby		Added addFormScroll to eliminate the scrollform.js.
		2001-02-21		Daniel Önnerby		Fixed the forceScrollBarHide-method
		2001-02-21		Daniel Önnerby		Fixed the scrollCalc-method. The calculation didn't work correctly with large content
		2001-03-28		Daniel Önnerby		Fixed scroll on a scrollpage bug in IE.
		
	This is a scrollscript that adds a DHTML-scrollbar at the right side of a layer (DIV-tag)
	The scrollscript is very simple.
	
	Functions to use:
	
	addScroll( sContentLayerName, height, top button-image, bottom button-image, background image, box-image , [margin], [width]);
	
		adds a scroll to a layer

		sContentLayerName	=	the name of the layer to put a scroll on
		height				=	the height of the layer after putting the scroll on it
		top button-image	=	the buttonimage to scroll up
		bottom button-image	=	the buttonimage to scroll down
		background image	=	the image to put between the up and the down-buttons (1 pix height that is stretched)
		box-image 			=	the image of the scrollbox that indicates were you are in the layer
		[margin]			=	optional. Space in pixels between the content and the scroll
		[width]				=	optional. Width of the contentlayer. Use this if the original content is to small.

	addFormScroll
		
		Same parameters as addScroll. This functions is used for scrolllayers with forms in.

	
	scrollVisibility( sContentLayerName , visibility )
	
		Shows the layer and the scroll (if the content is to big)

		sContentLayerName	=	the content-layers name
		visibility			=	true or false

		
	updateScrolls()
	
		Updates all the scrolls if the content has been updated

		
	forceScrollBarHide( sLayerName )
	
		Forces the scrollbar to be hidden even if the contentlayer is to big
		This state will be removed the next time you call updateScrolls
		PLEASE DONT USE THIS FUNCTION UNLESS YOU HAVE TO. It's not supported.

*/

var firstScrollTime=false;

function scrollsGlobalInit(){
	ns=document.layers?true:false;
	dom=document.layers?1:document.all?2:document.getElementById?3:0;
	if(typeof(bgPix)!="string"){
		bgPix='clear_dot.gif';	
	}
	aScrolls=new Array();
	scrollWidth=10;
	scrollButtonHeight=6;
	scrollButtonWidth=10;
	scrollBoxHeight=10;
	activeScroll=0;
	captureWindow=false;
}

function updateScrolls(bRunThis){
	if(bRunThis){
		for(var a=0;a<aScrolls.length;a++){
			aScrolls[a].contentHeight=ns?document.layers[aScrolls[a].name].document.height:dom==2?document.all[aScrolls[a].name].offsetHeight:document.getElementById(aScrolls[a].name).offsetHeight;
			aScrolls[a].showScroll=(aScrolls[a].contentHeight>=aScrolls[a].height);
			if( !aScrolls[a].showNextScroll ){
				aScrolls[a].showScroll=false;
				aScrolls[a].showNextScroll=true;
			}
			if( aScrolls[a].moveable ){
				aScrolls[a].calc(0);
			}
			aScrolls[a].clip();
			scrollVisibility(aScrolls[a].name,aScrolls[a].showContent);
		}
	}
}

function addScroll(sContentLayerName,height,topImg,bottomImg,bgImg,boxImg,margin,width,form){
	if(!firstScrollTime){
		scrollsGlobalInit();
		firstScrollTime=true;
	}
	iNewID=aScrolls.length;
	aScrolls[iNewID]=new oScroll(iNewID,sContentLayerName,height,topImg,bottomImg,bgImg,boxImg,margin,width,form);
}

function addFormScroll(sContentLayerName,height,topImg,bottomImg,bgImg,boxImg,margin,width){
	addScroll(sContentLayerName,height,topImg,bottomImg,bgImg,boxImg,margin,width,true);
}

function scrollSetUpdateMoveable( sContentLayerName, bMoveable ){
	scrollId=findScroll(sContentLayerName);
	aScrolls[scrollId].moveable=bMoveable;
}

function oScroll(id,sContentLayerName,iHeight,topImg,bottomImg,bgImg,boxImg,iMargin,iWidth,bForm){
	this.name=sContentLayerName;
	this.contentY=0;
	this.scrollY=0;
	this.id=id;
	this.height=iHeight;
	this.margin=iMargin;
	this.isform=bForm?true:false;
	this.showNextScroll=true;
	this.moveable=true;
	
	switch(dom){
		case 1:
			this.contentLayer=document.layers[sContentLayerName];
			this.x=this.contentLayer.pageX;
			this.y=this.contentLayer.pageY;
			this.contentHeight=this.contentLayer.clip.height;
			this.contentWidth=iWidth?iWidth:this.contentLayer.clip.width;
			this.contentLayer.resizeTo(this.contentWidth,this.contentHeight);
			break;
		case 2:
			this.contentLayer=document.all[sContentLayerName];
			break;
		case 3:
			this.contentLayer=document.getElementById(sContentLayerName);
			break;
	}
	if(dom>1){
		this.x=this.contentLayer.offsetLeft;
		this.y=this.contentLayer.offsetTop;
		this.contentHeight=this.contentLayer.offsetHeight;
		this.contentWidth=iWidth?iWidth:this.contentLayer.offsetWidth;
		this.contentLayer.style.width=this.contentWidth;
	}

	this.scrollX=this.contentWidth+this.x;
	if(iMargin){
		this.scrollX+=iMargin;
	}
	this.scrollY=this.y;
	
	tempContent= '<table border=0 cellpadding=0 cellspacing=0>';
	tempContent+=' <tr> <td><a href="Javascript:void(null);" onMouseDown="aScrolls['+this.id+'].up_on();return false;" onMouseUp="aScrolls['+this.id+'].up_off();" onMouseOut="aScrolls['+this.id+'].up_off();"><img src="'+topImg+'" border=0 width='+scrollButtonWidth+' height='+scrollButtonHeight+'></a></td> </tr>';
	tempContent+=' <tr> <td height="'+(this.height-scrollButtonHeight*2)+'"><img src="'+bgImg+'" border=0 height="'+(this.height-scrollButtonHeight*2)+'" width="'+scrollWidth+'"></td></tr>';
	tempContent+=' <tr> <td><a href="Javascript:void(null);" onMouseDown="aScrolls['+this.id+'].down_on();return false;" onMouseUp="aScrolls['+this.id+'].down_off();" onMouseOut="aScrolls['+this.id+'].down_off();"><img src="'+bottomImg+'" border=0 width='+scrollButtonWidth+' height='+scrollButtonHeight+'></a></td> </tr>';
	tempContent+=' </table> ';

	this.scrollbgLayer=scrollCreateLayer(sContentLayerName+"scrollbg",this.scrollX,this.scrollY,tempContent,scrollWidth,this.height);

this.scrollBoxLayer=scrollCreateLayer(sContentLayerName+"scrollbox",this.scrollX,this.scrollY+scrollButtonHeight,'<img src="'+boxImg+'" border=0>',scrollWidth,scrollWidth);

this.scrollFrontLayer=scrollCreateLayer(sContentLayerName+"scrollfront",this.scrollX,this.scrollY+scrollButtonHeight,'<img src="' +bgPix+ '" border=0 height="'+(this.height-(2*scrollButtonHeight))+'" width="'+scrollWidth+'">',scrollWidth,this.height-(2*scrollButtonHeight),'runScrollDown',this.id);
			
	this.up_on=scrollUpOn;
	this.up_off=scrollUpOff;
	this.up=scrollUp;
	this.down_on=scrollDownOn;
	this.down_off=scrollDownOff;
	this.down=scrollDown;
	
	this.upfront=scrollUpFront;
	this.downfront=scrollDownFront;
	this.movefront=scrollMoveFront;
	this.clip=scrollClip;
	
	this.calc=scrollCalc;
	this.calc(0);
	this.clip();
	this.captureMove=false;
	this.showScroll=true;
	if(dom==1){
		this.showContent=(this.contentLayer.visibility=='show');
	}
	if(dom>1){
		this.showContent=(this.contentLayer.style.visibility=='visible');
	}

	if(this.contentHeight<this.height){
		this.showScroll=false;
	}
}

function scrollClip(){
	switch(dom){
		case 1:
			this.scrollBoxLayer.moveTo(this.scrollX,this.scrollY);
			this.contentLayer.moveTo(this.x,this.y-this.contentY);
			setTimeout("scrollClipNS("+this.id+");",1);
			this.contentLayer.clip.bottom=this.contentY+this.height;
			this.contentLayer.clip.top=this.contentY;
			break;
		case 2:
			this.scrollBoxLayer.top=this.scrollY;
			this.contentLayer.style.top=this.y-this.contentY;
			this.contentLayer.style.clip="rect("+this.contentY+" "+(this.scrollX-this.x)+" "+(this.contentY+this.height)+" 0)";
			break;
		case 3:
			this.scrollBoxLayer.top=this.scrollY;
			this.contentLayer.setAttribute("style","position:absolute;left:"+this.x+";top:"+(this.y-this.contentY)+";clip:rect("+this.contentY+" "+(this.scrollX-this.x)+" "+(this.contentY+this.height)+" 0);width:"+(this.scrollX-this.x-this.margin)+";");
			break;
	}
}

// STRANGE FIX FOR NS<4.6
function scrollClipNS(id){
	aScrolls[id].contentLayer.clip.bottom=aScrolls[id].contentY+aScrolls[id].height;
	aScrolls[id].contentLayer.clip.top=aScrolls[id].contentY;
}

function scrollUpOn(){
	this.scrolling=true;
	this.calc(this.scrollY,-10);
	this.clip();		
	setTimeout("aScrolls["+this.id+"].up();",400);
}
function scrollUp(){
	if(this.scrolling){
		var temp=this.calc(this.scrollY,-3);
		this.clip();		
		setTimeout("aScrolls["+this.id+"].up();",10);	
		if(!temp){
			setTimeout("aScrolls["+this.id+"].up_off();",1);
		}
	}
}
function scrollUpOff(){
	if(ns && this.scrolling && this.isform){
		this.contentLayer.visibility='hide';
		setTimeout("aScrolls["+this.id+"].contentLayer.visibility='show';",100);
	}
	this.scrolling=false;
}
function scrollDownOn(){
	this.scrolling=true;
	this.calc(this.scrollY,+10);
	this.clip();
	setTimeout("aScrolls["+this.id+"].down();",400);	
}
function scrollDown(){
	if(this.scrolling){
		var temp=this.calc(this.scrollY,+3);
		this.clip();		
		setTimeout("aScrolls["+this.id+"].down();",10);	
		if(!temp){
			setTimeout("aScrolls["+this.id+"].down_off();",1);
		}
	}
}
function scrollDownOff(){
	if(ns && this.scrolling && this.isform){
		this.contentLayer.visibility='hide';
		setTimeout("aScrolls["+this.id+"].contentLayer.visibility='show';",100);
	}
	this.scrolling=false;
}

function scrollUpFront(e){
	this.captureMove=false;
}
function scrollDownFront(e){
	this.captureMove=true;
	this.movefront(e);
}
function scrollMoveFront(e){
	if(this.captureMove){
		y=dom==1?e.pageY-4:dom==2?e.clientY-4+document.body.scrollTop:e.clientY-4+document.body.scrollTop;

		this.calc(y);
		this.clip();
		return false;
	}else{
		return true;
	}
}

function scrollCalc(y,dY){
	if(arguments.length>1){
		ret=true;
		this.contentY+=dY;
		if(this.contentY<0){
			this.contentY=0;
			ret=false;
		}
		if(this.contentY>this.contentHeight-this.height){
			this.contentY=this.contentHeight-this.height;
			ret=false;
		}
		scrollRelHeight=this.height-(scrollButtonHeight*2)-scrollBoxHeight;
		contentRelHeight=(this.contentY)/(this.contentHeight-this.height);
		this.scrollY=(scrollRelHeight*contentRelHeight)+this.y+scrollButtonHeight;
		return ret;
	}else{
		var tempY=this.scrollY;
		if(y<this.y+scrollButtonHeight){y=this.y+scrollButtonHeight;}
		if(y>this.y+this.height-scrollBoxHeight-scrollButtonHeight){y=this.y+this.height-scrollBoxHeight-scrollButtonHeight;}
		rel=(y-(this.y+scrollButtonHeight))/(this.height-scrollBoxHeight-(scrollButtonHeight*2));
		this.scrollY=y;
		this.contentY=(this.contentHeight-this.height)*rel;
		return (tempY!=y);
	}
}

function runScrollDown(e,scrollid){
	switch(dom){
		case 1:
			activeScroll=scrollid;
			aScrolls[scrollid].downfront(e);
			window.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
			window.onmouseup=runScrollUp;
			window.onmousemove=runScrollMove;
			break;
		case 2:
			activeScroll=e;
			aScrolls[activeScroll].downfront(window.event);
			captureWindow=true;
			document.onmousemove=runScrollMove;
			document.onmouseup=runScrollUp;
			break;
		case 3:
			activeScroll=scrollid;
			aScrolls[activeScroll].downfront(e);
			captureWindow=true;
			document.addEventListener('mousemove', runScrollMove, true);
			document.addEventListener('mouseup', runScrollUp, true);
			break;
	}
	return false;
}


function runScrollUp(e){
	switch(dom){
		case 1:
			aScrolls[activeScroll].upfront(e);
			window.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
			window.onmouseup=null;
			window.onmousemove=null;
			if(aScrolls[activeScroll].isform){
				aScrolls[activeScroll].contentLayer.visibility='hide';
				setTimeout("aScrolls["+activeScroll+"].contentLayer.visibility='show';",100);
			}
			return false;
			break;
		case 2:
			if(captureWindow){
				aScrolls[activeScroll].upfront(window.event);
				captureWindow=false;
				document.onmouseup=null;
				document.onmousemove=null;
				return false;
			}else{
				return true;
			}
			break;
		case 3:
			if(captureWindow){
				aScrolls[activeScroll].upfront(window.event);
				captureWindow=false;
				document.removeEventListener('mousemove', runScrollMove, true);
				document.removeEventListener('mouseup', runScrollUp, true);
				return false;
			}else{
				return true;
			}
			break;
	}
}
function runScrollMove(e){
	switch(dom){
		case 1:
			return aScrolls[activeScroll].movefront(e);
			break;
		case 2:
			if(captureWindow){
				aScrolls[activeScroll].movefront(window.event);
				return false;
			}else{
				return true;
			}
			break;
		case 3:
			if(captureWindow){
				aScrolls[activeScroll].movefront(e);
				return false;
			}else{
				return true;
			}
			break;
	}
}

function scrollCreateLayer(name,x,y,text,width,height,onmousedown,scrollid){
	switch(dom){
		case 1:
			if(onmousedown){
				text='<a href="Javascript:void(null);" onMouseDown="return '+onmousedown+'(event,'+scrollid+');">'+text+'</a>';
			}
			Layer.prototype.scrollid=null;
			document.layers[name]=new Layer(width);
			document.layers[name].document.open();
			document.layers[name].document.write(text);
			document.layers[name].document.close();
			document.layers[name].resizeTo(width,height);
			document.layers[name].moveTo(x,y);
			document.layers[name].visibility='hide';
			document.layers[name].scrollid=scrollid;
			return document.layers[name];
			break;
		case 2:
			tempEvent="";
			if(onmousedown){
				tempEvent+=' onMouseDown="'+onmousedown+'('+scrollid+');" ';
			}
			document.body.insertAdjacentHTML('BeforeEnd','<div id="'+name+'" '+tempEvent+' style="position:absolute;width:'+width+';height:'+height+';top:'+y+';left:'+x+';visibility:hidden;">'+text+'</div>');
			return document.all[name].style;
			break;
		case 3:
			if(onmousedown){
				text='<a href="Javascript:void(null);" onMouseDown="return '+onmousedown+'(event,'+scrollid+');">'+text+'</a>';
			}
			var div=document.createElement('DIV');
			div.id=name;
			div.setAttribute('style','position:absolute;left:'+x+';top:'+y+';width:'+width+';height:'+height+';visibility:hidden;');
			div.innerHTML=text;
			document.body.appendChild(div);
			return div.style;
			break;
	}
}

function scrollVisibility(sLayerName,visibility){
	scrollId=findScroll(sLayerName);
	aScrolls[scrollId].showContent=visibility;
	switch(dom){
		case 1:
			visibility=visibility?'show':'hide';
			document.layers[sLayerName].visibility=visibility;
			if(!aScrolls[scrollId].showScroll){
				visibility='hide';
			}
			document.layers[sLayerName+"scrollbg"].visibility=visibility;
			document.layers[sLayerName+"scrollbox"].visibility=visibility;
			document.layers[sLayerName+"scrollfront"].visibility=visibility;
			break;
		case 2:
			visibility=visibility?'visible':'hidden';
			document.all[sLayerName].style.visibility=visibility;
			if(!aScrolls[scrollId].showScroll){
				visibility='hidden';
			}
			document.all[sLayerName+"scrollbg"].style.visibility=visibility;
			document.all[sLayerName+"scrollbox"].style.visibility=visibility;
			document.all[sLayerName+"scrollfront"].style.visibility=visibility;
			break;
		case 3:
			visibility=visibility?'visible':'hidden';
			document.getElementById(sLayerName).style.visibility=visibility;
			if(!aScrolls[scrollId].showScroll){
				visibility='hidden';
			}
			document.getElementById(sLayerName+"scrollbg").style.visibility=visibility;
			document.getElementById(sLayerName+"scrollbox").style.visibility=visibility;
			document.getElementById(sLayerName+"scrollfront").style.visibility=visibility;
			break;
	}
}

function findScroll(sLayer){
	ret=false;
	for(var a=0;a<aScrolls.length;a++){
		if(aScrolls[a].name==sLayer){
			ret=a;
		}
	}
	return ret;
}

function forceScrollBarHide(sLayerName){
	scrollID=findScroll(sLayerName);
	aScrolls[scrollID].showScroll=false;
	aScrolls[scrollID].showNextScroll=false;
	scrollVisibility(sLayerName,aScrolls[scrollID].showContent);
}



