/* =======================================================
* ypSimpleScroll
* 3/11/2001
* 
* http://www.yougnpup.net/
* ======================================================= */
var scrolldir = -1;
var scrollstep = 0;
ypSimpleScroll.prototype.scrollNorth = function() { scrolldir = -1; this.startScroll(90) }
ypSimpleScroll.prototype.scrollSouth = function() { scrolldir = 1; this.startScroll(270) }
ypSimpleScroll.prototype.scrollWest = function() { this.startScroll(180) }
ypSimpleScroll.prototype.scrollEast = function() { this.startScroll(0) }

// start scrolling in an arbitrary direction, with an optional arbitrary speed.
// if no speed is provided, the default speed from the constructor parameter is used.
ypSimpleScroll.prototype.startScroll = function(deg, speed) {

    if (this.loaded) {
        if (this.aniTimer) window.clearTimeout(this.aniTimer)
        this.overrideScrollAngle(deg)
        this.speed = speed ? speed : this.origSpeed
        this.lastTime = (new Date()).getTime() - this.y.minRes
        
        this.aniTimer = window.setTimeout(this.gRef + ".scroll();updateLocator();", this.y.minRes)
    }
}

// kills pending calls to .scroll(), resets speed
ypSimpleScroll.prototype.endScroll = function() {
   
    if (this.loaded) {
        window.clearTimeout(this.aniTimer)
        this.aniTimer = 0;
        this.speed = this.origSpeed
        
    }
}

// override the current angle from startScroll() with a new angle
// this method should not be used with scrollTo() because it will
// override the angle created by the first two params to scrollTo().
ypSimpleScroll.prototype.overrideScrollAngle = function(deg) {
if (this.loaded)
{
deg = deg % 360
if (deg % 90 == 0) {
var cos = deg == 0 ? 1 : deg == 180 ? -1 : 0
var sin = deg == 90 ? -1 : deg == 270 ? 1 : 0
} else {
var angle = deg * Math.PI / 180
var cos = Math.cos(angle)
var sin = Math.sin(angle)
sin = -sin
}
this.fx = cos / (Math.abs(cos) + Math.abs(sin))
this.fy = sin / (Math.abs(cos) + Math.abs(sin))
this.stopH = deg == 90 || deg == 270 ? this.scrollLeft : deg < 90 || deg > 270 ? this.scrollW : 0
this.stopV = deg == 0 || deg == 180 ? this.scrollTop : deg < 180 ? 0 : this.scrollH
}
}

// override the speed for the current scroll.
// should not be used with scrollTo, unless you want to 
// override the speed computed from the aniLen param of scrollTo()
ypSimpleScroll.prototype.overrideScrollSpeed = function(speed) {
if (this.loaded) this.speed = speed
}

// scroll to an arbitrary position in two dimensions over in
// an arbitrary lenth of time.
ypSimpleScroll.prototype.scrollTo = function(stopH, stopV, aniLen) {
    if (this.loaded) {

        if (stopH != this.scrollLeft || stopV != this.scrollTop) {
            if (this.aniTimer) window.clearTimeout(this.aniTimer)
            this.lastTime = (new Date()).getTime()
            var dx = Math.abs(stopH - this.scrollLeft)
            var dy = Math.abs(stopV - this.scrollTop)
            var d = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2))
            this.fx = (stopH - this.scrollLeft) / (dx + dy)
            this.fy = (stopV - this.scrollTop) / (dx + dy)
            this.stopH = stopH
            this.stopV = stopV
            this.speed = d / aniLen * 1000

            window.setTimeout(this.gRef + ".scroll();updateLocator();", this.y.minRes)
        }
        
    }
}

// jump to an arbitrary position in two dimensions.
ypSimpleScroll.prototype.jumpTo = function(nx, ny) { 
if (this.loaded)
{
nx = Math.min(Math.max(nx, 0), this.scrollW)
ny = Math.min(Math.max(ny, 0), this.scrollH)
this.scrollLeft = nx
this.scrollTop = ny
if (this.y.ns4)this.content.moveTo(-nx, -ny)
else {
this.content.style.left = -nx + "px"
this.content.style.top = -ny + "px"
}
}
}

