// var tileLocation = "" // local
var tileLocation = "http://www.inselferien.de" // remote

//=============================================================================

function Position(pX, pY) {
    var x = pX;
    var y = pY;

    this.initialize = function(pX, pY) {
        this.set(pX, pY);
    }

    //---------------------------------------------------------------------------

    this.set = function(pX, pY) {
        x = pX;
        y = pY;
    }

    this.getX = function() {
        return x;
    }

    this.getY = function() {
        return y;
    }

    this.move = function(pX, pY) {
        x += pX;
        y += pY;
    }

    this.toString = function() {
        return "x: " + x + " - y: " + y;
    }

    //---------------------------------------------------------------------------

    this.initialize(pX, pY);
}

//=============================================================================

function Rectangle(pXol, pYol, pXur, pYur) {
    var xol;
    var yol;
    var xur;
    var yur;

    this.initialize = function(pXol, pYol, pXur, pYur) {
        this.set(pXol, pYol, pXur, pYur);
    }

    //---------------------------------------------------------------------------

    this.set = function(pXol, pYol, pXur, pYur) {
        xol = pXol;
        yol = pYol;
        xur = pXur;
        yur = pYur;
    }

    this.getXol = function() {
        return xol;
    }

    this.getYol = function() {
        return yol;
    }

    this.getXur = function() {
        return xur;
    }

    this.getYur = function() {
        return yur;
    }

    this.containsPoint = function(pX, pY) {
        return xol <= pX && pX <= xur && yol <= pY && pY <= yur;
    }

    this.touchesRectangle = function(pRect) {
        return this.containsPoint(pRect.getXol(), pRect.getYol()) ||
                this.containsPoint(pRect.getXol(), pRect.getYur()) ||
                this.containsPoint(pRect.getXur(), pRect.getYol()) ||
                this.containsPoint(pRect.getXur(), pRect.getYur());

    }

    this.toString = function() {
        return "xol: " + xol + " - yol: " + yol + " : xur: " + xur + " - yur: " + yur;
    }

    //---------------------------------------------------------------------------

    this.initialize(pXol, pYol, pXur, pYur);
}

//=============================================================================

function ToolTipManager(pDiv) {
    var div;

    var peToolTipShow;
    var peToolTipHide;

    var toolTipFkt;
    var toolTipHiddenFkt;
    var toolTipMouseMoveFkt;
    var toolTipMapMoveFkt;
    var toolTipMapMoveDoneFkt;
    var toolTipDiv;
    var toolTipStyle;
    var toolTipShowDelay;
    var toolTipHideDelay;
    var isToolTipShown;
    var mouseInside;
    var moved;
    var isMoving;

    var self = this;

    this.initialize = function(pDiv) {
        div = pDiv;

        peToolTipShow = null;
        peToolTipHide = null;

        toolTipFkt = null;
        toolTipHiddenFkt = null;
        toolTipMouseMoveFkt = null;
        toolTipMapMoveFkt = null;
        toolTipMapMoveDoneFkt = null;
        toolTipDiv = null;
        toolTipStyle = null;
        toolTipShowDelay = 1.0;
        toolTipHideDelay = 3.0;
        isToolTipShown = false;
        mouseInside = false;
        isMoving = false;
    }

    //---------------------------------------------------------------------------

    this.toolTipShow = function(pText, pMouseXYnow) {
        var peMove = null;
        var x;
        var y;
        var xMove;
        var yMove;

        isToolTipShown = true;

        // console.info("toolTipShow - " + div + " " + toolTipStyle + " " + pText + " " + pMouseXYnow);
        if (toolTipDiv == null) {
            self.toolTipShowStop();
            self.toolTipHideStop();

            toolTipDiv = new Element("div", {"id": toolTipStyle});
            $(toolTipDiv).update(pText);
            $(toolTipDiv).observe('mouseover', function() {
                self.toolTipHideStop();
                mouseInside = true;
            });
            $(toolTipDiv).observe('mousemove', function(event) {
                self.toolTipHideStop();
                mouseInside = true;

                if (toolTipMouseMoveFkt != null) {
                    toolTipMouseMoveFkt(div, event);
                }
            });
            $(toolTipDiv).observe('mouseout', function() {
                self.toolTipStopAndHideDelayed();
                mouseInside = false;
            });

            $(div).appendChild($(toolTipDiv));

            x = pMouseXYnow.getX() - $(div).cumulativeOffset().left;
            y = pMouseXYnow.getY() - $(div).cumulativeOffset().top;

            if (toolTipMapMoveFkt != null) {
                x -= Math.round($(toolTipDiv).getWidth() / 2);
                y -= $(toolTipDiv).getHeight() + 2;
                if (x < 0) {
                    x = 0;
                }
                if (x > $(div).getWidth() - $(toolTipDiv).getWidth()) {
                    x = $(div).getWidth() - $(toolTipDiv).getWidth();
                }

                //console.info($(div).getWidth());
                //console.info($(toolTipDiv).getWidth());
                //console.info("------------");

                if (y < 0) {
                    isMoving = true;
                    yMove = -y;
                    moved = yMove;
                    y = 0;
                    if (peMove == null) {
                        peMove = new PeriodicalExecuter(function() {
                            toolTipMapMoveFkt(0, Math.min(yMove, 5));
                            yMove -= 5;
                            if (yMove <= 0) {
                                peMove.stop();
                                peMove = null;

                                if (toolTipMapMoveDoneFkt != null) {
                                    //alert('toolTipMapMoveDoneFkt - 03.12.2009');
                                    toolTipMapMoveDoneFkt(moved);
                                }
                                isMoving = false;
                            }
                        }, 0.02);
                    }
                }
            } else {
                x += 7;
                y += 17;
                if (x < 0) {
                    x = 0;
                }
                if (y < 0) {
                    y = 0;
                }
                if (x > $(div).getWidth() - $(toolTipDiv).getWidth()) {
                    x = $(div).getWidth() - $(toolTipDiv).getWidth();
                }
                if (y > $(div).getHeight() - $(toolTipDiv).getHeight()) {
                    y = $(div).getHeight() - $(toolTipDiv).getHeight();
                }
            }
        }

        $(toolTipDiv).setStyle("visibility: visible; z-index: 2; left: " + x + "px; top: " + y + "px;");

        self.toolTipHideDelayed();
    }

    this.toolTipHide = function() {
        if (!isMoving) {
            //console.info("toolTipHide - ");
            if (toolTipDiv != null) {
                $(toolTipDiv).remove();
                toolTipDiv = null;
            }

            if (toolTipHiddenFkt != null) {
                toolTipHiddenFkt();
            }

            isToolTipShown = false;
        }
    }

    this.toolTipShowStop = function() {
        if (!isMoving) {
            if (peToolTipShow instanceof PeriodicalExecuter) {
                //console.info("peToolTipShow.stop() - ");
                peToolTipShow.stop();
            }
            peToolTipShow = null;
        }
    }

    this.toolTipHideStop = function() {
        if (!isMoving) {
            if (peToolTipHide instanceof PeriodicalExecuter) {
                //console.info("peToolTipHide.stop() - ");
                peToolTipHide.stop();
            }
            peToolTipHide = null;
        }
    }

    this.toolTipShowDelayed = function(pMouseXYnow) {
        if (!isMoving) {
            //console.info("toolTipShowDelayed - " + pMouseXYnow + " - " + peToolTipShow + " - " + toolTipFkt);
            if (peToolTipShow == null && toolTipFkt != null) {
                peToolTipShow = new PeriodicalExecuter(function() {
                    //console.info("toolTipShowDelayed - " + toolTipFkt);
                    toolTipFkt(div, pMouseXYnow);
                    self.toolTipShowStop();
                }, toolTipShowDelay);
            }
        }
    }

    this.toolTipHideDelayed = function() {
        if (!isMoving) {
            //console.info("toolTipHideDelayed - ");
            if (peToolTipHide == null && toolTipFkt != null) {
                peToolTipHide = new PeriodicalExecuter(function() {
                    self.toolTipHideStop();
                    self.toolTipHide();
                }, toolTipHideDelay);
            }
        }
    }

    this.toolTipStopAndHide = function() {
        if (!isMoving) {
            self.toolTipShowStop();
            self.toolTipHideStop();
            self.toolTipHide();
        }
    }

    this.toolTipStopAndHideDelayed = function() {
        if (!isMoving) {
            self.toolTipShowStop();
            self.toolTipHideDelayed();
        }
    }

    this.isMouseInside = function() {
        return mouseInside;
    }

    //---------------------------------------------------------------------------

    this.setToolTipFkt = function(pToolTipFkt) {
        toolTipFkt = pToolTipFkt;
    }

    this.setToolTipHiddenFkt = function(pToolTipHiddenFkt) {
        toolTipHiddenFkt = pToolTipHiddenFkt;
    }

    this.setToolTipMouseMoveFkt = function(pToolTipMouseMoveFkt) {
        toolTipMouseMoveFkt = pToolTipMouseMoveFkt;
    }

    this.setToolTipMapMoveFkt = function(pToolTipMapMoveFkt) {
        toolTipMapMoveFkt = pToolTipMapMoveFkt;
    }

    this.setToolTipMapMoveDoneFkt = function(pToolTipMapMoveDoneFkt) {
        toolTipMapMoveDoneFkt = pToolTipMapMoveDoneFkt;
    }

    this.setToolTipStyle = function(pToolTipStyle) {
        toolTipStyle = pToolTipStyle;
    }

    this.setToolTipShowDelay = function(pToolTipShowDelay) {
        toolTipShowDelay = pToolTipShowDelay;
        if (peToolTipShow != null && toolTipFkt != null) {
            self.toolTipShowStop();
            self.toolTipShowDelayed();
        }
    }

    this.setToolTipHideDelay = function(pToolTipHideDelay) {
        toolTipHideDelay = pToolTipHideDelay;
        if (peToolTipHide != null && toolTipFkt != null) {
            self.toolTipHideStop();
            self.toolTipHideDelayed();
        }
    }

    this.isToolTipShown = function() {
        return isToolTipShown;
    }

    this.getToolTipBounds = function() {
        var rectangle;
        if ($(toolTipDiv) != null) {
            var left = parseInt($(toolTipDiv).style.left);
            var top = parseInt($(toolTipDiv).style.top);
            rectangle = new Rectangle(left, top, left + $(toolTipDiv).getWidth(), top + $(toolTipDiv).getHeight());
        } else {
            rectangle = new Rectangle(0, 0, 0, 0);
        }
        return rectangle;
    }

    //---------------------------------------------------------------------------

    this.initialize(pDiv);
}

//=============================================================================

function MouseManager(pDiv) {
    var div;

    var downFkt;
    var moveFkt;
    var upFkt;
    var clickFkt;
    var overFkt;
    var outFkt;
    var wheelFkt;

    var peClick;
    var peClickLock;
    var clickCount;

    var mouseXY;
    var mouseDownXY;
    var mouseClickXY;
    var mouseDown;

    var handleOverOut;
    var isOver;

    var self = this;

    this.initialize = function(pDiv) {
        div = pDiv;

        document.getElementById(div).onmousedown = this.mouseDown;
        if (document.addEventListener != null) { // e.g. Firefox, Opera, Safari
            document.addEventListener("mousemove", this.mouseMove, true);
        } else { // e.g. Internet Explorer (also would work on Opera)
            document.attachEvent("onmousemove", this.mouseMove);
        }
        $(document).observe("mouseup", this.mouseUp);
        Event.observe($(div), "mousewheel", this.mouseWheel, false);
        Event.observe($(div), "DOMMouseScroll", this.mouseWheel, false); // Firefox

        downFkt = null;
        moveFkt = null;
        upFkt = null;
        clickFkt = null;
        overFkt = null;
        outFkt = null;
        wheelFkt = null;

        peClick = null;
        peClickLock = null;
        clickCount = 0;

        mouseXY = new Position(-1, -1);
        mouseDownXY = new Position(-1, -1);
        mouseClickXY = new Position(-1, -1);
        mouseDown = false;

        handleOverOut = false;
        isOver = false;
    }

    //---------------------------------------------------------------------------

    this.mousePosition = function(event) {
        var position = new Position(0, 0);

        if (event && event.pageX && event.pageY) {
            position = new Position(event.pageX, event.pageY)
        } else {
            if (window.event && window.event.clientX && window.event.clientY) {
                var isStrictMode = document.compatMode && document.compatMode != 'BackCompat' ? true : false;

                var scrollX = isStrictMode ? document.documentElement.scrollLeft : document.body.scrollLeft;
                var scrollY = isStrictMode ? document.documentElement.scrollTop : document.body.scrollTop;

                position = new Position(window.event.clientX + scrollX, window.event.clientY + scrollY)
            }
        }

        return position;
    }

    this.mouseClicked = function(event, mouseXY) {
        if (peClickLock == null) {
            clickCount++;
            if (peClick == null) {
                peClick = new PeriodicalExecuter(function() {
                    self.mouseClick(event, mouseXY, clickCount);
                    clickCount = 0;
                    peClick.stop();
                    peClick = null;

                    if (peClickLock == null) {
                        peClickLock = new PeriodicalExecuter(function() {
                            peClickLock.stop();
                            peClickLock = null;
                        }, 0.3);
                    }
                }, 0.5);
            }
        }
    }


    //---------------------------------------------------------------------------

    this.mouseDown = function(event) {
        var leftClick;

        if (Prototype.Browser.IE) {
            leftClick = Event.isLeftClick(window.event);
        } else {
            leftClick = Event.isLeftClick(event);
        }
        if (leftClick) {
            mouseXY = mouseDownXY = mouseClickXY = self.mousePosition(event);
            mouseDown = true;

            if (downFkt != null) {
                downFkt(div, event, mouseXY);
            }

            //console.info("mouseDown - " + mouseXY);
        }

        //Event.stop(event); //** 23-02-10

        return false;
    }

    this.mouseMove = function(event) {
        var mouseXYnow = self.mousePosition(event);

        if (moveFkt != null) {
            moveFkt(div, event, mouseXYnow, mouseXY, mouseDown);
        }

        mouseXY = mouseXYnow;

        var left = parseInt($(div).cumulativeOffset().left);
        var top = parseInt($(div).cumulativeOffset().top);
        var divRect = new Rectangle(left, top, left + $(div).getWidth(), top + $(div).getHeight());
        var isOverNow = divRect.containsPoint(mouseXYnow.getX(), mouseXYnow.getY());

        if (handleOverOut) {
            if (isOver && !isOverNow) {
                if (outFkt != null) {
                    outFkt(div, event, mouseXY);
                }
            }
            if (!isOver && isOverNow) {
                if (overFkt != null) {
                    overFkt(div, event, mouseXY);
                }
            }
        }

        isOver = isOverNow;
        handleOverOut = true;

        //Event.stop(event); //** 23-02-10

        //console.info("mouseMove - " + div);
        //console.info("mouseMove - " + mouseXYnow);
        //console.info("mouseMove - " + divRect);
        return false;
    }

    this.mouseUp = function(event) {
        var mouseClicked;

        mouseXY = self.mousePosition(event);

        //console.info(mouseXY.getX() + " - " + mouseClickXY.getX() + " | " + mouseXY.getY() + " - " + mouseClickXY.getY());
        mouseClicked = Math.abs(mouseXY.getX() - mouseClickXY.getX()) <= 3 && Math.abs(mouseXY.getY() == mouseClickXY.getY()) <= 3;
        if (mouseClicked) {
            self.mouseClicked(event, mouseXY);
        }

        if (upFkt != null) {
            upFkt(div, event, mouseXY);
        }

        //Event.stop(event); //** 23-02-10

        mouseXY = new Position(-1, -1);
        mouseDownXY = new Position(-1, -1);
        mouseDown = false;
        //console.info("mouseUp - ");
        return false;
    }

    this.mouseWheel = function(event) {
        var delta = 0;

        if (!event) {
            event = window.event;
        }
        if (event.wheelDelta) {
            delta = event.wheelDelta / 120;
            if (window.opera) {
                delta = -delta;
            }
        } else {
            if (event.detail) {
                delta = - event.detail / 3;
            }
        }
        delta = -Math.round(delta);

        if (wheelFkt != null) {
            wheelFkt(div, event, delta);
        }

        //Event.stop(event); //** 23-02-10

        //console.info("mouseWheel - " + div + " " + delta);
        return false;
    }

    this.mouseClick = function(event, mouseXY, clickCount) {
        if (clickFkt != null) {
            clickFkt(div, event, mouseXY, clickCount);
        }
        //if(clickCount > 0) alert(clickCount);

        //console.info("mouseClick - " + mouseXY + " - " + clickCount);
        return false;
    }

    this.isMouseOver = function() {
        return isOver;
    }

    //---------------------------------------------------------------------------

    this.setDownFkt = function(pDownFkt) {
        downFkt = pDownFkt;
    }

    this.setMoveFkt = function(pMoveFkt) {
        moveFkt = pMoveFkt;
    }

    this.setUpFkt = function(pUpFkt) {
        upFkt = pUpFkt;
    }

    this.setClickFkt = function(pClickFkt) {
        clickFkt = pClickFkt;
    }

    this.setOverFkt = function(pOverFkt) {
        overFkt = pOverFkt;
    }

    this.setOutFkt = function(pOutFkt) {
        outFkt = pOutFkt;
    }

    this.setWheelFkt = function(pWheelFkt) {
        wheelFkt = pWheelFkt;
    }

    //---------------------------------------------------------------------------

    this.initialize(pDiv);
}

//=============================================================================

function MapInfo() {
    var DEFAULT_WIDTH = 400;
    var DEFAULT_HEIGHT = 400;
    var DEFAULT_X = 118590;
    var DEFAULT_Y = 196850;

    var scaledContainerRect;
    var scaledMapRect;
    var mapRect;
    var x;
    var y;
    var width;
    var height;

    var self = this;

    this.initialize = function() {
        containerRect = new Rectangle(0, 0, 0, 0);
        mapRect = new Rectangle(0, 0, 0, 0);
        self.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        self.setXY(DEFAULT_X, DEFAULT_Y);
    }

    //---------------------------------------------------------------------------

    this.setSize = function(pWidth, pHeight) {
        width = pWidth;
        height = pHeight;
    }

    this.setXY = function(pX, pY) {
        x = pX;
        y = pY;
    }

    this.setX = function(pX) {
        x = pX;
    }

    this.setY = function(pY) {
        y = pY;
    }

    this.setRectangles = function(pTilesInfo, pDir) {
        var adjustScale;
        var xol;
        var yol;
        var xur;
        var yur;

        adjustScale = pTilesInfo.getAdjustScale();

        xol = Math.round(this.getX() / adjustScale - this.getWidth() - pTilesInfo.getWidth());
        yol = Math.round(this.getY() / adjustScale - this.getHeight() - pTilesInfo.getHeight());
        xur = Math.round(this.getX() / adjustScale + this.getWidth() + pTilesInfo.getWidth());
        yur = Math.round(this.getY() / adjustScale + this.getHeight() + pTilesInfo.getHeight());

        if (pDir == 'R') {
            xol -= 2 * pTilesInfo.getWidth();
        }
        if (pDir == 'L') {
            xur += 2 * pTilesInfo.getWidth();
        }
        if (pDir == 'D') {
            yol -= 2 * pTilesInfo.getHeight();
        }
        if (pDir == 'U') {
            yur += 2 * pTilesInfo.getHeight();
        }

        scaledContainerRect = new Rectangle(xol, yol, xur, yur);

        xol = Math.floor(xol / pTilesInfo.getWidth()) * pTilesInfo.getWidth();
        yol = Math.floor(yol / pTilesInfo.getHeight()) * pTilesInfo.getHeight();
        xur = Math.ceil(xur / pTilesInfo.getWidth()) * pTilesInfo.getWidth();
        yur = Math.ceil(yur / pTilesInfo.getHeight()) * pTilesInfo.getHeight();

        scaledMapRect = new Rectangle(xol, yol, xur, yur);

        if (xol < (pTilesInfo.getXol(this) - 1) * pTilesInfo.getWidth()) {
            xol = Math.round((pTilesInfo.getXol(this) - 1) * pTilesInfo.getWidth());
        }
        if (yol < (pTilesInfo.getYol(this) - 1) * pTilesInfo.getHeight()) {
            yol = Math.round((pTilesInfo.getYol(this) - 1) * pTilesInfo.getHeight());
        }
        if (xur > pTilesInfo.getXur(this) * pTilesInfo.getWidth()) {
            xur = Math.round(pTilesInfo.getXur(this) * pTilesInfo.getWidth());
        }
        if (yur > pTilesInfo.getYur(this) * pTilesInfo.getWidth()) {
            yur = Math.round(pTilesInfo.getYur(this) * pTilesInfo.getWidth());
        }

        xol = xol * adjustScale;
        yol = yol * adjustScale;
        xur = xur * adjustScale;
        yur = yur * adjustScale;

        mapRect = new Rectangle(xol, yol, xur, yur);
    }

    this.getX = function() {
        return x;
    }

    this.getY = function() {
        return y;
    }

    this.getScaledContainerRect = function() {
        return scaledContainerRect;
    }

    this.getScaledMapRect = function() {
        return scaledMapRect;
    }

    this.getMapRect = function() {
        return mapRect;
    }

    this.getWidth = function() {
        return width;
    }

    this.getHeight = function() {
        return height;
    }

    this.moveBy = function(pX, pY) {
        x = Math.round(x - pX);
        y = Math.round(y - pY);
    }

    this.clone = function() {
        var mi = new MapInfo();
        mi.setSize(self.getWidth(), self.getHeight());
        mi.setXY(self.getX(), self.getY());
        return mi;
    }

    this.toString = function() {
        return mapRect + " - x: " + x + " - y: " + y + " - width: " + width + " - height: " + height;
    }

    //---------------------------------------------------------------------------

    this.initialize();
}

