﻿



function ScrollButton(target, directioned, scrollPaneObj) {
    var home = this;

    var element = target;
    var direction = directioned;
    var onState = "";
    var overState = "";
    var offState = "";
    var setBackground = function(url) {
        element.style.backgroundImage = "url(" + url + ")";
    };
    var activated = true;
    var over = false;
    
    this.setDirection = function(newDirection) {
        direction = newDirection
    };
    
    this.setOnState = function(url) {
        onState = url;
        if (activated && !over) {
            setBackground(onState);
        }
    };
    this.setOverState = function(url) {
        overState = url;
        if (activated && over) {
            setBackground(overState);
        }
    };
    this.setOffState = function(url) {
        offState = url;
        if (!activated && !over) {
            setBackground(offState);
        }
    };
    this.active = function(bool) {
        if (bool) {
            activated = true;
            if (!over) setBackground(onState);
        } else {
            activated = false;
            over = false;
            setBackground(offState);
        }
    };
    
    element.onmouseover = function() {
        if (activated) {
            over = true;
            setBackground(overState);
        }
    };
    
    element.onmouseout = function() {
        if (activated) {
            over = false;
            setBackground(onState);
        }
    };
    
    element.onclick = function() {
        element.blur();
        scrollPaneObj.shift(element, direction);
        return false;
    };
};

function ScrollBar(slider, track, axis, scrollPaneObj) {
    var axis = "";
    
    this.setAxis = function(newAxis) {
        if (newAxis == "x" || newAxis == "y") {
            axis = newAxis;
        }
    };
    
    
};

function ScrollWheel(axisXOrY, scrollPaneObj) {
    /* TODO: Conditions for chrome */
    var obj = scrollPaneObj.getPane();
    
    var mouseWheel = new MouseWheel(function(delta) {
        scrollPaneObj.updateSpeed(100);
        if (delta < 0) {
            //down
            scrollPaneObj.shift(null, (axisXOrY == "x") ? "right" : "down"); //CHANGE SHIFT SO WE HAVE TWO OPTIONS (tween and linear/direct!!)
        } else {
            //up
            scrollPaneObj.shift(null, (axisXOrY == "x") ? "left" : "up");
        }
        scrollPaneObj.revertSpeed();
    });
    
    obj.onmouseover = function() {
        //activate onmouse over
        mouseWheel.activate();
    };
    
    obj.onmouseout = function() {
        mouseWheel.deactivate();
    };
};

function ScrollPane(targetEl, intervalCanBeNull, speed) {    
    var obj = targetEl;
    
    var sizeX = -1*obj.clientWidth;
    var sizeY = -1*obj.clientHeight;
    
    var interval = (intervalCanBeNull) ? intervalCanBeNull : 100;
    var active = false;
    //var stack = new Array(); //stacked arrow scrolling
    
    var oldSpeed = speed;
    
    var home = this;   
    
    this.updateSize = function() {
        sizeX = -1*obj.clientWidth;
        sizeY = -1*obj.clientHeight;
    };
    
    this.updateInterval = function(newInterval) {
        interval = (newInterval) ? newInterval : 200;
    };
    
    this.updateSpeed = function(newSpeed) {
        oldSpeed = speed;
        speed = newSpeed;
    };
    
    this.revertSpeed = function() {
        speed = oldSpeed;
    };
    
    this.getPane = function() {
        return obj;
    };
    
    var getNextPos = function(currPos, forward, axisSize, axisContainer) {
        currPos++;
        currPos--;
        if (forward) {
            //Down or right
            var difference = (currPos - interval) % interval;
            return (currPos > (axisSize + axisContainer)) ? Math.max(currPos - interval - ((difference) ? (difference + interval) : 0), axisSize + axisContainer) : currPos;
        } else {
            //Up or left
            var difference = (currPos + interval) % interval;
            return (currPos < 0) ? Math.min(currPos + interval - difference, 0) : currPos;
        }
    };
    
    var oppDir = function(direction) {
        switch(direction) {
            case "up":
                direction = "down";
                break;
            case "down":
                direction = "up";
                break;
            case "left":
                direction = "right";
                break;
            case "right":
                direction = "left";
                break;
        }
        return direction;
    };
    
    var tweener = null;
    
    this.shift = function(caller, direction) {
        //if (!active) {
            var currPos = 0;
            var finalist = 0;
            switch(direction) {
                case "up":
                    currPos = (obj.style.top.split("px")[0]) ? obj.style.top.split("px")[0] : 0;
                    finalist = getNextPos(currPos, false, -interval, null);
                    break;
                case "down":
                    currPos = (obj.style.top.split("px")[0]) ? obj.style.top.split("px")[0] : 0;
                    finalist = getNextPos(currPos, true, sizeY, obj.parentNode.clientHeight);
                    break;
                case "left":
                    currPos = (obj.style.left.split("px")[0]) ? obj.style.left.split("px")[0] : 0;
                    finalist = getNextPos(currPos, false, -interval, null);
                    break;
                case "right":
                    currPos = (obj.style.left.split("px")[0]) ? obj.style.left.split("px")[0] : 0;
                    finalist = getNextPos(currPos, true, sizeX, obj.parentNode.clientWidth);
                    break;
                default:
                    return;
                    break;
            }
            if (!active) {
                active = true;
                tweener = new AnimationManager.tween("translation", obj, finalist, speed, direction, function () {
                    active = false;
                    /*if (stack.length > 0) {
                        var arrayer = stack.shift();
                        home.shift(arrayer[0], arrayer[1]);
                    }*/
                });
            } else {
                tweener.setFinalPos(finalist);
            }
        //} else {
            /*if (stack.length == 0 || stack[stack.length - 1][1] == direction) {
                //we're here only if the requested direction is the same as the last
                stack.push(new Array(caller, direction));
            } else {
                //We've pressed the other direction... so let's just cancel all requests
                stack = new Array();
                home.shift(caller, oppDir(direction));
            }*/
        //}
    };
};

function MouseWheel(toPerform) {

    var wheeler = function(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)
                toPerform(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;
    };    

    this.activate = function() {
        if (window.addEventListener) {
            /** DOMMouseScroll is for mozilla. */
            window.addEventListener('DOMMouseScroll', wheeler, false);
        } else {
            /** IE/Opera. */
            window.onmousewheel = document.onmousewheel = wheeler;
        }
    };
    
    this.deactivate = function() {
        if (window.addEventListener) {
            /** DOMMouseScroll is for mozilla. */
            window.removeEventListener('DOMMouseScroll', wheeler, false);
        } else {
            /** IE/Opera. */
            window.onmousewheel = document.onmousewheel = null;
            //delete window.onmousewheel;
            //delete document.onmousewheel;
        }
    };
};