var FrameBox;

$(document).ready(function(){

  FrameBox = new SliderFramework('Texts', 'ScrollBarBox', 'ScrollBar', 'Slider', 'ScrollButtonUp', 'ScrollButtonDown');
  FrameBox.selectFrame('divDescription');

});

//Events.Attach(window, 'onload', function(){
//  FrameBox = new SliderFramework('Texts', 'ScrollBarBox', 'ScrollBar', 'Slider', 'ScrollButtonUp', 'ScrollButtonDown');
//  FrameBox.selectFrame('divDescription');
//});

var AnimEng = new function(){
	this.add = function(obj){
		if(obj.animEngineServiced==0){
			this.servicedObjs.add(obj);
			obj.animEngineServiced=1;
		}
	};
	
	this.remove=function(obj){
		var tempServicedObjs=[];
		for(var i=0;i<this.servicedObjs.length;i++){
			if(this.servicedObjs[i]!=obj)
				tempServicedObjs.add(this.servicedObjs[i]);
			else
				this.servicedObjs[i].animEngineServiced=0;
		}
		this.servicedObjs=tempServicedObjs;
	};
	
	this.go=function(frameRate){
		this.frameDelay=1000/frameRate;
		this.timeline=setInterval(this.name+".animFrame()",this.frameDelay)
	};
	
	this.stop=function(){
		clearInterval(this.timeline)
	};
	
	this.animFrame=function(){
		for(var i=0;i<this.servicedObjs.length;i++){
			this.servicedObjs[i].animFrame();
		}
	};
	
	this.timeline = null;
	this.servicedObjs=[];
	this.name='AnimEng';
	//var frameRate=40; 
	this.go(40);
}