//=============================================================================

function TilesInfo() {
    var DEFAULT_WIDTH = 200;
    var DEFAULT_HEIGHT = 200;
    var DEFAULT_SCALE = 3;
    var DEFAULT_TYPE = 0;

    var TILES_XOL = 0;
    var TILES_YOL = 1;
    var TILES_XUR = 2;
    var TILES_YUR = 3;
    var TILES_XCNT = 4;
    var TILES_YCNT = 5;
    var TILES_ALLOWED = 6;

    var TILES_ADJUST_SCALE = 0;
    var TILES_ADJUST_X = 1;
    var TILES_ADJUST_Y = 2;

    var TILES_MIN_SCALE = 1;
    var TILES_MAX_SCALE = 10;

    var TILES = new Array(// Karte
            [
                [ 1 , 1 ,    0 ,    0 ,    0 ,    0 , false ] ,
                //  0 -
                [ 1 , 1 , 2070 , 1860 , 2070 , 1860 , false ] ,
                //  1 - 1:1000
                [ 1 , 1 ,  518 ,  465 ,  518 ,  465 , false ] ,
                //  2 - 1:4000
                [ 1 , 1 ,  207 ,  186 ,  207 ,  186 , true  ] ,
                //  3 - 1:10000
                [ 1 , 1 ,  103 ,   93 ,  103 ,   93 , true  ] ,
                //  4 - 1:20000
                [ 1 , 1 ,   52 ,   46 ,   52 ,   46 , true  ] ,
                //  5 - 1:40000
                [ 1 , 1 ,   35 ,   31 ,   35 ,   31 , true  ] ,
                //  6 - 1:60000
                [ 1 , 1 ,   24 ,   21 ,   24 ,   21 , true  ] ,
                //  7 - 1:100000
                [ 1 , 1 ,   11  ,  11 ,    7 ,    7 , true  ] ,
                //  8 - 1:400000
                [ 1 , 1 ,    5 ,    5 ,    5 ,    5 , true  ] ,
                //  9 - 1:600000
                [ 1 , 1 ,    4 ,    4 ,    4 ,    4 , true  ]   // 10 - 1:750000
            ],
        // Luftbild
            [
                [ 1 , 1 ,    0 ,    0 ,    0 ,    0 , false ] ,
                //  0 -
                [ 1 , 1 , 2070 , 1860 , 2070 , 1860 , false ] ,
                //  1 - 1:1000
                [ 1 , 1 ,  518 ,  465 ,  518 ,  465 , true  ] ,
                //  2 - 1:4000
                [ 1 , 1 ,  207 ,  186 ,  207 ,  186 , true  ] ,
                //  3 - 1:10000
                [ 1 , 1 ,  103 ,   93 ,  103 ,   93 , true  ] ,
                //  4 - 1:20000
                [ 1 , 1 ,   52 ,   46 ,   52 ,   46 , true  ] ,
                //  5 - 1:40000
                [ 1 , 1 ,   35 ,   31 ,   35 ,   31 , false ] ,
                //  6 - 1:60000
                [ 1 , 1 ,   24 ,   21 ,   24 ,   21 , false ] ,
                //  7 - 1:100000
                [ 1 , 1 ,    6 ,    5 ,    6 ,    5 , false ] ,
                //  8 - 1:400000
                [ 1 , 1 ,    4 ,    4 ,    4 ,    4 , false ] ,
                //  9 - 1:600000
                [ 1 , 1 ,    3 ,    3 ,    3 ,    3 , false ]   // 10 - 1:750000
            ]
            );

    var TILES_ADJUST = new Array(// Karte
            [
                [  -1,  0,    0 ],
                //  0 -
                [   1,  0,    0 ],
                //  1 - 1:1000
                [   4,  0,    0 ],
                //  2 - 1:4000
                [  10,  0,    0 ],
                //  3 - 1:10000
                [  20,  0,    0 ],
                //  4 - 1:20000
                [  40,  0,  100 ],
                //  5 - 1:40000
                [  60,  0,    0 ],
                //  6 - 1:60000
                [ 90.9, 0, -106 ],
                //  7 - 1:100000
                [ 400,  -307,  39 ],
                //  8 - 1:400000
                [ 600, -165, -145 ],
                //  9 - 1:600000
                [ 750, -116, -162 ]  // 10 - 1:750000
            ],
        // Luftbild
            [
                [  -1, 0, 0 ],
                //  0 -
                [   1, 0, 0 ],
                //  1 - 1:1000
                [   4, 0, 0 ],
                //  2 - 1:4000
                [  10, 0, 0 ],
                //  3 - 1:10000
                [  20, 0, 0 ],
                //  4 - 1:20000
                [  40, 0, 0 ],
                //  5 - 1:40000
                [  60, 0, 0 ],
                //  6 - 1:60000
                [ 100, 0, 0 ],
                //  7 - 1:100000
                [ 400, 0, 0 ],
                //  8 - 1:400000
                [ 600, 0, 0 ],
                //  9 - 1:600000
                [ 750, 0, 0 ]  // 10 - 1:750000
            ]
            );

    var TILES_SCALE_CAPTION = new Array("1:1.000", "1:4.000", "1:10.000", "1:20.000", "1:40.000", "1:60.000", "1:100.000", "1:400.000", "1:600.000", "1:750.000");
    var TILES_MASS_CAPTION = new Array("50", "200", "500", "1000", "2000", "3400", "5200", "23000", "34500", "43000");

    var scale;
    var type;
    var width;
    var height;

    var self = this;

    this.initialize = function() {
        self.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        scale = DEFAULT_SCALE;
        type = DEFAULT_TYPE;
    }

    //---------------------------------------------------------------------------

    this.adjustScale = function() {
        //console.info(scale);
        if (!self.getAllowedScale(scale)) {
            if (scale < 5) {
                while (!self.getAllowedScale(scale)) {
                    scale++;
                }
            } else {
                while (!self.getAllowedScale(scale)) {
                    scale--;
                }
            }
        }
        //console.info(scale);
        //console.info("++++++++++++++++++");
    }

    this.setSize = function(pWidth, pHeight) {
        width = pWidth;
        height = pHeight;
    }

    this.setScaleType = function(pScale, pType) {
        self.setScale(pScale);
        self.setType(pType);
    }

    this.setScale = function(pScale) {
        scale = pScale;
        self.adjustScale();
    }

    this.setType = function(pType) {
        type = pType;
        self.adjustScale();
    }

    this.getWidth = function() {
        return width;
    }

    this.getHeight = function() {
        return height;
    }

    this.getScale = function() {
        return scale;
    }

    this.getScaleCaption = function(pScale) {
        if (pScale < TILES_MIN_SCALE) pScale = TILES_MIN_SCALE;
        if (pScale > TILES_MAX_SCALE) pScale = TILES_MAX_SCALE;
        return TILES_SCALE_CAPTION[pScale - TILES_MIN_SCALE];
    }

    this.getMass = function(pScale) {
        if (pScale < TILES_MIN_SCALE) pScale = TILES_MIN_SCALE;
        if (pScale > TILES_MAX_SCALE) pScale = TILES_MAX_SCALE;
        return TILES_MASS_CAPTION[pScale - TILES_MIN_SCALE];
    }

    this.getMinScale = function() {
        return TILES_MIN_SCALE;
    }

    this.getMaxScale = function() {
        return TILES_MAX_SCALE;
    }

    this.getType = function() {
        return type;
    }

    this.getXol = function() {
        return TILES[type][scale][TILES_XOL];
    }

    this.getYol = function() {
        return TILES[type][scale][TILES_YOL];
    }

    this.getXur = function() {
        return TILES[type][scale][TILES_XUR];
    }

    this.getYur = function() {
        return TILES[type][scale][TILES_YUR];
    }

    this.getXolMin = function() {
        return (TILES[type][TILES_MIN_SCALE][TILES_XOL] - 1) * this.getWidth();
    }

    this.getYolMin = function() {
        return (TILES[type][TILES_MIN_SCALE][TILES_YOL] - 1) * this.getHeight();
    }

    this.getXurMax = function() {
        return TILES[type][TILES_MIN_SCALE][TILES_XUR] * this.getWidth();
    }

    this.getYurMax = function() {
        return TILES[type][TILES_MIN_SCALE][TILES_YUR] * this.getHeight();
    }

    this.getXcnt = function(pMapInfo) {
        return TILES[type][scale][TILES_XCNT];
    }

    this.getYcnt = function() {
        return TILES[type][scale][TILES_YCNT];
    }

    this.getAllowedScale = function(pScale) {
        return TILES[type][pScale][TILES_ALLOWED];
    }

    this.getAdjustX = function() {
        return TILES_ADJUST[type][scale][TILES_ADJUST_X];
    }

    this.getAdjustY = function() {
        return TILES_ADJUST[type][scale][TILES_ADJUST_Y];
    }

    this.getAdjustScale = function() {
        return TILES_ADJUST[type][scale][TILES_ADJUST_SCALE];
    }

    this.getAdjustScaleForScale = function(pScale) {
        return TILES_ADJUST[type][pScale][TILES_ADJUST_SCALE];
    }

    this.clone = function() {
        var ti = new TilesInfo();
        ti.setSize(self.getWidth(), self.getHeight());
        ti.setScale(self.getScale());
        ti.setType(self.getType());
        return ti;
    }

    this.toString = function() {
        return "scale: " + scale + " - type: " + type + " - width: " + width + " - height: " + height +
                " - xol: " + self.getXol() + " - yol: " + self.getYol() + " - xur: " + self.getXur() + " - yur: " + self.getYur();
    }

    //---------------------------------------------------------------------------

    this.initialize();
}

//=============================================================================

function AjaxTiles(pMoveManager) {
    var moveManager;
    var mapInfo;
    var tilesInfo;
    var image;
    var x;
    var y;
    var xDispCenter;
    var yDispCenter;
    var scalesMapRect;

    this.initialize = function(pMoveManager) {
        moveManager = pMoveManager;
        mapInfo = moveManager.getMapInfo();
        tilesInfo = moveManager.getTilesInfo();
        image = new Image();

        x = 0;
        y = 0;
        xDispCenter = Math.round(mapInfo.getWidth() / 2);
        yDispCenter = Math.round(mapInfo.getHeight() / 2);
        scalesMapRect = new Rectangle(0, 0, 0, 0);
    }

    //---------------------------------------------------------------------------

    this.moveBy = function(pX, pY) {
        x = Math.round(x + pX);
        y = Math.round(y + pY);
        image.style.left = x + "px";
        image.style.top = y + "px";
    }

    this.zoom = function(pZoom) {
        var zoom = parseFloat(pZoom);
        image.style.left = Math.round(xDispCenter + (x - xDispCenter) / zoom) + "px";
        image.style.top = Math.round(yDispCenter + (y - yDispCenter) / zoom) + "px";
        image.style.width = Math.round(tilesInfo.getWidth() / zoom + 0.5) + "px";
        image.style.height = Math.round(tilesInfo.getHeight() / zoom + 0.5) + "px";
    }

    //---------------------------------------------------------------------------

    this.append = function(pXmap, pYmap) {
        var xTiles;
        var yTiles;

        scalesMapRect = new Rectangle(pXmap, pYmap, pXmap + tilesInfo.getWidth(), pYmap + tilesInfo.getHeight());

        x = Math.round(pXmap - mapInfo.getX() / tilesInfo.getAdjustScale() + xDispCenter) + tilesInfo.getAdjustX();
        y = Math.round(pYmap - mapInfo.getY() / tilesInfo.getAdjustScale() + yDispCenter) + tilesInfo.getAdjustY();

        image.src = tileLocation + "/tiles/white.png";
        image.style.left = x + "px";
        image.style.top = y + "px";
        image.style.width = tilesInfo.getWidth() + "px";
        image.style.height = tilesInfo.getHeight() + "px";
        image.style.position = "absolute";
        //image.style.border = "1px solid";

        $(moveManager.getMoverDiv()).appendChild(image);

        xTiles = Math.round(pXmap / tilesInfo.getWidth()) + 1;
        yTiles = tilesInfo.getYcnt() - Math.round(pYmap / tilesInfo.getWidth());
        //console.info(xTiles + " " + yTiles);

        if (xTiles < tilesInfo.getXol() || xTiles > tilesInfo.getXur() || yTiles < tilesInfo.getYol() || yTiles > tilesInfo.getYur()) {
            image.src = /*tileLocation +*/ "/tiles/blue.png";
        } else {
            if (tilesInfo.getType() == 0) {
                //image.src = tileLocation + "/tiles-out/view/20000/30/30_44.png";
                switch (tilesInfo.getScale()) {
                    case 2: {
                        image.src = tileLocation + "/tiles/4000/" + xTiles + "_" + yTiles + ".png";
                    }
                        break;
                    case 3: {
                        image.src = tileLocation + "/tiles-out/view/10000/" + xTiles + "/" + xTiles + "_" + yTiles + ".png";
                    }
                        break;
                    case 4: {
                        image.src = tileLocation + "/tiles-out/view/20000/" + xTiles + "/" + xTiles + "_" + yTiles + ".png";
                    }
                        break;
                    case 5: {
                        image.src = tileLocation + "/tiles-out/view/40000/" + xTiles + "/" + xTiles + "_" + yTiles + ".png";
                    }
                        break;
                    case 6: {
                        image.src = tileLocation + "/tiles-out/view/60000/" + xTiles + "/" + xTiles + "_" + yTiles + ".png";
                    }
                        break;
                    case 7: {
                        image.src = tileLocation + "/tiles-out/view/100000/" + xTiles + "/" + xTiles + "_" + yTiles + ".png";
                    }
                        break;
                    case 8: {
                        image.src = /*tileLocation +*/ "/tiles-out/view/400000/" + xTiles + "/" + xTiles + "_" + yTiles + ".png";
                    }
                        break;
                    case 9: {
                        image.src = /*tileLocation +*/ "/tiles-out/view/600000/" + xTiles + "/" + xTiles + "_" + yTiles + ".png";
                    }
                        break;
                    case 10: {
                        image.src = /*tileLocation +*/ "/tiles-out/view/750000/" + xTiles + "/" + xTiles + "_" + yTiles + ".png";
                    }
                        break;
                }
            } else {
                //image.src = tileLocation + "/tiles-out/view/20000/30/30_44.png";
                switch (tilesInfo.getScale()) {
                    case 2: {
                        image.src = tileLocation + "/tiles/LB4000/" + xTiles + "_" + yTiles + ".jpg";
                    }
                        break;
                    case 3: {
                        image.src = tileLocation + "/tiles/LB10000/" + xTiles + "_" + yTiles + ".jpg";
                    }
                        break;
                    case 4: {
                        image.src = tileLocation + "/tiles/LB20000/" + xTiles + "_" + yTiles + ".jpg";
                    }
                        break;
                    case 5: {
                        image.src = tileLocation + "/tiles/LB40000/" + xTiles + "_" + yTiles + ".jpg";
                    }
                        break;
                }
            }
        }
    }

    this.remove = function() {
        $(image).remove();
    }

    this.getScalesMapRect = function() {
        return scalesMapRect;
    }

    //---------------------------------------------------------------------------

    this.initialize(pMoveManager);
}

//=============================================================================

function Icon() {
    var ICON_UP_VALUE; //added by Maxim.Kostin
    var image;
    var cnt;
    var idAddress;
    var idCategory;
    var idNews;
    var idMaster;
    var clusterNr;
    var type;
    var x;
    var y;
    var curPosition;
    var distance;
    var width2;
    var height2;
    var mapPosition;
    var mapBounds;
    var text;
    var category;
    var iconUrl = "0.gif";

    var self = this;

    this.initialize = function() {
        ICON_UP_VALUE = 0;
        image = new Image();
        cnt = 0;
        idAddress = 0;
        idCategory = 0;
        idNews = 0;
        idMaster = 0;
        clusterNr = 0;
        type = "";
        x = 0;
        y = 0;
        curPosition = new Position(-1, -1);
        distance = 0;
        width2 = 0;
        height2 = 0;
        mapPosition = new Position(0, 0);
        mapBounds = new Rectangle(0, 0, 0, 0);
        text = "";
        category = "";
        iconUrl = "0.gif";
    }

    //---------------------------------------------------------------------------

    this.moveBy = function(pX, pY) {
        x = Math.round(x + pX);
        y = Math.round(y + pY);
        image.style.left = x + "px";
        image.style.top = (y + ICON_UP_VALUE) + "px";
    }

    this.setIconPosition = function(pMapInfo, pTilesInfo, pXmap, pYmap) {
        width2 = Math.round($(image).width / 2);
        height2 = Math.round($(image).height / 2);
        if (width2 > 0 && height2 > 0) {
            x = Math.round((pXmap - pMapInfo.getX()) / pTilesInfo.getAdjustScale() + pMapInfo.getWidth() / 2) - width2;
            y = Math.round((pYmap - pMapInfo.getY()) / pTilesInfo.getAdjustScale() + pMapInfo.getHeight() / 2) - height2;
            image.style.left = x + "px";
            image.style.top = (y + ICON_UP_VALUE) + "px";
        }
    }

    this.append = function(pContentDiv, pMapInfo, pTilesInfo, pCnt, pIdAddress, pIdCategory, pIdMaster, pClusterNr, pType, pSrc, pXmap, pYmap, pMapBounds) {
        cnt = pCnt;
        idAddress = pIdAddress;
        idCategory = pIdCategory;
        idMaster = pIdMaster;
        clusterNr = pClusterNr;
        type = pType;
        mapPosition = new Position(pXmap, pYmap);
        mapBounds = pMapBounds;

        image.src = pSrc;
        image.style.position = "absolute";
        //image.style.border = "1px solid";
        self.setIconPosition(pMapInfo, pTilesInfo, pXmap, pYmap);
        image.onload = function() {
            self.setIconPosition(pMapInfo, pTilesInfo, pXmap, pYmap);
        }

        $(pContentDiv).appendChild(image);
    }

    this.remove = function() {
        $(image).remove();
    }

    this.isSelected = function(pX, pY) {
        var selected = x <= pX && pX <= x + 2 * width2 && y <= pY && pY <= y + 2 * height2;
        if (selected) {
            curPosition = new Position(pX - x - width2, pY - y);
        } else {
            curPosition = new Position(-1, -1);
        }
        return selected;
    }

    this.setSrc = function(pSrc) {
        image.src = pSrc;
    }
    this.getSrc = function() {
        return image.src;
    }

    this.setVisible = function(pVisible) {
        if (pVisible) {
            $(image).setOpacity(1.0);
        } else {
            $(image).setOpacity(0.0);
        }
    }

    this.flash = function(pOn) {
        //console.info(pOn);
        self.setVisible(!pOn);
        if (pOn) {
            //$(image).setStyle("border: 1px solid;");
        } else {
            //$(image).setStyle("border: 0px;");
        }
    }

    this.getIconUrl = function() {
        return iconUrl;
    }
    this.setIconUrl = function(value) {
        iconUrl = value;
    }

    this.getImage = function() {
        return image;
    }

    this.getCnt = function() {
        return cnt;
    }

    this.getIdAddress = function() {
        return idAddress;
    }

    this.getIdCategory = function() {
        return idCategory;
    }

    this.setIdNews = function(value) {
        idNews = value;
    }

    this.getIdNews = function() {
        return idNews;
    }

    this.getIdMaster = function() {
        return idMaster;
    }

    this.getType = function() {
        return type;
    }

    this.getCurPosition = function() {
        return curPosition;
    }

    this.getMapPosition = function() {
        return mapPosition;
    }

    this.getMapBounds = function() {
        return mapBounds;
    }

    this.getClusterNr = function() {
        return clusterNr;
    }

    this.getDistance = function() {
        return distance;
    }

    this.setDistance = function(pXMap, pYMap) {
        distance = Math.pow(mapPosition.getX() - pXMap, 2) + Math.pow(mapPosition.getY() - pYMap, 2);
    }

    this.getText = function() {
        return text;
    }

    this.setText = function(pText) {
        text = pText;
    }

    this.getCategory = function() {
        return category;
    }

    this.setCategory = function(pCategory) {
        category = pCategory;
    }

    this.toString = function() {
        return "cnt: " + cnt + " - idAddress: " + idAddress + " - idCategory: " + idCategory + " - idMaster: " + idMaster + " - x: " + x + " - y: " + y + " - mapPosition: " + mapPosition;
    }

    //---------------------------------------------------------------------------

    this.initialize();
}

//=============================================================================

