/**
  @desc scrolling div content
  @author Grzegorz Bak <grzegorz.bak@sofpol.com>
*/
var myScroll_class = function(){
  this.lastOffset = 0;
  this.maxSteps = 0; //max liczba krokow
  this.step = 0; //aktualny krok
  this.heightBox = 0; //wysokosc 'scrola'
  this.heightBoxContainer = 0; //wysokosc kontenera dla scrola
  this.maxBoxY = 0;
  this.div = null; //uchwyt do kontenera dla tekstu
  this.maxVisibleArea = 0;
  this.heightArea = 0;
  this.hiddenArea;
  this.prefix = '';
  this.isScrolling = false;
  this.startPosY = 0;
  this.div_2 = null;
  this.idContentToScroll = 'myScroll_content';
  
  this.init = function(div_id,prefix){
    if(prefix!=undefined) this.prefix = prefix;
	
    try{
	  this.div = document.getElementById(div_id);
	}catch(err){
	  //div not found...
	}
	
	if(this.div!=null){
	  this.maxVisibleArea = parseInt(this.div.offsetHeight);
	  this.heightArea = parseInt(document.getElementById(this.idContentToScroll+this.prefix).offsetHeight);
      //alert('this.maxVisibleArea: '+this.maxVisibleArea+' || this.heightArea: '+this.heightArea);
      if(this.maxVisibleArea < this.heightArea){
	    container = document.getElementsByClassName('myScroll_container'+this.prefix);
		container[0].style.display = 'block';
	    this.hiddenArea = this.heightArea-this.maxVisibleArea+20;
		
		div_1 = document.getElementsByClassName('myScrollbar');
		div_1 = div_1[0];
		this.div_2 = document.getElementsByClassName('myScroll_box'+this.prefix);
		this.div_2 = this.div_2[0];
		scrollUp = document.getElementsByClassName('myScroll_arrow_up'+this.prefix);
		scrollUp = scrollUp[0];
		scrollDown = document.getElementsByClassName('myScroll_arrow_down'+this.prefix);
		scrollDown = scrollDown[0];
		
		this.heightBoxContainer = parseInt(div_1.offsetHeight);
		this.heightBox = parseInt(this.div_2.offsetHeight);
		document.lastOffset = 0;
		this.step = parseInt(this.hiddenArea/(this.heightBoxContainer - this.heightBox));
		
		this.maxSteps = this.heightBoxContainer - this.heightBox;
		//div_2.onmousedown = function(){ this.startScroll(); }

    if (window.addEventListener){
        /** DOMMouseScroll is for mozilla. */
        window.addEventListener('DOMMouseScroll', wheel, true);
        }
      else{
        /** IE/Opera. */
        window.onmousewheel = document.onmousewheel = wheel;
        }


		try{
	      document.getElementsByTagName('body').item(0).attachEvent('onmousemove', this.moveSlider);
	      document.getElementsByTagName('body').item(0).attachEvent('onmouseup', this.stopSlider);
		  this.div_2.attachEvent('onmousedown',this.startScroll);
		  scrollUp.attachEvent('onmousedown',this.clickUp);
		  scrollDown.attachEvent('onmousedown',this.clickDown);
		}catch(err){
		  this.div_2.addEventListener('mousedown', this.startScroll, true);
		  scrollDown.addEventListener('mousedown', this.clickDown, true);
		  scrollUp.addEventListener('mousedown', this.clickUp, true);
	      document.getElementsByTagName('body').item(0).addEventListener('mousemove', this.moveSlider, true);
	      document.getElementsByTagName('body').item(0).addEventListener('mouseup', this.stopSlider, true);
		}
	  }
	  
	  
	}
	
  }


  function moveByScroll(delta){
  if(delta<0){
     document.lastOffset = document.lastOffset + 35;
       offset = document.lastOffset;
       if(offset>myScroll.maxSteps){
         offset = myScroll.maxSteps;
         document.lastOffset = offset;
       }
       if(offset<0){
         offset = 0;
         document.lastOffset = offset;
       }

       st = offset*(myScroll.hiddenArea/myScroll.maxSteps);
       document.getElementById('myScroll_content').style.marginTop = '-'+st+'px';

       myScroll.div_2.style.marginTop = offset+'px';
      }
    if(delta>0){
       document.lastOffset = document.lastOffset - 35;
       offset = document.lastOffset;
      //alert(myScroll.step);
       if(offset>myScroll.maxSteps){
         offset = myScroll.maxSteps;
         document.lastOffset = offset;
       }
       if(offset<0){
         offset = 0;
         document.lastOffset = offset;
       }

       st = offset*(myScroll.hiddenArea/myScroll.maxSteps);
       document.getElementById('myScroll_content').style.marginTop = '-'+st+'px';

       myScroll.div_2.style.marginTop = offset+'px';
    }
    return true;
  }

function wheel(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)
                moveByScroll(delta);
        return true;
}


  this.startScroll = function(e){
    myScroll_class.startPosY = e.clientY;
    myScroll_class.isScrolling = true;
  }
  
  this.stopSlider = function(e){
     if(myScroll_class.isScrolling){
       myScroll_class.isScrolling = false;
	   offset = e.clientY-myScroll_class.startPosY + document.lastOffset;
	   if(offset>myScroll.maxSteps) offset = myScroll.maxSteps;
	   if(offset<0) offset = 0;
	   document.lastOffset = offset;
	   myScroll.div_2.style.marginTop = offset+'px';
	 }
  }
  
  this.moveSlider = function(e){
    if(myScroll_class.isScrolling){
	   offset = e.clientY - myScroll_class.startPosY + document.lastOffset;
	   if(offset>myScroll.maxSteps) offset = myScroll.maxSteps;
	   if(offset<0) offset = 0;
	   //alert(myScroll.div_2);
	   st = offset*(myScroll.hiddenArea/myScroll.maxSteps);
	   document.getElementById('myScroll_content').style.marginTop = '-'+st+'px';
	   //document.title = st;
	   myScroll.div_2.style.marginTop = offset+'px';
	}
  }

  this.clickDown = function(e){
     document.lastOffset = document.lastOffset + 5;
	 offset = document.lastOffset;
  //alert(myScroll.step);
	 if(offset>myScroll.maxSteps){
	   offset = myScroll.maxSteps;
	   document.lastOffset = offset;
	 }  
	 if(offset<0){
	   offset = 0;
	   document.lastOffset = offset;
	 }  

	 st = offset*(myScroll.hiddenArea/myScroll.maxSteps);
	 document.getElementById('myScroll_content').style.marginTop = '-'+st+'px';

	 myScroll.div_2.style.marginTop = offset+'px';
  }
  
  this.clickUp = function(e){
     document.lastOffset = document.lastOffset - 5;
	 offset = document.lastOffset;
  //alert(myScroll.step);
	 if(offset>myScroll.maxSteps){
	   offset = myScroll.maxSteps;
	   document.lastOffset = offset;
	 }  
	 if(offset<0){
	   offset = 0;
	   document.lastOffset = offset;
	 }  

	 st = offset*(myScroll.hiddenArea/myScroll.maxSteps);
	 document.getElementById('myScroll_content').style.marginTop = '-'+st+'px';

	 myScroll.div_2.style.marginTop = offset+'px';
  }
}

var myScroll = new myScroll_class('myScroll');

document.getElementsByClassName = function(className, parentElement) {
  if (typeof parentElement == 'string'){
    parentElement = document.getElementById(parentElement);
  } else if (typeof parentElement != 'object' ||
             typeof parentElement.tagName != 'string'){
    parentElement = document.body;
  }
  var children = parentElement.getElementsByTagName('*');
  var re = new RegExp('\\b' + className + '\\b');
  var el, elements = [];
  var i = 0;
  while ( (el = children[i++]) ){
    if ( el.className && re.test(el.className)){
      elements.push(el);
    }
  }
  return elements;

} 
