var sliderDragged = false;
var mouse_x=0, mouse_y=0;
var start_mouse_y = 0;
var start_slider_y = 0;
var start_mouse_x = 0;
var start_slider_x = 0;
var scrolltextLoaded = false;
var sliderId = '';
var scrollingArea = '';

function ScrollToPercent(percentage) {	
	if(oScroll.scrollDirection == 'vertical')
	{
		//var maxScroll = oScroll.scrollHeight - oScroll.el.parentNode.offsetHeight;	
		var maxScroll = oScroll.el.scrollHeight - oScroll.el.parentNode.offsetHeight;
		oScroll.moveIt(0, -maxScroll * percentage);
	}
	else
	{
		//var maxScroll = oScroll.scrollWidth - oScroll.el.parentNode.offsetWidth;	
		var maxScroll = oScroll.el.scrollWidth - oScroll.el.parentNode.offsetWidth;
		oScroll.moveIt(-maxScroll * percentage,0);
	}
}

function Slider_MouseUp(e) {
	sliderDragged = false;	
}

function Slider_MouseDown(e) {	
	var slider = document.getElementById(sliderId);	
	if(slider.style.top == "")
	{
		slider.style.top = 0;
	}
	if(slider.style.left == "")
	{
		slider.style.left = 0;
	}
	start_slider_y = parseInt(slider.style.top);		
	start_mouse_y = mouse_y;
	start_slider_x = parseInt(slider.style.left);		
	start_mouse_x = mouse_x;
	sliderDragged = true;
}

function CalcScrollPercentageY(newPositionY)
{
	var slider = document.getElementById(sliderId);
	var scrollArea = document.getElementById(scrollingArea);
	//var newPositionY = SliderstartY + moveY;
	if (newPositionY<0) newPositionY = 0;
		
	//var maxPositionY = scrollArea.height - slider.offsetHeight;
	var maxPositionY = scrollArea.clientHeight - slider.offsetHeight;
	if (newPositionY > maxPositionY) newPositionY = maxPositionY;
	slider.style.top = newPositionY + "px";						
	var percentage = newPositionY / maxPositionY;
	return percentage;
}

function CalcScrollPercentageX(newPositionX)
{
	var slider = document.getElementById(sliderId);
	var scrollArea = document.getElementById(scrollingArea);	
	if (newPositionX<0) newPositionX = 0;			
	var maxPositionX = scrollArea.clientWidth - slider.offsetWidth;
	if (newPositionX > maxPositionX) newPositionX = maxPositionX;
	slider.style.left = newPositionX + "px";						
	var percentage = newPositionX / maxPositionX;
	return percentage;
}

document.onmousemove = function(e) {
	mouse_x = e?e.clientX:event.screenX;
	mouse_y = e?e.clientY:event.screenY;
	if (scrolltextLoaded && sliderDragged) {
	if(oScroll.scrollDirection == 'vertical')
	{				
		var percentage = CalcScrollPercentageY(start_slider_y-start_mouse_y + mouse_y);				
	}
	else
	{
		var percentage = CalcScrollPercentageX(start_slider_x-start_mouse_x + mouse_x);				
	}
	ScrollToPercent(percentage);
	}			
}

document.onmouseup = function(e) {
	if (sliderDragged) sliderDragged = false;
}
function lib_bwcheck(){ //Browsercheck (needed)
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera5=(navigator.userAgent.indexOf("Opera")>-1 && document.getElementById)?1:0
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6
	this.mac=this.agent.indexOf("Mac")>-1
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
	return this
}
var bw=lib_bwcheck()


//If you want it to move faster you can set this lower, it's the timeout:
var speed = 100

//Sets variables to keep track of what's happening
var loop, timer

//Object constructor
function makeObj(obj,nest,scrollDirection){
    nest=(!nest) ? "":'document.'+nest+'.'
	this.el=bw.dom?document.getElementById(obj):bw.ie4?document.all[obj]:bw.ns4?eval(nest+'document.'+obj):0;
  	this.css=bw.dom?document.getElementById(obj).style:bw.ie4?document.all[obj].style:bw.ns4?eval(nest+'document.'+obj):0;
	this.scrollHeight=bw.ns4?this.css.document.height:this.el.offsetHeight
	this.scrollWidth=bw.ns4?this.css.document.width:this.el.offsetWidth
	if(scrollDirection != null)
	{
		this.scrollDirection = scrollDirection;
	}
	this.clipHeight=bw.ns4?this.css.clip.height:this.el.offsetHeight
	this.clipWidth=bw.ns4?this.css.clip.width:this.el.offsetWidth
	this.up=goUp;this.down=goDown;
	this.left=goLeft;this.right=goRight;
	this.moveIt=moveIt; this.x=0; this.y=0;
    this.obj = obj + "Object"
    eval(this.obj + "=this")
    return this
}

