var ns=document.layers?true:false;
var aScrolls=new Array();
var scrollWidth=10;
var scrollButtonHeight=12;
var scrollButtonWidth=10;
var scrollBoxHeight=14;
var firstScrollTime=false;
var activeScroll=0;
var captureWindow=false;

function scrollsGlobalInit(){
	ns=document.layers?true:false;
	aScrolls=new Array();
	scrollWidth=10;
	scrollButtonHeight=12;
	scrollButtonWidth=10;
	scrollBoxHeight=14;
	activeScroll=0;
	captureWindow=false;
	document.onmousemove=runScrollMove;
	document.onmouseup=runScrollUp;
}

function dummy(){}

function updateScrolls(){
	for(a=0;a<aScrolls.length;a++){
		aScrolls[a].contentHeight=ns?document.layers[aScrolls[a].name].document.height:document.all[aScrolls[a].name].offsetHeight;
		if(aScrolls[a].contentHeight<aScrolls[a].height){
			alert('1')
			aScrolls[a].calc(0);
			aScrolls[a].clip();
			scrollVisibility3(aScrolls[a].name,false)
		}else{
			alert('2')
			aScrolls[a].calc(0);
			aScrolls[a].clip();
			scrollVisibility3(aScrolls[a].name,true)
		}
	}
}

function addScroll(sContentLayerName,height,topImg,bottomImg,bgImg,boxImg){
	if(!firstScrollTime){
		scrollsGlobalInit();
		firstScrollTime=true;
	}
	iNewID=aScrolls.length;
	aScrolls[iNewID]=new oScroll(iNewID,sContentLayerName,height,topImg,bottomImg,bgImg,boxImg);	
}

function oScroll(id,sContentLayerName,iHeight,topImg,bottomImg,bgImg,boxImg){
	this.name=sContentLayerName;
	this.contentY=0;
	this.scrollY=0;
	this.id=id;
	this.height=iHeight;

	if(ns){
		this.contentLayer=document.layers[sContentLayerName];
		this.x=this.contentLayer.pageX;
		this.y=this.contentLayer.pageY;
		this.contentHeight=this.contentLayer.clip.height;
	}else{
		this.contentLayer=document.all[sContentLayerName];
		this.x=this.contentLayer.offsetLeft;
		this.y=this.contentLayer.offsetTop;
		this.contentHeight=this.contentLayer.offsetHeight;
	}

	if(ns){
		this.scrollX=this.x+this.contentLayer.clip.width;
	}else{
		this.scrollX=this.x+this.contentLayer.offsetWidth;
	}
	this.scrollY=this.y;

	tempContent= '<table border=0 cellpadding=0 cellspacing=0>';
	tempContent+='<tr><td valign=bottom><a href="Javascript:void dummy();" 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></a></td></tr>';
	tempContent+='<tr><td height="'+(this.height-35)+'"><img src="'+bgImg+'" height="'+(this.height-scrollButtonHeight*2)+'" width="'+scrollWidth+'"></td></tr>';
	tempContent+='<tr><td valign=top><a href="Javascript:void dummy();" 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></a></td></tr>';
	tempContent+='</td></tr></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,ns?'':'<img src="images/pix.gif" 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.visibility=true;

	if(this.contentHeight<this.height){
		this.visibility=false;
	}
}
function scrollClip(){
	if(ns){
		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;
	}else{
		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)";
	}
}

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){
		this.calc(this.scrollY-3);
		this.clip();		
		setTimeout("aScrolls["+this.id+"].up();",10);	
	}
}
function scrollUpOff(){
	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){
		this.calc(this.scrollY+3);
		this.clip();		
		setTimeout("aScrolls["+this.id+"].down();",10);	
	}
}
function scrollDownOff(){
	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=ns?e.pageY-4:e.clientY-4;
		this.calc(y);
		this.clip();
	}
}

function scrollCalc(y){
	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;
}

function runScrollDown(e){
	if(ns){
		aScrolls[this.scrollid].downfront(e);
		activeScroll=this.scrollid;
		window.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
		window.onMouseUp=runScrollUp;
		window.onMouseMove=runScrollMove;
	}else{
		activeScroll=e;
		aScrolls[activeScroll].downfront(window.event);
		captureWindow=true;
	}
	return false;
}


function runScrollUp(e){
	if(ns){
		aScrolls[activeScroll].upfront(e);
		window.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
		return false;
	}else{
		if(captureWindow){
			aScrolls[activeScroll].upfront(window.event);
			captureWindow=false;
			return false;
		}else{
			return true;
		}
	}
}
function runScrollMove(e){
	if(ns){
		aScrolls[activeScroll].movefront(e);
		return false;
	}else{
		if(captureWindow){
			aScrolls[activeScroll].movefront(window.event);
			return false;
		}else{
			return true;
		}
	}
}

