
function AllDocumentScroll(arrPref)
{ this.allScroll = new Array();
 for(var i=0;i<arrPref.length;i++){
     var scroll =  new ScrollOb(arrPref[i]);
     if(scroll && !scroll.error){
	     	scroll.InitScroll();
	     	this.allScroll.push(scroll); 		}
 	}
 var link  = this;

 document.body.onmouseup =  function(e){link.StopAll(e)};
 document.onmousemove =  function(e){link.ScrollAll(e)};

 this.InitScroll  = function(pref){
  var only_scroll = (arguments[0]) ? arguments[0] : false;
  for(var i=0;i<this.allScroll.length;i++){
	      if(!only_scroll || (only_scroll && only_scroll ==  this.allScroll[i].pref)){
	      	this.allScroll[i].InitScroll();
	      	}
	 	} };

 this.StopAll  =   function(e){     for(var i=0;i<this.allScroll.length;i++){
	      this.allScroll[i].SetScrollStop(e);
	 	} 	};

  this.ScrollAll  =   function(e){
     if(window.go_scroll_arr && go_scroll_arr == 1) return;
     for(var i=0;i<this.allScroll.length;i++){
	      this.allScroll[i].GoBegScroll(e);
	 	}
 	};

   this.SetscrollTop = function(e){   	  for(var i=0;i<this.allScroll.length;i++){
	      this.allScroll[i].InitScroll();
	 	}   };
}