// A unit of measure that will be added when setting the position of a layer.
var px = bw.ns4||window.opera?"":"px";

function moveIt(x,y){
	this.x = x
	this.y = y		
	this.css.left = this.x+px		
	this.css.top = this.y+px				
		
}

//Makes the object go up
function goDown(move){
	if (this.y>-this.scrollHeight+oCont.clipHeight){
	var slider = document.getElementById(sliderId);	
	if(slider.style.top == "")
	{
		slider.style.top = 0;
	}
	start_slider_y = parseInt(slider.style.top);	
	var percentage = CalcScrollPercentageY(start_slider_y+move);		
	ScrollToPercent(percentage);
		/*this.moveIt(0,this.y-move)*/
			
			if (loop) setTimeout(this.obj+".down("+move+")",speed)
	}
}
//Makes the object go down
function goUp(move){
	if (this.y<0){
		//this.moveIt(0,this.y-move)
	var slider = document.getElementById(sliderId);	
	if(slider.style.top == "")
	{
		slider.style.top = 0;
	}
	start_slider_y = parseInt(slider.style.top);	
	var percentage = CalcScrollPercentageY(start_slider_y+move);		
	ScrollToPercent(percentage);
		if (loop) setTimeout(this.obj+".up("+move+")",speed)
	}
}
function goRight(move){
	if (this.x>-this.scrollWidth+oCont.clipWidth){
	var slider = document.getElementById(sliderId);	
	if(slider.style.left == "")
	{
		slider.style.left = 0;
	}
	start_slider_x = parseInt(slider.style.left);
	var percentage = CalcScrollPercentageX(start_slider_x+move);		
	ScrollToPercent(percentage);
		//this.moveIt(this.x-move, 0)
			if (loop) setTimeout(this.obj+".right("+move+")",speed)
	}
}
//Makes the object go down
function goLeft(move){
	if (this.x<0){
	var slider = document.getElementById(sliderId);	
	if(slider.style.left == "")
	{
		slider.style.left = 0;
	}
	start_slider_x = parseInt(slider.style.left);
	var percentage = CalcScrollPercentageX(start_slider_x+move);
	ScrollToPercent(percentage);
		//this.moveIt(this.x-move, 0)
		if (loop) setTimeout(this.obj+".left("+move+")",speed)
	}
}

//Calls the scrolling functions. Also checks whether the page is loaded or not.
function scrollV(speed){
	if (scrolltextLoaded){
		loop = true;		
		if (speed>0) oScroll.down(speed)
		else oScroll.up(speed)
	}
}

//Calls the scrolling functions. Also checks whether the page is loaded or not.
function scrollH(speed){
	if (scrolltextLoaded){
		loop = true;
		if (speed>0) oScroll.right(speed)
		else oScroll.left(speed)
	}
}

//Stops the scrolling (called on mouseout)
function noScroll(){
	loop = false
	if (timer) clearTimeout(timer)
}
//Makes the object
function scrolltextInit(sContainerId, sContentId, sSliderId, sScrollArea, sScrollBar, scrollDirection){	
	var Container = document.getElementById(sContainerId);
	var Content = document.getElementById(sContentId);
	if(Container!= null && Content!= null)
	{
		oCont = new makeObj(sContainerId);
		oScroll = new makeObj(sContentId,sContainerId, scrollDirection);
		oScroll.moveIt(0,0);
		oCont.css.visibility = "visible";
		scrolltextLoaded = true;
		sliderId = sSliderId;
		scrollingArea = sScrollArea;
		if (oScroll.y>-oScroll.scrollHeight+oCont.clipHeight){
			
			if (window.addEventListener)
			/** DOMMouseScroll is for mozilla. */
				window.addEventListener('DOMMouseScroll', wheel, false);
			/** IE/Opera. */
				window.onmousewheel = document.onmousewheel = wheel;
			var scrollArea = document.getElementById(sScrollBar);
			scrollArea.style.visibility = 'visible';
		}
	}
}

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)
       {
            if (delta>0)
            {
                  scrollV(-6);
                  noScroll();
            }
            else
            {
                  scrollV(6);
                  noScroll();
            }
        }
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
	    event.returnValue = false;
}

