/*

*/
var scroller_js=true;

function MSCR(winf,tekst,scroller)
{
	this.clipping=winf;
	this.tekst=tekst;
	this.scroller=scroller;
	
	this.clipheight=0;
	this.scrollheight=0;
	this.y=0;
	this.up=null;
	this.down=null;
	this.schuif=null;
	this.fup=null;
	this.fdown=null;
	this.scr=null;
	this.line=10;
	this.fast=100;
	this.speed=200;
	this.loop=false;
	this.obj = "Object";
	this.active =null;
	this.collordown="#FF6";

	this.init = function()
	{
		eval(this.obj + "=this");
		this.ver=navigator.appVersion;
		this.ie=(this.ver.indexOf("MSIE")>-1)?1:0;

		if(!document.getElementById)
		{
			if(document.layers) this.clipping.overflow="scroll";
			else if(document.all)this.clipping.style.overflow="scroll";
			return;
		}
		if (typeof dom_drag_js=="undefined")alert("dom-drag.js should be loaded");

		this.clipheight=parseInt(this.clipping.offsetHeight);
		this.scrollheight=parseInt(this.tekst.offsetHeight);
		for(var i=0,j=0;i<this.scroller.childNodes.length;i++)
		{
			if('DIV'==this.scroller.childNodes[i].nodeName)
			{
				if(j==0){this.up=this.scroller.childNodes[i];j=1;}
				else if(j==1){this.down=this.scroller.childNodes[i];j=2;}
				else if(j==2){this.schuif=scroller.childNodes[i];j=3;}
			}
		}
		if(this.up)
		{
			this.up.onmousedown=this.ScrollUp;
			this.up.onmouseup=this.ScrollStop;
			this.up.onmouseout=this.ScrollStop;
		}
		if(this.down)
		{
			this.down.onmousedown=this.ScrollDown;
			this.down.onmouseup=this.ScrollStop;
			this.down.onmouseout=this.ScrollStop;
		}
		
		document.onkeydown=this.testkey;
		document.onkeyup=this.ScrollStop;
		if (document.addEventListener)
        		document.addEventListener('DOMMouseScroll', this.wheel, false);/** DOMMouseScroll is for mozilla. */
		window.onmousewheel = document.onmousewheel = this.wheel;/** IE/Opera. */

		for(var i=0,j=0;i<this.schuif.childNodes.length;i++)
		{
			if('DIV'==this.schuif.childNodes[i].nodeName)
			{
				if(j==0){this.fup=this.schuif.childNodes[i];j=1;}
				else if(j==1){this.fdown=this.schuif.childNodes[i];j=2;}
				else if(j==2){this.scr=this.schuif.childNodes[i];j=3;}
			}
		}
		if(this.fup)this.fup.onmousedown=this.ScrollFUp;
		if(this.fdown)this.fdown.onmousedown=this.ScrollFDown;
		if(this.fdown || this.fup)this.fast=this.clipheight-this.line;
		
		if(this.scr)
		{
			self.setscroller(0,0);
			Drag.init(this.schuif,this.scr,0,0,0,parseInt(this.schuif.style.height)-parseInt(this.scr.style.height));
		
			this.scr.onDragStart=this.dragscrollstart;
			this.scr.onDrag=this.dragscroll;
			this.scr.onDragEnd=this.dragscrollstop;
			this.setscroller(0,0);
		}
	}
this.testkey = function (e)
{
var evtobj=window.event? event : e; //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode;
//alert("code: "+unicode+"key:"+String.fromCharCode(unicode));
if(unicode==40)self.ScrollDown();
if(unicode==38)self.ScrollUp();
if(unicode==34)self.ScrollFDown();
if(unicode==33)self.ScrollFUp();
}

	this.resize = function ()
	{
		if(0 > (self.clipheight-self.scrollheight))pl=self.y/(self.clipheight-self.scrollheight);
		else pl=0;
		self.clipheight=parseInt(this.clipping.offsetHeight);
		self.scrollheight=parseInt(this.tekst.offsetHeight);
		if(0 > (self.clipheight-self.scrollheight)){
			npl=(self.clipheight-self.scrollheight)*pl;
			if(npl<self.clipheight-self.scrollheight)npl=self.clipheight-self.scrollheight;
			}
		else npl=0;
//alert("npl:"+npl+" cliph"+self.clipheight+" scrh"+self.scrollheight);
		
		self.MoveArea(0,npl);
	}
	this.MoveArea = function (x,y)
	{
		self.y=y;
//		alert("Moveto:"+self.y);
		self.tekst.style.top=y+'px';
		self.setscroller(0,y);
	}

	this.MoveDown = function(move)
	{
		if(self.y>-self.scrollheight+self.clipheight)
		{
			m=self.y-move;
			if(m<-self.scrollheight+self.clipheight)m=-self.scrollheight+self.clipheight;
			self.MoveArea(0,m);
			if(self.loop) setTimeout(self.obj+".MoveDown("+move+")",self.speed);
		}
	}
	this.ScrollProc = function(proc)
	{
//alert(proc);
		self.MoveArea(0,parseInt((-self.scrollheight+self.clipheight)*proc/100));
		
	}
	this.MoveUp = function(move)
	{
		if(this.y<0)
		{
			m=self.y+move;
			if(m>0)m=0;
			self.MoveArea(0,m);
			if(self.loop)setTimeout(self.obj+".MoveUp("+move+")",self.speed);
		}
	}

	this.ScrollDown = function(){self.Knopdown(self.down);self.MoveDown(self.line);}
	this.ScrollUp = function(){self.Knopdown(self.up);self.MoveUp(self.line);}
	this.ScrollFDown = function(){self.MoveDown(self.fast);}
	this.ScrollFUp = function(){self.MoveUp(self.fast);}

	this.ScrollStop = function()
	{
		if(self.loop)
		{
			obj=self.active;
			temp=obj.style.borderTop;
			obj.style.borderTop=obj.style.borderBottom;
			obj.style.borderBottom=temp;
			temp=obj.style.borderLeft;
			obj.style.borderLeft=obj.style.borderRight;
			obj.style.borderRight=temp;
			obj.style.background=obj.color;
			self.loop=false;
			self.active=null;
		}
	}
	this.Knopdown = function(obj)
	{
		if(!self.loop)
		{	
			self.active=obj;
			obj.color=obj.style.background;
			temp=obj.style.borderTop;
			obj.style.borderTop=obj.style.borderBottom;
			obj.style.borderBottom=temp;
			temp=obj.style.borderLeft;
			obj.style.borderLeft=obj.style.borderRight;
			obj.style.borderRight=temp;
			obj.style.background=self.collordown;
			self.loop=true;
		}
	}
	this.dragscrollstop = function(nx,ny)
	{
		self.ScrollStop();
	}
	this.dragscrollstart = function(nx,ny)
	{
		self.Knopdown(self.scr);
	}
	this.dragscroll = function(nx,ny)
	{
		var proc=(100*ny)/(parseInt(self.schuif.style.height)-parseInt(self.scr.style.height));
		var y = parseInt((-self.scrollheight+self.clipheight)*proc/100);
		self.y=y;
		self.tekst.style.top=y+'px';

		self.fup.style.height=ny+"px";
		self.fdown.style.height=(parseInt(self.schuif.style.height)-ny-parseInt(self.scr.style.height))+"px";
	}
	this.setscroller = function(x,y)
	{
		if(self.scrollheight <= self.clipheight)
		{
			self.scroller.style.visibility="hidden";
		}else{
			var th=parseInt(self.schuif.style.height);
			if(!self.ie)th -= parseInt(self.scr.style.borderTopWidth)+parseInt(self.scr.style.borderBottomWidth);
			var sh=parseInt(th*self.clipheight/self.scrollheight);
			self.scroller.style.visibility="visible";
			th-=sh;

			var ny=parseInt(-th*y/(self.scrollheight - self.clipheight));

			self.fup.style.height=ny+"px";
			self.fdown.style.height=(th-ny)+"px";
			self.fdown.style.height=(th-ny)+"px";
			self.scr.style.top=(ny)+"px";//ie:-2
			self.scr.style.height=(sh)+"px";//f:-border ie:-3
		}
	}
	
	this.handlewheel = function (delta) 
	{
        if (delta < 0)
		self.MoveDown(3*self.line);
        else
		self.MoveUp(3*self.line);
	}

	this.wheel = function (event)
	{
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                self.handlewheel(delta);
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
	event.returnValue = false;
	}

	var self=this;

//	this.init();
}


