
var contentHeight = 0; 	// The total height of the content
var visibleContentHeight = 0;
var scrollActive = false;

var scrollHandleObj = false; // reference to the scroll handle
var scrollHandleHeight = false;
var scrollbarTop = false;
var eventYPos = false;

var scrollbuttonActive = false;
var scrollbuttonDirection = false;
var scrollbuttonSpeed = 10; // How fast the content scrolls when you click the scroll buttons(Up and down arrows)
var scrollTimer = 0;	// Also how fast the content scrolls. By decreasing this value, the content will move faster

var scrollMoveToActive = false;
var scrollMoveToYPosition = false;

var operaBrowser = false;
if(navigator.userAgent.indexOf('Opera')>=0)operaBrowser=1;

function obj(o){
    return document.getElementById(o);
}


function scrollDiv_startScroll(e){
    if(document.all && !operaBrowser) e = event;
    scrollbarTop = obj('scrolldiv_theScroll').offsetTop;
    eventYPos = e.clientY;
    scrollActive = true;
}

function scrollDiv_stopScroll(){
    scrollActive = false;
    scrollbuttonActive = false;
    scrollMoveToActive = false;
}
function scrollDiv_scroll(e){
    if(!scrollActive)return;
    if(document.all && !operaBrowser)e = event;
    if(e.button!=1 && document.all)return;
    var topPos = scrollbarTop + e.clientY - eventYPos;
    if(topPos<0)topPos=0;
    if(topPos/1>visibleContentHeight-(scrollHandleHeight+4)/1) topPos = visibleContentHeight-(scrollHandleHeight+4);
    obj('scrolldiv_theScroll').style.top = topPos + 'px';
    obj('scrolldiv_content').style.top = 0 - Math.floor((contentHeight) * ((topPos)/(visibleContentHeight-scrollHandleHeight)))+'px'
}

/*
	Click on the slider
	Move the content to the this point
	*/
function scrolldiv_scrollMoveToInit(e){
    if(document.all && !operaBrowser)e = event;
    scrollMoveToActive = true;
    scrollMoveToYPosition = e.clientY - obj('scrolldiv_scrollbar').offsetTop;
    if(obj('scrolldiv_theScroll').offsetTop/1 > scrollMoveToYPosition) scrollbuttonDirection = scrollbuttonSpeed*-2;
    else  scrollbuttonDirection = scrollbuttonSpeed*2;
    scrolldiv_scrollMoveTo();
}
function gotoAncor(ancor){
    if(document.all && !operaBrowser)e = event;
    scrollMoveToActive = true;
    var point=((obj(ancor).offsetTop)*obj('scrolldiv_slider').offsetHeight)/obj('scrolldiv_content').offsetHeight;
    scrollMoveToYPosition = point;
   //document.title=point;
    if(obj('scrolldiv_theScroll').offsetTop/2 > scrollMoveToYPosition) scrollbuttonDirection = scrollbuttonSpeed*-2;
    else  scrollbuttonDirection = scrollbuttonSpeed*2;
    scrolldiv_scrollMoveTo();
}

function scrolldiv_scrollMoveTo(){
    if(!scrollMoveToActive || scrollActive)return;
    var topPos = obj('scrolldiv_theScroll').style.top.replace('px','');
    topPos = topPos/1 + scrollbuttonDirection;
    if(topPos<0){
        topPos=0;
        scrollMoveToActive=false;
    }
    if(topPos/1>visibleContentHeight-(scrollHandleHeight+4)/1){
        topPos = visibleContentHeight-(scrollHandleHeight+4);
        scrollMoveToActive=false;
    }
    if(scrollbuttonDirection<0 && topPos<scrollMoveToYPosition-scrollHandleHeight/2)return;
    if(scrollbuttonDirection>0 && topPos>scrollMoveToYPosition-scrollHandleHeight/2)return;
    obj('scrolldiv_theScroll').style.top = topPos + 'px';
    obj('scrolldiv_content').style.top = 0 - Math.floor((contentHeight) * ((topPos)/(visibleContentHeight-scrollHandleHeight)))+'px'
    setTimeout('scrolldiv_scrollMoveTo()',scrollTimer);
}
function cancelEvent(){
    return false;
}

function scrolldiv_scrollButton()
{
    if(this.id=='scrolldiv_scrollDown')scrollbuttonDirection = scrollbuttonSpeed;
    else scrollbuttonDirection = scrollbuttonSpeed*-1;
    scrollbuttonActive=true;
    scrolldiv_scrollButtonScroll();
}
function scrolldiv_scrollButtonScroll()
{
    if(!scrollbuttonActive)return;
    var topPos = obj('scrolldiv_theScroll').style.top.replace('px','');
    topPos = topPos/1 + scrollbuttonDirection;
    if(topPos<0){
        topPos=0;
        scrollbuttonActive=false;
    }
    if(topPos/1>visibleContentHeight-(scrollHandleHeight+4)/1){
        topPos = visibleContentHeight-(scrollHandleHeight+4);
        scrollbuttonActive=false;
    }
    obj('scrolldiv_theScroll').style.top = topPos + 'px';
    obj('scrolldiv_content').style.top = 0 - Math.floor((contentHeight) * ((topPos)/(visibleContentHeight-scrollHandleHeight)))+'px'
    var scrolling=setTimeout('scrolldiv_scrollButtonScroll()',scrollTimer);
}
function scrolldiv_scrollButtonStop(){
    scrollbuttonActive = false;
}
/*
	Change from the default color
	*/