function NavManager(pNavCompass, pNavScale, pNavSlider, pNavDiv, pMapWidth, pMapHeight, pMoveManager) {
    var SLIDER_OFFSET_X = 9;
    var SLIDER_OFFSET_Y = 10;
    var SLIDER_Y_STEP = 9;
    var SLIDER_TOOLTIP_X = 10;
    var SLIDER_TOOLTIP_Y = 19;

    var COMPASS_MOVE_MIN = 80;
    var COMPASS_MOVE_MIN = 70;
    var COMPASS_MOVE_MAX = 105;
    var COMPASS_TYPE_MIN = 50;
    var COMPASS_TYPE_MAX = 65
    var COMPASS_CENTER_X = 117;
    var COMPASS_CENTER_Y = 114;
    var COMPASS_SCROLL_MAX = 75;
    var COMPASS_XS_MIN = 62;
    var COMPASS_YS_MIN = 73;
    var COMPASS_XS_MAX = 170 - COMPASS_XS_MIN;
    var COMPASS_YS_MAX = 158 - COMPASS_YS_MIN;
    var COMPASS_XL_MIN = 20300;
    var COMPASS_YL_MIN = 23178;
    var COMPASS_XL_MAX = 405880 - COMPASS_XL_MIN;
    var COMPASS_YL_MAX = 321932 - COMPASS_YL_MIN;
    var COMPASS_ELLIPSE_A = 60;
    var COMPASS_ELLIPSE_B = 50;

    var moveManager;
    var navCompass;
    var navScale;
    var navSlider;
    var navDiv;

    var fkIcon;
    var peHideFk;

    var mouseManagerSlider;
    var toolTipDivSlider;

    var sliderScaleSteps;
    var sliderScale;
    var sliderY;
    var sliderYStep;
    var sliderYMin;
    var sliderYMax;
    var sliderScaleDown;
    var sliderYDown;
    var sliderDown;

    var mouseManagerCompass;
    var toolTipManagerCompass;
    var toolTip;
    var peCompass;

    var navIcon;
    var stepScrollCompass;
    var supressClickCompass;
    var compassDown;

    var self = this;

    this.initialize = function(pNavCompass, pNavScale, pNavSlider, pNavDiv, pMapWidth, pMapHeight, pMoveManager) {
        navCompass = pNavCompass;
        navScale = pNavScale;
        navSlider = pNavSlider;
        navDiv = pNavDiv;
        moveManager = pMoveManager;

        fkIcon = new Image();
        fkIcon.src = "/mallorca/web/img/fadenkreuz.gif";
        fkIcon.style.position = "absolute";
        fkIcon.style.visibility = "hidden";
        $(navDiv).appendChild(fkIcon);
        peHideFk = null;

        //-----

        mouseManagerSlider = new MouseManager(navSlider);
        mouseManagerSlider.setDownFkt(self.downFktSlider);
        mouseManagerSlider.setMoveFkt(self.moveFktSlider);
        mouseManagerSlider.setUpFkt(self.upFktSlider);
        mouseManagerSlider.setOverFkt(self.overFktSlider);
        mouseManagerSlider.setOutFkt(self.outFktSlider);
        toolTipDivSlider = null;

        sliderScaleSteps = 0;
        sliderScale = 0;
        sliderSteps = (moveManager.getMaxScale() - moveManager.getMinScale() + 1);
        sliderY = 0;
        sliderYMin = 0;
        sliderYMax = sliderSteps * SLIDER_Y_STEP;
        sliderScaleDown = 0;
        sliderYDown = 0;
        sliderDown = false;

        //-----

        mouseManagerCompass = new MouseManager(navCompass);
        mouseManagerCompass.setDownFkt(self.downFktCompass);
        mouseManagerCompass.setMoveFkt(self.moveFktCompass);
        mouseManagerCompass.setUpFkt(self.upFktCompass);
        mouseManagerCompass.setClickFkt(self.clickFktCompass);

        toolTipManagerCompass = new ToolTipManager(navCompass);
        toolTipManagerCompass.setToolTipFkt(self.ttFkt);
        toolTipManagerCompass.setToolTipStyle("tooltip");
        toolTipManagerCompass.setToolTipShowDelay(0.5);
        toolTip = "";

        peCompass = null;

        navIcon = new Image();
        navIcon.src = "/mallorca/web/img/Nadel-17x17-N.png";
        navIcon.style.position = "absolute";
        //navIcon.style.visibility = "hidden";
        $(navCompass).appendChild(navIcon);

        stepScrollCompass = -1;
        supressClickCompass = false;
        compassDown = false;

        //-----

        self.setDivSize(pMapWidth, pMapHeight);

        moveManager.setNavManager(this);
    }

    //---------------------------------------------------------------------------

    this.downFktSlider = function(div, event, pMouseXY) {
        sliderYDown = pMouseXY.getY() - sliderY;
        sliderScaleDown = moveManager.getScale();
        sliderDown = true;
        moveManager.getContentManager().removeAll();
        moveManager.getContentManager().setHomeVisible(false);
        moveManager.clickZoomClear();
    }

    this.moveFktSlider = function(div, event, pMouseXYnow, pMouseXY, pMouseDown) {
        if (pMouseDown) {
            var sliderY = pMouseXYnow.getY() - sliderYDown;
            self.setSliderXY(0, sliderY);
            if (sliderScaleDown != sliderScale) {
                moveManager.zoom(moveManager.getAdjustScaleForScale(sliderScale) / moveManager.getAdjustScaleForScale(sliderScaleDown));
            } else {
                moveManager.zoom(1);
            }
        }
        if (toolTipDivSlider != null) {
            $(toolTipDivSlider).setStyle("visibility: visible; left: " + SLIDER_TOOLTIP_X + "px; top: " + (parseInt($(navSlider).cumulativeOffset().top) - SLIDER_TOOLTIP_Y) + "px;");
            $(toolTipDivSlider).update(moveManager.getScaleCaption(sliderScale));
        }
    }

    this.upFktSlider = function(div, event, pMouseXY) {
        self.sliderSnap();
        if (sliderDown) {
            moveManager.setScale(sliderScale);
        }
        sliderYDown = 0;
        sliderDown = false;
    }

    this.overFktSlider = function(div, event, pMouseXY) {
        toolTipDivSlider = new Element("div", {"id": "tooltip"});
        $(navDiv).appendChild($(toolTipDivSlider));
    }

    this.outFktSlider = function(div, event, pMouseXY) {
        $(toolTipDivSlider).remove();
        toolTipDivSlider = null;
    }

    //---------------------------------------------------------------------------

    this.getAngle = function(x, y) {
        if (x == 0) {
            angle = y > 0 ? 90 : 270;
        }
        else {
            angle = Math.round(Math.atan(y / x) * 180 / Math.PI);
            if (x < 0) {
                angle += 180;
            } else {
                if (y < 0) {
                    angle += 360;
                }
            }
        }
        return angle;
    }

    this.getAngleRadiusXY = function(pMouseXY) {
        var x = pMouseXY.getX() - $(pNavCompass).cumulativeOffset().left;
        var y = pMouseXY.getY() - $(pNavCompass).cumulativeOffset().top;
        var dx = x - COMPASS_CENTER_X;
        var dy = -(y - COMPASS_CENTER_Y);
        var angle = self.getAngle(dx, dy);
        var radius;

        if (angle == 0) {
            radius = dy >= 0 ? dy : -dy;
        }
        else {
            radius = Math.round(Math.sqrt(dx * dx + dy * dy));
        }

        return [angle , radius, dx, dy];
    }

    this.getCompassRegion = function(pMouseXY) {
        var wiraxy = self.getAngleRadiusXY(pMouseXY);
        var angle = wiraxy[0];
        var radius = wiraxy[1];
        var x = wiraxy[2];
        var y = wiraxy[3];
        var region = "";
        var dataA = 0;
        var dataB = 0;

        //console.info("angle: " + angle + " - radius: " + radius + " - x: " + x + " - y: " + y);

        if (COMPASS_TYPE_MIN <= radius && radius <= COMPASS_TYPE_MAX) {
            dataA = -1;
            if (110 <= angle && angle <= 160) {
                region = "type";
                dataA = 1;
            }
            if (290 <= angle && angle <= 340) {
                region = "type";
                dataA = 0;
            }
        }

        if (region != "type") {
            if (-COMPASS_ELLIPSE_A <= x && x <= COMPASS_ELLIPSE_A) {
                yi = COMPASS_ELLIPSE_B * Math.sqrt(1 - x * x / COMPASS_ELLIPSE_A / COMPASS_ELLIPSE_A);
                if (-yi <= y && y <= yi) {
                    region = "map";
                    dataA = x;
                    dataB = y;
                }
            }

            if (region != "map") {
                if (COMPASS_MOVE_MIN <= radius && radius <= COMPASS_MOVE_MAX) {
                    region = "move";
                    if (85 <= angle && angle <= 95) { // oben
                        dataA = 0;
                        dataB = 1;
                    }
                    if (265 <= angle && angle <= 275) { // unten
                        dataA = 0;
                        dataB = -1;
                    }
                    if (175 <= angle && angle <= 185) { // links
                        dataA = 1;
                        dataB = 0;
                    }
                    if (0 <= angle && angle <= 5 || 355 <= angle && angle <= 360) { // rechts
                        dataA = -1;
                        dataB = 0;
                    }
                    if (40 <= angle && angle <= 50) { // oben - rechts
                        dataA = -1;
                        dataB = 1;
                    }
                    if (130 <= angle && angle <= 140) { // oben - links
                        dataA = 1;
                        dataB = 1;
                    }
                    if (220 <= angle && angle <= 230) { // unten - links
                        dataA = 1;
                        dataB = -1;
                    }
                    if (310 <= angle && angle <= 320) { // unten - rechts
                        dataA = -1;
                        dataB = -1;
                    }
                } else {
                }
            }
        }

        return [region, dataA, dataB];
    }

    this.ttFkt = function(pDiv, pMouseXY) {
        toolTipManagerCompass.toolTipShow(toolTip, new Position(pMouseXY.getX(), pMouseXY.getY())); //****
    }

    this.downFktCompass = function(div, event, pMouseXY) {
        var region = self.getCompassRegion(pMouseXY);

        toolTipManagerCompass.toolTipStopAndHide();

        stopClickCompass = false;
        compassDown = true;

        if (region[0] == "move") {
            peCompass = new PeriodicalExecuter(function() {
                stepScrollCompass += 2;
                if (stepScrollCompass > COMPASS_SCROLL_MAX) stepScrollCompass = COMPASS_SCROLL_MAX;
                moveManager.moveBy(region[1] * stepScrollCompass, region[2] * stepScrollCompass);
            }, 0.125);
            supressClickCompass = true;
        } else {
            if (region[0] == "type") {
                if (region[1] != -1) {
                    moveManager.setType(region[1]);
                    supressClickCompass = true;
                }
            } else { // region[0] == "map" || region[0] == ""
                moveManager.clickZoomClear();
            }
        }
    }

    this.moveFktCompass = function(div, event, pMouseXYnow, pMouseXY, pMouseDown) {
        var region = self.getCompassRegion(pMouseXYnow);
        //console.info("region: " + region[0] + " - A: " + region[1] + " - B: " + region[2]);

        toolTipManagerCompass.toolTipStopAndHide();

        if (region[0] == "type" && region[1] != -1) {
            if (region[1] == 0) {
                toolTip = "Straßenkarten anzeigen";
            }
            if (region[1] == 1) {
                toolTip = "Luftbilder anzeigen";
            }
            toolTipManagerCompass.toolTipShowDelayed(pMouseXYnow);
        }
        if (region[0] == "map") {
            toolTip = "Direkt hier hin beamen";

            var x = pMouseXY.getX() - $(pNavCompass).cumulativeOffset().left - COMPASS_XS_MIN;
            var y = pMouseXY.getY() - $(pNavCompass).cumulativeOffset().top - COMPASS_YS_MIN;

            var xMap = x * COMPASS_XL_MAX / COMPASS_XS_MAX + COMPASS_XL_MIN;
            var yMap = y * COMPASS_YL_MAX / COMPASS_YS_MAX + COMPASS_YL_MIN;

            new Ajax.Request("/mallorca/web/ajax/ajaxToolTipCompass.php", {
                method: "post",
                asynchronous: true,
                parameters: "&x=" + xMap + "&y=" + yMap,

                onFailure: function(response) {
                    alert(response.responseText);
                },

                onSuccess: function(response) {
                    //console.info(response.responseText);
                    if (response.responseText.length > 0) {
                        toolTip = response.responseText;

                        toolTipManagerCompass.toolTipShowDelayed(pMouseXYnow);
                    }
                }
            });
        }

        if (pMouseDown) {
        }
    }

    this.upFktCompass = function(div, event, pMouseXY) {
        Event.stop(event); //** 23-02-10

        if (peCompass instanceof PeriodicalExecuter) {
            peCompass.stop();
        }
        peCompass = null;
        stepScrollCompass = -1;

        if (compassDown) {
            moveManager.updateContentList();
        }
        compassDown = false;
    }

    this.clickFktCompass = function(div, event, pMouseXY, pClickCount) {
        if (!supressClickCompass) {
            var x = pMouseXY.getX() - $(pNavCompass).cumulativeOffset().left - COMPASS_XS_MIN;
            var y = pMouseXY.getY() - $(pNavCompass).cumulativeOffset().top - COMPASS_YS_MIN;
            //console.info("x: " + x + " - y: " + y);
            var region = self.getCompassRegion(pMouseXY);
            if (region[0] == "map") {
                //console.info("x: " + x + " + y: " + y);
                self.setNavIconBy(x, y);

                var xMap = x * COMPASS_XL_MAX / COMPASS_XS_MAX + COMPASS_XL_MIN;
                var yMap = y * COMPASS_YL_MAX / COMPASS_YS_MAX + COMPASS_YL_MIN;
                moveManager.setXY(xMap, yMap);
            }
        }
        supressClickCompass = false;
    }

    //---------------------------------------------------------------------------

    this.setNavIconBy = function(pX, pY) {
        var x = pX + COMPASS_XS_MIN - COMPASS_CENTER_X;
        var y = pY + COMPASS_YS_MIN - COMPASS_CENTER_Y;
        var yi;

        if (x < -COMPASS_ELLIPSE_A) x = -COMPASS_ELLIPSE_A;
        if (x > COMPASS_ELLIPSE_A) x = COMPASS_ELLIPSE_A;
        yi = COMPASS_ELLIPSE_B * Math.sqrt(1 - x * x / COMPASS_ELLIPSE_A / COMPASS_ELLIPSE_A);
        if (y < -yi) y = -yi;
        if (y > yi) y = yi;
        //console.info("x: " + x + " - y: " + y);

        var angle = self.getAngle(x, -y);
        //console.info("angle: " + angle);

        if (0 <= angle && angle <= 22 || 337 <= angle && angle <= 360) { // O
            navIcon.src = "/mallorca/web/img/Nadel-17x17-O.png";
        }
        if (22 <= angle && angle <= 67) { // NO
            navIcon.src = "/mallorca/web/img/Nadel-17x17-NO.png";
        }
        if (67 <= angle && angle <= 112) { // N
            navIcon.src = "w/mallorca/eb/img/Nadel-17x17-N.png";
        }
        if (112 <= angle && angle <= 157) { // NW
            navIcon.src = "/mallorca/web/img/Nadel-17x17-NW.png";
        }
        if (157 <= angle && angle <= 202) { // W
            navIcon.src = "/mallorca/web/img/Nadel-17x17-W.png";
        }
        if (202 <= angle && angle <= 247) { // SW
            navIcon.src = "/mallorca/web/img/Nadel-17x17-SW.png";
        }
        if (247 <= angle && angle <= 292) { // S
            navIcon.src = "/mallorca/web/img/Nadel-17x17-S.png";
        }
        if (292 <= angle && angle <= 337) { // SO
            navIcon.src = "/mallorca/web/img/Nadel-17x17-SO.png";
        }


        navIcon.style.left = (x + COMPASS_CENTER_X - parseInt(navIcon.width) / 2) + "px";
        navIcon.style.top = (y + COMPASS_CENTER_Y - parseInt(navIcon.height) / 2) + "px";
    }

    this.setNavIconByMap = function(pXMap, pYMap) {
        pXMap -= COMPASS_XL_MIN;
        if (pXMap < 0) pXMap = 0;
        if (pXMap > COMPASS_XL_MAX) pXMap = COMPASS_XL_MAX;
        pYMap -= COMPASS_YL_MIN;
        if (pYMap < 0) pYMap = 0;
        if (pYMap > COMPASS_YL_MAX) pYMap = COMPASS_YL_MAX;
        self.setNavIconBy(Math.round((pXMap * COMPASS_XS_MAX) / COMPASS_XL_MAX), Math.round((pYMap * COMPASS_YS_MAX) / COMPASS_YL_MAX));
    }

    this.setDivSize = function(pMapWidth, pMapHeight) {
        $(navDiv).style.width = pMapWidth + 'px';
        $(navDiv).style.height = pMapHeight + 'px';

        fkIcon.style.left = (Math.round((pMapWidth - parseInt(fkIcon.width)) / 2)) + "px";
        fkIcon.style.top = (Math.round((pMapHeight - parseInt(fkIcon.height)) / 2)) + "px";
    }

    this.setSize = function(pMapWidth, pMapHeight) {
        self.setDivSize(pMapWidth, pMapHeight);
    }

    this.showFk = function() {
        if (peHideFk instanceof PeriodicalExecuter) {
            peHideFk.stop();
        }
        peHideFk = null;

        fkIcon.style.visibility = "visible";
    }

    this.hideFk = function() {
        if (peHideFk == null) {
            peHideFk = new PeriodicalExecuter(function() {
                fkIcon.style.visibility = "hidden";
                peHideFk.stop();
                peHideFk = null;
            }, 1.0);
        }
    }

    this.sliderSnap = function() {
        sliderY = (sliderScale - 1) * SLIDER_Y_STEP + Math.round(SLIDER_Y_STEP / 2);
        self.setSliderXY(0, sliderY);
    }

    this.setSliderXY = function(pX, pY) {
        if (pY < sliderYMin) pY = sliderYMin;
        if (pY > sliderYMax) pY = sliderYMax;
        sliderY = pY;

        sliderScale = Math.ceil((pY - sliderYMin) * (moveManager.getMaxScale() - moveManager.getMinScale() + 1) / (sliderYMax - sliderYMin));
        if (sliderScale < moveManager.getMinScale()) sliderScale = moveManager.getMinScale();
        if (sliderScale > moveManager.getMaxScale()) sliderScale = moveManager.getMaxScale();

        //console.info(sliderY + " " + sliderScale)

        $(navSlider).style.left = (SLIDER_OFFSET_X + pX) + 'px';
        $(navSlider).style.top = (SLIDER_OFFSET_Y + pY) + 'px';
    }

    this.setSlider = function(pScale) {
        $(navSlider).style.visibility = "visible";
        self.setSliderXY(0, Math.round(pScale * (sliderYMax - sliderYMin) / (moveManager.getMaxScale() - moveManager.getMinScale() + 1)));
        self.sliderSnap();
    }

    this.set = function() {
        self.setSlider(moveManager.getScale());
    }

    this.setOpacity = function(pOpacity) {
        //console.info(pOpacity);
        if (pOpacity) {
            $(navCompass).style.opacity = 1.0;
            $(navScale).style.opacity = 1.0;
            $(navSlider).style.opacity = 1.0;
        } else {
            $(navCompass).style.opacity = 0.4;
            $(navScale).style.opacity = 0.5;
            $(navSlider).style.opacity = 0.5;
        }
    }

    //---------------------------------------------------------------------------

    this.initialize(pNavCompass, pNavScale, pNavSlider, pNavDiv, pMapWidth, pMapHeight, pMoveManager);
}

//=============================================================================

