
if(!Control) var Control = {};
Control.slidingDiv = Class.create();
Control.slidingDiv.prototype = {

    initialize: function(target, options) {
        var slidingdiv = this;
        this.node = $(target);
        var options = Object.extend({
            speed: 8,
            fps: 25,
            stopOnOut: true
        }, arguments[1] || {});
        this.options = options;
        this.blockScroll = true;
        this.scrolling = false;
        this.setup(this.node);


    },

    mouseInEvent: function (e) {
        alert("Mouse IN");
        this.blockScroll = false;
    },

    mouseOutEvent: function (e) {
        alert("Mouse Out");
        this.blockScroll = true;
    },

    setup: function(t) {
        //t.style.position = 'relative';

        t.style.overflow = 'hidden';

        if (this.options.width) t.style.width = this.options.width + "px";
        if (this.options.height) t.style.height = this.options.height + "px";
        this.eventMouseMove = this.scrollCheck.bindAsEventListener(this);
        //		this.eventMouseIn = this.mouseInEvent.bindAsEventListener(this);
        //		this.eventMouseOut = this.mouseOutEvent.bindAsEventListerner(this);
        Event.observe(t, 'mousemove', this.eventMouseMove);
        //		Event.observe(t, 'mouseover', this.eventMouseIn);
        //		Event.observe(t, 'mouseout', this.eventMouseOut);

    },




    findPosX: function (obj)
    {
        var curleft = 0;
        if (obj.offsetParent)
        {
            while (obj.offsetParent)
            {
                curleft += obj.offsetLeft
                obj = obj.offsetParent;

            }
        }
        else if (obj.x)
        curleft += obj.x;
        return curleft;
    },

    findPosY: function (obj)
    {
        var curtop = 0;
        if (obj.offsetParent)
        {
            while (obj.offsetParent)
            {
                curtop += obj.offsetTop
                obj = obj.offsetParent;
            }
        }
        else if (obj.y)
        curtop += obj.y;
        return curtop;
    },

    scrollCheck: function(e) {

        this.blockScroll = false;
        var posx = 0;
        var posy = 0;
        if (!e) var e = window.event;
        if (e.pageX || e.pageY)
        {
            posx = e.pageX;
            posy = e.pageY;
        }
        else if (e.clientX || e.clientY)
        {
            posx = e.clientX + document.body.scrollLeft;
            posy = e.clientY + document.body.scrollTop;
        }

        posy = posy - this.findPosY(this.node);
        posx = posx - this.findPosX(this.node);
        var nextScrollY = posy * (this.node.scrollHeight - parseInt(this.node.style.height)) / parseInt(this.node.style.height);
        this.scrollY = Math.round((nextScrollY));
        var nextScrollX = posx * (this.node.scrollWidth - parseInt(this.node.style.width)) / parseInt(this.node.style.width);
        this.scrollX = Math.round((nextScrollX));

        if (this.scrolling == false) {
            this.scroll();

        }


    },

    scroll: function() {

        /*
        if (this.blockScroll == true) {
        this.scrolling = false;
        clearInterval(this.scrollTimer);
        this.scrollTimer = null;
        return;
        }

        */
        this.scrolling = true;



        var stepX = Math.round((this.node.scrollLeft-this.scrollX)/(this.options.speed));
        var stepY = Math.round((this.node.scrollTop-this.scrollY)/(this.options.speed));
        if ( stepX != 0 || stepY != 0) {
            if (this.doing != true) {
                this.doing = true;
                this.node.scrollTop -= stepY;
                this.node.scrollLeft -= stepX;
                this.doing = false;
            }
            if (!this.scrollTimer) {
                this.scrollTimer = setInterval(this.scroll.bind(this),1000/this.options.fps);
            }
        } else {
            this.scrolling = false;
            clearInterval(this.scrollTimer);
            this.scrollTimer = null;

        }

    }



}