// =========================================================================
// =============================== private =================================
// =========================================================================

ypSimpleScroll.minRes = 10
ypSimpleScroll.ie = document.all ? 1 : 0
ypSimpleScroll.ns4 = document.layers ? 1 : 0
ypSimpleScroll.dom = document.getElementById ? 1 : 0
ypSimpleScroll.mac = navigator.platform == "MacPPC"
ypSimpleScroll.mo5 = document.getElementById && !document.all ? 1 : 0

// recursively calls itself to animate
ypSimpleScroll.prototype.scroll = function() {
    this.aniTimer = window.setTimeout(this.gRef + ".scroll();updateLocator();", this.y.minRes)
    var nt = (new Date()).getTime()
    var d = Math.round((nt - this.lastTime) / 1000 * this.speed)
    if (d > 0) {
        var nx = d * this.fx + this.scrollLeft
        var ny = d * this.fy + this.scrollTop
        var xOut = (nx >= this.scrollLeft && nx >= this.stopH) || (nx <= this.scrollLeft && nx <= this.stopH)
        var yOut = (ny >= this.scrollTop && ny >= this.stopV) || (ny <= this.scrollTop && ny <= this.stopV)
        if (nt - this.lastTime != 0 &&
((this.fx == 0 && this.fy == 0) ||
(this.fy == 0 && xOut) ||
(this.fx == 0 && yOut) ||
(this.fx != 0 && this.fy != 0 && xOut && yOut))) {
            this.jumpTo(this.stopH, this.stopV)
            this.endScroll()
            d = document;
                        var curLocator = d.getElementById('locator');
                        if ((scrolldir == 1) && (curLocator.style.top != "0px"))
                            curLocator.style.top = 345+"px";
                        else
                            if ((scrolldir == -1) && (curLocator.style.top != "345px"))
                            curLocator.style.top = 0+"px";
        }
        else {
            this.jumpTo(nx, ny)
            this.lastTime = nt
        }
    }
}

function updateLocator() {
    if (obj2.scrollHeight != 380) {
        d = document;
        var ipos;
        ipos = ipos * 1;
        var spos = obj2.style.top;
        spos = spos.substring(0, spos.length - 2);
        ipos = spos * 1; //345 * (Math.abs(obj2.style.top) / 533);
        ipos = Math.abs(ipos);
        ipos = 345 * (ipos / (obj2.scrollHeight - 380));
        var curLocator = d.getElementById('locator');
        curLocator.style.top = ipos+"px";
    }
}