function ContentManager(pContentDiv, pDivContentText, pDivHome, pMapWidth, pMapHeight, pMoveManager) {
    //var CONTENT_LIST_LEN = 10;
    //var CONTENT_LIST_PAGES = 4; // 5 - 1
    //var CONTENT_LIST_LP2 = 20; // Math.floor(CONTENT_LIST_LEN * (CONTENT_LIST_PAGES + 1) / 2)

    this.checkWindowSize = function() {
        var myWidth = 0, myHeight = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
            //Non-IE
            myWidth = window.innerWidth;
            myHeight = window.innerHeight;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            myWidth = document.documentElement.clientWidth;
            myHeight = document.documentElement.clientHeight;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            myWidth = document.body.clientWidth;
            myHeight = document.body.clientHeight;
        }
        if((myWidth > 1024) && (myHeight > 768)) return true;
        return false;
    }

    var CONTENT_LIST_LEN = (this.checkWindowSize()) ? 6 : 4;
    var CONTENT_LIST_PAGES = 4; // 5 - 1
    var CONTENT_LIST_LP2 = 10; // Math.floor(CONTENT_LIST_LEN * (CONTENT_LIST_PAGES + 1) / 2)

    var moveManager;
    var divContent;
    var divContentText;
    var divHome;
    var mapInfo;
    var tilesInfo;

    var mouseManager;
    var toolTipManagerInfo;
    var toolTipManagerCluster;
    var activeIcon;
    var curPosition;
    var ttCounter;

    var iconCatArray;
    var iconNewsArray;
    var iconAdrArray;
    var selectedIcon;
    var catRequest;
    var newsRequest;
    var newsCounter;
    var newsFlag;
    var catRequestOld;
    var catCounter;
    var adrRequest;
    var adrCounter;
    var adrSearch1;
    var adrSearch2;
    var adrSearch3;
    var adrSearch4;
    var showPoiFlag;
    var homeRequest;
    var homeIcon;
    var homeIdAdr;
    var homeIdCat;
    var homeChecked;
    var homePermanent;

    var contentListArray;
    var contentListCounter;
    var contentListIdx;

    var contentListIdxBack;
    var contentListIdxBackBack;

    var clusterArray;
    var clusterMouseXY;
    var clusterIdx;
    var clusterEvent;

    var peFlash;
    var flashIcon;
    var flashState;
    var highlightIdAdr;

    var clickCallback;

    //fix poi icons overlaping
    //    var isAdressesClusters = false;
    //
    //    this.setAdressesClusters = function(isAdrClusters) {
    //        isAdressesClusters = isAdrClusters;
    //    }
    //
    //    this.isAdrClusters = function(isAdrClusters) {
    //        return isAdressesClusters;
    //    }

    var self = this;

    this.initialize = function(pContentDiv, pDivContentText, pDivHome, pMapWidth, pMapHeight, pMoveManager) {
        divContent = pContentDiv;
        divContentText = pDivContentText;
        divHome = pDivHome;
        moveManager = pMoveManager;
        mapInfo = moveManager.getMapInfo();
        tilesInfo = moveManager.getTilesInfo();

        mouseManager = new MouseManager(divContent);
        mouseManager.setDownFkt(self.downFkt);
        mouseManager.setMoveFkt(self.moveFkt);
        mouseManager.setUpFkt(self.upFkt);
        mouseManager.setClickFkt(null);
        mouseManager.setOverFkt(self.overFkt);
        mouseManager.setOutFkt(self.outFkt);

        toolTipManagerInfo = new ToolTipManager(divContent);
        toolTipManagerInfo.setToolTipHiddenFkt(self.ttInfoHiddenFkt);
        toolTipManagerInfo.setToolTipFkt(self.ttInfoFkt);
        toolTipManagerInfo.setToolTipMouseMoveFkt(self.ttInfoMouseMoveFkt);
        toolTipManagerInfo.setToolTipMapMoveFkt(moveManager.moveBy);
        toolTipManagerInfo.setToolTipMapMoveDoneFkt(self.ttInfoMapMoveDoneFkt);
        toolTipManagerInfo.setToolTipStyle("tooltip2");
        toolTipManagerInfo.setToolTipShowDelay(1.0);
        toolTipManagerInfo.setToolTipHideDelay(0.25);

        toolTipManagerCluster = new ToolTipManager(divContent);
        toolTipManagerCluster.setToolTipFkt(self.ttClusterFkt);
        toolTipManagerCluster.setToolTipHiddenFkt(self.ttClusterHiddenFkt);
        toolTipManagerCluster.setToolTipMouseMoveFkt(self.ttClusterMouseMoveFkt);
        toolTipManagerCluster.setToolTipMapMoveFkt(moveManager.moveBy);
        toolTipManagerCluster.setToolTipMapMoveDoneFkt(self.ttClusterMapMoveDoneFkt);
        toolTipManagerCluster.setToolTipStyle("tooltip3");
        toolTipManagerCluster.setToolTipShowDelay(1.0);
        toolTipManagerCluster.setToolTipHideDelay(0.25);

        activeIcon = new Icon();
        curPosition = new Position(-1, -1);
        ttCounter = 0;

        iconCatArray = new Array();
        iconAdrArray = new Array();
        iconNewsArray = new Array();
        selectedIcon = null;
        catRequest = null;
        newsRequest = null;
        newsFlag = false;
        catRequestOld = null;
        catCounter = 0;
        newsCounter = 0;
        adrRequest = null;
        adrCounter = 0;
        adrSearch1 = false;
        adrSearch2 = false;
        adrSearch3 = false;
        adrSearch4 = false;
        showPoiFlag = false;
        homeRequest = null;
        homeIcon = null;
        homeIdAdr = "";
        homeIdCat = "";
        homeChecked = false;
        homePermanent = false;

        contentListArray = null;
        contentListCounter = 0;
        contentListIdx = 0;

        contentListIdxBack = 0;
        contentListIdxBackBack = 0;

        clusterArray = new Array();
        clusterMouseXY = null;
        clusterIdx = -1;
        clusterEvent = null;

        peFlash = null;
        flashIcon = null;
        flashState = false;
        highlightIdAdr = "";

        clickCallback = null;

        self.setDivSize(pMapWidth, pMapHeight);

        moveManager.setContentManager(this);
        moveManager.setNavManager(this);
    }

    //---------------------------------------------------------------------------

    this.setDivSize = function(pMapWidth, pMapHeight) {
        $(divContent).style.width = pMapWidth + 'px';
        $(divContent).style.height = pMapHeight + 'px';
    }

    this.setSize = function(pMapWidth, pMapHeight) {
        self.setDivSize(pMapWidth, pMapHeight);
    }

    this.isAdrSet = function() {
        return adrRequest != null;
    }

    this.isCatSet = function() {
        return catRequest != null;
    }

    this.isNewsSet = function() {
        return newsRequest != null;
      }

    this.showAdrOld = function() {//changed by Maxim Kostin
        if (self.isAdrSet()) {
            //console.info(adrSearch1 + " " + adrSearch2 + " " + adrSearch3 + " " + adrSearch4);
            new Ajax.Request("/mallorca/web/ajax/ajaxAddresses.php", {
                method: "post",
                asynchronous: true,
                parameters: "&addresses=" + adrRequest + "&home=" + homeRequest + "&adrCounter=" + (++adrCounter),

                onFailure: function(response) {
                    alert(response.responseText);
                },

                onSuccess: function(response) {
                    var icon;
                    var src;
                    var xol;
                    var yol;
                    var xur;
                    var yur;

                    self.removeAllAdr();

                    if (response.responseText.length > 0) {
                        //console.info(adrCounter + " " + response.responseText);
                        var ajaxResponse = response.responseText.split("\|");
                        if (ajaxResponse[0] == adrCounter) {
                            for (var i = 1; i < ajaxResponse.length - 1; i += 12) {
                                icon = new Icon();

                                if (ajaxResponse[i + 5] == "V") {
                                    src = "/mallorca/web/img/vignette/" + tilesInfo.getScale() + "/" + ajaxResponse[i + 1].replace(",", "") + ".png";
                                } else {
                                    if (ajaxResponse[i + 5] == "S") {
                                        src = "/mallorca/web/img/icons/icon-strasse.gif";
                                    } else {
                                        if (ajaxResponse[i + 5] == "T") {
                                            src = "/mallorca/web/img/icons/icon-siedlung.gif";
                                        } else {
                                            src = "/mallorca/web/img/kategorien/" + ajaxResponse[i + 12];
                                        }
                                    }
                                }

                                icon.append(divContent, mapInfo, tilesInfo, ajaxResponse[i + 0], ajaxResponse[i + 1], ajaxResponse[i + 2], ajaxResponse[i + 3], ajaxResponse[i + 4], ajaxResponse[i + 5], src, ajaxResponse[i + 6], ajaxResponse[i + 7], new Rectangle(parseInt(ajaxResponse[i + 8]), parseInt(ajaxResponse[i + 9]), parseInt(ajaxResponse[i + 10]), parseInt(ajaxResponse[i + 11])));
                                icon.setIconUrl(ajaxResponse[i + 12]);

                                iconAdrArray.push(icon);

                                xol = parseInt(ajaxResponse[i + 8]);
                                yol = parseInt(ajaxResponse[i + 9]);
                                xur = parseInt(ajaxResponse[i + 10]);
                                yur = parseInt(ajaxResponse[i + 11]);
                            }

                            if (adrSearch1 && adrRequest != "") { //++
                                moveManager.setForBoundsAndGo(xol, yol, xur, yur);
                                adrSearch2 = true;
                            }else {
                                if (self.isCatSet()) {
                                    self.showCat();
                                }
                            }

                            if (!adrSearch1 && adrSearch2) {
                                self.updateContentList(); //++ Adresse entfernen, falls adrRequest leer ist

                                adrSearch2 = false;
                                adrSearch3 = true;

                                self.viewLine(3.0);
                            }

                            adrSearch1 = false;
                        }
                    }
                }
            });
        }
    }

    this.showAdr = function() {
        //parameters: "&categories=" + catRequest + "&addresses=" + adrRequest + "&home=" + homeRequest + "&xol=" +
        // + mapRect.getXol() + "&yol=" + mapRect.getYol() + "&xur=" + mapRect.getXur() + "&yur=" + mapRect.getYur() +
        // + "&scale=" + tilesInfo.getScale() + "&catCounter=" + (++catCounter),
        if (self.isAdrSet()) {
            //console.info(adrSearch1 + " " + adrSearch2 + " " + adrSearch3 + " " + adrSearch4);
            new Ajax.Request("/mallorca/web/ajax/ajaxAddresses.php", {
                //            new Ajax.Request("/mallorca/web/ajax/ajaxAdresses.php", {
                method: "post",
                asynchronous: true,
                parameters: "&addresses=" + adrRequest + "&home=" + homeRequest + "&adrCounter=" + (++adrCounter) + "&scale=" + tilesInfo.getScale(),

                onFailure: function(response) {
                    alert(response.responseText);
                },

                onSuccess: function(response) {
                    var icon;
                    var src;
                    var xol;
                    var yol;
                    var xur;
                    var yur;

                    self.removeAllAdr();

                    if (response.responseText.length > 0) {
                        //console.info(adrCounter + " " + response.responseText);
                        var ajaxResponse = response.responseText.split("\|");
                        var prefix = ajaxResponse[0].split(",");
                        if (prefix[0] == adrCounter) {
                            for (var i = 1; i < ajaxResponse.length - 1; i += 13) {
                                icon = new Icon();

                                if (ajaxResponse[i + 4] != 0) { // cluster
                                    src = "/mallorca/web/img/icons/icon-cluster.gif";
                                }else {
                                    if (ajaxResponse[i + 5] == "V") {
                                        src = "/mallorca/web/img/vignette/" + tilesInfo.getScale() + "/" + ajaxResponse[i + 1].replace(",", "") + ".png";
                                    } else {
                                        if (ajaxResponse[i + 5] == "S") {
                                            src = "/mallorca/web/img/icons/icon-strasse.gif";
                                        } else {
                                            if (ajaxResponse[i + 5] == "T") {
                                                src = "/mallorca/web/img/icons/icon-siedlung.gif";
                                            } else {
                                                src = "/mallorca/web/img/kategorien/" + ajaxResponse[i + 12];
                                            }
                                        }
                                    }
                                }
                                //alert(src);
                                icon.append(divContent, mapInfo, tilesInfo, ajaxResponse[i + 0], ajaxResponse[i + 1], ajaxResponse[i + 2],
                                        ajaxResponse[i + 3], ajaxResponse[i + 4], ajaxResponse[i + 5], src, ajaxResponse[i + 6], ajaxResponse[i + 7],
                                        new Rectangle(parseInt(ajaxResponse[i + 8]), parseInt(ajaxResponse[i + 9]), parseInt(ajaxResponse[i + 10]), parseInt(ajaxResponse[i + 11])));

                                iconAdrArray.push(icon);


                            }
                            //alert(iconAdrArray.length);
                            xol = parseInt(prefix[1]);
                            yol = parseInt(prefix[2]);
                            xur = parseInt(prefix[3]);
                            yur = parseInt(prefix[4]);

                            if (adrSearch1 && adrRequest != "") { //++
                                moveManager.setForBoundsAndGo(xol, yol, xur, yur);
                                adrSearch2 = true;
                            } else {
                                if (self.isCatSet()) {
                                    self.showCat();
                                }
                            }

                            if (!adrSearch1 && adrSearch2) {
                                self.updateContentList(); //++ Adresse entfernen, falls adrRequest leer ist

                                adrSearch2 = false;
                                adrSearch3 = true;

                                self.viewLine(3.0);
                            }

                            adrSearch1 = false;
                        }
                    }
                }
            });
        }
    }

    this.showNews = function() { 
        if (self.isNewsSet()) {
            var mapRect = mapInfo.getMapRect();
            new Ajax.Request("/mallorca/web/ajax/ajaxAdjustNews.php", {
                method: "post",
                asynchronous: true,
                parameters: "&news=" + newsRequest + "&home=" + homeRequest + "&xol=" + "&scale=" + tilesInfo.getScale() + "&newsCounter=" + (++newsCounter),

                onFailure: function(response) {
                    alert(response.responseText);
                },

                onSuccess: function(response) {
                    var icon;
                    var src;
                    
                    self.removeAllNews();

                    if(response.responseText.length > 0) {
                        var ajaxResponse = response.responseText.split("\|");
                        var prefix = ajaxResponse[0].split(",");
                        //alert(ajaxResponse.length);
                        if(prefix[0] == newsCounter) {
                            var counter = 1;
                            for(var i = 1; i < ajaxResponse.length - 1; i+=9) {

                                icon = new Icon();
                                var img_name = (ajaxResponse.length == 11) ? "news.gif" : counter + ".gif";
                                src = "/mallorca/web/img/news/" + img_name;

                                icon.append(divContent, mapInfo, tilesInfo, 1, "0", "0", "0", "0", ajaxResponse[i+1], src, ajaxResponse[i+3], ajaxResponse[i+4], new Rectangle(parseInt(ajaxResponse[i+5]), parseInt(ajaxResponse[i+6]), parseInt(ajaxResponse[i+7]), parseInt(ajaxResponse[i+8])));
                                icon.setIdNews(ajaxResponse[i+0]);
                                iconNewsArray.push(icon);
                                counter++;
                            }
                        }                        

                        var xol = parseInt(prefix[1]);
                        var yol = parseInt(prefix[2]);
                        var xur = parseInt(prefix[3]);
                        var yur = parseInt(prefix[4]);

                        if (newsRequest != "" && newsFlag) { //++
                            newsFlag = false;
                            self.updateContentList();
                            moveManager.setForBoundsAndGo(xol, yol, xur, yur);                            
                        }                                                
                    }
                }
            });
        }
    }

    this.showCat = function() {
        if (self.isCatSet()) {
            //            var eroskiAdr = "7640%2C 7728%2C 7729%2C 7730%2C 7731%2C 7732%2C 7733%2C 7734%2C 7735%2C 7736%2C 7737%2C 7738%2C 7739%2C 7740%2C 7741%2C 7742%2C 7743%2C 7744%2C 7745%2C 7746%2C 7747%2C 7748%2C 7749%2C 7750%2C 7751%2C 7752%2C 7753%2C 7754%2C 7755%2C 7756%2C 7757%2C 7758%2C 7759%2C 7760%2C 7761%2C 7762%2C 7763%2C 7764%2C 7765%2C 7768%2C 7769%2C 7770%2C 7771%2C 7772%2C 7773%2C 7774%2C 7775%2C 7776%2C 7777%2C 7778%2C 7779%2C 7780%2C 7781%2C 7782%2C 7783%2C 7784%2C 7785%2C 7786%2C 7787%2C 7788%2C 7789%2C 7790%2C 7791%2C 7792%2C 7793%2C 7794%2C 7798%2C 7799%2C 7800%2C 7801%2C 7802%2C 7803%2C 7804%2C 7805%2C 7806%2C 7807%2C 7808%2C 7809%2C 7810%2C 7811%2C 7812%2C 7813%2C 7814%2C 7815%2C 7816%2C 7817%2C 7818%2C 7819%2C 7820%2C 7821%2C 7822%2C 7823%2C 7824%2C 7825%2C 7826%2C 7827%2C 7974%2C 8412%2C 8562%2C 8823%2C 9700%2C 10225%2C 10276%2C 10327%2C 10511%2C ";

            //            eroskiAdr.replace(/ /g,"_");
            //            adrRequest = "7640,7728,7729,7730,7731,7732,7733,7734,7735,7736,7737,7738,7739,7740,7741,7742,7743,7744,7745,7746,7747,7748,7749,7750,7751,7752,7753,7754,7755,7756,7757,7758,7759,7760,7761,7762,7763,7764,7765,7768,7769,7770,7771,7772,7773,7774,7775,7776,7777,7778,7779,7780,7781,7782,7783,7784,7785,7786,7787,7788,7789,7790,7791,7792,7793,7794,7798,7799,7800,7801,7802,7803,7804,7805,7806,7807,7808,7809,7810,7811,7812,7813,7814,7815,7816,7817,7818,7819,7820,7821,7822,7823,7824,7825,7826,7827,7974,8412,8562,8823,9700,10225,10276,10327,10511,";
            var mapRect = mapInfo.getMapRect();
            new Ajax.Request("/mallorca/web/ajax/ajaxCategories.php", {
                method: "post",
                asynchronous: true,
                //                parameters: "&categories=" + catRequest + "&addresses=" + eroskiAdr + "&home=" + homeRequest + "&xol=" + mapRect.getXol() + "&yol=" + mapRect.getYol() + "&xur=" + mapRect.getXur() + "&yur=" + mapRect.getYur() + "&scale=" + tilesInfo.getScale() + "&catCounter=" + (++catCounter),
                parameters: "&categories=" + catRequest + "&addresses=" + adrRequest + "&home=" + homeRequest + "&xol=" + mapRect.getXol() + "&yol=" + mapRect.getYol() + "&xur=" + mapRect.getXur() + "&yur=" + mapRect.getYur() + "&scale=" + tilesInfo.getScale() + "&catCounter=" + (++catCounter),

                onFailure: function(response) {
                    alert(response.responseText);
                },

                onSuccess: function(response) {
                    var icon;
                    var src;

                    self.removeAllCat();

                    if (response.responseText.length > 0) {
                        //alert(response.responseText);
                        var ajaxResponse = response.responseText.split("\|");
                        if (ajaxResponse[0] == catCounter) {
                            for (var i = 1; i < ajaxResponse.length - 1; i += 13) {
                                icon = new Icon();                                
                                if (ajaxResponse[i + 4] != 0) { // cluster
                                    src = "/mallorca/web/img/icons/icon-cluster.gif";
                                }else {
                                    if (ajaxResponse[i + 5] == "V") {
                                        src = "/mallorca/web/img/vignette/" + tilesInfo.getScale() + "/" + ajaxResponse[i + 1].replace(",", "") + ".png";
                                    }else {
                                        src = "/mallorca/web/img/kategorien/" + ajaxResponse[i + 12];
                                    }
                                }

                                icon.append(divContent, mapInfo, tilesInfo, ajaxResponse[i + 0], ajaxResponse[i + 1], ajaxResponse[i + 2],
                                        ajaxResponse[i + 3], ajaxResponse[i + 4], ajaxResponse[i + 5], src, ajaxResponse[i + 6], ajaxResponse[i + 7],
                                        new Rectangle(parseInt(ajaxResponse[i + 8]), parseInt(ajaxResponse[i + 9]), parseInt(ajaxResponse[i + 10]), parseInt(ajaxResponse[i + 11])));
                                icon.setIconUrl(ajaxResponse[i + 12]);
                                iconCatArray.push(icon);
                            }
                        }
                    }
                    self.updateContentList();
                    self.toFront(self.iconForAdr(adrRequest));
                }
            });
        }
    }

    this.showIcons = function(pDir) {
        //                alert("showIcons - " + pDir + " - " + adrRequest + " - " + catRequest);
        homeRequest = "";
        if (homeIcon != null) {
            homeRequest = homeIcon.getIdAddress() + ",";
        } else {
            if (homeIdAdr != "") {
                homeRequest = homeIdAdr + ",";
            }
        }
        self.setHomeVisible(false);
        self.hideLineFromHomeTo();
        //        if (!self.isAdrClusters()) {//changed by Maxim Kostin
        if (self.isAdrSet()) {
            self.showAdr();
        }
        else if (self.isCatSet()) {            
            self.showCat();            
        }
        else if (self.isNewsSet()) {
            self.showNews();
        }
    }

    this.requestContentList = function() {
        var request = "";

        for (var i = contentListIdx; i < Math.min(contentListArray.length, contentListIdx + CONTENT_LIST_LEN); i++) {
            request += contentListArray[i].getIdAddress() + "|" + contentListArray[i].getIdCategory() + "|";
        }

        var addPar = "";
        if(newsFlag) {
            addPar = "&isnews=1";
        }
        
        new Ajax.Request("/mallorca/web/ajax/ajaxContentList.php", {
            method: "post",
            asynchronous: true,
            parameters: "&request=" + request + "&contentListCounter=" + (++contentListCounter) + addPar,

            onFailure: function(response) {
                alert(response.responseText);
            },

            onSuccess: function(response) {
                //console.info(response.responseText);
                if (response.responseText.length > 0) {
                    var ajaxResponse = response.responseText.split("\|");
                    if (ajaxResponse[0] == contentListCounter) {
                        var j = 0;
                        for (var i = 1; i <= ajaxResponse.length - 2; i += 2) {
                            var text = ajaxResponse[i];
                            if (contentListArray[j].getClusterNr() == 0) {
                                text = text.replace(/::::/g, "");
                            } else {
                                text = text.replace(/::::/g, " *");
                            }
                            contentListArray[contentListIdx + j].setText(text);
                            contentListArray[contentListIdx + j].setCategory(ajaxResponse[i + 1]);
                            j++;
                        }

//                        var content = "";
//                        new Ajax.Request("/mallorca/web/ajax/ajaxGetter.php", {
//                            method: "post",
//                            asynchronous: false,
//                            parameters: "&idCat=" + contentListArray[i].getIdCategory(),
//
//                            onFailure: function(response) {
//                                alert(response.responseText);
//                            },
//
//                            onSuccess: function(response) {
//                                content = response.responseText;
//                            }
//                        });
//
//                        var div = document.getElementById('divContent0');
//                        if(contentListIdx / CONTENT_LIST_LEN != 0) {
//                            div.innerHTML = "";
//                        } else {
//                            div.innerHTML = content;
//                        }

                        self.displayContentList();
                    }
                }
            }
        });
    }

    this.updateContentList = function() {
        var viewPort = moveManager.getViewport();
        var adjustScale = moveManager.getTilesInfo().getAdjustScale();
        var xol = viewPort.getXol() - 0 * adjustScale;
        var yol = viewPort.getYol() - 0 * adjustScale;
        var xur = viewPort.getXur() + 0 * adjustScale;
        var yur = viewPort.getYur() + 0 * adjustScale;
        var x;
        var y;
        var icon;

        contentListArray = new Array();

        // Viewport

        //console.info(viewPort);
        for (var i = 0; i < iconAdrArray.length; i++) {
            if (xol < iconAdrArray[i].getMapPosition().getX() && iconAdrArray[i].getMapPosition().getX() < xur &&
                    yol < iconAdrArray[i].getMapPosition().getY() && iconAdrArray[i].getMapPosition().getY() < yur) {
                contentListArray.push(iconAdrArray[i]);
            }
        }
        for (var i = 0; i < iconCatArray.length; i++) {
            if (xol < iconCatArray[i].getMapPosition().getX() && iconCatArray[i].getMapPosition().getX() < xur &&
                    yol < iconCatArray[i].getMapPosition().getY() && iconCatArray[i].getMapPosition().getY() < yur) {
                contentListArray.push(iconCatArray[i]);
            }
        }        
        for (var i = 0; i < iconNewsArray.length; i++) {            
            if (xol < iconNewsArray[i].getMapPosition().getX() && iconNewsArray[i].getMapPosition().getX() < xur &&
                    yol < iconNewsArray[i].getMapPosition().getY() && iconNewsArray[i].getMapPosition().getY() < yur) {
                contentListArray.push(iconNewsArray[i]);
            }
        }

        // catTreeDisplay updaten

        catTreeDisplay.catInMapClear();

        for (var i = 0; i < contentListArray.length; i++) {
            //debugger;
            //console.info(contentListArray[i].getIdCategory() + " - " + contentListArray[i].getCnt() + " - " + contentListArray[i].getType());
            if (contentListArray[i].getType() == "V") {
                catTreeDisplay.catInMapAdd(-1, contentListArray[i].getCnt());
            } else {
                catTreeDisplay.catInMapAdd(contentListArray[i].getIdCategory(), contentListArray[i].getCnt());
            }
        }

        catTreeDisplay.catInMapShow();

        // Sortieren

        if (homeIcon != null) { //++ In welcher Reihenfolge werde die Icons in der Liste angezeigt
            x = homeIcon.getMapPosition().getX();
            y = homeIcon.getMapPosition().getY();
        } else {
            x = mapInfo.getX();
            y = mapInfo.getY();
        }

        for (var i = 0; i < contentListArray.length; i++) {
            contentListArray[i].setDistance(x, y);
            //console.info(contentListArray[i].getDistance());
        }

        for (var i = 0; i < contentListArray.length - 1; i++) {
            for (var j = i + 1; j < contentListArray.length; j++) {
                //console.info(i +" " + j);
                if (contentListArray[i].getDistance() > contentListArray[j].getDistance()) {
                    icon = contentListArray[i];
                    contentListArray[i] = contentListArray[j];
                    contentListArray[j] = icon;
                }
            }
        }

        contentListIdx = contentListIdxBack;
        contentListIdxBack = 0;

        self.requestContentList();
    }

    this.viewLine = function(t) {
        var icon = self.iconForAdr(adrRequest);
        self.toFront(icon);
        self.showLineFromHomeTo(icon);

        var pe = new PeriodicalExecuter(function() {
            self.hideLineFromHomeTo()
            //self.highlightContentText(adrRequest);
            pe.stop();
            pe = null;
        }, t);
    }

    this.displayContentList = function() {
        var start;
        var ende;
        var data;
        var kmText;
        var scale;

        if(!self.isNewsSet()) {
            if (slider != null) {
                slider.close();
            }
            $("divContent2_content").update("");
        }
        
        //split('|', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
        
        if (showPoiFlag) {
            self.showPoi(adrRequest);
            showPoiFlag = false;
            //adrRequest = "";
            contentListIdx = contentListIdxBack = contentListIdxBackBack = self.getContentListIdx(adrRequest);
            //console.info(contentListIdx);
            self.viewLine(3.0);
        } else if (self.isNewsSet()) {
            
        } else {
            start = Math.max(Math.floor((contentListIdx - CONTENT_LIST_LP2) / CONTENT_LIST_LEN), 0);
            ende = Math.min(start + CONTENT_LIST_PAGES, Math.floor((contentListArray.length - 1) / CONTENT_LIST_LEN));
            start = Math.max(ende - CONTENT_LIST_PAGES, 0);
            ende = Math.min(start + CONTENT_LIST_PAGES, Math.floor((contentListArray.length - 1) / CONTENT_LIST_LEN));
            //console.info(contentListArray.length + " " + contentListIdx + " " + start + " " + ende);

            $(divContentText).update("");

            var div = new Element("div", { });
            //$(div).style.margin = "4px";
            //$(div).style.paddingTop = "10px";
            //$(div).style.paddingLeft = "4px";
            $(div).update("<b>Suchergebnis<b><br /><br />");

            $(divContentText).appendChild(div);

            var div = new Element("div", { });
            $(div).style.height = "4px";
            $(div).update("");

            $(divContentText).appendChild(div);

            if (homeIcon != null) {
                kmText = " km zum Ausgangspunkt";
            } else {
                kmText = " km zum Kartenmittelpunkt";
            }
            scale = moveManager.getMass(tilesInfo.getScale()) / 200 / tilesInfo.getAdjustScale();

            for (var i = contentListIdx; i < Math.min(contentListArray.length, contentListIdx + CONTENT_LIST_LEN); i++) {
                var dxy;

                data = contentListArray[i].getText();

                dxy = Math.round(Math.sqrt(contentListArray[i].getDistance()) * scale);
                if (dxy > 1000) {
                    dxy = Math.round(dxy / 100) / 10;
                } else {
                    dxy = Math.round(dxy / 10) / 100;
                }

                data += dxy + kmText;

                //var div = new Element("div", { });
                //$(div).style.height = "4px";
                //$(div).update("");

                //$(divContentText).appendChild(div);

                var idAdr = contentListArray[i].getIdAddress();
                var div = new Element("div", { });
                $(div).id = "id" + contentListArray[i].getIdAddress().replace(/,/g, "-");
                $(div).style.border = "0px dotted";
                //$(div).style.margin = "4px";
                //$(div).style.paddingLeft = "4px";
                //$(div).style.paddingRight = "4px";
                $(div).update(data);

                $(divContentText).appendChild(div);
            }

            if (contentListArray.length > 0) {
                if (contentListArray.length > CONTENT_LIST_LEN) {
                    data = "<br /><b>Treffer:</b> " + (contentListIdx + 1) + " bis " + Math.min(contentListIdx + CONTENT_LIST_LEN, contentListArray.length) + " von " + contentListArray.length;
                    data += "<br /><b>Seite:</b> <a href='javascript:map.gotoPage(1)' style='text-decoration: underline'>&lt;&lt;&lt;</a> <a href='javascript:map.gotoPage(\"--\")' style='text-decoration: underline'>&lt;</a> ";
                    for (var i = start; i <= ende; i++) {
                        if (i * CONTENT_LIST_LEN == contentListIdx) {
                            data += "<b><font size='2'>" + (i + 1) + "</font></b> ";
                        } else {
                            data += "<a href='javascript:map.gotoPage(" + (i + 1) + ")' onmousemove='window.status=\"\"; return true;' style='text-decoration: underline'>" + (i + 1) + "</a>  ";
                        }
                    }
                    data += " <a href='javascript:map.gotoPage(\"++\")' style='text-decoration: underline'>&gt;</a> <a href='javascript:map.gotoPage(" + (Math.floor((contentListArray.length - 1) / CONTENT_LIST_LEN) + 1) + ")' style='text-decoration: underline'>&gt;&gt;&gt;</a><br />";
                } else {
                    data = "<br /><b>Treffer:</b> " + contentListArray.length;
                }

                var div = new Element("div", { });
                //$(div).style.border = "1px solid";
                $(div).style.position = "absolute";
                //$(div).style.margin = "4px";
                //$(div).style.paddingLeft = "4px";
                //$(div).style.paddingRight = "4px";
                //$(div).style.top = ($(divContentText).getHeight() - 35) + "px";
                $(div).update(data);

                $(divContentText).appendChild(div);

                if (!adrSearch1 && !adrSearch2 && adrSearch3) {
                    self.highlightContentText(adrRequest);
                    adrSearch3 = false;
                    adrSearch4 = true;
                } else {
                    if (!adrSearch1 && !adrSearch2 && !adrSearch3 && adrSearch4) {
                        self.highlightContentText(adrRequest);
                        adrSearch4 = false;
                    }
                }

                if (highlightIdAdr != "") {
                    self.highlightAddress(highlightIdAdr);
                    highlightIdAdr = "";
                }
            } else {
                var div = new Element("div", { });
                //$(div).style.margin = "4px";
                //$(div).style.paddingTop = "10px";
                //$(div).style.paddingLeft = "4px";
                $(div).update("Geben Sie Ihren Suchbegriff (z.B. Ihr Hotel, eine Sehenswürdigkeit, einen Ort, eine Strasse) in das Eingabefeld ein, und wählen Sie ihn danach in der Liste aus!");

                $(divContentText).appendChild(div);

                split('|', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
            }
        }
    }

    this.getContentListIdx = function(pIdAddress) {
        var idx = -1;
        for (var i = 0; i < contentListArray.length; i++) {
            if (contentListArray[i].getIdAddress() == pIdAddress) {
                idx = i;
            }
        }
        if (idx != -1) {
            idx = Math.floor(idx / CONTENT_LIST_LEN) * CONTENT_LIST_LEN;
        }
        return idx;
    }

    this.highlightAddress = function(pIdAddress) {
        if ($("id" + pIdAddress.replace(/,/g, "-"))) { //++
            $("id" + pIdAddress.replace(/,/g, "-")).setStyle("background-color: #fff; border: 0px solid;");
        }
    }

    this.highlightContentText = function(pIdAddress) {
        if (contentListArray != null) {
            for (var i = 0; i < contentListArray.length; i++) {
                var id = "id" + contentListArray[i].getIdAddress().replace(/,/g, "-");
                if ($(id) != null) {
                    $(id).setStyle("background-color: transparent; border: 0px dotted;");
                }
            }

            var idx = self.getContentListIdx(pIdAddress);
            if (idx != -1) {
                if (idx != contentListIdx) {
                    contentListIdx = idx;
                    self.requestContentList();
                    highlightIdAdr = pIdAddress;
                } else {
                    self.highlightAddress(pIdAddress);
                }
            }
        }
    }

    this.gotoPage = function(pPage) {
        window.status = "";
        if (pPage == "++") {
            contentListIdx = Math.min(contentListIdx + CONTENT_LIST_LEN, Math.floor((contentListArray.length - 1) / CONTENT_LIST_LEN) * CONTENT_LIST_LEN);
            //console.info(contentListIdx);
        } else {
            if (pPage == "--") {
                contentListIdx = Math.max(contentListIdx - CONTENT_LIST_LEN, 0);
            } else {
                contentListIdx = (pPage - 1) * CONTENT_LIST_LEN;
            }
        }
        self.requestContentList();
    }

    this.centerIconBounds = function(pIdAdr) {
        toolTipManagerInfo.toolTipStopAndHide();

        new Ajax.Request("/mallorca/web/ajax/ajaxAddressesBounds.php", {
            method: "post",
            asynchronous: true,
            parameters: "&addresses=" + pIdAdr,

            onFailure: function(response) {
                alert(response.responseText);
            },

            onSuccess: function(response) {
                //alert(response.responseText);
                var bounds = response.responseText.split("\|");
                moveManager.setForBoundsAndGo(bounds[0], bounds[1], bounds[2], bounds[3]);
            }
        });
    }

    this.centerClusterBounds = function(pIdAdr) {
        toolTipManagerInfo.toolTipStopAndHide();

        var icon = self.iconForAdr(pIdAdr);
        //alert("centerClusterBounds " + icon);
        if (icon != null) {
            var mapBounds = icon.getMapBounds();
            moveManager.setForBoundsAndGo(mapBounds.getXol(), mapBounds.getYol(), mapBounds.getXur(), mapBounds.getYur());
        }
    }

    this.showPoi = function(pIdAdr) {
        //console.info("show poi");
        self.iconFlashStop();
        self.setBack();

        new Ajax.Request("/mallorca/web/ajax/ajaxGetter.php", {
            method: "post",
            asynchronous: true,
            parameters: "&idAdr=" + pIdAdr.replace(/,/, ""),

            onFailure: function(response) {
                alert(response.responseText);
            },

            onSuccess: function(response) {
                //alert(response.responseText);
                var ajaxResponse = response.responseText.split("\|");

                if ($("divContent2_content")) {
                    $("divContent2_content").update("");
                }

                for (var i = 0; i < ajaxResponse.length - 1; i += 2) {
                    if (ajaxResponse[i] != "layer_zwei_mitte_content_xxx") {
                        //alert(i + " - " + ajaxResponse[i] + " - " + $(ajaxResponse[i]));
                        $(ajaxResponse[i]).update(ajaxResponse[i + 1]);
                    } else {
                        //alert(ajaxResponse[i+1]);
                    }
                }

                split('|', ' ', '|', ' ', ' ', ' ', ' ', ' ');

//                var div = document.getElementById('divContent0');
//                div.innerHTML = "";

                //if(slider != null) {
                //  slider.toggle("layer_eins");
                //}
            }
        });
    }

    this.removeHomeIcon = function() {
        if (homeIcon != null) {
            homeIcon.remove();
            homeIcon = null;
        }
    }

    this.removeHome = function() {
        homeIdAdr = "";
        homeIdCat = "";
        homeChecked = false;
        homePermanent = false;
        self.removeHomeIcon();

        $(divHome).update("Machen Sie eine<br />Adresse oder eine<br />Straße zu Ihrem<br />Ausgangspunkt.<br /><img src='/mallorca/web/img/icons/icon-home.gif' title='Ausgangspunkt' /><br />Halten Sie die<br />Maus über ein <br />Icon in der Karte<br />und klicken Sie<br />im sich öffnenden<br />Info-Fenster auf<br /><nobr>'Mein Ausgangspunkt'</nobr>");

        self.showIcons("");
    }

    this.updateHome = function(pUpdateContentList) {
        if (homeChecked) {
            new Ajax.Request("/mallorca/web/ajax/ajaxHome.php", {
                method: "post",
                asynchronous: true,
                parameters: "&idAddress=" + homeIdAdr + "&idCategory=" + homeIdCat,

                onFailure: function(response) {
                    alert(response.responseText);
                },

                onSuccess: function(response) {
                    //console.info(response.responseText);
                    if (response.responseText.length > 0) {
                        self.removeHomeIcon();

                        //$(divHome).update(response.responseText.replace(/\|/g, "<br />"));
                        var ajaxResponse = response.responseText.split("\|");
                        homeIcon = new Icon();
                        homeIcon.append(divContent, mapInfo, tilesInfo, ajaxResponse[0], ajaxResponse[1], ajaxResponse[2], ajaxResponse[3], ajaxResponse[4], ajaxResponse[5], "/mallorca/web/img/icons/icon-home.gif", ajaxResponse[6], ajaxResponse[7], new Rectangle(parseInt(ajaxResponse[8]), parseInt(ajaxResponse[9]), parseInt(ajaxResponse[10]), parseInt(ajaxResponse[11])));
                        //alert(homeIcon);
                        homeIcon.setText(ajaxResponse[12]);
                        $(divHome).update(homeIcon.getText());
                        if (pUpdateContentList) {
                            self.updateContentList(); // km aktualisieren
                        }
                    }
                }
            });
        }
    }

    this.setHomeVisible = function(pVisible) {
        if (homeIcon != null) {
            homeIcon.setVisible(pVisible);
        }
    }

    this.setHome = function(pIdAdr, pIdCat, pChecked, pPermanent) {
        //alert(pIdAdr + " " + pIdCat + " " + pChecked + " " + pPermanent);
        if (!homePermanent || pPermanent) {
            homeIdAdr = (pIdAdr + "").replace(/,/, "");
            homeIdCat = (pIdCat + "").replace(/,/, "");
            homeChecked = pChecked;
            homePermanent = pPermanent;
        }

        if (homeChecked) {
            self.updateHome(true);
        } else {
            self.removeHome();
        }
    }

    this.goHome = function(pX, pY) {
        //alert("goHome " + pX + " " + pY);
        moveManager.setXY(pX, pY);
        if (homeIcon != null) {
            homeIcon.setIconPosition(mapInfo, tilesInfo, pX, pY)
        }
    }

    this.setBack = function() {
        contentListIdxBackBack = contentListIdx;
        moveManager.setBack();
        contentListIdxBack = 0;
    }

    this.getBack = function() {
        contentListIdxBack = contentListIdxBackBack;
        moveManager.getBack(); // Löst Aktualisierung der Contentliste aus
    }

    this.setAdrCatHome = function(pAdr, pCat, pHome) {
        //    alert(pAdr + " - " + pCat + " - " + pHome);
        var homeArray = pHome.split("\|");
        homeIdAdr = (homeArray[0] + "").replace(/,/, "");

        new Ajax.Request("/mallorca/web/ajax/ajaxAddresses.php", {
            method: "post",
            asynchronous: true,
            parameters: "&addresses=" + pAdr + "&home=" + homeIdAdr + "," + "&adrCounter=-1",

            onFailure: function(response) {
                alert(response.responseText);
            },

            onSuccess: function(response) {
                var viewPort;
                var xol;
                var yol;
                var xur;
                var yur;

                if (response.responseText.length > 0) {
                    adrAray = response.responseText.split("\|");
                    if (adrAray.length >= 13) {
                        xol = parseInt(adrAray[9]);
                        yol = parseInt(adrAray[10]);
                        xur = parseInt(adrAray[11]);
                        yur = parseInt(adrAray[12]);
                        //alert(xol + " - " + yol + " - " + xur + " - " + yur);

                        moveManager.setForBounds(xol, yol, xur, yur);

                        viewPort = moveManager.getViewport();
                        xol = viewPort.getXol();
                        yol = viewPort.getYol();
                        xur = viewPort.getXur();
                        yur = viewPort.getYur();
                        //alert(xol + " - " + yol + " - " + xur + " - " + yur);

                        new Ajax.Request("/mallorca/web/ajax/ajaxAdjustCategories.php", {
                            method: "post",
                            asynchronous: true,
                            parameters: "&categories=" + pCat + "&xol=" + xol + "&yol=" + yol + "&xur=" + xur + "&yur=" + yur,

                            onFailure: function(response) {
                                alert(response.responseText);
                            },

                            onSuccess: function(response) {
                                //alert(response.responseText);
                                if (response.responseText.length > 0) {
                                    adjustCatArray = response.responseText.split("\|");
                                    xol = parseInt(adjustCatArray[0]);
                                    yol = parseInt(adjustCatArray[1]);
                                    xur = parseInt(adjustCatArray[2]);
                                    yur = parseInt(adjustCatArray[3]);
                                    //alert(xol + " - " + yol + " - " + xur + " - " + yur);

                                    moveManager.setForBounds(xol, yol, xur, yur);
                                    moveManager.set(mapInfo.getX(), mapInfo.getY(), tilesInfo.getScale(), tilesInfo.getType());

                                    adrRequest = pAdr;
                                    catRequest = pCat;
                                    catRequestOld = "";
                                    var homeArray = pHome.split("\|");
                                    homeIdAdr = (homeArray[0] + "").replace(/,/, "");
                                    homeIdCat = (homeArray[1] + "").replace(/,/, "");
                                    homeChecked = homeArray[2];
                                    homePermanent = homeArray[3];
                                    //homeRequest = homeIdAdr + ",";
                                    //alert(adrRequest + " - " + catRequest + " - " + homeIdAdr + " - " + homeIdCat + " - " + homeChecked + " - " + homePermanent);

                                    showPoiFlag = true;
                                    self.updateHome(false)
                                    self.showIcons();
                                }
                            }
                        });
                    }
                }
            }
        });
    }

    //
    this.setAdrCat = function(pAdr, pCat, pHome) {
        //            alert(pAdr + " - " + pCat + " - " + pHome);
        var homeArray = pHome.split("\|");
        homeIdAdr = (homeArray[0] + "").replace(/,/, "");

        new Ajax.Request("/mallorca/web/ajax/ajaxAddresses.php", {
            method: "post",
            asynchronous: true,
            parameters: "&addresses=" + pAdr + "&home=" + homeIdAdr + "," + "&adrCounter=-1",

            onFailure: function(response) {
                alert(response.responseText);
            },

            onSuccess: function(response) {
                var viewPort;
                var xol;
                var yol;
                var xur;
                var yur;

                if (response.responseText.length > 0) {
                    adrAray = response.responseText.split("\|");
                    if (adrAray.length >= 13) {
                        xol = parseInt(adrAray[9]);
                        yol = parseInt(adrAray[10]);
                        xur = parseInt(adrAray[11]);
                        yur = parseInt(adrAray[12]);
                        //                        alert(xol + " - " + yol + " - " + xur + " - " + yur);

                        moveManager.setForBounds(xol, yol, xur, yur);

                        viewPort = moveManager.getViewport();
                        xol = viewPort.getXol();
                        yol = viewPort.getYol();
                        xur = viewPort.getXur();
                        yur = viewPort.getYur();
                        //                        alert(xol + " - " + yol + " - " + xur + " - " + yur);

                        new Ajax.Request("/mallorca/web/ajax/ajaxAdjustCategories.php", {
                            method: "post",
                            asynchronous: true,
                            parameters: "&categories=" + pCat + ",&xol=" + xol + "&yol=" + yol + "&xur=" + xur + "&yur=" + yur,

                            onFailure: function(response) {
                                alert(response.responseText);
                            },

                            onSuccess: function(response) {
                                //alert(response.responseText);
                                if (response.responseText.length > 0) {
                                    adjustCatArray = response.responseText.split("\|");
                                    xol = parseInt(adjustCatArray[0]);
                                    yol = parseInt(adjustCatArray[1]);
                                    xur = parseInt(adjustCatArray[2]);
                                    yur = parseInt(adjustCatArray[3]);
                                    //                                    alert(xol + " - " + yol + " - " + xur + " - " + yur);

                                    moveManager.setForBounds(xol, yol, xur, yur);
                                    moveManager.set(mapInfo.getX(), mapInfo.getY(), tilesInfo.getScale(), tilesInfo.getType());

                                    adrRequest = pAdr;
                                    catRequest = pCat;
                                    catRequestOld = "";
                                    var homeArray = pHome.split("\|");
                                    homeIdAdr = (homeArray[0] + "").replace(/,/, "");
                                    homeIdCat = (homeArray[1] + "").replace(/,/, "");
                                    homeChecked = homeArray[2];
                                    homePermanent = homeArray[3];
                                    //homeRequest = homeIdAdr + ",";
                                    //alert(adrRequest + " - " + catRequest + " - " + homeIdAdr + " - " + homeIdCat + " - " + homeChecked + " - " + homePermanent);

                                    // TODO show poi flag functionality (before changing: true)
                                    showPoiFlag = false;
                                    self.updateHome(false)
                                    self.showIcons();
                                }
                            }
                        });
                    }
                }
            }
        });
    }

    //---------------------------------------------------------------------------

    this.remove = function(pArray, pIdx) {
        pArray[pIdx].remove();
        pArray.splice(pIdx, 1);
    }

    this.removeAllAdr = function() {
        while (iconAdrArray.length > 0) {
            self.remove(iconAdrArray, 0);
        }
    }

    this.removeAllCat = function() {
        while (iconCatArray.length > 0) {
            self.remove(iconCatArray, 0);
        }
    }

    this.removeAllNews = function() {
        while (iconNewsArray.length > 0) {
            self.remove(iconNewsArray, 0);
        }
    }

    this.removeAll = function() {
        self.removeAllAdr();
        self.removeAllCat();
        self.removeAllNews();
    }

    this.preUpdate = function(pDir) {
        if (pDir == "I") {
            self.removeAll();
        }
    }

    this.update = function(pDir) {
        var icon;
        //                        console.info("update - " + pDir);
        self.showIcons(pDir);
    }

    this.moveBy = function(pX, pY) {
        if (homeIcon != null) {
            homeIcon.moveBy(pX, pY);
        }
        for (var i = 0; i < iconAdrArray.length; i++) {
            iconAdrArray[i].moveBy(pX, pY);
        }
        for (var i = 0; i < iconCatArray.length; i++) {
            iconCatArray[i].moveBy(pX, pY);
        }
        for (var i = 0; i < iconNewsArray.length; i++) {
            iconNewsArray[i].moveBy(pX, pY);
        }

        toolTipManagerInfo.toolTipStopAndHide();
        toolTipManagerCluster.toolTipStopAndHide();
    }

    this.tryToFront = function(pIconArray, pSelIcon) {
        var lenPre;
        var lenPost;
        var iconArray = null;

        lenPre = pIconArray.length;
        pIconArray = $(pIconArray).without(pSelIcon);
        lenPost = pIconArray.length;
        if (lenPre > lenPost) {
            pIconArray.push(pSelIcon);
            iconArray = pIconArray;
        }
        return iconArray;
    }

    this.toFront = function(pSelIcon) {
        if (pSelIcon != null) {
            var iconArray;

            $(pSelIcon.getImage()).remove();
            $(divContent).appendChild(pSelIcon.getImage());

            iconArray = self.tryToFront(iconCatArray, pSelIcon);
            if (iconArray != null) {
                iconCatArray = iconArray;
            } else {
                iconArray = self.tryToFront(iconAdrArray, pSelIcon);
                if (iconArray != null) {
                    iconAdrArray = iconArray;
                } else {
                    iconArray = self.tryToFront(iconNewsArray, pSelIcon);
                    if (iconArray != null) {
                        iconNewsArray = iconArray;
                    }
                }
            }
        }
    }

    this.iconForMapPoint = function(pX, pY) {
        var iconArray = new Array();
        var icon = null;

        for (var i = iconCatArray.length - 1; i >= 0 && icon == null; i--) {
            if (iconCatArray[i].isSelected(pX, pY)) {
                icon = iconCatArray[i];
            }
        }

        for (var i = iconAdrArray.length - 1; i >= 0 && icon == null; i--) {
            if (iconAdrArray[i].isSelected(pX, pY)) {
                icon = iconAdrArray[i];
            }
        }

        for (var i = iconNewsArray.length - 1; i >= 0 && icon == null; i--) {
            if (iconNewsArray[i].isSelected(pX, pY)) {
                icon = iconNewsArray[i];
            }
        }

        /*for(var i=iconAdrArray.length-1 ; i>=0 ; i--) {
         iconArray.push(iconAdrArray[i]);
         }

         for(var i=iconCatArray.length-1 ; i>=0 ; i--) {
         iconArray.push(iconCatArray[i]);
         }

         for(var i=iconArray.length-1 ; i>=0 && icon==null ; i--) {
         if(iconArray[i].isSelected(pX, pY)) {
         icon = iconArray[i];
         }
         }*/

        return icon;
    }

    this.iconForAdr = function(pIdAdr) {
        var icon = null;

        if (homeIcon != null && homeIcon.getIdAddress() == pIdAdr) {
            icon = homeIcon;
        }

        for (var i = iconCatArray.length - 1; i >= 0 && icon == null; i--) {
            if (iconCatArray[i].getIdAddress() == pIdAdr) {
                icon = iconCatArray[i];
            }
        }
        for (var i = iconAdrArray.length - 1; i >= 0 && icon == null; i--) {
            if (iconAdrArray[i].getIdAddress() == pIdAdr) {
                icon = iconAdrArray[i];
            }
        }
        for (var i = iconNewsArray.length - 1; i >= 0 && icon == null; i--) {
            if (iconNewsArray[i].getIdAddress() == pIdAdr) {
                icon = iconNewsArray[i];
            }
        }

        return icon;
    }

    //---------------------------------------------------------------------------

    this.showLineFromHomeTo = function(pIcon) {
        if (pIcon != null && homeIcon != null) {
            var viewPort = moveManager.getViewport();
            var xol = viewPort.getXol();
            var yol = viewPort.getYol();
            var xur = viewPort.getXur();
            var yur = viewPort.getYur();

            var x1 = Math.round((homeIcon.getMapPosition().getX() - xol) * mapInfo.getWidth() / (xur - xol));
            var y1 = Math.round((homeIcon.getMapPosition().getY() - yol) * mapInfo.getHeight() / (yur - yol));
            //console.info(x1 + " " + y1);

            var x2 = Math.round((pIcon.getMapPosition().getX() - xol) * mapInfo.getWidth() / (xur - xol));
            var y2 = Math.round((pIcon.getMapPosition().getY() - yol) * mapInfo.getHeight() / (yur - yol));
            //console.info(x2 + " " + y2);

            //self.toFront(homeIcon);

            if ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) > 1000) {
                var canvas = document.getElementById("canvas");
                var ctx = canvas.getContext("2d");
                ctx.strokeStyle = "rgba(200, 0, 0, 0.40)";
                //ctx.fillStyle = "rgba(200, 0, 0, 0.40)";
                ctx.beginPath();
                ctx.moveTo(x1, y1);
                ctx.lineTo(x2, y2);
                ctx.lineWidth = 5;
                //ctx.lineCap = "round";
                ctx.closePath();
                ctx.stroke();
            }

            $("canvas").setStyle("z-index: 1;");
        }
    }

    this.hideLineFromHomeTo = function() {
        var canvas = document.getElementById("canvas");
        var ctx = canvas.getContext("2d");
        ctx.clearRect(0, 0, canvas.width, canvas.height);

        $("canvas").setStyle("z-index: 0;");
    }

    this.iconFlash = function() {
        flashState = !flashState;
        flashIcon.flash(flashState);
    }

    this.iconFlashStart = function(pIdAdr) {
        if (!(peFlash instanceof PeriodicalExecuter)) {
            flashIcon = self.iconForAdr(pIdAdr);            
            if (flashIcon != null) {
                if (flashIcon.getClusterNr() != 0) {                
                    flashIcon.setSrc("/mallorca/web/img/kategorien/" + flashIcon.getIconUrl());                    
                }

                self.toFront(flashIcon);

                peFlash = new PeriodicalExecuter(function() {
                    self.iconFlash();
                }, 0.25);

                if (homeIcon != null) {
                    self.showLineFromHomeTo(flashIcon);
                }
            }
        }

        return flashIcon;
    }

    this.iconFlashStop = function() {
        if (peFlash instanceof PeriodicalExecuter) {
            peFlash.stop();
        }
        peFlash = null;

        flashState = false;

        if (flashIcon != null) {
            if (flashIcon.getClusterNr() != 0) {
                flashIcon.setSrc("/mallorca/web/img/icons/icon-cluster.gif");
            }

            flashIcon.flash(flashState);
            flashIcon = null;

            if (homeIcon != null) {
                self.hideLineFromHomeTo();
            }
        }
    }

    //---------------------------------------------------------------------------

    this.ttInfoFkt = function(pDiv, pMouseXY) {        
        var mouseMapPosition = moveManager.mouseMapPosition(pMouseXY);
        var idHome = "";

        self.displayContentList();

        if (homeIcon != null && homePermanent) {
            idHome = homeIcon.getIdAddress();
            idHome = idHome.replace(/,/, "");
        }        
        //console.info("ttInfoFkt " + activeIcon.getIdMaster() + " " + activeIcon.getIdAddress() + " " + activeIcon.getIdCategory());        
        new Ajax.Request("/mallorca/web/ajax/ajaxToolTipInfo.php", {
            method: "post",
            asynchronous: true,
            parameters: "&cnt=" + activeIcon.getCnt() + "&idMaster=" + activeIcon.getIdMaster() + "&idAddress=" + activeIcon.getIdAddress() + "&idCategory=" + activeIcon.getIdCategory() + "&idNews=" + activeIcon.getIdNews() + "&idHome=" + idHome + "&ttCounter=" + (++ttCounter),

            onFailure: function(response) {
                alert(response.responseText);
            },

            onSuccess: function(response) {
                var ajaxResponse = response.responseText.split("\|");
                if (ajaxResponse[0] == ttCounter) {
                    if (activeIcon.getClusterNr() == 0) {
                        self.highlightContentText(activeIcon.getIdAddress());
                    }
                    if (ajaxResponse[1] != "-") {
                        if (activeIcon.getIdCategory() == 86000) {
                            var text = "<img src='" + tileLocation + "/slb/" + (parseInt(activeIcon.getIdAddress()) - 5951) + ".jpg' width='600' height='400' />";
                            text += "<center>NAME " + (parseInt(activeIcon.getIdAddress()) - 5951) + "</center>";
                            toolTipManagerInfo.toolTipShow(text, new Position(pMouseXY.getX() - curPosition.getX(), pMouseXY.getY() - curPosition.getY())); //++
                        } else {
                            toolTipManagerInfo.toolTipShow(ajaxResponse[1], new Position(pMouseXY.getX() - curPosition.getX(), pMouseXY.getY() - curPosition.getY())); //++
                        }
                    } else {
                        clusterIdx = -1;
                    }
                }
            }
        });
    }

    this.ttClusterFkt = function(pDiv, pMouseXY) {
        var text = "";

        // Cluster-Icon auflösen
        clusterArray = new Array();
        for (var i = 0; i < contentListArray.length; i++) {
            if (contentListArray[i].getClusterNr() == activeIcon.getClusterNr()) {
                clusterArray.push(contentListArray[i]);
            }
        }
        for (var i = 0; i < clusterArray.length; i++) {
            text += "<img src='/mallorca/web/img/kategorien/" + clusterArray[i].getIconUrl() + "' style='border: 0px solid' width='20' height='31' onclick=\"map.centerIconBounds('" + clusterArray[i].getIdAddress() + "');\" />";
        }

        //text += "<br />CLUSTER-NR " + activeIcon.getClusterNr();

        clusterMouseXY = new Position(pMouseXY.getX(), pMouseXY.getY());

        toolTipManagerCluster.toolTipShow(text, new Position(pMouseXY.getX() - curPosition.getX(), pMouseXY.getY() - curPosition.getY() + 34)); //++

        if (clusterEvent != null) {
            clusterIdx = -1;
            self.ttClusterMouseMoveFkt(pDiv, clusterEvent);
        }
    }

    this.ttInfoHiddenFkt = function() {
        //console.info("ttInfoHiddenFkt");
        if (toolTipManagerCluster.isToolTipShown()) {
            toolTipManagerCluster.toolTipHideStop();
            toolTipManagerCluster.setToolTipHideDelay(0.25);
            toolTipManagerCluster.toolTipStopAndHideDelayed();
        }
    }

    this.ttInfoMouseMoveFkt = function() {
        moveManager.toolTipStopAndHide();
        toolTipManagerCluster.toolTipHideStop();
        toolTipManagerInfo.setToolTipHideDelay(0.25);
        self.highlightContentText(activeIcon.getIdAddress());
        clusterIdx = -1;
    }

    this.ttInfoMapMoveDoneFkt = function(pMoved) {
        if (activeIcon.getClusterNr() != 0) {
            toolTipManagerCluster.toolTipStopAndHide();
            self.ttClusterFkt(divContent, new Position(clusterMouseXY.getX(), clusterMouseXY.getY() + pMoved));
        }
        self.updateContentList();
    }

    this.ttClusterHiddenFkt = function() {
        //console.info("ttClusterHiddenFkt");
        toolTipManagerInfo.toolTipHideStop();
        toolTipManagerInfo.setToolTipHideDelay(0.25);
        toolTipManagerInfo.toolTipStopAndHideDelayed();
        self.highlightContentText("");
        clusterIdx = -1;
    }

    this.ttClusterMouseMoveFkt = function(pDiv, pEvent) {
        var mousePosition = mouseManager.mousePosition(pEvent);
        var bounds = toolTipManagerCluster.getToolTipBounds();
        var x = mousePosition.getX() - $(pDiv).cumulativeOffset().left - bounds.getXol();
        var y = mousePosition.getY() - $(pDiv).cumulativeOffset().top - bounds.getYol();
        var idx = Math.min(clusterArray.length - 1, Math.max(0, Math.floor(x / (bounds.getXur() - bounds.getXol()) * clusterArray.length)));
        //console.info(x + " " + y + " " + clusterArray.length + " " + (bounds.getXur() - bounds.getXol()) + " " + idx);
        //console.info(clusterIdx + " " + idx);

        moveManager.toolTipStopAndHide();
        if (clusterIdx != idx) {
            clusterIdx = idx;

            activeIcon = clusterArray[idx];
            //console.info("ttClusterMouseMoveFkt " + activeIcon.getIdMaster() + " " + activeIcon.getIdAddress() + " " + activeIcon.getIdCategory());

            toolTipManagerInfo.toolTipStopAndHide();
            toolTipManagerInfo.setToolTipHideDelay(10.0);

            //self.ttInfoFkt(pDiv, clusterMouseXY);
            //toolTipManagerInfo.toolTipShowDelayed(clusterMouseXY);
            toolTipManagerInfo.toolTipShowDelayed(new Position(mousePosition.getX(), clusterMouseXY.getY()));
        }

        if (clusterArray && clusterArray[idx]) {
            self.highlightContentText(clusterArray[idx].getIdAddress());
        }

        toolTipManagerCluster.toolTipHideStop();
        //console.info("toolTipManagerCluster.toolTipHideStop");
    }

    this.ttClusterMapMoveDoneFkt = function(pMoved) {
        self.highlightContentText("");
        clusterIdx = -1;
    }

    this.downFkt = function(div, event, pMouseXY) {
        if (!toolTipManagerInfo.isMouseInside()) {
            //toolTipManagerInfo.toolTipStopAndHide();
        }
    }

    this.moveFkt = function(div, event, pMouseXYnow, pMouseXY, pMouseDown) {
        var x = pMouseXYnow.getX() - $(divContent).cumulativeOffset().left;
        var y = pMouseXYnow.getY() - $(divContent).cumulativeOffset().top;
        var iconForMapPoint = self.iconForMapPoint(x, y);

        //console.info("moveFkt - " + div + " " + pMouseXYnow + " " + pMouseXY + " " + pMouseDown)
        //console.info(x + " " + y);
        //console.info("moveFkt " + toolTipManagerCluster.isToolTipShown());
        if (iconForMapPoint != null) {
            moveManager.toolTipStopAndHide();
            mouseManager.setClickFkt(self.clickFkt);

            if (!self.isToolTipShown()) {
                if (selectedIcon != iconForMapPoint) {
                    selectedIcon = iconForMapPoint;

                    self.toFront(selectedIcon);

                    // Daten des selektierten Icons
                    activeIcon = selectedIcon;
                    curPosition = selectedIcon.getCurPosition();

                    toolTipManagerInfo.toolTipStopAndHide();
                    toolTipManagerCluster.toolTipStopAndHide();

                    if (activeIcon.getClusterNr() == 0) {
                        toolTipManagerInfo.setToolTipHideDelay(10.0);
                        toolTipManagerInfo.toolTipShowDelayed(pMouseXYnow);

                    } else {
                        clusterEvent = event;
                        toolTipManagerCluster.setToolTipHideDelay(10.0);
                        toolTipManagerCluster.toolTipShowDelayed(pMouseXYnow);
                    }

                    if (Math.abs(pMouseXYnow.getX() - pMouseXY.getX()) < 2 && Math.abs(pMouseXYnow.getY() - pMouseXY.getY()) < 2) {
                        if (activeIcon.getClusterNr() == 0) {
                            self.highlightContentText(activeIcon.getIdAddress());
                        }
                    }
                }
            }
        } else {
            if (selectedIcon != iconForMapPoint) {
                mouseManager.setClickFkt(null);

                if (activeIcon.getClusterNr() == 0) {
                    toolTipManagerInfo.setToolTipHideDelay(0.25);
                    toolTipManagerInfo.toolTipStopAndHideDelayed();
                }

                toolTipManagerCluster.setToolTipHideDelay(0.25);
                toolTipManagerCluster.toolTipStopAndHideDelayed();
                //console.info("toolTipManagerCluster.toolTipStopAndHideDelayed");

                self.highlightContentText("");
            }

            selectedIcon = null;
        }
    }

    this.upFkt = function(div, event, pMouseXY) {
        //console.info("upFkt - " + div + " " + pMouseXY)
        if (!toolTipManagerInfo.isMouseInside()) {
            //toolTipManagerInfo.toolTipStopAndHide();
        }
    }

    this.clickFkt = function(div, event, pMouseXY, pClickCount) {
        var x = pMouseXY.getX() - $(divContent).cumulativeOffset().left;
        var y = pMouseXY.getY() - $(divContent).cumulativeOffset().top;
        var iconForMapPoint = self.iconForMapPoint(x, y);

        //alert("clickFkt - " + div + " " + pMouseXY + " " + pClickCount);
        //console.info("clickFkt - " + div + " " + pMouseXY + " " + pClickCount)
        //console.info("clickFkt " + toolTipManagerCluster.isToolTipShown());
        // Click auf ICON's hinter ToolTip unterdrücken
        if (pClickCount == 1 && !toolTipManagerInfo.isMouseInside()) {
            //console.info(iconForMapPoint);
            if (iconForMapPoint != null/* && iconForMapPoint.getCnt() <= 1*/) {
                if (!toolTipManagerInfo.isMouseInside()) {
                    toolTipManagerInfo.toolTipStopAndHide();
                }

                var mapBounds = iconForMapPoint.getMapBounds();
                //alert(mapBounds);
                moveManager.setForBoundsAndGo(mapBounds.getXol(), mapBounds.getYol(), mapBounds.getXur(), mapBounds.getYur());
                if (clickCallback != null) {
                    clickCallback(mapInfo.getX(), mapInfo.getY(), tilesInfo.getScale(), tilesInfo.getType());
                }
            }
        }
    }

    this.overFkt = function(div, event, pMouseXY) {
        //toolTipManagerInfo.toolTipStopAndHide();
        //toolTipManagerCluster.toolTipStopAndHide();
    }

    this.outFkt = function(div, event, pMouseXY) {
        toolTipManagerInfo.toolTipStopAndHide();
        toolTipManagerCluster.toolTipStopAndHide();
        //console.info(pMouseXY);
    }

    this.isToolTipShown = function() {
        return toolTipManagerInfo.isToolTipShown() || toolTipManagerCluster.isToolTipShown();
    }

    //---------------------------------------------------------------------------

    this.set = function(pCatRequest, pAdrRequest) {
        catRequest = pCatRequest;
        adrRequest = pAdrRequest;
        self.update("IS");
    }

    this.adjustAndSetCat = function(pCatRequest) {
        //      alert(pCatRequest);
        var categories;

        catRequest = pCatRequest;

        if (catRequest != null && catRequestOld != null && catRequest.length > catRequestOld.length) {
            categories = "";
            var cat = catRequest.split(",");
            var catOld = catRequestOld.split(",");
            for (var i = 0; i < catOld.length; i++) {
                cat = cat.without(catOld[i]);
            }
            for (var i = 0; i < cat.length; i++) {
                categories += cat[i] + ",";
            }
        } else {
            categories = catRequest;
        }
        //console.info(categories);


        if (catRequest != null && catRequest.length > 0) {
            var viewPort = moveManager.getViewport();
            var xol = viewPort.getXol();
            var yol = viewPort.getYol();
            var xur = viewPort.getXur();
            var yur = viewPort.getYur();
            //            console.info("old");
            //            console.info("xol = " + xol);
            //            console.info("yol = " + yol);
            //            console.info("xur = " + xur);
            //            console.info("yur = " + yur);

            new Ajax.Request("/mallorca/web/ajax/ajaxAdjustCategories.php", {
                method: "post",
                asynchronous: true,
                parameters: "&categories=" + categories + "&xol=" + xol + "&yol=" + yol + "&xur=" + xur + "&yur=" + yur,

                onFailure: function(response) {
                    alert(response.responseText);
                },

                onSuccess: function(response) {
                    //console.info(response.responseText);
                    if (response.responseText.length > 0) {
                        var ajaxResponse = response.responseText.split("\|");
                        var xolA = parseInt(ajaxResponse[0]);
                        var yolA = parseInt(ajaxResponse[1]);
                        var xurA = parseInt(ajaxResponse[2]);
                        var yurA = parseInt(ajaxResponse[3]);
                        newsRequest = null;
                        self.removeAllNews();
                        if (catRequest != null && catRequestOld != null && catRequest.length > catRequestOld.length && (xol != xolA || yol != yolA || xur != xurA || yur != yurA)) {
                            moveManager.setForBoundsAndGo(xolA, yolA, xurA, yurA);
                            //                alert("11111");
                        } else {
                            self.update("IAASC11");
                            //                alert("22222");
                        }
                    }
                    catRequestOld = catRequest;
                }
            });
        } else {
            self.update("IAASC12");
            catRequestOld = catRequest;
        }
    }

    //todo :method should set address, category and check are there at least one category object
    this.adjustAndSetCatAndAddress = function(pCatRequest, pAdrRequest) {
        //      alert(pCatRequest);
        var categories;

        catRequest = pCatRequest;
        adrRequest = pAdrRequest;

        showPoiFlag = false;
        adrSearch1 = true;

        if (catRequest != null && catRequestOld != null && catRequest.length > catRequestOld.length) {
            categories = "";
            var cat = catRequest.split(",");
            var catOld = catRequestOld.split(",");
            for (var i = 0; i < catOld.length; i++) {
                cat = cat.without(catOld[i]);
            }
            for (var i = 0; i < cat.length; i++) {
                categories += cat[i] + ",";
            }
        } else {
            categories = catRequest;
        }
        //        console.info(categories);

        if (catRequest != null && catRequest.length > 0) {
            var viewPort = moveManager.getViewport();
            var xol = viewPort.getXol();
            var yol = viewPort.getYol();
            var xur = viewPort.getXur();
            var yur = viewPort.getYur();
            //            console.info("new");
            //            console.info("xol = " + xol);
            //            console.info("yol = " + yol);
            //            console.info("xur = " + xur);
            //            console.info("yur = " + yur);
            //

            new Ajax.Request("/mallorca/web/ajax/ajaxAdjustCategories.php", {
                method: "post",
                asynchronous: true,
                parameters: "&categories=" + categories + "&xol=" + xol + "&yol=" + yol + "&xur=" + xur + "&yur=" + yur,

                onFailure: function(response) {
                    alert(response.responseText);
                },

                onSuccess: function(response) {
                    //                    console.info(response.responseText);
                    if (response.responseText.length > 0) {
                        var ajaxResponse = response.responseText.split("\|");
                        //                        var ajaxResponse = "200720|178392|213660|216380".split("\|");
                        var xolA = parseInt(ajaxResponse[0]);
                        var yolA = parseInt(ajaxResponse[1]);
                        var xurA = parseInt(ajaxResponse[2]);
                        var yurA = parseInt(ajaxResponse[3]);
                        if (catRequest != null && catRequestOld != null && catRequest.length > catRequestOld.length && (xol != xolA || yol != yolA || xur != xurA || yur != yurA)) {
                            //                            console.info("11111");
                            moveManager.setForBoundsAndGo(xolA, yolA, xurA, yurA);
                        } else {
                            //                            console.info("22222");
                            moveManager.setForBoundsAndGo(xolA, yolA, xurA, yurA);
                            //                            self.update("IAASC11");
                        }
                    }
                    catRequestOld = catRequest;
                }
            });
        } else {
            self.update("IAASC12");
            catRequestOld = catRequest;
        }
    }

    this.setCatPage = function(pCatRequest) {
        //alert(tilesInfo.getXolMin() + " " + tilesInfo.getYolMin() + " " + tilesInfo.getXurMax(); + " " + tilesInfo.getYurMax(););
        newsRequest = null;
        moveManager.setForBoundsAndGo(tilesInfo.getXolMin(), tilesInfo.getYolMin(), tilesInfo.getXurMax(), tilesInfo.getYurMax());
        self.setCat(pCatRequest);
    }

    this.setNewsPage = function(pNewsRequest) {
        //moveManager.setForBoundsAndGo(tilesInfo.getXolMin(), tilesInfo.getYolMin(), tilesInfo.getXurMax(), tilesInfo.getYurMax());
        self.setNews(pNewsRequest);
    }

    this.setCat = function(pCatRequest) {
        catRequest = pCatRequest;

        if ($("divContent2_content")) {
            $("divContent2_content").update("");
        }
        split('|', ' ', ' ', ' ', ' ', ' ', ' ', ' ');

        self.update("ISC");
    }

    this.getCat = function() {
        return catRequest;
    }

    this.setNews = function(pNewsRequest) {
        newsRequest = pNewsRequest;
        newsFlag = true;
        self.update("ISC");
    }

    this.getNews = function() {
        return newsRequest;
    }

    this.setAdr = function(pAdrRequest, pShowPoiFlag) {
        adrRequest = pAdrRequest;
        showPoiFlag = pShowPoiFlag;
        adrSearch1 = true;

        if ($("divContent2_content")) {
            $("divContent2_content").update("");
        }
        split('|', ' ', ' ', ' ', ' ', ' ', ' ', ' ');

        self.update("ISA");
    }

    this.getAdr = function() {
        return adrRequest;
    }

    this.setCategoryInAddress = function(pCatRequest, pAdrRequest) {
        //        catRequest = pCatRequest;
        //        adrRequest = pAdrRequest;
        //        showPoiFlag = false;
        //        adrSearch1 = true;

        if ($("divContent2_content")) {
            $("divContent2_content").update("");
        }
        split('|', ' ', ' ', ' ', ' ', ' ', ' ', ' ');

        self.setAdrCat(pAdrRequest, pCatRequest, "");
        //                self.update("ISCA");
        //        self.adjustAndSetCatAndAddress(pCatRequest, pAdrRequest);
        //        self.adjustAndSetCat(pCatRequest);
    }

    this.getHome = function() {
        return [
            homeIdAdr,
            homeIdCat,
            homeChecked,
            homePermanent
        ];
    }

    this.setClickCallback = function(pClickCallback) {
        clickCallback = pClickCallback;
    }

    //---------------------------------------------------------------------------

    this.initialize(pContentDiv, pDivContentText, pDivHome, pMapWidth, pMapHeight, pMoveManager);
}

