navigation = {  
    minX: 50,
    maxX: 200,
    minY: 50,
    maxY: 200,
    cursor: {x:0, y:0},
    
    TopLeft:function (X,Y) {
//        alert('TopLeft');
        return true;
    },
    Top:function (X,Y) {
//        alert('Top');
        return true;
    },
    TopRight:function (X,Y) {
//        alert('TopRight');
        return true;
    },
    Right:function (X,Y) {
//        alert('Right');
        return true;
    },
    BottomRight:function (X,Y) {
//        alert('BottomRight');
        return true;
    },
    Bottom:function (X,Y) {
//        alert('Bottom');
        return true;
    },
    BottomLeft:function (X,Y) {
//        alert('BottomLeft');
        return true;
    },
    Left:function (X,Y) {
//        alert('Left');
        return true;
    },
    
    // clear selection and other default actions
    _TopLeft:function (X,Y) {
        navigation._clearSelection();
        return true;
    },
    _Top:function (X,Y) {
        navigation._clearSelection();
        return true;
    },
    _TopRight:function (X,Y) {
        navigation._clearSelection();
        return true;
    },
    _Right:function (X,Y) {
        navigation._clearSelection();
        return true;
    },
    _BottomRight:function (X,Y) {
        navigation._clearSelection();
        return true;
    },
    _Bottom:function (X,Y) {
        navigation._clearSelection();
        return true;
    },
    _BottomLeft:function (X,Y) {
        navigation._clearSelection();
        return true;
    },
    _Left:function (X,Y) {
        navigation._clearSelection();
        return true;
    },
    
    mousedown:function (event) {
        navigation.cursor = navigation._getPosition(event);
    },
    mouseup:function (event) {
        var cursor = navigation._getPosition(event);
        X = cursor.x - navigation.cursor.x;
        Y = cursor.y - navigation.cursor.y;
        if ((Math.abs(X) >= navigation.minX && Math.abs(X) <= navigation.maxX) || (Math.abs(Y) >= navigation.minY && Math.abs(Y) <= navigation.maxY)) {
            switch (true) {
                case (Math.abs(X)<=Math.abs(Y)*2 && Math.abs(Y)<=Math.abs(X)*2):                
                    switch (true) {
                        case (X >= 0 && Y >= 0):
                            navigation._BottomRight(X,Y);
                            navigation.BottomRight(X,Y);
                            break;
                        case (X >= 0 && Y < 0):
                            navigation._TopRight(X,Y);
                            navigation.TopRight(X,Y);
                            break;
                        case (X < 0 && Y >= 0):
                            navigation._BottomLeft(X,Y);
                            navigation.BottomLeft(X,Y);
                            break;
                        case (X < 0 && Y < 0):
                            navigation._TopLeft(X,Y);
                            navigation.TopLeft(X,Y);
                            break;
                    }
                    break;
                case (Math.abs(X)>Math.abs(Y)*2):
                    if (X > 0) {
                        navigation._Right(X,Y);
                        navigation.Right(X,Y);
                    } else {
                        navigation._Left(X,Y);
                        navigation.Left(X,Y);
                    }
                    break;
                case (Math.abs(Y)>Math.abs(X)*2):                
                    if (Y > 0) {
                        navigation._Bottom(X,Y);
                        navigation.Bottom(X,Y);
                    } else {
                        navigation._Top(X,Y);
                        navigation.Top(X,Y);
                    }
                    break;
            }
        }
    },
    
    _getPosition:function (e) {
        e = e || window.event;
        var cursor = {x:0, y:0};
        if (e.pageX || e.pageY) {
            cursor.x = e.pageX;
            cursor.y = e.pageY;
        } else {
            var de = document.documentElement;
            var b  = document.body;
            cursor.x = e.clientX + 
                (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
            cursor.y = e.clientY + 
                (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
        }
        return cursor;
    },
    
    _clearSelection:function() { 
        var sel ; 
        if(document.selection && document.selection.empty){ 
            document.selection.empty() ; 
        } else if(window.getSelection) { 
            sel=window.getSelection(); 
            if (sel && sel.removeAllRanges) 
            sel.removeAllRanges() ;
        }
    }
}