var prevMouse;
var dragging=false;
var scroll;

var scrolling=false;
var page;

	function getCookie(name) { 
	   var start = document.cookie.indexOf(name+"="); 
	   var len = start+name.length+1; 
	   if ((!start) && (name != document.cookie.substring(0,name.length))) return null; 
	   if (start == -1) return null; 
	   var end = document.cookie.indexOf(";",len); 
	   if (end == -1) end = document.cookie.length; 
	   return unescape(document.cookie.substring(len,end)); 
	} 
	// This function has been slightly modified
	function setCookie(name,value,expires,path,domain,secure) { 
		expires = expires * 60*60*24*1000;
		var today = new Date();
		var expires_date = new Date( today.getTime() + (expires) );
	    var cookieString = name + "=" +escape(value) + 
	       ( (expires) ? ";expires=" + expires_date.toGMTString() : "") + 
	       ( (path) ? ";path=" + path : "") + 
	       ( (domain) ? ";domain=" + domain : "") + 
	       ( (secure) ? ";secure" : ""); 
	    document.cookie = cookieString; 
	} 



function initScroll(){
   document.getElementById("scroll").style.display = "block";
   scroll = new Scrollbar();  
   document.getElementById("scroll_up").onmousedown = function() { scroll.startScroll(10); return false; };
   document.getElementById("scroll_down").onmousedown = function() { scroll.startScroll(-10); return false; };
   document.getElementById("scrollbar").onmousedown = function(event) { scroll.startDrag(event); return false;};
   
   if (window.addEventListener)     /** DOMMouseScroll is for mozilla. */
        document.getElementById("left").addEventListener('DOMMouseScroll', wheel, false);     
   //window.onmousewheel = document.onmousewheel = wheel;  /** IE/Opera. */
   document.getElementById("left").onmousewheel = wheel;  /** IE/Opera. */
   document.onmouseup = stopAll;
   document.onmousemove = function(event) { checkMove(event); }
   
   
   //debug = document.getElementById("debug");    
   page = document.getElementById("pageId").value;
   
   var cookie = parseInt(getCookie("rado"+page));
   var initScroll = document.getElementById("initialScroll").value*514;
   var toScroll = cookie < initScroll ? cookie : initScroll;
   //alert(initScroll);
   scroll.moveScrollBar(-toScroll);
}


function checkMove(event){
   if(dragging){
      var newY = getMouseY(event);
      scroll.moveScrollBar(-newY+prevMouse);
      prevMouse = newY;
   }  
}

function stopAuto(){
   scrolling = false;
}

function stopAll(){
   scrolling = false;
   dragging = false;
}

function Scrollbar(){
   var element = document.getElementById("thumbs");
   var elementHeight = parseInt(element.style.height);;
   element.style.marginTop = "0px";//element.style.top = "0px";
   var scrollbar = document.getElementById("scrollbar");
   scrollbar.style.top = "0px";
   var SCROLLED_HEIGHT = 546;     //pohybovany
   var SCROLL_HEIGHT = 514;      //scrollbar celej
   var SCROLLBAR_HEIGHT = (parseFloat(SCROLLED_HEIGHT) / parseFloat(elementHeight)) * SCROLL_HEIGHT; //aktivni prvek
   scrollbar.style.height = SCROLLBAR_HEIGHT+"px";
   var MAX_ELEMENT_SCROLL = SCROLLED_HEIGHT - elementHeight;;  //max scrollovani
   var MAX_SCROLLBAR_SCROLL = SCROLL_HEIGHT - SCROLLBAR_HEIGHT; 
   var RATIO = MAX_ELEMENT_SCROLL/MAX_SCROLLBAR_SCROLL;
   var currentY = 0;
   var speed = 0;

   
   this.startDrag = function(event){
      dragging = true;
      prevMouse = getMouseY(event);
      
   }
   
   this.keepScrolling = function(){
      //debug.innerHTML = this.speed+""+scrolling;
      if(scrolling){
         //debug.innerHTML = debug.innerHTML + "."; 
         this.moveScrollBar(this.speed);
         setTimeout("scroll.keepScrolling();", 100);
     }
   }
   
   this.moveScrollBar = function(amount){
      //alert(amount);
      var y = parseInt(currentY) - parseInt(amount);
      if(y<0){
         y = 0;
         stopAuto();
      } else if (y > MAX_SCROLLBAR_SCROLL) {
         y = MAX_SCROLLBAR_SCROLL;
         stopAuto();
      }
      scrollbar.style.top = y +"px";
      currentY = parseInt(y);  
      element.style.marginTop = (currentY*RATIO) +"px"
      setCookie("rado"+page,y,60000000);

   }
   
   
   this.startScroll = function(amount){
      this.speed = amount;
      scrolling = true;
      setTimeout("scroll.keepScrolling();", 10);
   }
   
   
}

function getMouseY(evt) {
   if(!evt)    /* milujeme IEcko */
      evt = window.event;
   if (evt.pageY) 
      return evt.pageY;
   else if (evt.clientY)
      return evt.clientY + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
   else 
      return evt.offsetY;
}
function wheel(event){
   if (!event){ /* For IE. */
      event = window.event;
      event.wheeldata *= -1;
   }
   if (event.wheelDelta) { /* IE/Opera. */
      delta = event.wheelDelta/120;
   } else if (event.detail) { /** Mozilla case. */
      delta = -event.detail/3;
   }
   if (delta)
      scroll.moveScrollBar(delta*10);
   if (event.preventDefault) 
      event.preventDefault();
   event.returnValue = false; 
   return false;
}