function scrolldiv_setColor(rgbColor)
{
    obj('scrolldiv_scrollbar').style.borderColor = rgbColor;
    obj('scrolldiv_theScroll').style.backgroundColor = rgbColor;
    obj('scrolldiv_scrollUp').style.borderColor = rgbColor;
    obj('scrolldiv_scrollDown').style.borderColor = rgbColor;
    obj('scrolldiv_scrollUp').style.color = rgbColor;
    obj('scrolldiv_scrollDown').style.color = rgbColor;
    obj('scrolldiv_parentContainer').style.borderColor = rgbColor;
}
/*
	Setting total width of scrolling div
	*/
function scrolldiv_setWidth(newWidth)
{
    obj('container_scrolldiv').style.width = newWidth + 'px';
    obj('scrolldiv_parentContainer').style.width = newWidth-40 + 'px';
}

/*
	Setting total height of scrolling div
	*/
function scrolldiv_setHeight(newHeight)
{
    obj('container_scrolldiv').style.height = newHeight + 'px';
    obj('scrolldiv_parentContainer').style.height = newHeight + 'px';
    obj('scrolldiv_slider').style.height = newHeight + 'px';
    obj('scrolldiv_scrollbar').style.height = newHeight-40 + 'px';
}
/*
	Setting new background color to the slider
	*/
function setSliderBgColor(rgbColor)
{
    obj('scrolldiv_scrollbar').style.backgroundColor = rgbColor;
    obj('scrolldiv_scrollUp').style.backgroundColor = rgbColor;
    obj('scrolldiv_scrollDown').style.backgroundColor = rgbColor;
}
/*
	Setting new content background color
	*/
function setContentBgColor(rgbColor)
{
    obj('scrolldiv_parentContainer').style.backgroundColor = rgbColor;
}

/*
	Setting scroll button speed
	*/
function setScrollButtonSpeed(newScrollButtonSpeed)
{
    scrollbuttonSpeed = newScrollButtonSpeed;
}
/*
	Setting interval of the scroll
	*/
function setScrollTimer(newInterval)
{
    scrollTimer = newInterval;
}

// MOUSE WHEEL
function handle(delta) {
    if (delta < 0){
        scrollbuttonDirection = scrollbuttonSpeed;
        scrollbuttonActive=true;
        scrolldiv_scrollButtonScroll();
        setTimeout('scrollbuttonActive=false;',(-delta*50));
    }else if (delta > 0){
        scrollbuttonDirection = scrollbuttonSpeed*-1;
        scrollbuttonActive=true;
        scrolldiv_scrollButtonScroll();
        setTimeout('scrollbuttonActive=false;',(delta*50));
    }else{
        setTimeout('scrollbuttonActive=false;',200);
    }
}

function wheel(event){
    var delta = 0;
    if (!event) event = window.event;
    if (event.wheelDelta) {
        delta = event.wheelDelta/120;
        if (window.opera) delta = -delta;
    } else if (event.detail) {
        delta = -event.detail/3;
    }
    if (delta)
        handle(delta);
    if (event.preventDefault)
        event.preventDefault();
    event.returnValue = false;
}

function scrolldiv_initScroll(){
    visibleContentHeight = obj('scrolldiv_scrollbar').offsetHeight ;
    contentHeight = obj('scrolldiv_content').offsetHeight - visibleContentHeight;
    scrollHandleObj = obj('scrolldiv_theScroll');
    scrollHandleHeight = scrollHandleObj.offsetHeight;
    scrollbarTop = obj('scrolldiv_scrollbar').offsetTop;
    obj('scrolldiv_theScroll').onmousedown = scrollDiv_startScroll;
    document.body.onmousemove = scrollDiv_scroll;
    obj('scrolldiv_scrollbar').onselectstart = cancelEvent;
    obj('scrolldiv_theScroll').onmouseup = scrollDiv_stopScroll;
    if(document.all)
        document.body.onmouseup = scrollDiv_stopScroll;
    else
        document.documentElement.onmouseup = scrollDiv_stopScroll;
    obj('scrolldiv_scrollDown').onmousedown = scrolldiv_scrollButton;
    obj('scrolldiv_scrollUp').onmousedown = scrolldiv_scrollButton;
    obj('scrolldiv_scrollDown').onmouseup = scrolldiv_scrollButtonStop;
    obj('scrolldiv_scrollUp').onmouseup = scrolldiv_scrollButtonStop;
    obj('scrolldiv_scrollUp').onselectstart = cancelEvent;
    obj('scrolldiv_scrollDown').onselectstart = cancelEvent;
    obj('scrolldiv_scrollbar').onmousedown = scrolldiv_scrollMoveToInit;
    if (obj("scrolldiv_content").offsetHeight>obj("scrolldiv_parentContainer").offsetHeight){
        obj("scrolldiv_slider").style.display="";
        if (window.addEventListener)
           obj("container_scrolldiv").addEventListener('DOMMouseScroll', wheel, false);
          obj("container_scrolldiv").onmousewheel = wheel;
    }else
        obj("scrolldiv_slider").style.display="none";
}