//=============================================================================

function MoveManager(pMapDiv, pMapDivScaleMass, pMoverDiv, pMapWidth, pMapHeight, pTilesWidth, pTilesHeight) {
    var DEFAULT_SCALE = 3;

    var mapDiv;
    var mapDivScaleMass;
    var moverDiv;

    var contentManager;
    var navManager;
    var mouseManager;
    var toolTipManager;

    var ajaxTilesArray;

    var mapInfo;
    var mapInfoBack;
    var mapInfoZoomed;
    var tilesInfo;
    var tilesInfoBack;
    var tilesInfoZoomed;

    var moveByCallback;

    var mapDivScaleMass;
    var imageScaleMass;
    var mapDivMass;
    var mapDivScale;

    var mouseMoved;
    var runsFirstTime;

    var self = this;

    this.initialize = function(pMapDiv, pMapDivScaleMass, pMoverDiv, pMapWidth, pMapHeight, pTilesWidth, pTilesHeight) {
        mapDiv = pMapDiv;
        mapDivScaleMass = pMapDivScaleMass;
        moverDiv = pMoverDiv;

        contentManager = null;
        navManager = null;

        mouseManager = new MouseManager(moverDiv);
        mouseManager.setDownFkt(self.downFkt);
        mouseManager.setMoveFkt(self.moveFkt);
        mouseManager.setUpFkt(self.upFkt);
        mouseManager.setClickFkt(self.clickFkt);
        mouseManager.setOutFkt(self.outFkt);
        mouseManager.setWheelFkt(self.wheelFkt);

        toolTipManager = new ToolTipManager(moverDiv);
        toolTipManager.setToolTipFkt(self.ttFkt);
        toolTipManager.setToolTipStyle("tooltip");

        ajaxTilesArray = new Array();

        mapInfo = new MapInfo();
        mapInfoBack = null;
        mapInfoZoomed = null;
        tilesInfo = new TilesInfo();
        tilesInfoBack = null;
        tilesInfoZoomed = null;

        moveByCallback = null;

        mapInfo.setSize(pMapWidth, pMapHeight);

        self.setDivSize(pMapWidth, pMapHeight);

        tilesInfo.setSize(pTilesWidth, pTilesHeight);

        //-----

        var h = parseInt($(mapDivScaleMass).style.height);

        imageScaleMass = new Image();
        imageScaleMass.src = "/mallorca/img/bg/bg_rechtliches.jpg";
        imageScaleMass.style.position = "relative";
        $(mapDivScaleMass).appendChild(imageScaleMass);

        mapDivMass = new Element("div", { });
        self.initializeDiv(mapDivMass, h, h + 1, "#000");
        $(mapDivScaleMass).appendChild(mapDivMass);

        mapDivScale = new Element("div", { });
        self.initializeDiv(mapDivScale, h, 2 * h - 1, "#fff");
        $(mapDivScaleMass).appendChild(mapDivScale);

        mouseMoved = false;
        runsFirstTime = true;
    }

    this.initializeDiv = function(pDiv, pHeight, pTop, pColor) {
        $(pDiv).style.position = "relative";
        $(pDiv).style.textAlign = "center";
        $(pDiv).style.paddingTop = "4px";
        $(pDiv).style.height = (pHeight - 2 - 4) + "px";
        $(pDiv).style.width = "200px";
        $(pDiv).style.top = (-pTop) + "px";
        $(pDiv).style.color = pColor;
    }

    this.updateScaleMass = function() {
        $(mapDivScale).update(self.getScaleCaption(self.getScale()));
        $(mapDivMass).update(self.getMassCaption(self.getScale()));

        navManager.setSlider(tilesInfo.getScale());
    }

    //---------------------------------------------------------------------------

    this.setDivSize = function(pMapWidth, pMapHeight) {
        $(moverDiv).style.width = pMapWidth + 'px';
        $(moverDiv).style.height = pMapHeight + 'px';
    }

    //---------------------------------------------------------------------------

    this.refresh = function(pSrcFkt) {
        //console.info(pSrcFkt);
        xMapMoved = 0;
        yMapMoved = 0;

        //if(!runsFirstTime) {
        if (contentManager != null) {
            contentManager.preUpdate("I");
        }
        if (navManager != null) {
            navManager.setNavIconByMap(mapInfo.getX(), mapInfo.getY());
        }

        self.removeAll();
        self.update("IR");
        //}
        //runsFirstTime = false;
    }

    this.setMapInfo = function(pXMap, pYMap) {
        mapInfo.setXY(pXMap, pYMap);
    }

    this.setTilesInfo = function(pScale, pType) {
        tilesInfo.setScaleType(pScale, pType);
    }

    this.set = function(pXMap, pYMap, pScale, pType) {
        self.setMapInfo(pXMap, pYMap);
        self.setTilesInfo(pScale, pType);

        self.refresh("moveManager.set()");

        self.updateScaleMass();
        //info1(mapInfo, tilesInfo, "xMapMoved "+xMapMoved, "yMapMoved "+yMapMoved);
    }

    this.setXY = function(pXMap, pYMap) {
        self.setMapInfo(pXMap, pYMap);

        self.refresh("moveManager.setXY()");

        //info1(mapInfo, tilesInfo, "xMapMoved "+xMapMoved, "yMapMoved "+yMapMoved);
    }

    this.setSize = function(pMapWidth, pMapHeight) {
        mapInfo.setSize(pMapWidth, pMapHeight);

        if (contentManager != null) {
            contentManager.updateHome(true);
        }

        self.setDivSize(pMapWidth, pMapHeight);

        self.refresh("moveManager.setSize()");
        //self.moveBy(0, 0);
        //info1(mapInfo, tilesInfo, "xMapMoved "+xMapMoved, "yMapMoved "+yMapMoved);

        $(mapDivScaleMass).style.top = (pMapHeight + parseInt($(pMapDiv).style.top)) + "px";
        $(mapDivScaleMass).style.width = pMapWidth + "px";
        $(imageScaleMass).style.left = (-900 + Math.round(pMapWidth / 2)) + "px";

        $(mapDivMass).style.left = (Math.round(pMapWidth / 2) - 200) + "px";
        $(mapDivScale).style.left = (Math.round(pMapWidth / 2)) + "px";
    }

    this.setScale = function(pScale) {
        tilesInfo.setScale(pScale);
        if (contentManager != null) {
            contentManager.updateHome(true);
        }
        if (navManager != null) {
            navManager.setSlider(tilesInfo.getScale());
        }
        self.refresh("moveManager.setScale()");

        self.updateScaleMass();
    }

    this.setType = function(pType) {
        tilesInfo.setType(pType); // *** calls tilesInfo.adjustScale(); ***
        if (navManager != null) {
            navManager.setSlider(tilesInfo.getScale());
        }

        self.refresh("moveManager.setType()");

        self.updateScaleMass();
    }

    this.setForBounds = function(pXol, pYol, pXur, pYur) {
        var xol = parseInt(pXol);
        var yol = parseInt(pYol);
        var xur = parseInt(pXur);
        var yur = parseInt(pYur);
        var dx = xur - xol;
        var dy = yur - yol;
        var newScale = tilesInfo.getMaxScale();

        if (contentManager != null) {
            contentManager.updateHome(true);
        }
        for (var scale = newScale; scale >= 3; scale--) {
            if (dx <= mapInfo.getWidth() * tilesInfo.getAdjustScaleForScale(scale) &&
                    dy <= mapInfo.getHeight() * tilesInfo.getAdjustScaleForScale(scale)) {
                newScale = scale;
            }
        }

        //alert(xol + " - " + yol + " - " + xur + " - " + yur);

        if (xol < tilesInfo.getXolMin()) xol = tilesInfo.getXolMin();
        if (yol < tilesInfo.getYolMin()) yol = tilesInfo.getYolMin();
        if (xur > tilesInfo.getXurMax()) xur = tilesInfo.getXurMax();
        if (yur > tilesInfo.getYurMax()) yur = tilesInfo.getYurMax();

        //alert(xol + " - " + yol + " - " + xur + " - " + yur);
        //alert(Math.round((xol + xur) / 2) + " - " + Math.round((yol + yur) / 2) + " - " + mapInfo.getX() + " - " + mapInfo.getY());

        self.setMapInfo(Math.round((xol + xur) / 2), Math.round((yol + yur) / 2));
        self.setTilesInfo(newScale, tilesInfo.getType());
    }

    this.setForBoundsAndGo = function(pXol, pYol, pXur, pYur) {
        self.setForBounds(pXol, pYol, pXur, pYur);

        if (contentManager != null) {
            contentManager.updateHome(true);
        }

        self.set(mapInfo.getX(), mapInfo.getY(), tilesInfo.getScale(), tilesInfo.getType());
    }

    this.zoom = function(pScale) {
        //self.refresh(); // Symmetry herstellen
        for (var i = 0; i < ajaxTilesArray.length; i++) {
            ajaxTilesArray[i].zoom(pScale);
        }
    }

    //---------------------------------------------------------------------------

    this.remove = function(pIdx) {
        ajaxTilesArray[pIdx].remove();
        ajaxTilesArray.splice(pIdx, 1);
    }

    this.removeAll = function() {
        while (ajaxTilesArray.length > 0) {
            self.remove(0);
        }
    }

    this.append = function(xMap, yMap) {
        ajaxTiles = new AjaxTiles(this);
        ajaxTiles.append(xMap, yMap);
        ajaxTilesArray.push(ajaxTiles);
    }

    //---------------------------------------------------------------------------

    this.update = function(pDir) {
      //alert("update(" + pDir + ")");
        var scaledContainerRect;
        var scaledMapRect;

        mapInfo.setRectangles(tilesInfo, pDir);

        scaledContainerRect = mapInfo.getScaledContainerRect();

        del = 0;
        for (var i = 0; i < ajaxTilesArray.length;) {
            //console.info(ajaxTilesArray[i].getScalesMapRect());
            if (!scaledContainerRect.touchesRectangle(ajaxTilesArray[i].getScalesMapRect())) {
                this.remove(i);
                del++;
            } else {
                i++;
            }
        }

        scaledMapRect = mapInfo.getScaledMapRect();

        append = 0;
        for (var x = scaledMapRect.getXol(); x < scaledMapRect.getXur(); x += tilesInfo.getWidth()) {
            for (var y = scaledMapRect.getYol(); y < scaledMapRect.getYur(); y += tilesInfo.getHeight()) {
                imageFound = false;
                for (var i = 0; !imageFound && i < ajaxTilesArray.length; i++) {
                    if (x == ajaxTilesArray[i].getScalesMapRect().getXol() && y == ajaxTilesArray[i].getScalesMapRect().getYol()) {
                        imageFound = true;
                    }
                }

                if (!imageFound) {
                    this.append(x, y);
                    append++;
                }
            }
        }

        //console.info("update - " + pDir);
        if (contentManager != null) {
            contentManager.update(pDir);
        }
    }

    this.moveBy = function(pXmove, pYmove) {
        var adjustScale;

        adjustScale = tilesInfo.getAdjustScale();

        if (mapInfo.getX() <= (tilesInfo.getXol() - 1) * tilesInfo.getWidth() * adjustScale && pXmove >= 0) {
            mapInfo.setX(0);
            pXmove = 0;
            self.refresh("moveManager.moveBy()");
        }
        if (mapInfo.getY() <= (tilesInfo.getYol() - 1) * tilesInfo.getHeight() * adjustScale && pYmove >= 0) {
            mapInfo.setY(0);
            pYmove = 0;
            self.refresh("moveManager.moveBy()");
        }
        if (mapInfo.getX() >= tilesInfo.getXur() * tilesInfo.getWidth() * adjustScale && pXmove <= 0) {
            mapInfo.setX(tilesInfo.getXur(mapInfo) * tilesInfo.getWidth() * adjustScale);
            pXmove = 0;
            self.refresh("moveManager.moveBy()");
        }
        if (mapInfo.getY() >= tilesInfo.getYur() * tilesInfo.getHeight() * adjustScale && pYmove <= 0) {
            mapInfo.setY(tilesInfo.getYur(mapInfo) * tilesInfo.getHeight() * adjustScale);
            pYmove = 0;
            self.refresh("moveManager.moveBy()");
        }

        mapInfo.moveBy(pXmove * adjustScale, pYmove * adjustScale);

        for (var i = 0; i < ajaxTilesArray.length; i++) {
            ajaxTilesArray[i].moveBy(pXmove, pYmove);
        }

        if (contentManager != null) {
            contentManager.moveBy(pXmove, pYmove);
        }
        if (navManager != null) {
            navManager.setNavIconByMap(mapInfo.getX(), mapInfo.getY());
        }

        xMapMoved += pXmove;
        if (xMapMoved > tilesInfo.getWidth()) {
            xMapMoved -= tilesInfo.getWidth();
            //xMapMoved = 0;
            self.update("R");
        }
        if (xMapMoved < -tilesInfo.getWidth()) {
            xMapMoved += tilesInfo.getWidth();
            //xMapMoved = 0;
            self.update("L");
        }
        yMapMoved += pYmove;
        if (yMapMoved > tilesInfo.getHeight()) {
            yMapMoved -= tilesInfo.getHeight();
            //yMapMoved = 0;
            self.update("D");
        }
        if (yMapMoved < -tilesInfo.getHeight()) {
            yMapMoved += tilesInfo.getHeight();
            //yMapMoved = 0;
            self.update("U");
        }

        if (moveByCallback != null) {
            moveByCallback(mapInfo.getX(), mapInfo.getY());
        }

        //console.info(mapInfo);
    }

    //---------------------------------------------------------------------------

    this.mousePosition = function(pMouseXY) {
        var x = pMouseXY.getX() - $(moverDiv).cumulativeOffset().left - Math.round(mapInfo.getWidth() / 2);
        var y = pMouseXY.getY() - $(moverDiv).cumulativeOffset().top - Math.round(mapInfo.getHeight() / 2);

        return new Position(x, y);
    }

    this.mouseMapPosition = function(pMouseXY) {
        var mousePosition = self.mousePosition(pMouseXY);

        x = mousePosition.getX() * tilesInfo.getAdjustScale() + mapInfo.getX();
        y = mousePosition.getY() * tilesInfo.getAdjustScale() + mapInfo.getY();

        return new Position(x, y);
    }

    this.setCursorStyle = function(pCursorStyle) {
        $(moverDiv).style.cursor = pCursorStyle;
    }

    this.toolTipStopAndHide = function() {
        toolTipManager.toolTipStopAndHide();
    }

    this.ttFkt = function(pDiv, pMouseXY) {
        var mouseMapPosition = self.mouseMapPosition(pMouseXY);
        new Ajax.Request("/mallorca/web/ajax/ajaxToolTip.php", {
            method: "post",
            asynchronous: false,
            parameters: "&type=street" + "&xMap=" + mouseMapPosition.getX() + "&yMap=" + mouseMapPosition.getY() + "&scale=" + tilesInfo.getScale(),

            onFailure: function(response) {
                alert(response.responseText);
            },

            onSuccess: function(response) {
                toolTipManager.toolTipShow("<nobr>" + response.responseText + "</nobr>", pMouseXY);
            }
        });
    }

    //-----

    this.downFkt = function(div, event, pMouseXY) {
        //console.info("downFkt - " + div + " " + pMouseXY)
        if (navManager != null) {
            navManager.showFk();
        }
        mouseMoved = false;
    }

    this.moveFkt = function(div, event, pMouseXYnow, pMouseXY, pMouseDown) {
        //console.info("moveFkt - " + div + " " + pMouseXYnow + " " + pMouseXY + " " + pMouseDown)

        toolTipManager.toolTipStopAndHide();

        if (pMouseDown) {
            self.setCursorStyle("move");
            self.moveBy(pMouseXYnow.getX() - pMouseXY.getX(), pMouseXYnow.getY() - pMouseXY.getY());
            mouseMoved = true;
        } else {
            if (mouseManager.isMouseOver()) {
                if (contentManager != null) {
                    if (!contentManager.isToolTipShown()) {
                        toolTipManager.toolTipShowDelayed(pMouseXYnow);
                    }
                } else {
                    toolTipManager.toolTipShowDelayed(pMouseXYnow);
                }
            }
        }

        //info1(mapInfo, tilesInfo, "xMapMoved "+xMapMoved, "yMapMoved "+yMapMoved);
        //info2(self.mouseMapPosition(pMouseXYnow));
    }

    this.upFkt = function(div, event, pMouseXY) {
        //console.info("upFkt - " + div + " " + pMouseXY)
        self.setCursorStyle("default");

        if (navManager != null) {
            navManager.hideFk();
        }
        if (mouseMoved) {
            self.updateContentList();
        }
        mouseMoved = false;
    }

    this.clickFkt = function(div, event, pMouseXY, pClickCount) {
        //console.info("clickFkt - " + div + " " + pMouseXY + " " + pClickCount)
        if (pClickCount >= 2) {
            //alert("clickFkt - " + div + " " + self.mousePosition(pMouseXY) + " " + pClickCount)
            if (mapInfoZoomed != null && tilesInfoZoomed != null) {
                self.clickZoomOut()
            } else {
                var mouseMapPosition = self.mouseMapPosition(pMouseXY);
                var mousePosition = self.mousePosition(pMouseXY);
                if (self.getScale() <= 3) {
                    new Ajax.Request("/mallorca/web/ajax/ajaxGeo.php", {
                        method: "get",
                        asynchronous: true,
                        parameters: "&xc=" + mouseMapPosition.getX() + "&yc=" + mouseMapPosition.getY(),

                        onFailure: function(response) {
                            alert(response.responseText);
                        },

                        onSuccess: function(response) {
                            //console.info(response.responseText);
                            ll = response.responseText.split("|");
                            //alert("latitude longitude\n" + ll[0] + " " + ll[1]);
                            alert("x y\n" + mouseMapPosition.getX() + " " + mouseMapPosition.getY() + "\n\nlat lon\n" + ll[0] + " " + ll[1]);
                        }
                    });
                } else {
                    self.clickZoomIn(mouseMapPosition, mousePosition);
                }
            }
        }
    }

    this.outFkt = function(div, event, pMouseXY) {
        toolTipManager.toolTipStopAndHide();
    }

    this.wheelFkt = function(div, event, delta) {
        Event.stop(event); //** 23-02-10

        //console.info("wheelFkt - " + div + " " + delta)
        if (delta > 0) {
            if (self.getScale() < self.getMaxScale()) {
                self.setScale(self.getScale() + 1);
            }
        } else {
            if (self.getScale() > self.getMinScale()) {
                self.setScale(self.getScale() - 1);
            }
        }
    }

    //---------------------------------------------------------------------------

    this.clickZoomIn = function(pMouseMapPosition, pMousePosition) {
        mapInfoZoomed = mapInfo.clone();
        tilesInfoZoomed = tilesInfo.clone();

        self.set(pMouseMapPosition.getX(), pMouseMapPosition.getY(), 3, tilesInfo.getType());
        self.moveBy(pMousePosition.getX(), pMousePosition.getY());
    }

    this.clickZoomOut = function() {
        self.set(mapInfoZoomed.getX(), mapInfoZoomed.getY(), tilesInfoZoomed.getScale(), tilesInfoZoomed.getType());
        self.clickZoomClear();
    }

    this.clickZoomClear = function() {
        mapInfoZoomed = null;
        tilesInfoZoomed = null;
    }

    this.getMoverDiv = function() {
        return moverDiv;
    }

    this.getMapInfo = function() {
        return mapInfo;
    }

    this.getTilesInfo = function() {
        return tilesInfo;
    }

    this.getScale = function() {
        return tilesInfo.getScale();
    }

    this.getScaleCaption = function(pScale) {
        return tilesInfo.getScaleCaption(pScale);
    }

    this.getMass = function(pScale) {
        return tilesInfo.getMass(pScale);
    }

    this.getMassCaption = function(pScale) {
        var mass = self.getMass(pScale);
        var massLen = mass.length;
        if (massLen > 3) {
            mass = mass.substring(0, massLen - 3) + "." + mass.substring(massLen - 3);
        }
        return mass + "m";
    }

    this.getMinScale = function() {
        return tilesInfo.getMinScale();
    }

    this.getMaxScale = function() {
        return tilesInfo.getMaxScale();
    }

    this.getAdjustScaleForScale = function(pScale) {
        return tilesInfo.getAdjustScaleForScale(pScale);
    }

    this.getViewport = function() {
        var width2 = Math.round(mapInfo.getWidth() * tilesInfo.getAdjustScale() / 2);
        var height2 = Math.round(mapInfo.getHeight() * tilesInfo.getAdjustScale() / 2);

        return new Rectangle(Math.round(mapInfo.getX() - width2), Math.round(mapInfo.getY() - height2),
                Math.round(mapInfo.getX() + width2), Math.round(mapInfo.getY() + height2));
    }

    this.setBack = function() {
        mapInfoBack = mapInfo.clone();
        tilesInfoBack = tilesInfo.clone();
    }

    this.getBack = function() {
        if (mapInfoBack != null && tilesInfoBack != null) {
            self.set(mapInfoBack.getX(), mapInfoBack.getY(), tilesInfoBack.getScale(), tilesInfoBack.getType());
            mapInfoBack = null;
            tilesInfoBack = null;
        }
    }

    this.setContentManager = function(pContentManager) {
        contentManager = pContentManager;
    }

    this.getContentManager = function() {
        return contentManager;
    }

    this.setNavManager = function(pNavManager) {
        navManager = pNavManager;
    }

    this.getNavManager = function() {
        return navManager;
    }

    this.updateContentList = function() {
        if (contentManager != null) {
            contentManager.updateContentList();
        }
    }

    this.setMoveByCallback = function(pMoveByCallback) {
        moveByCallback = pMoveByCallback;
    }

    //---------------------------------------------------------------------------

    this.initialize(pMapDiv, pMapDivScaleMass, pMoverDiv, pMapWidth, pMapHeight, pTilesWidth, pTilesHeight);
}