function ScrollOb()
{
	this.pref 						= (arguments[0]) ? arguments[0] : '';
	this.offcetY 					= 0;
	this.scroll_go					= 0;
	this.main_scroll				= ById(this.pref+'mainscroll');
	this.scroll_div  				= ById(this.pref+'scroll');
	this.scroll_begun				= ById(this.pref+'scrollbegun');
	this.scroll_body 				= ById(this.pref+'scrollbody');
	this.scroll_up					= ById(this.pref+'scrollup');
	this.scroll_down				= ById(this.pref+'scrolldown');
	this.scroll_body_offcet_k		= 1;
	this.arr_height                	= 0;
	this.scroll_begun_height		= 157;
	this.mouse_pos_y				= 0
    var link 						= this;

    if(!this.main_scroll || !this.scroll_begun){    	   return {error:true};
    	}

    this.scroll_begun.onmousedown  	= function(e){link.SetScrollStart(e)};
	this.scroll_begun.onmouseup    	= function(e){link.SetScrollStop(e)};


	if(this.scroll_up && this.scroll_down){
		this.scroll_up.onmousedown = function(e){link.SetScrollUp(e)};
		this.scroll_up.onmouseup = function(e){link.SetScrollStop(e)};
		this.scroll_down.onmousedown = function(e){link.SetScrollDown(e)};
		this.scroll_down.onmouseup = function(e){link.SetScrollStop(e)};
     }
    if(document.addEventListener){
			this.main_scroll.addEventListener('DOMMouseScroll', function(e){link.wheel(e)},false);
			this.main_scroll.addEventListener('mousewheel', function(e){link.wheel(e)},false);
	}
	 else{
			this.main_scroll.onmousewheel = function(){link.wheel()};
			}

	this.InitScroll  =   function(e)
	{
		if(!this.main_scroll || !this.scroll_div) return;
        var left_col = $('.left_coumn');
        if(left_col){
	        var left_col_h = parseInt(left_col.css('height'));
	        if(left_col_h>680){
	        	$(this.main_scroll).css('height',left_col_h-80+'px');
	        	$(this.scroll_body).css('height',left_col_h-100+'px');
			}
		}
		this.set_scroll_height();
		this.scroll_begun.style.top = this.arr_height+'px';
		if(this.main_scroll.scrollTop){
			this.main_scroll.scrollTop = 0;
        }
		if(this.main_scroll.clientHeight>=this.main_scroll.scrollHeight){
			 this.scroll_div.style.visibility='hidden';
		 }
		 else{
				this.offcetY = (this.main_scroll.scrollHeight > this.main_scroll.clientHeight) ? this.main_scroll.scrollHeight :this.main_scroll.clientHeight;
	            this.scroll_begun.style.height = this.scroll_begun_height+'px'
				this.scroll_body_offcet_k = (this.offcetY-this.main_scroll.clientHeight)/(this.scroll_body.clientHeight-this.scroll_begun_height);
				this.scroll_div.style.visibility='visible';
		 	 }	}

	this.SetScrollDown = function(e)
	{
		go_scroll_arr = 1;
		this.scroll_go = 1;
		if(this.main_scroll.scrollTop>=0)
		this.GoScroll(10,0,e);
	};


	this.SetScrollUp  = function(e)
	{
		this.scroll_go = 1;
		if(this.main_scroll.scrollTop>=0 && this.scroll_go)
		this.GoScroll(-10,0,e);
		go_scroll_arr = 1;
	};

	this.GoScroll  = function(off,wheel,e)
	{
		this.main_scroll.scrollTop += off;
		//this.scroll_begun.style.marginTop = parseInt(this.main_scroll.scrollTop/this.scroll_body_offcet_k)+this.arr_height+'px';
		this.scroll_begun.style.top = parseInt(this.main_scroll.scrollTop/this.scroll_body_offcet_k)+this.arr_height+'px';
		if(wheel==1)
			{
			if((parseInt(this.scroll_begun.style.top)<(this.scroll_body.clientHeight - this.arr_height-this.scroll_begun_height)-5) &&
						  parseInt(this.scroll_begun.style.top)>this.arr_height)
				{
					//alert(this.scroll_body.clientHeight);
					ClearEvent(e);
				}
			}
		if(this.scroll_go)
			{
				var link  = this;
				setTimeout(function(){link.GoScroll(off,wheel,e);},20);
			}
	};


   this.GoBegScroll = function(e)
		{
			if(!this.mouse_pos_y) return;
			if(window.go_scroll_arr && go_scroll_arr == 1) return;
			if(this.scroll_go)
			{
				e = e || window.event;
				var mp = defMousePosition(e);
				var delta  = mp.y- this.mouse_pos_y;
				var old_top  = this.scroll_begun.style.top;
				this.scroll_begun.style.top = parseInt(old_top) + delta+'px';
				if(parseInt(this.scroll_begun.style.top)>(this.scroll_body.clientHeight-this.scroll_begun_height+this.arr_height))
					{
						this.scroll_begun.style.top = (this.scroll_body.clientHeight-this.scroll_begun_height+this.arr_height) + 'px';
					}
				if(parseInt(this.scroll_begun.style.top)<=this.arr_height)
					{
						this.scroll_begun.style.top = this.arr_height + 'px';
					}
				this.main_scroll.scrollTop = parseInt((parseInt(this.scroll_begun.style.top)-this.arr_height)*this.scroll_body_offcet_k);
				this.mouse_pos_y = mp.y;
			ClearEvent(e);
			}
		}

	this.SetScrollStop = function(e)
	{
		this.scroll_go = 0;
		go_scroll_arr  = 0;
		//this.scroll_begun.style.cursor = 'default';
	};

	this.SetScrollStart = function(e)
	{
		e = e || window.event;
		this.scroll_begun.style.cursor = 'pointer';
		this.mouse_pos_y = defMousePosition(e).y;
		this.scroll_go = 1;
		ClearEvent(e);
	};

	this.wheel = function(event)
		{
		e  = event || window.event;
		this.scroll_go = 0;
		var wheelDelta = 0;
		var step = 25;
		 if (e.wheelDelta)
	    {
	        // В IE и Opera
	        wheelDelta = -e.wheelDelta/120;
	    }
	    else if (e.detail)
	    {
	        // В Mozilla
	        wheelDelta = e.detail/3;
	    }
		this.GoScroll(wheelDelta*step,1,e);
		//ClearEvent(e);
		}
	  this.set_scroll_height = function()
	  	{	  	var scroll_parent = this.scroll_div.parentNode;
	  	var bot = 0;//(this.pref !='news_')?10:10;
	  	//this.scroll_div.style.height = this.scroll_div.parentNode.clientHeight - parseInt(getStyle(this.scroll_div,'top'))-bot+'px';	  	}
}


