function getXScroll() {
    if (ns4) return window.pageXOffset;
    else if (ie4) return document.body.scrollLeft;
    else if (dom) return document.getElementByName("body").scrollLeft;
    else return -1;
}

function getYScroll() {
    if (ns4) return window.pageYOffset;
    else if ((ie4) || (dom)) return document.body.scrollTop;
    else return -1;
}

function getWindowWidth() {
    if (ns4) return window.innerWidth;
    else if (ie4) return document.body.clientWidth;
    else if (dom) return window.innerWidth;
}

function getWindowHeight() {
    if (ns4) return window.innerHeight;
    else if (ie4) return document.body.clientHeight;
    else if (dom) return window.innerHeight;
}

function MouseEventsObj() {
    this.add = add;
    this.remove = remove;
    this.removeAll = removeAll;
    this.getObjPosition = getObjPosition;
    this.assignFunction = assignFunction;
    this.clearAssignment = clearAssignment;
    this.objs = new Array();
    this.x = 0;
    this.y = 0;
    this.assignMoveFunction = assignMoveFunction;
    this.moveFunction = null;
    this.over = null;
    document.onmousemove = layerMouseMove;
    document.onmousedown = layerMouseDown;
    document.onmouseup = layerMouseUp;
    if (ns4) document.captureEvents(Event.MOUSEMOVE | Event.MOUSEDOWN | Event.MOUSEUP);
}

function removeArrayElementAt(nr, array) {
    var a1 = array.slice(0, nr);
    var a2 = array.slice(nr + 1, array.length);
    return a1.concat(a2);
}

function add() {
    var counter = this.objs.length;
    for (var i = 0; i < arguments.length; i++) {
        if (this.getObjPosition(arguments[i]) == -1) {
            arguments[i].setPos("z", counter);
            this.objs[counter] = arguments[i];
            this.objs[counter].events = new Array();
            this.objs[counter].events["mousedown"] = this.objs[counter].events["mouseup"] = this.objs[counter].events["mouseover"] = this.objs[counter].events["mouseout"] = null;
            counter++;
        }
    }
}

function remove() {
    for (var i = 0; i < arguments.length; i++) {
        arguments[i].events = null;
        for (var j = 0; j < this.objs.length; j++) if (arguments[i] == this.objs[j]) this.objs = removeArrayElementAt(j, this.objs);
    }
    for (var i = 0; i < this.objs.length; i++) this.objs[i].setPos("z", i);
}

function removeAll() {
    for (var i = 0; i < this.objs.length; i++) this.objs[i].events = null;
    this.objs = new Array();
}

function getObjPosition(what) {
    for (var j = 0; j < this.objs.length; j++) if (what == this.objs[j]) return j;
    return -1;
}

function assignFunction(whichobj, whichevent, functionName) {
    whichevent = whichevent.toLowerCase();
    if      ((whichevent == "mousedown") || (whichevent == "onmousedown") || (whichevent == "down")) whichevent = "mousedown";
    else if ((whichevent == "mouseup")   || (whichevent == "onmouseup")   || (whichevent == "up"))   whichevent = "mouseup";
    else if ((whichevent == "mouseover") || (whichevent == "onmouseover") || (whichevent == "over")) whichevent = "mouseover";
    else if ((whichevent == "mouseout")  || (whichevent == "onmouseout")  || (whichevent == "out"))  whichevent = "mouseout";
    var nr = this.getObjPosition(whichobj);
    if (nr != -1) this.objs[nr].events[whichevent] = functionName;
}

function assignMoveFunction(functionName) {
    this.moveFunction = functionName;
}

function clearAssignment(whichobj, whichevent) {
    if (whichevent == null) {
        this.assignFunction(whichobj, "down", null);
        this.assignFunction(whichobj, "up", null);
        this.assignFunction(whichobj, "over", null);
        this.assignFunction(whichobj, "out", null);
    }
    else this.assignFunction(whichobj, whichevent, null);
}

function layerMouseMove(e) {
    mouseEvents.x = (! ie4) ? e.pageX : event.x;
    mouseEvents.y = (! ie4) ? e.pageY : event.y;
    if (mouseEvents.moveFunction != null) eval(mouseEvents.moveFunction + "(" + mouseEvents.x + ", " + mouseEvents.y + ")");
    if (mouseEvents.over == null) {
        for (var i = mouseEvents.objs.length - 1; i >= 0; i--) if (mouseEvents.objs[i].isInside(mouseEvents.x, mouseEvents.y)) {
            mouseEvents.over = i;
            if (mouseEvents.objs[i].events["mouseover"] != null) {
                eval(mouseEvents.objs[i].events["mouseover"] + "(mouseEvents.objs[" + i + "])");
                return false;
            }
            return true;
        }
    }
    else if (!mouseEvents.objs[mouseEvents.over].isInside(mouseEvents.x, mouseEvents.y)) {
        if (mouseEvents.objs[mouseEvents.over].events["mouseout"] != null) {
            eval(mouseEvents.objs[mouseEvents.over].events["mouseout"] + "(mouseEvents.objs[" + mouseEvents.over + "])");
            mouseEvents.over = null;
            return false;
        }
        mouseEvents.over = null;
        return true;
    }
    return true;
}

function layerMouseDown(e) {
    if ((ns4 && e.which != 1) || (ie4 && event.button != 1)) return true;
    mouseEvents.x = (! ie4) ? e.pageX : event.x;
    mouseEvents.y = (! ie4) ? e.pageY : event.y;
    for (var i = mouseEvents.objs.length - 1; i >= 0; i--) {
        if ((mouseEvents.objs[i].isInside(mouseEvents.x, mouseEvents.y)) && (mouseEvents.objs[i].events["mousedown"] != null)) {
            eval(mouseEvents.objs[i].events["mousedown"] + "(mouseEvents.objs[" + i + "])");
            return false;
        }
    }
    if (ns4) document.routeEvent(e);
    return true;
}