function updateLocator2() {
    var step = Math.ceil((obj2.scrollHeight - 345) / 345);
  
    scrollstep = scrollstep + 1;

    var mod = scrollstep % step;
    if (mod == 0) {
        d = document;
        var curLocator = d.getElementById('locator');
        var spos = curLocator.style.top;
        spos = spos.substring(0, spos.length - 2);
        var ipos;
        ipos = ipos * 1;
        var ipos = (spos * 1) + (1 * scrolldir);

        if (obj2.scrollHeight + obj2.style.top == 380)
            ipos = 345;
        if (ipos >= 345) {
            //alert("150 end scrollstep: "+scrollstep);
            ipos = 345;
            
            //obj2.scrollHeightvar step = Math.floor((obj2.scrollHeight - 150) / 150);
            //scrolldir = scrolldir * -1;
        }
        else {
            if (ipos <= 0) {
                //alert("0 end scrollstep: " + scrollstep);
                ipos = 0;
                
                //scrolldir = scrolldir * -1;
            }
        }
        curLocator.style.top = ipos+"px";
    }

}

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)
        handle(delta);
    /** 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;
}

function pausecomp(millis) {
    //alert(millis);
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while (curDate - date < millis);
}

function handle(delta) {
    //alert(delta);
    //alert(test.scrollTop + (delta * -40));
    test.scrollTo(test.scrollLeft, test.scrollTop + (delta*-40), 40);
    //alert(test.scrollTop);
    //this.startScroll(0 - (delta * 20));
    
//    if (delta >= 0)
//        test.scrollNorth();
//    else
//        test.scrollSouth();

//    window.clearTimeout();
//    window.setTimeout("test.endScroll()", 1000);
//    window.clearTimeout();
    //pausecomp(3000);
    //alert("after");
    //test.endScroll();
  
}

// constructor
function ypSimpleScroll(id, left, top, width, height, speed, contentWidth, initLeft, initTop)
{
var y = this.y = ypSimpleScroll
if (!initLeft) initLeft = 0
if (!initTop) initTop = 0
if (!contentWidth) contentWidth = width
if (document.layers && !y.ns4) history.go(0)
if (y.ie || y.ns4 || y.dom) {
this.loaded = false
this.id = id
this.origSpeed = speed
this.aniTimer = false
this.op = ""
this.lastTime = 0
this.clipH = height
this.clipW = width
this.scrollTop = initTop
this.scrollLeft = initLeft

// global reference to this object
this.gRef = "ypSimpleScroll_"+id
eval(this.gRef+"=this")
var d = document
d.write('<style type="text/css">')
d.write('#' + this.id + 'Container { left:' + left + 'px; top:' + top + 'px; width:' + width + 'px; height:' + height + 'px; clip:rect(0 ' + width + ' ' + height + ' 0); overflow:hidden; }')
d.write('#' + this.id + 'Container, #' + this.id + 'Content { position:absolute; }')
d.write('#' + this.id + 'Content { left:' + (-initLeft) + 'px; top:' + (-initTop) + 'px; width:' + contentWidth + 'px; }')
d.write('</style>')
}
}


// once the nceesary objects are present in the DOM, this gathers some info about them.
ypSimpleScroll.prototype.load = function() {
    var d, lyrId1, lyrId2

    if (window.addEventListener)
        window.addEventListener('DOMMouseScroll', wheel, false);
    window.onmousewheel = document.onmousewheel = wheel;



    d = document
    lyrId1 = this.id + "Container"
    lyrId2 = this.id + "Content"
    this.container = this.y.dom ? d.getElementById(lyrId1) : this.y.ie ? d.all[lyrId1] : d.layers[lyrId1]
    this.content = obj2 = this.y.ns4 ? this.container.layers[lyrId2] : this.y.ie ? d.all[lyrId2] : d.getElementById(lyrId2)

    if (obj2.scrollHeight <= 380) {
        d.getElementById("buttonDownCell").style.visibility = "hidden";
        d.getElementById("buttonUpCell").style.visibility = "hidden";
        d.getElementById("rightScrollCell").style.visibility = "hidden";
        d.getElementById("locator").style.visibility = "hidden";
    }
    else {
        this.docH = Math.max(this.y.ns4 ? this.content.document.height : this.content.offsetHeight, this.clipH)
        this.docW = Math.max(this.y.ns4 ? this.content.document.width : this.content.offsetWidth, this.clipW)
        this.scrollH = this.docH - this.clipH
        this.scrollW = this.docW - this.clipW
        this.loaded = true
        this.scrollLeft = Math.max(Math.min(this.scrollLeft, this.scrollW), 0)
        this.scrollTop = Math.max(Math.min(this.scrollTop, this.scrollH), 0)
        var curLocator = this.y.dom ? d.getElementById("locator") : this.y.ie ? d.all["locator"] : d.layers["locator"]
        curLocator.style.top = this.scrollTop + "px";
        //curLocator.style.top = 345;
        //alert(obj2.scrollHeight);
        updateLocator()
        this.jumpTo(this.scrollLeft, this.scrollTop)
        
    }
}