//=============================================================================

function Map(pNavCompass, pNavScale, pNavSlider, pMapDiv, pMapDivScaleMass, pDivContentText, pDivHome, pMapWidth, pMapHeight) {
    var TILES_WIDTH = 200;
    var TILES_HEIGHT = 200;

    var MOVER_DIV = "moverDiv";
    var CONTENT_DIV = "divContent";
    var NAV_DIV = "navDiv";

    var moveManager;
    var contentManager;
    var navManager;

    var xDynamic;
    var yDynamic;

    var self = this;

    this.initializeStyle = function(pDiv) {
        pDiv.style.opacity = "1.0";
        pDiv.style.overflow = "hidden";
        pDiv.style.background = "#fff";
        pDiv.style.position = "relative";
        pDiv.style.border = "0px solid";
        pDiv.style.top = "0px";
        pDiv.style.left = "0px";
        pDiv.style.width = "0px";
        pDiv.style.height = "0px";
    }

    this.initialize = function(pNavCompass, pNavScale, pNavSlider, pMapDiv, pMapDivScaleMass, pDivContentText, pDivHome, pMapWidth, pMapHeight) {
        var divContent = new Element("div", {"id": CONTENT_DIV});
        self.initializeStyle(divContent);
        $(pMapDiv).appendChild(divContent);

        var moverDiv = new Element("div", {"id": MOVER_DIV});
        self.initializeStyle(moverDiv);
        $(divContent).appendChild(moverDiv);

        moveManager = new MoveManager(pMapDiv, pMapDivScaleMass, MOVER_DIV, pMapWidth, pMapWidth, TILES_WIDTH, TILES_HEIGHT);
        contentManager = new ContentManager(CONTENT_DIV, pDivContentText, pDivHome, pMapWidth, pMapHeight, moveManager);
        navManager = new NavManager(pNavCompass, pNavScale, pNavSlider, pMapDiv, pMapWidth, pMapHeight, moveManager);

        xDynamic = 0;
        yDynamic = 0;

        window.onresize = self.dynamicSize;
    }

    this.removeNewsFlags = function() {
        contentManager.removeAllNews();
    }

    this.getSize = function() {
        var myWidth = 0, myHeight = 0;

        if (typeof(window.innerWidth) == 'number') {
            //Non-IE
            myWidth = window.innerWidth;
            myHeight = window.innerHeight;
        } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
            //IE 6+ in 'standards compliant mode'
            myWidth = document.documentElement.clientWidth;
            myHeight = document.documentElement.clientHeight;
        } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
            //IE 4 compatible
            myWidth = document.body.clientWidth;
            myHeight = document.body.clientHeight;
        }
        return [myWidth, myHeight];
    }

    this.getPageSizeWithScroll = function() {
        var xWithScroll = 0, yWithScroll = 0;
        if (window.innerHeight && window.scrollMaxY) {// Firefox
            xWithScroll = window.innerWidth + window.scrollMaxX;
            yWithScroll = window.innerHeight + window.scrollMaxY;
        } else {
            if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
                xWithScroll = document.body.scrollWidth;
                yWithScroll = document.body.scrollHeight;
            } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
                xWithScroll = document.body.offsetWidth;
                yWithScroll = document.body.offsetHeight;
            }
        }
        return [xWithScroll, yWithScroll];
    }

    this.dynamicSize = function() {
        var size = self.getPageSizeWithScroll();
        //alert(size[0] + " " + size[1]);
        //alert($(pMapDiv).cumulativeOffset().left + " " + $(pMapDiv).cumulativeOffset().top);
        var width = size[0] - $(pMapDiv).cumulativeOffset().left + xDynamic;
        var height = size[1] - $(pMapDiv).cumulativeOffset().top + yDynamic;
        //alert(width + " " + height);
        if (width >= 0 && height >= 0) {
            self.setSize(width, height);
        } else {
            //alert("Map.dynamicSize: width=" + width + " - height=" + height);
        }

        return true;
    }

    this.setBack = function() {
        contentManager.setBack();
    }

    this.back = function() {
        contentManager.getBack();
    }

    //---------------------------------------------------------------------------

    this.setMoveManager = function(pXMap, pYMap, pScale, pType) {
        moveManager.set(pXMap, pYMap, pScale, pType, true);
    }

    this.setSize = function(pMapWidth, pMapHeight) {
        //pMapWidth = 550;
        //pMapHeight = 550;
        if (pMapWidth < 0 && pMapHeight < 0) {
            xDynamic = pMapWidth;
            yDynamic = pMapHeight;
            self.dynamicSize();
        } else {
            moveManager.setSize(pMapWidth, pMapHeight);
            contentManager.setSize(pMapWidth, pMapHeight);
            navManager.setSize(pMapWidth, pMapHeight);

            var canvas = document.getElementById("canvas");
            canvas.width = pMapWidth;
            canvas.height = pMapHeight;
        }
    }

    this.setScale = function(pScale) {
        moveManager.setScale(pScale);
    }

    this.setType = function(pType) {
        moveManager.setType(pType);
    }

    this.setForBounds = function(pXol, pYol, pXur, pYur) {
        moveManager.setForBounds(pXol, pYol, pXur, pYur);
    }

    this.setForBoundsAndGo = function(pXol, pYol, pXur, pYur) {
        moveManager.setForBoundsAndGo(pXol, pYol, pXur, pYur);
    }

    this.home = function() {
        moveManager.home();
    }

    this.refresh = function() {
        moveManager.refresh();
    }

    this.setMoveByCallbackMoveManager = function(pMoveByCallback) {
        moveManager.setMoveByCallback(pMoveByCallback);
    }

    //-----

    this.setContentManager = function(pCatRequest, pAdrRequest) {
        //        contentManager.setAdressesClusters(false);
        contentManager.set(pCatRequest, pAdrRequest);
    }

    this.adjustAndSetCatContentManager = function(pCatRequest) {
        //        contentManager.setAdressesClusters(false);
        contentManager.adjustAndSetCat(pCatRequest);
    }

    this.adjustAndSetCatPageContentManager = function(pCatRequest) {
        //        contentManager.setAdressesClusters(false);
        contentManager.setCatPage(pCatRequest);
    }

    this.showNewsFlags = function(pNewsRequest) {
        contentManager.setNewsPage(pNewsRequest);
    }

    this.setAdrContentManager = function(pAdrRequest, pShowPoiFlag) {
        //        contentManager.setAdressesClusters(false);
        contentManager.setAdr(pAdrRequest, pShowPoiFlag);
    }

    //todo: method should set poi list and REMOVE overlapping of icons
    this.setAdrContentManagerAndHideRedundancyIcons = function(pAdrRequest, pShowPoiFlag) {
        //        contentManager.setAdressesClusters(true);
        contentManager.setAdr(pAdrRequest, pShowPoiFlag);
    }

    this.setCategoryInAddress = function(pCatRequest, pAdrRequest) {
        //        contentManager.setAdressesClusters(false);
        contentManager.setCategoryInAddress(pCatRequest, pAdrRequest);
    }

    this.iconFlashStart = function(pIdAdr) {
        var flashIcon;
        var viewPort = moveManager.getViewport();
        var adjustScale = moveManager.getTilesInfo().getAdjustScale();
        var x = viewPort.getXol();
        var y = viewPort.getYol() + 108 * adjustScale;
        var r = 110 * adjustScale;
        flashIcon = contentManager.iconFlashStart(pIdAdr);
        if (flashIcon != null) {
            //console.info(x + " " + y + " " + r);
            //console.info(flashIcon.getMapPosition().getX() + " " + flashIcon.getMapPosition().getY());
            if ((flashIcon.getMapPosition().getX() - x) * (flashIcon.getMapPosition().getX() - x) +
                    (flashIcon.getMapPosition().getY() - y) * (flashIcon.getMapPosition().getY() - y) < r * r ||
                    flashIcon.getMapPosition().getX() - x < 10 * adjustScale && flashIcon.getMapPosition().getY() - y < 240 * adjustScale) {
                navManager.setOpacity(false);
            }
        }
    }

    this.iconFlashStop = function(pIdAdr) {
        contentManager.iconFlashStop(pIdAdr);
        navManager.setOpacity(true);
    }

    this.gotoPage = function(pPage) {
        contentManager.gotoPage(pPage);
    }

    this.centerIconBounds = function(pIdAdr) {
        contentManager.centerIconBounds(pIdAdr);
    }

    this.centerClusterBounds = function(pIdAdr) {
        contentManager.centerClusterBounds(pIdAdr);
    }

    this.showPoi = function(pIdAdr) {
        contentManager.showPoi(pIdAdr);
    }

    this.setHome = function(pIdAdr, pIdCat, pChecked, pPermanent) {
        if (pPermanent) {
            var days = 1;
            var date = new Date();
            date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
            var expires = ";expires=" + date.toGMTString();

            document.cookie = pIdAdr + "|" + pIdCat + "|" + pChecked + "|" + pPermanent + ";path=/;" + expires;
            //alert(document.cookie);
        }
        contentManager.setHome(pIdAdr, pIdCat, pChecked, pPermanent);
    }

    this.goHome = function(pX, pY) {
        contentManager.goHome(pX, pY);
    }

    this.removeHome = function() {
        document.cookie = ";path=/";
        contentManager.removeHome();
    }

    this.setAdrCatHome = function(pAdr, pCat, pHome) {
        contentManager.setAdrCatHome(pAdr, pCat, pHome);
    }

    this.setClickCallbackContentManager = function(pClickCallback) {
        contentManager.setClickCallback(pClickCallback);
    }

    //-----

    this.setNavManager = function() {
        navManager.set();
    }

    //-----

    this.getMoveManagerInfo = function() {
        return [
            moveManager.getMapInfo().getX(),
            moveManager.getMapInfo().getY(),
            moveManager.getTilesInfo().getScale(),
            moveManager.getTilesInfo().getType()
        ];
    }

    this.getContentManagerInfo = function() {
        return [
            contentManager.getAdr(),
            contentManager.getCat(),
            contentManager.getHome()
        ];
    }

    //---------------------------------------------------------------------------

    this.initialize(pNavCompass, pNavScale, pNavSlider, pMapDiv, pMapDivScaleMass, pDivContentText, pDivHome, pMapWidth, pMapHeight);
}