function scrollCreateLayer(name,x,y,text,width,height,onmousedown,scrollid){
	if(ns){
		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';
		if(onmousedown){
			document.layers[name].captureEvents(Event.MOUSEDOWN);
			document.layers[name].onMouseDown=eval(onmousedown);
		}
		document.layers[name].scrollid=scrollid;
		return document.layers[name];
	}else{
		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;
	}
}

function scrollVisibility(sLayerName,visibility){
	if(ns){
		visibility=visibility?'show':'hide';
		document.layers[sLayerName].visibility=visibility;
		if(aScrolls[findScroll(sLayerName)].visibility){
			document.layers[sLayerName+"scrollbg"].visibility=visibility;
			document.layers[sLayerName+"scrollbox"].visibility=visibility;
			document.layers[sLayerName+"scrollfront"].visibility=visibility;
		}
	}else{
		visibility=visibility?'visible':'hidden';
		document.all[sLayerName].style.visibility=visibility;
		if(aScrolls[findScroll(sLayerName)].visibility){
			document.all[sLayerName+"scrollbg"].style.visibility=visibility;
			document.all[sLayerName+"scrollbox"].style.visibility=visibility;
			document.all[sLayerName+"scrollfront"].style.visibility=visibility;
		}
	}
}

function scrollVisibility2(sLayerName,visibility){
	if(ns){
		visibility=visibility?'show':'hide';
		alert(aScrolls[findScroll(sLayerName)].visibility)
		if(aScrolls[findScroll(sLayerName)].visibility){
			document.layers[sLayerName+"scrollbg"].visibility=visibility;
			document.layers[sLayerName+"scrollbox"].visibility=visibility;
			document.layers[sLayerName+"scrollfront"].visibility=visibility;
		}
	}else{
		visibility=visibility?'visible':'hidden';
		alert(aScrolls[findScroll(sLayerName)].visibility)
		if(aScrolls[findScroll(sLayerName)].visibility){
			document.all[sLayerName+"scrollbg"].style.visibility=visibility;
			document.all[sLayerName+"scrollbox"].style.visibility=visibility;
			document.all[sLayerName+"scrollfront"].style.visibility=visibility;
		}
	}
}

function scrollVisibility3(sLayerName,visibility){
	if(ns){
		visibility=visibility?'show':'hide';
			document.layers[sLayerName+"scrollbg"].visibility=visibility;
			document.layers[sLayerName+"scrollbox"].visibility=visibility;
			document.layers[sLayerName+"scrollfront"].visibility=visibility;
	}else{
		visibility=visibility?'visible':'hidden';
			document.all[sLayerName+"scrollbg"].style.visibility=visibility;
			document.all[sLayerName+"scrollbox"].style.visibility=visibility;
			document.all[sLayerName+"scrollfront"].style.visibility=visibility;
	}
}



function findScroll(sLayer){
	ret=false;
	for(a=0;a<aScrolls.length;a++){
		if(aScrolls[a].name==sLayer){
			ret=a;
		}
	}
	return ret;
}

function initScroll(oContent,oBG,oBox,oBGMiddle,x,y,picUp,picDown,picBox){
	sContentLayerName=scrollGetLayer(oContent);
	if(findScroll(sContentLayerName)==false){
		height=ns?document.layers[scrollGetLayer(oBG)].clip.height:document.all[scrollGetLayer(oBG)].offsetHeight;

		if(ns){
			document.layers[sContentLayerName].moveTo(x,y);
			document.layers[scrollGetLayer(oBG)].visibility='hide';
			document.layers[scrollGetLayer(oBox)].visibility='hide';
			document.layers[scrollGetLayer(oBGMiddle)].visibility='hide';
		}else{
			document.all[sContentLayerName].style.top=y;
			document.all[sContentLayerName].style.left=x;
			document.all[scrollGetLayer(oBG)].style.visibility='hidden';
			document.all[scrollGetLayer(oBox)].style.visibility='hidden';
			document.all[scrollGetLayer(oBGMiddle)].style.visibility='hidden';
		}
		addScroll(sContentLayerName,height,scrollTopImg,scrollBottomImg,scrollBGImg,scrollBoxImg);
		scrollVisibility(sContentLayerName,true);
	}else{
		id=findScroll(sContentLayerName);
		aScrolls[id].contentHeight=ns?document.layers[aScrolls[id].name].document.height:document.all[aScrolls[id].name].offsetHeight;
		aScrolls[id].calc(0);
		if(ns){
			aScrolls[id].contentLayer.moveTo(aScrolls[id].x,aScrolls[id].y);
			aScrolls[id].scrollbgLayer.moveTo(aScrolls[id].scrollX,aScrolls[id].scrollY);
			aScrolls[id].scrollFrontLayer.moveTo(aScrolls[id].scrollX,aScrolls[id].scrollY+scrollButtonHeight);
		}
		aScrolls[id].clip();
	}
}
function scrollGetLayer(uLayer){
	if(typeof(uLayer)=="string"){
		return uLayer;
	}else{
		if(ns){
			return uLayer.name;
		}else{
			return uLayer.id;
		}
	}
}