Control.betterSlidingDiv = Class.create();
Control.betterSlidingDiv.prototype = {

    initialize: function(target, options) {
        var slidingdiv = this;
        this.node = $(target);
        var options = Object.extend({
            fps: 25,
            points: 10,
            stopOnExit: true,
            onSlideStart: null,
            onSlideUpdate: null,
            onSlideComplete: null,
            onSlideStop: null,
            style: null
        }, arguments[1] || {});
        this.options = options;
        this.blockScroll = false;
        this.scrolling = false;
        this.setup(this.node);

    },

    mouseIn: function (e) {
        this.blockScroll = false;
    },

    mouseOut: function (e) {
        this.blockScroll = true;
    },

    setup: function(t) {
        t.style.overflow = 'hidden';
        if (typeof this.options.style == 'object') {
            for (property in this.options.style) {
                t.style[property] = this.options.style[property];
            }
        }

        this.eventMouseMove = this.mouseMove.bindAsEventListener(this);
        this.eventMouseIn = this.mouseIn.bindAsEventListener(this);
        this.eventMouseOut = this.mouseOut.bindAsEventListener(this);
        Event.observe(t, 'mousemove', this.eventMouseMove, true);
        if (this.options.stopOnExit == true) {
            Event.observe(t, 'mouseover', this.eventMouseIn, true);
            Event.observe(t, 'mouseout', this.eventMouseOut);
        }

    },

    scrollTo: function(x,y) {

        if (x > (this.node.scrollWidth - parseInt(this.node.offsetWidth))) x = this.node.scrollWidth - parseInt(this.node.offsetWidth);
        if (y > (this.node.scrollHeight - parseInt(this.node.offsetHeight))) y = this.node.scrollHeight - parseInt(this.node.offsetHeight);

        //alert(this.node.scrollHeight - parseInt(this.node.style.height) + " " + y);

        if (this.clickScroll == true || this.scrolling == true) {
            
            clearInterval ( this.autoScroller );
            this.autoScroller = null;
            return;
            
        }
        
        var deltaX = 0;
        var deltaY = 0;
        
        deltaY = (this.node.scrollTop - y) / 4;
        deltaX = (this.node.scrollLeft - x) / 4;
        //alert(this.node.scrollTop + " " + y);
        if (deltaX > 0) {
            deltaX = Math.min(Math.round(deltaY), this.options.points);
        } else {
            deltaX = Math.max(Math.round(deltaY), -1 * this.options.points);
        }
        
        if (deltaY > 0) {
            deltaY = Math.min(Math.round(deltaX), this.options.points);
            
        } else {
            deltaY = Math.max(Math.round(deltaX), -1 * this.options.points);
        }
        this.node.scrollTop -= deltaY;
        this.node.scrollLeft -= deltaX;
        //alert(Math.min(Math.floor(deltaY), this.options.points));
        if (deltaY != 0 || deltaX != 0) {
            //alert("Recall");
                clearInterval ( this.autoScroller );
                this.autoScroller = setInterval(this.scrollTo.bind(this, x , y),1000/this.options.fps);
        } else {
            clearInterval ( this.autoScroller );
            this.autoScroller = null;
        }
        
    },

    mouseMove: function(e) {
        this.blockScroll = false; // Only useful if stopOnExit is true
        var posx = Event.getX(e);
        var posy = Event.getY(e);
        posy -= Element.getY(this.node);
        posx -= Element.getX(this.node);


        var pointx = parseInt(Element.getStyle(this.node,'width'))/2 - posx;
        var pointy = parseInt(Element.getStyle(this.node,'height'))/2 - posy;
        
        if (pointx < 0) { pointx = pointx * -1 }
        if (pointy < 0) { pointy = pointy * -1 }
        
        this.scrollX = (pointx * this.options.points) / (parseInt(Element.getStyle(this.node,'width'))/2);
        this.scrollY = (pointy * this.options.points) / (parseInt(Element.getStyle(this.node,'height'))/2);

        if (posx < parseInt(Element.getStyle(this.node,'width'))/2) { this.scrollX = this.scrollX * -1 }
        if (posy < parseInt(Element.getStyle(this.node,'height'))/2) { this.scrollY = this.scrollY * -1 }

        if (this.scrolling == false) {
            
            if (this.options.onSlideStart != null) {
                    this.options.onSlideStart.bind(this)();
            }
            this.scroll();
        }
    },

    scroll: function() {
        if (this.blockScroll == true && this.options.stopOnExit == true) {
            this.scrolling = false;
            clearInterval(this.scrollTimer);
            this.scrollTimer = null;
            this.stopping = this.options.points;
            if (this.options.onSlideStop != null) {
                    this.options.onSlideStop.bind(this)();
            }
            this.gentleStop();
            return;
        }

        this.scrolling = true;
        var stepX = Math.round(this.scrollX);
        var stepY = Math.round(this.scrollY);
        if ( stepX != 0 || stepY != 0) {
            if (this.doing != true) {

                this.doing = true;
                this.node.scrollTop += stepY;
                this.node.scrollLeft += stepX;
                if (this.options.onSlideUpdate != null) {
                        this.options.onSlideUpdate.bind(this)();
                }
                this.doing = false;
            }

            var elH = parseInt(Element.getStyle(this.node,'height'));
            var elW = parseInt(Element.getStyle(this.node,'width'));            
            var againH = true;
            var againW = true;
            if (stepY >= 0) {
                //going down
                if ((elH + this.node.scrollTop) >= this.node.scrollHeight) againH = false;
                
            } else {
                if (this.node.scrollTop <= 0) againH = false;
            }
            
            if (stepX >= 0) {
                if ((elW + this.node.scrollLeft) >= this.node.scrollWidth) againW = false;
            } else {
                if (this.node.scrollLeft <= 0) againW = false;
            }

            if (!againH && !againW) {
                this.scrolling = false;
                clearInterval(this.scrollTimer);
                this.scrollTimer = null;
                if (this.options.onSlideComplete != null) {
                        this.options.onSlideComplete.bind(this)();
                }
            } else {
                if (!this.scrollTimer) {
                    this.scrollTimer = setInterval(this.scroll.bind(this),1000/this.options.fps);
                }
            }
        } else {
            this.scrolling = false;
            clearInterval(this.scrollTimer);
            this.scrollTimer = null;
                if (this.options.onSlideComplete != null) {
                        this.options.onSlideComplete.bind(this)();
                }
        }
    },

    gentleStop: function() {

        if (this.scrolling == true) {
            //clear the interval!
            if (this.gentleScrollTimer) {
                clearInterval(this.gentleScrollTimer);
                this.gentleScrollTimer = null;
            }
            return;
        }

        if (this.stopping > 1) {
            this.stopping--;
        }

        var stepX = 0;
        var stepY = 0;

        if (this.scrollX > 0) {
            stepX = Math.floor(this.scrollX-1);
        }
        if (this.scrollX < 0) {
            stepX = Math.floor(this.scrollX+1);
        }
        if (this.scrollY > 0) {
            stepY = Math.floor(this.scrollY-1);
        }
        if (this.scrollY < 0) {
            stepY = Math.floor(this.scrollY+1);
        }

        this.node.scrollTop += stepY;
        this.node.scrollLeft += stepX;
        if (stepY != 0) this.scrollY = stepY;
        if (stepX != 0) this.scrollX = stepX;

        if (this.stopping > 1) {
            if (!this.gentleScrollTimer) this.gentleScrollTimer = setInterval(this.gentleStop.bind(this),1000/this.options.fps);
        } else {
            clearInterval(this.gentleScrollTimer);
            this.gentleScrollTimer = null;
        }
    }
}
