// <![CDATA[

var scrollDiv;
var hDiv;
var tClip;
var bClip;
var wClip;
var interval;
var topPos = 0;

function initScroller(id) {

    var elem = document.getElementById(id);
    if (elem.addEventListener) {
        elem.addEventListener("DOMMouseScroll", do_wheel, false);
    }

    scrollDiv = document.getElementById(id);   
    var margin = parseInt(scrollDiv.offsetTop);
    
    var scrollDiv__controls = document.getElementById(id + '__controls');

    /* Style per il div interno (testo) */
    scrollDiv.style.width    = (scrollDiv.parentNode.offsetWidth - (margin * 2)) - 20 + 'px';
    scrollDiv.style.height   = 'auto';
    scrollDiv.style.overflow = 'hidden';
    
    /* Style per il div esterno (contenitore) */
    scrollDiv.parentNode.style.overflow = 'hidden';
    
    /* Style per il div con i controlli (testo) */
    scrollDiv__controls.style.display = 'block';  


    /* Impostazioni per visualizzare la parte di testo superiore */
    hDiv = scrollDiv.offsetHeight;

    //document.getElementById('check').innerHTML = hDiv;
    var the_height=parseInt(hDiv);
    if (the_height < 395) {
        document.getElementById('imgup').style.display = "none";
        document.getElementById('imgdown').style.display = "none";
    }
    else {
        document.getElementById('imgup').style.display = "block";
        document.getElementById('imgdown').style.display = "block";
    }

    tClip = 0;   
    wClip = scrollDiv.parentNode.offsetWidth - (margin * 2);
    bClip = scrollDiv.parentNode.offsetHeight - (margin * 2);

    scrollDiv.style.clip = 'rect('+ tClip +'px,'+ wClip +'px,'+ bClip +'px,0)';
    //alert("tClip:"+tClip+"\nwClip:"+wClip+"\nbClip:"+bClip+"\n");
    //rect (top, right, bottom, left)
}

function scroll(scrollBy, time) {
    tClip += scrollBy;
    bClip += scrollBy;
    topPos -= scrollBy;

    if (tClip < 0 || bClip > hDiv) {
        tClip -= scrollBy;
        bClip -= scrollBy;
        topPos += scrollBy;    
    }
    
    scrollDiv.style.clip = 'rect('+ tClip +'px, '+ wClip +'px, '+ bClip +'px, 0)';   
    scrollDiv.style.top = topPos + 'px';
    interval = setTimeout('scroll(' + scrollBy + ', ' + time + ')', time);
}

function stopScroll() {
    if (interval) clearTimeout(interval);
}

function do_wheel(event) {
    if (event.wheelDelta === undefined) {   // Firefox
        if (event.detail <= 0) {
            scroll(-15, 50);
        }
        else {
            scroll(15, 50);
        }
    }
    else {
        if (event.wheelDelta >= 120) {
            scroll(-15, 50);
        }
        else if (event.wheelDelta <= -120) {
            scroll(15, 50);
        }
    }
    stopScroll();
}

function reset_scrolling(id) {
    topPos = 0;

    document.getElementById(id).style.top = "0px";
    document.getElementById('container').style.display = "none";
    document.getElementById('scrolldiv__controls').style.display = "none";
}

// ]]>