//=============================================================================

function ImageViewer(pDiv) {
    var div;
    var toolTipDiv;
    var peHide;

    var self = this;

    this.initialize = function(pDiv) {
        div = pDiv;
        toolTipDiv = null;
        peHide = null;
    }

    //---------------------------------------------------------------------------

    this.show = function(src) {
        var image = new Image();

        image.src = src;
        image.onload = function() {
            //self.hide();

            if (toolTipDiv == null) {
                var xp = Math.round(($(div).getWidth() - parseInt($(image).width)) / 2);
                var yp = Math.round(($(div).getHeight() - parseInt($(image).height)) / 2);

                toolTipDiv = new Element("div", { });
                $(toolTipDiv).appendChild(image);
                $(toolTipDiv).setStyle("position: absolute; border-top: 2px solid; border-left: 2px solid; border-right: 2px solid; left: " + xp + "px; top: " + yp + "px; margin: 0px; padding: 0px; background: #000;");
                $(div).appendChild($(toolTipDiv));

                if (peHide == null) {
                    peHide = new PeriodicalExecuter(function() {
                        self.hide();
                    }, 6.0);
                }
            }
        }
    }

    this.hide = function() {
        if (peHide instanceof PeriodicalExecuter) {
            peHide.stop();
        }
        peHide = null;

        if (toolTipDiv != null) {
            $(toolTipDiv).remove();
            toolTipDiv = null;
        }
    }

    //---------------------------------------------------------------------------

    this.initialize(pDiv);
}