function layerMouseUp(e) {
    if ((ns4 && e.which != 1) || (ie4 && event.button != 1)) return true;
    mouseEvents.x = (! ie4) ? e.pageX : event.x;
    mouseEvents.y = (! ie4) ? e.pageY : event.y;
    for (var i = mouseEvents.objs.length - 1; i >= 0; i--) {
        if ((mouseEvents.objs[i].isInside(mouseEvents.x, mouseEvents.y)) && (mouseEvents.objs[i].events["mouseup"] != null)) {
            eval(mouseEvents.objs[i].events["mouseup"] + "(mouseEvents.objs[" + i + "])");
            return false;
        }
    }
    if (ns4) document.routeEvent(e);
    return true;
}

function LayerObj(layerName, nsParentAdress) {
    this.layerName = layerName;
    if (ns4) {
        nsParentAdress = checkNull(nsParentAdress, nsParentAdress + ".");
        this.style = this.object = eval(nsParentAdress + 'document.layers["' + layerName + '"]');
    }
    else if (ie4) {
        this.style = document.all[layerName].style;
        this.object = document.all[layerName];
    }
    else if (dom) {
        this.style = document.getElementById(layerName).style;
        this.object = document.getElementById(layerName);
    }
    this.setPosXY = setPosXY;
    this.setPos = setPos;
    this.getPos = getPos;
    this.moveTo = moveTo;
    this.moveBy = moveBy;
    this.isInside = isInside;
    this.pos = new Array();
    this.pos["x"] = parseInt(this.style.left);
    this.pos["y"] = parseInt(this.style.top);
    this.pos["z"] = parseInt(this.style.zIndex);
    this.moving = false;

    this.setClip = setClip;
    this.getClip = getClip;
    this.getSize = getSize;
    this.clip = new Array();
    if ((ie4) || (dom)) {
        if (this.style.clip != "") {
            var clips = this.style.clip.split("rect(")[1].split(")")[0].split("px");
            this.clip["top"] = parseInt(clips[0]);
            this.clip["right"] = parseInt(clips[1]);
            this.clip["bottom"] = parseInt(clips[2]);
            this.clip["left"] = parseInt(clips[3]);
        }
        else this.clip["left"] = this.clip["top"] = this.clip["right"] = this.clip["bottom"] = 0;
    }
    else if(ns4) {
        this.clip["left"] = this.style.clip.left;
        this.clip["top"] = this.style.clip.top;
        this.clip["right"] = this.style.clip.right;
        this.clip["bottom"] = this.style.clip.bottom;
    }
    this.clipping = false;
    this.hide = hide;
    this.show = show;

    this.referenceName = this.layerName + "REFERENCE";
    eval(this.referenceName + " = this");
}

function checkNull(variable, ifNotNull, ifNull) {
    if (variable == null) {
        if (ifNull == null) return "";
        else return ifNull;
    }
    else return ifNotNull;
}

function closeLayer() {
    if (ns4) return "</layer>";
    else if ((ie4) || (dom)) return "</div>";
}

defaultMoveSpeed = 10;

function setPosXY(x, y) {
    this.pos["x"] = x;
    this.pos["y"] = y;
    this.style.left = this.pos["x"];
    this.style.top = this.pos["y"];
}

function setPos(which, value) {
    this.pos[which] = value;
    if (which == "x") this.style.left = parseInt(this.pos[which]);
    else if (which == "y") this.style.left = parseInt(this.pos[which]);
    else if (which == "z") this.style.zIndex = parseInt(this.pos[which]);
}

function getPos(which) {
    return eval(this.pos[which]);
}

function moveBy(x, y) {
    this.setPosXY(this.getPos("x") + x, this.getPos("y") + y);
}

function moveTo(posx, posy, steps, speed) {
    speed = checkNull(speed, speed, defaultMoveSpeed);
    if (steps == null) {
    	this.setPosXY(posx, posy);
    }
    else {
        if (steps == 0) {
            this.moving = false;
        }
        else {
            this.moving = true;
            var xStep = (posx - this.getPos("x")) / steps;
            var yStep = (posy - this.getPos("y")) / steps;
            this.moveBy(xStep, yStep);
            steps -= 1;
            setTimeout(this.referenceName + ".moveTo(" + posx + ", " + posy + ", " + steps + ", " + speed + ")", speed);
        }
    }
}

function isInside(x, y) {
    var xko = this.getPos("x");
    var yko = this.getPos("y");
    var width = (this.getClip("right") == 0) ? this.getSize("width") : this.getClip("right");
    var height = (this.getClip("bottom") == 0) ? this.getSize("height") : this.getClip("bottom");
    return ((x >= xko) && (x <= xko + width) && (y >= yko) && (y <= yko + height));
}

defaultClipSpeed = 10;

function setClip(which, value) {
    this.clip[which] = value;
    this.setAllClips();
}

function getClip(which) {
    return eval(this.clip[which]);
}

function getSize(which) {
    if (which == "width") return (ns4) ? this.object.document.width : (dom) ? this.object.offsetWidth : this.object.scrollWidth;
    if (which == "height") return (ns4) ? this.object.document.height : (dom) ? this.object.offsetHeight : this.object.scrollHeight;
}

function hide() {
    if (ns4) {
        this.style.visibility = "hide";
    }
    else if ((ie4) || (dom)) {
        this.style.visibility = "hidden";
    }
}

function show() {
    if (ns4) {
        this.style.visibility = "show";
    }
    else if ((ie4) || (dom)) {
        this.style.visibility = "visible";
    }
}

