
var FrameBox;

loadImage('/images/arr_top.gif', '/images/arr_bottom.gif', '/images/scroolbg.gif');

Events.Attach(window, 'onload', function(){
  Menu.Init('divMenu', 'divDownload');
  FrameBox = new SliderFramework('Texts', 'ScrollBarBox', 'ScrollBar', 'Slider', 'ScrollButtonUp', 'ScrollButtonDown');
  Menu.Load(1);
});

var Menu = new function(){
  this.divMenu = null;
  this.menuLamp = null;
  this.download = null;
  
  this.curentSection = -1;
  
  this.cache = new Object();
  this.sections = ['news', 'overview', 'screens', 'video'];
  
  this.Init = function(name, download){
    this.divMenu = document.getElementById(name); 
    this.menuLamp = this.divMenu.getElementsByTagName('DIV')
    this.download = document.getElementById(download); 
  }
  
  this.Show = function(i, vis){ 
    if(this.menuLamp!=null) 
      if(this.curentSection!=i)
        this.menuLamp[i-1].className = vis==1 ? 'el lamp_over': 'el lamp';
  }
  
  this.Screen = function(id){
    var html = [];
    
    var i = id-5110+1;
    var next = id+1<=5145 ? id+1 : 5110;
    var back = id-1>=5110 ? id-1 : 5145;
    
    html.add('<div><img src="/images/screens/'+id+'.jpg" width="387" height="290" /></div>');
    
    html.add('<div align="center" style="margin-top:10px;">');
    html.add('<table cellpadding="0" cellspacing="0" border="0"><tr>');
    html.add('<td width="20" ><a href="javascript:void(0);" onclick="Menu.Screen('+back+');"><img src="/images/screen_back.gif" width="87" height="14" style="border-width:0px;position:static;" /></a></td>');
    html.add('<td width="120" align="center">Снимок '+i+' из 36</td>');
    html.add('<td width="20" align=:right"><a href="javascript:void(0);" onclick="Menu.Screen('+next+');"><img src="/images/screen_next.gif" width="87" height="14" style="border-width:0px;position:static;" /></a></td>');
    html.add('</tr></table>');
    html.add('</div>');
  
    FrameBox.selectFrame('divDescription', html.join(''));
  }
  
  this.CreateScreens = function(){
    var html = [];
    
    for(var i=5110; i<5146; i++)
      html.add('<a href="javascript:void(0);" onclick="Menu.Screen('+i+');"><img src="/images/screens/'+i+'_s.jpg" width="100" height="75" style="margin:0px 10px 10px 0px;" /></a> ');
      
    return html.join('');
  }
  
  this.Load = function(i){
    var sec = this.sections[i-1];
    var val = this.cache[sec];
    
    if(typeof(val)!='undefined'){
      this.ShowValue(sec, val);
    }
    else{
      if(sec=='screens'){
        this.ShowValue(sec, this.CreateScreens());
      }
      else if(sec=='video'){
        this.ShowValue(sec, '<div style="margin-top:20px;text-align:center;"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="320" height="240" viewastext><param name="movie" value="/images/video.swf"><param name="quality" value="high"><embed src="/images/video.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="320" height="240"></embed></object></div>');
      }
      else{
        FrameBox.SetDisplay(FrameBox.TextFrameCurrent, false);
        FrameBox.SetSBVisible(false);
        this.download.style.display = '';
        if(sec!=null) _default.GetListResult(sec, this, 'InitFrame');
      }
    }
  }
  
  this.InitFrame = function(result){
    if(result.error!=null){ alert(result.error); return; }
    this.download.style.display = 'none';
    
    var val = result.value;
    var index = val.indexOf(';');
    var sec = val.substr(0,index);
    
    this.ShowValue(sec, val.substr(index+1, val.length));
  }
  
  this.ShowValue = function(sec, val){
    var i;
    for(i=0; i<this.sections.length; i++)
      if(this.sections[i]==sec) break;
      
    i++;

    if(i != this.curentSection || this.curentSection==3){

      var old = this.curentSection;
      this.curentSection = -1;
      
      if(old!=-1) this.Show(old, false);
      
      this.Show(i, true);
      this.curentSection = i;      
      
      FrameBox.selectFrame('divDescription', val);
      this.cache[sec] = val;      
    }
  }
}

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 = 9;
  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_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;
      
      if(Browser.Type == Browser.IE && this.MouseWheelEventID == null){
        this.MouseWheelEventID = Events.Attach(this.TextConteinter, 'onmousewheel', this, 'MouseWheel');
      }
    }
    else{
      this.SetSBVisible(false);
      Events.Detach(this.MouseWheelEventID);
      this.MouseWheelEventID = null;
    }
    
    this.SetFramePosition(0);
  }
  
  this.MouseWheel = function(obj, ev){
    this.SetFramePosition(this.getFramePos() + (ev.wheelDelta > 0 ? this.constMouseWheelStep : -this.constMouseWheelStep));
    ev.returnValue = false;
    ev.cancelBubble = true;
  }
  
  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;
    
    if(topsl < 1) topsl = 1; 
    this.SB_Slider.style.top =  topsl;
  }
  
  this.SetFramePosition = function(top){
    if(top > 0) {
      top = 0;
    }
    else if(top < -this.FarmeOverhang){
      top = -this.FarmeOverhang;
    }

    topsl = (-top) * this.SlideBarSpace/this.FarmeOverhang;
    if(topsl < 1) topsl = 1; 
    
    this.TextFrameCurrent.style.top = top;
    this.SB_Slider.style.top =  topsl;
  }

  this.Init();

  this.animEngineServiced=0;
  this.animFrame=function(){}
}