function SliderFramework(TextConteinter, ScrlBar_Box, ScrlBar_Conteinter, ScrlBar_Slider, ScrlBar_ArrUp, ScrlBar_ArrDoun)
{
  this.constMouseWheelStep = 10;
  this.constArrScrollStep = 3;
  this.constSliderWidth = 1;
  this.constMinSliderHeight = 45

 // this.FindObject = function(name){ var o = document.getElementById(name); if(o == null){ alert('Не найден объект ' + name + '!'); } return o; }

  this.TextConteinter = this.FindObject(TextConteinter);
  this.SB_Box         = this.FindObject(ScrlBar_Box);
  this.SB_Conteinter  = this.FindObject(ScrlBar_Conteinter);
  this.SB_Slider      = this.FindObject(ScrlBar_Slider);
  this.SB_ArrUp       = this.FindObject(ScrlBar_ArrUp);
  this.SB_ArrDoun     = this.FindObject(ScrlBar_ArrDoun);
  
  this.TextFrameCurrent = null;
  
  this.TextHeight;
  this.FrameHeight;
  
  this.SB_ConteinterHeight
  this.SB_SliderHeight;

  this.Koef;
  this.FarmeOverhang;
  this.SlideBarSpace;
  
  this.DragEventID = null;
  this.ScrollPos;
  this.MouseWheelEventID = null;
  
  this.Init = function(){
  
    this.TextHeight = this.GetHeight(this.TextConteinter);
    this.SB_ConteinterHeight = this.GetHeight(this.SB_Conteinter);

    //this.SB_Slider.style.width = (Browser.Type == Browser.Mozilla) ? this.constSliderWidth - 2 : this.constSliderWidth;
    this.SB_Slider.style.width = this.constSliderWidth + 'px';

    this.SB_ArrUp.style.cursor = (Browser.Type != Browser.IE) ? 'pointer' : 'hand';
    Events.Attach(this.SB_ArrUp, 'onmousedown', this, 'ScrollDown');
    Events.Attach(this.SB_ArrUp, 'onmouseup', this, 'ScrollStop');
    Events.Attach(this.SB_ArrUp, 'onmouseout', this, 'ScrollStop');

    this.SB_ArrDoun.style.cursor = (Browser.Type != Browser.IE) ? 'pointer' : 'hand';
    Events.Attach(this.SB_ArrDoun, 'onmousedown', this, 'ScrollUp');
    Events.Attach(this.SB_ArrDoun, 'onmouseup', this, 'ScrollStop');
    Events.Attach(this.SB_ArrDoun, 'onmouseout', this, 'ScrollStop');
    
    Events.Attach(this.SB_Conteinter, 'onmousedown', this, 'DragInit');
    Events.Attach(this.SB_Slider, 'onmousedown', this, 'DragInit');
   
    if(Browser.Type == Browser.IE){
      var fnk = function(){return false;};
      Events.Attach(this.SB_ArrUp,      'onselectstart', fnk);
      Events.Attach(this.SB_ArrDoun,    'onselectstart', fnk);
      Events.Attach(this.SB_Conteinter, 'onselectstart', fnk);
      Events.Attach(this.SB_Slider,     'onselectstart', fnk);
    }
  }
  
  this.selectFrame = function(name, value){
  
    if(this.TextFrameCurrent != null)
      this.SetDisplay(this.TextFrameCurrent, false);
      
    this.TextFrameCurrent = this.FindObject(name);
    
    if(typeof(value)!='undefined'){
      var obj = document.getElementById('divDescription');
      obj.innerHTML = value + '<p>&nbsp;</p>';
    }
    
    this.SetDisplay(this.TextFrameCurrent, true);
    
    this.FrameHeight = this.GetHeight(this.TextFrameCurrent);
    
    this.Koef = this.TextHeight / this.FrameHeight;
    
    if(this.Koef < 1){
    
      this.SetSBVisible(true);
      this.FarmeOverhang = this.FrameHeight - this.TextHeight;
      this.SB_SliderHeight = this.SB_ConteinterHeight * this.Koef;
      if(this.SB_SliderHeight < this.constMinSliderHeight) {
        this.SB_SliderHeight = this.constMinSliderHeight;
      }
      this.SlideBarSpace = this.SB_ConteinterHeight - this.SB_SliderHeight;
      this.SB_Slider.style.height = this.SB_SliderHeight + 'px';
      
      if(this.MouseWheelEventID == null){
        if(Browser.Type == Browser.IE || Browser.Type == Browser.Opera){
          this.MouseWheelEventID = Events.Attach(this.TextConteinter, 'onmousewheel', this, 'MouseWheel');
        }
        else if(Browser.Family == Browser.GeckoFamily)
        {
          this.MouseWheelEventID = Events.Attach(this.TextConteinter, 'DOMMouseScroll', this, 'MouseWheel');
        }        
      }
    }
    else{
      this.SetSBVisible(false);
      if(this.MouseWheelEventID!=null){
        Events.Detach(this.MouseWheelEventID);
        this.MouseWheelEventID = null;
      }
    }
    
    this.SetFramePosition(0);
  }
  
  this.MouseWheel = function(obj, ev){
    var delta = (Browser.Family == Browser.GeckoFamily) ? -ev.detail : ev.wheelDelta;
    this.SetFramePosition(this.getFramePos() + (delta > 0 ? this.constMouseWheelStep : -this.constMouseWheelStep));
    Events.StopPropagation(ev);
  }
  
  this.DragInit = function(obj, ev){
    var isslider = (obj==this.SB_Slider);
    this.SetScrollPosition(this.getScrollPosDD(ev, isslider));
    this.DragEventID = '';
    this.DragEventID += Events.Attach(this.SB_Conteinter, 'onmousemove', this, 'fk_Drag');
    this.DragEventID += '|' + Events.Attach(this.SB_Slider,     'onmousemove', this, 'fk_Drag');
    this.DragEventID += '|' + Events.Attach(document, 'onmouseup', this, 'DragStop');
    ev.cancelBubble = true;
  }
  
  this.DragStop = function(){
    var arr = this.DragEventID.split('|');
    Events.Detach(arr[0]);
    Events.Detach(arr[1]);
    Events.Detach(arr[2]);
  }
  
  this.ScrollDown = function(){
    this.animFrame = this.fk_ScrollDown;
    AnimEng.add(this);
  } 
  
  this.ScrollUp = function(){
    this.animFrame = this.fk_ScrollUp;
    AnimEng.add(this);
  } 
  
  this.ScrollStop = function(){
    AnimEng.remove(this);
  }
  
  this.SetDisplay = function(obj, IsVis){
    if(obj!=null) obj.style.display = IsVis ? '' : 'none';
  }
  
  this.SetSBVisible = function(IsVis){
    this.SB_Box.style.visibility = IsVis ? 'visible' : 'hidden';
  }
  
  this.GetHeight = function(obj){
    return parseInt(obj.offsetHeight, 10);
  }
  
  this.fk_Drag = function(obj, ev){
    var isslider = (obj==this.SB_Slider)
    var y = this.getScrollPosDD(ev, isslider);
    this.SetScrollPosition(y);
    ev.cancelBubble = true;
  }
  
  this.fk_ScrollDown = function(){
    this.SetFramePosition(this.getFramePos() + this.constArrScrollStep);
  }
  
  this.fk_ScrollUp = function(){
    this.SetFramePosition(this.getFramePos() - this.constArrScrollStep);
  }
  
  this.getFramePos = function(){
    return parseInt(this.TextFrameCurrent.style.top, 10);
  }

  this.getScrollPosDD = function(ev, isscroll){
    
 	  var y;
    if(Browser.Type == Browser.IE || Browser.Type == Browser.Opera)
      y = ev.offsetY;
    else
      y = ev.layerY;
    
    return y - (this.SB_SliderHeight/2) + (isscroll ? this.getScrollPos() : 0);
  }
  
  this.getScrollPos = function(){
    return parseInt(this.SB_Slider.style.top, 10);
  }
  
  this.SetScrollPosition = function(topsl){
    if(topsl > this.SlideBarSpace) 
      topsl = this.SlideBarSpace;
    else if(topsl < 0)
      topsl = 0;
    
    var top = -topsl * this.FarmeOverhang / this.SlideBarSpace;
    this.TextFrameCurrent.style.top = top + 'px';
    
    if(topsl < 1) topsl = 1; 
    this.SB_Slider.style.top =  topsl + 'px';
  }
  
  this.SetFramePosition = function(top){
  
    if(top > 0) {
      top = 0;
    }
    else if(top < -this.FarmeOverhang){
      top = -this.FarmeOverhang;
    }
    
    this.TextFrameCurrent.style.top = top + 'px';
    
    if(this.SB_Box.style.visibility == 'visible'){
      topsl = (-top) * this.SlideBarSpace/this.FarmeOverhang;
      if(topsl < 1) topsl = 1; 
      this.SB_Slider.style.top =  topsl + 'px';
    }
  }

  this.Init();

  this.animEngineServiced=0;
  this.animFrame=function(){}
}

