    var MAPS={};

    Map = function(mapId, coords, _data, _show, _param, _config) {
        var _this = this;

        var set = {
            show: {
                obstacle:true,
                base:true,
                army:false,
                waypoint:false,
                area:false,
                nub:false
            },
            param: {
                cellSize:32,
                margin:1,
                step:16,
                speed:25,
                radius:0
            },
            config: {
                premium:false,
                scroll:false,
                select:false,
                inscriptions:false,
                resize:false,
                obstacle:false
            }
        }

        var map = document.getElementById(mapId);

/******************************************************************************/

        _this.updateParam = function(_set) {
            for (obj in set) {
                if (typeof(_set[obj]) == 'object') for(p in set[obj]) {
                    if (_set[obj][p] != undefined) set[obj][p] = _set[obj][p];
                }
            }
        }


        _this.resize = function() {
            if (set.config.resize) {
                var height = $(window).height();
                if (height > map.parentNode.offsetHeight) height = map.parentNode.offsetHeight;
                if (height > map.parentNode.offsetWidth) height = map.parentNode.offsetWidth;
                map.style.height = height + 'px';
            }
        }


        _this.click = function(e) {content.mapClick(e);}
        _this.reload = function() {content.reload();}
        _this.chCoords = function(c) {content.chCoords(c);}


        _this.refresh = function() {content.refresh();}


        _this.enableScroll = function() {scroll.enableScroll();}
        _this.disableScroll = function() {scroll.disableScroll();}


        _this.show = function(center_x, center_y) {content.show(center_x, center_y);}
        _this.showArea = function(area) {content.showArea(area);}

/******************************************************************************/

        _this.updateParam({show:_show, param:_param, config:_config});

        if (!set.config.scroll) set.param.margin = 0;

        _this.resize();

        set.config.mobileMode = map.ontouchstart === null;

        var maxScroll = (set.param.radius) ? {
            top: set.param.radius * set.param.cellSize,
            left: set.param.radius * set.param.cellSize,
            bottom: -((set.param.radius + 1) * set.param.cellSize - map.offsetHeight),
            right: -((set.param.radius + 1) * set.param.cellSize - map.offsetWidth)
        } : false;

        var controls = new mapInterface(map, set.config, {refresh:_this.refresh});
        var scroll = new mapScroll(map, controls, maxScroll, set, {click:_this.click, reload:_this.reload, chCoords:_this.chCoords});
        var content = new mapContent(map, controls, maxScroll, set, {enableScroll:_this.enableScroll, disableScroll:_this.disableScroll}, _data);

        _this.show(coords.x, coords.y);
    }

/*############################################################################*/

    mapInterface = function(map, config, func) {
        this.inside = document.createElement('div');
        this.inside.className = 'inside';
        map.appendChild(this.inside);

        if (config.scroll) {
            this.slideT = document.createElement('div');
            this.slideT.className = 'slide';
            this.slideT.style.width = map.offsetWidth + 'px';
            map.appendChild(this.slideT);

            this.slideB = document.createElement('div');
            this.slideB.className = 'slide';
            this.slideB.style.width = map.offsetWidth + 'px';
            this.slideB.style.bottom = '0px';
            map.appendChild(this.slideB);

            this.slideL = document.createElement('div');
            this.slideL.className = 'slide';
            this.slideL.style.height = map.offsetHeight + 'px';
            map.appendChild(this.slideL);

            this.slideR = document.createElement('div');
            this.slideR.className = 'slide';
            this.slideR.style.height = map.offsetHeight + 'px';
            this.slideR.style.right = '0px';
            map.appendChild(this.slideR);

            this.slideTL = document.createElement('div');
            this.slideTL.className = 'slide2';
            map.appendChild(this.slideTL);

            this.slideTR = document.createElement('div');
            this.slideTR.className = 'slide2';
            this.slideTR.style.right = '0px';
            map.appendChild(this.slideTR);

            this.slideBL = document.createElement('div');
            this.slideBL.className = 'slide2';
            this.slideBL.style.bottom = '0px';
            map.appendChild(this.slideBL);

            this.slideBR = document.createElement('div');
            this.slideBR.className = 'slide2';
            this.slideBR.style.bottom = '0px';
            this.slideBR.style.right = '0px';
            map.appendChild(this.slideBR);
        }

        if (config.inscriptions) {
            this.coordinates = document.createElement('div');
            this.coordinates.className = 'coordinates';
            map.appendChild(this.coordinates);

            this.refresh = document.createElement('div');
            this.refresh.className = 'refresh';
            this.refresh.onclick = function() {func.refresh();}
            this.refresh.innerHTML = '<a id="' + map.id + '_refresh" href="javascript:void(0);">Refresh</a>';
            if (config.mobileMode) this.refresh.ontouchstart = function(e) {fEvent.stopPropagation(e);}
            else this.refresh.onmousedown = function(e) {fEvent.stopPropagation(e);}
            map.appendChild(this.refresh);
        }
    }

/*############################################################################*/

    mapScroll = function(map, controls, maxScroll, set, func) {
        var _this = this;

        var drag = {flag:false}
        var scrollTimer;


        _this.scrollT = function(){
            var top = parseInt(controls.inside.style.top, 10) + set.param.step;
            if (set.param.radius) {
                if (maxScroll.top < top) top = maxScroll.top;
            }
            controls.inside.style.top = top + 'px';
            func.chCoords();
        }

        _this.scrollB = function(){
            var bottom = parseInt(controls.inside.style.top, 10) - set.param.step;
            if (set.param.radius) {
                if (maxScroll.bottom > bottom) bottom = maxScroll.bottom;
            }
            controls.inside.style.top = bottom + 'px';
            func.chCoords();
        }

        _this.scrollL = function(){
            var left = parseInt(controls.inside.style.left, 10) + set.param.step;
            if (set.param.radius) {
                if (maxScroll.left < left) left = maxScroll.left;
            }
            controls.inside.style.left = left + 'px';
            func.chCoords();
        }

        _this.scrollR = function(){
            var right = parseInt(controls.inside.style.left, 10) - set.param.step;
            if (set.param.radius) {
                if (maxScroll.right > right) right = maxScroll.right;
            }
            controls.inside.style.left = right + 'px';
            func.chCoords();
        }

        _this.scrollTL = function(){
            var top = parseInt(controls.inside.style.top, 10) + set.param.step;
            var left = parseInt(controls.inside.style.left, 10) + set.param.step;
            if (set.param.radius) {
                if (maxScroll.top < top) top = maxScroll.top;
                if (maxScroll.left < left) left = maxScroll.left;
            }
            controls.inside.style.top = top + 'px';
            controls.inside.style.left = left + 'px';
            func.chCoords();
        }

        _this.scrollTR = function(){
            var top = parseInt(controls.inside.style.top, 10) + set.param.step;
            var right = parseInt(controls.inside.style.left, 10) - set.param.step;
            if (set.param.radius) {
                if (maxScroll.top < top) top = maxScroll.top;
                if (maxScroll.right > right) right = maxScroll.right;
            }
            controls.inside.style.top = top + 'px';
            controls.inside.style.left = right + 'px';
            func.chCoords();
        }

        _this.scrollBL = function(){
            var bottom = parseInt(controls.inside.style.top, 10) - set.param.step;
            var left = parseInt(controls.inside.style.left, 10) + set.param.step;
            if (set.param.radius) {
                if (maxScroll.bottom > bottom) bottom = maxScroll.bottom;
                if (maxScroll.left < left) left = maxScroll.left;
            }
            controls.inside.style.top = bottom + 'px';
            controls.inside.style.left = left + 'px';
            func.chCoords();
        }

        _this.scrollBR = function(){
            var bottom = parseInt(controls.inside.style.top, 10) - set.param.step;
            var right = parseInt(controls.inside.style.left, 10) - set.param.step;
            if (set.param.radius) {
                if (maxScroll.bottom > bottom) bottom = maxScroll.bottom;
                if (maxScroll.right > right) right = maxScroll.right;
            }
            controls.inside.style.top = bottom + 'px';
            controls.inside.style.left = right + 'px';
            func.chCoords();
        }


        _this.mouseDown = function(e) {
            e = fEvent.fix(e);

            drag.down = {
                x:e.pageX,
                y:e.pageY
            }

//            document.ondragstart = document.body.onselectstart = function() {return false}

            if (set.config.mobileMode) {
                map.ontouchend = _this.mapMouseUp;
                map.ontouchmove = _this.mapMouseMove;
            }
            else {
                document.onmouseup = _this.mapMouseUp;
                document.onmousemove = _this.mapMouseMove;
            }
        }


        _this.mapMouseUp = function(e) {
            e = fEvent.fix(e);

            if (drag.down != undefined) func.click(e);
            else func.reload();

            _this.enableSlides();

            drag = {
                flag:false,
                down:undefined,
                elem:undefined,
                st:undefined
            }

//            document.ondragstart = document.body.onselectstart = null;

            if (set.config.mobileMode) map.ontouchmove = map.ontouchend = undefined;
            else {
                document.onmousemove = null;
                document.onmouseup = null;
            }
        }


        _this.mapMouseMove = function(e) {
            e = fEvent.fix(e);

            if (drag.down != undefined) {
                if (Math.abs(drag.down.x - e.pageX) + Math.abs(drag.down.y - e.pageY) > 5) {
                    drag.flag = true;

                    drag.elem = {
                        x:controls.inside.offsetLeft,
                        y:controls.inside.offsetTop
                    }
                    drag.st = {
                        x:drag.down.x - drag.elem.x,
                        y:drag.down.y - drag.elem.y
                    }
                    drag.down = undefined;

                    _this.disableSlides();
                }
            }

            if (!set.config.scroll) return;

            if (drag.st != undefined) {
                var top = e.pageY - drag.st.y;
                var left = e.pageX - drag.st.x;

                if (maxScroll) {
                    if (maxScroll.top < top) top = maxScroll.top;
                    else if (maxScroll.bottom > top) top = maxScroll.bottom;

                    if (maxScroll.left < left) left = maxScroll.left;
                    else if (maxScroll.right > left) left = maxScroll.right;
                }

                controls.inside.style.top = top + 'px';
                controls.inside.style.left = left + 'px';

                func.chCoords();
            }
        }


        _this.enableScroll = function(){
            if (set.config.mobileMode) map.ontouchstart = _this.mouseDown;
            else map.onmousedown = _this.mouseDown;

            _this.enableSlides();
        }


        _this.disableScroll = function(){
            if (set.config.mobileMode) map.ontouchstart = null;
            else map.onmousedown = null;
        }


        _this.stopSlide = function(){
            clearInterval(scrollTimer);
            func.reload();
        }


        _this.enableSlides = function(){
            if (set.config.mobileMode || !set.config.scroll) return;

            controls.slideT.onmouseover = function() {scrollTimer = setInterval(_this.scrollT, set.param.speed);}
            controls.slideB.onmouseover = function() {scrollTimer = setInterval(_this.scrollB, set.param.speed);}
            controls.slideL.onmouseover = function() {scrollTimer = setInterval(_this.scrollL, set.param.speed);}
            controls.slideR.onmouseover = function() {scrollTimer = setInterval(_this.scrollR, set.param.speed);}
            controls.slideTL.onmouseover = function() {scrollTimer = setInterval(_this.scrollTL, set.param.speed);}
            controls.slideTR.onmouseover = function() {scrollTimer = setInterval(_this.scrollTR, set.param.speed);}
            controls.slideBL.onmouseover = function() {scrollTimer = setInterval(_this.scrollBL, set.param.speed);}
            controls.slideBR.onmouseover = function() {scrollTimer = setInterval(_this.scrollBR, set.param.speed);}

            controls.slideT.onmouseout = controls.slideB.onmouseout = controls.slideL.onmouseout = controls.slideR.onmouseout = controls.slideTL.onmouseout = controls.slideTR.onmouseout = controls.slideBL.onmouseout = controls.slideBR.onmouseout = _this.stopSlide;
        }


        _this.disableSlides = function(){
            if (set.config.mobileMode || !set.config.scroll) return;

            controls.slideT.onmouseover = controls.slideB.onmouseover = controls.slideL.onmouseover = controls.slideR.onmouseover = controls.slideTL.onmouseover = controls.slideTR.onmouseover = controls.slideBL.onmouseover = controls.slideBR.onmouseover = null;
            controls.slideT.onmouseout = controls.slideB.onmouseout = controls.slideL.onmouseout = controls.slideR.onmouseout = controls.slideTL.onmouseout = controls.slideTR.onmouseout = controls.slideBL.onmouseout = controls.slideBR.onmouseout = null;
        }

        if (set.config.scroll) document.ondragstart = document.body.onselectstart = function() {return false};

        _this.enableScroll();
    }

/*############################################################################*/

    mapContent = function(map, controls, maxScroll, set, func, _data) {
        var _this = this;

        var data = {};
        var bases = {};
        var armies = {};
        var nub = {};

        var elements = {};
        var mustUpdate = {};

/******************************************************************************/

        var hc = Game.getHouseAreaCenters();

/******************************************************************************/

        _this.show = function(center_x, center_y) {
            _this.showCell(center_x, center_y);
            if (typeof(_data) == 'object') {
                _this.dataHandling(_data);
                _data = undefined;
            }
            else _this.reload();
        }

        _this.reload = function() {
            _this.getData(_this.getZone());
        }


        _this.showCell = function(x, y) {
            var center = _this.wrapperCenter();

            var top = y * set.param.cellSize + center.y;
            var left = (-x) * set.param.cellSize + center.x;

            if (set.param.radius) {
                if (maxScroll.top < top) top = maxScroll.top;
                if (maxScroll.left < left) left = maxScroll.left;
                if (maxScroll.bottom > top) top = maxScroll.bottom;
                if (maxScroll.right > left) left = maxScroll.right;
            }

            controls.inside.style.left = left + 'px';
            controls.inside.style.top = top + 'px';

            _this.chCoords({x:x, y:y});
        }


        _this.dataHandling = function(d) {
            for (p in d) {
                if (p == 'nub') {
                  nub = d[p];
                    continue;
                }
                if (d[p] == undefined) continue;
                if (!_this.compareCell(p, d[p])) mustUpdate[p] = true;

                var exp = p.split('x');
                data[p] = d[p];
                data[p].x = exp[0];
                data[p].y = exp[1];

                if (d[p].a != undefined) for (army in d[p].a) {
                    if (armies[d[p].a[army].i] != undefined) {
                        if (d[p].a[army].i == set.show.waypoint) d[p].a[army].wp = armies[d[p].a[army].i].wp;
                        if (armies[d[p].a[army].i].x != exp[0] || armies[d[p].a[army].i].y != exp[1]) {
                            d[armies[d[p].a[army].i].x+'x'+armies[d[p].a[army].i].y] = undefined;
                            _this.refreshCell(armies[d[p].a[army].i].x, armies[d[p].a[army].i].y);
                        }
                    }
                    armies[d[p].a[army].i] = d[p].a[army];
                    armies[d[p].a[army].i].x = exp[0];
                    armies[d[p].a[army].i].y = exp[1];
                }

                if (d[p].b != undefined) {
                    bases[d[p].b.i] = d[p].b;
                    bases[d[p].b.i].x = exp[0];
                    bases[d[p].b.i].y = exp[1];
                }

                if (d[p].w != undefined) {
                    if (armies[set.show.waypoint] == undefined) armies[set.show.waypoint] = {};
                    armies[set.show.waypoint].wp = {x:exp[0],y:exp[1]};
                    mustUpdate[exp[0] + 'x' + exp[1]] = true;
                }
            }

            _this.printZone(_this.getZone());
        }


        _this.getData = function(zone) {
            zone.show = set.show;
            zone.rnd = Math.random();
            $.getJSON('/map.php',zone,_this.dataHandling);
        }


        _this.getZone = function() {
            var zone = new Object;
            zone.x0 = -Math.ceil(parseInt(controls.inside.style.left, 10) / set.param.cellSize) - set.param.margin;
            zone.y0 = Math.ceil((parseInt(controls.inside.style.top, 10) - map.offsetHeight) / set.param.cellSize) - set.param.margin;
            zone.x1 = Math.ceil(map.offsetWidth / set.param.cellSize) + zone.x0 + 2 * set.param.margin;
            zone.y1 = Math.ceil(map.offsetHeight / set.param.cellSize) + zone.y0 + 2 * set.param.margin;
            return zone;
        }


        _this.wrapperCenter = function() {
            return {x:Math.floor(((map.offsetWidth - 2) - set.param.cellSize) / 2), y:Math.floor(((map.offsetHeight - 2) - set.param.cellSize) / 2)};
        }


        _this.chCoords = function(c) {
            if (set.config.inscriptions) {
                if (typeof(c) != 'object') c = _this.getCenter();
                controls.coordinates.innerHTML = '[' + c.x + ':' + c.y + ']';
            }
        }


        _this.compareCell = function(id, newData) {
            if (0
                || typeof(data[id]) != typeof(newData)
                || typeof(data[id].a) != typeof(newData.a)
                || typeof(data[id].b) != typeof(newData.b)
                || typeof(data[id].w) != typeof(newData.w)
            ) return false;

            return true;
        }


        _this.refreshCell = function(x, y) {
            mustUpdate[x + 'x' + y] = true;
            data[x + 'x' + y] = undefined;
            _this.getData({x0:x,y0:y,x1:x,y1:y});
        }


        _this.printZone = function(zone) {
            for (x = zone.x0; x <= zone.x1; x++) for (y = zone.y0; y <= zone.y1; y++) _this.printCell(x, y);
        }


        _this.getCenter = function() {
            return {
                x:-Math.ceil((controls.inside.offsetLeft - map.offsetWidth / 2) / set.param.cellSize),
                y:Math.ceil((controls.inside.offsetTop - map.offsetHeight / 2) / set.param.cellSize)
            }
        }


        _this.printCell = function(x,y) {
            var flag = false;
            var id = x + 'x' + y;

            var area = _this.checkHouseArea(x, y);

            if (mustUpdate[id] == undefined && !area) return;
            mustUpdate[id] = undefined;

            if (elements[id] != undefined) _this.removeCell(x, y);


            if (data[id] !== undefined || area) {
                var cell = document.createElement('div');
                cell.style.left = (x * set.param.cellSize) + 'px';
                cell.style.top = ((-y) * set.param.cellSize) + 'px';

                var title = '[' + x + ':' + y + ']';

                var cellFocus = false;


                if (data[id] !== undefined) {

                    if (data[id] == false) {
                        flag = true;
                        cell.className = (area) ? 'r' + set.show.area : 'r';
                    }
                    else {
                        if (data[id].b != undefined) {
                            flag = true;
                            cell.className = 'b' + data[id].b.h;
                            title += '/ / ' + translate(lang.map.base, {'house':lang.map.house[data[id].b.h]}) + ' ' + data[id].b.n + '/ ' + lang.player + ': ' + ((data[id].b.a != undefined)?'['+data[id].b.a+'] '+data[id].b.p:data[id].b.p) + '/ ' + lang.level + ': ' + data[id].b.l;
                            if (nub[data[id].b.p] != undefined) title += '/ ' + _this.nubString(nub[data[id].b.p]);
                        }

                        if (data[id].a != undefined) {
                            flag = true;
                            var subcell = document.createElement('div');
                            var i = 0;
                            for (n in data[id].a) {
                                i++;
                                if (data[id].a[n].wp != undefined) cellFocus = true;
                                title += '/ / ' + translate(lang.map.army, {'house':lang.map.house[data[id].a[n].h]}) + ' ' + data[id].a[n].n + '/ ' + lang.player + ': ' + ((data[id].a[n].a != undefined)?'['+data[id].a[n].a+'] '+data[id].a[n].p:data[id].a[n].p) + '/ ' + lang.army.rank + ': ' + data[id].a[n].r + '/ ' + lang.army.wins + ': ' + data[id].a[n].w;
                                if (nub[data[id].a[n].p] != undefined) title += '/ ' + _this.nubString(nub[data[id].a[n].p]);
                            }
                            subcell.className = 'a' + ((i>1)?'':data[id].a[0].h);
                            cell.appendChild(subcell);
                        }

                        if (data[id].w != undefined) {
                            flag = true;
                            var subcell = document.createElement('div');
                            title += '/ / ' + translate(lang.map.waypoint, {'name':armies[set.show.waypoint].n});
                            subcell.className = 'w';
                            (cell.firstChild !== null ? cell.firstChild : cell).appendChild(subcell);
                        }
                    }
                }
                else if (area) {
                    flag = true;
                    cell.className = 't' + set.show.area;
                    title += '/ / ' + translate(lang.map.territory, {'house':lang.map.house[set.show.area]});
                }


                if (flag) {
                    cell.title = title;

                    $(cell).tooltip({
                        track: true,
                        delay: 0,
                        showURL: false,
                        showBody: "/",
                        fade: 250
                    });

                    controls.inside.appendChild(cell);
                    elements[id] = cell;

                    if (cellFocus && set.config.select) _this.cellFocus(cell);
                }
            }
        }


        _this.checkHouseArea = function(x, y) {
            if (set.show.area && Math.round(Math.sqrt(Math.pow(x - hc[set.show.area].x, 2) + Math.pow(y - hc[set.show.area].y, 2))) < 30) return true;
            return false;
        }


        _this.removeCell = function(x, y) {
            if (elements[x + 'x' + y] != undefined) {
                controls.inside.removeChild(elements[x + 'x' + y]);
                elements[x + 'x' + y] = undefined;
            }
        }


        _this.cellFocus = function(el){
            var child = el.firstChild;

            child.style.margin = '5px';

            child.style.width = (set.param.cellSize - 14) + 'px';
            child.style.height = (set.param.cellSize - 14) + 'px';
            child.style.border = '2px solid #f00';
        }


        _this.nubString = function(days) {
            if (days > 0) return translate(lang.map.nub, {'days':days});
            return translate(lang.map.you_nub, {'days':-days});
        }


        _this.mapClick = function(e){
            var c = {
                x:Math.floor((e.pageX - controls.inside.offsetLeft - map.offsetLeft) / set.param.cellSize),
                y:Math.ceil((controls.inside.offsetTop + map.offsetTop - e.pageY) / set.param.cellSize)
            }

            var id = c.x + 'x' + c.y;


            if (set.show.waypoint != false) {
                if (armies[set.show.waypoint].x == c.x && armies[set.show.waypoint].y == c.y) _this.clickArmy(armies[set.show.waypoint]);
                else if (armies[set.show.waypoint].wp != undefined && armies[set.show.waypoint].wp.x == c.x && armies[set.show.waypoint].wp.y == c.y) ajax.stopArmy(set.show.waypoint, function(){_this.deleteWP();});
                else if (!set.config.premium && !(0
                    || armies[set.show.waypoint].x == c.x && Math.abs(armies[set.show.waypoint].y - c.y) == 1
                    || armies[set.show.waypoint].y == c.y && Math.abs(armies[set.show.waypoint].x - c.x) == 1
                )) _this.clickArmy(armies[set.show.waypoint]);
                else ajax.moveArmy(set.show.waypoint, c.x, c.y, function(){
                    _this.deleteWP();
                    _this.reload();
                });
            }
            else if (set.config.obstacle) ajax.obstacle(c.x, c.y, function(data){_this.refreshCell(c.x, c.y);});
            else if (set.config.select && data[id] != undefined) {
                var arms = _this.getUserArmies(data[id].a, set.config.select.user);

                if (arms.length > 1 || arms.length == 1 && data[id].b != undefined && data[id].b.p == set.config.select.user) {
                    func.disableScroll();

                    var menu = document.createElement('p');
                    menu.className = 'tooltip';
                    menu.style.left = (e.pageX - controls.inside.offsetLeft - map.offsetLeft - 5) + 'px';
                    menu.style.top = (e.pageY - controls.inside.offsetTop - map.offsetTop - 5) + 'px';


                    if (data[id].b != undefined && data[id].b.p == set.config.select.user) {
                        var a = document.createElement('a');
                        a.href = 'javascript:void(0);';
                        a.value = data[id].b.i;
                        a.onclick = function() {_this.clickBase(this.value);};
                        a.innerHTML = '<strong>' + lang.Base + ' ' + data[id].b.n + '</strong>';
                        menu.appendChild(a);
                    }

                    for (army in data[id].a) if (data[id].a[army].p == set.config.select.user) {
                        var a = document.createElement('a');
                        a.href = 'javascript:void(0);';
                        a.value = data[id].a[army].i;
                        a.onclick = function() {
                            _this.clickArmy(armies[this.value]);
                            func.enableScroll();
                            $(menu).fadeOut(250, function(){$(menu).remove();});
                        };
                        a.innerHTML = lang.Army + ' ' + data[id].a[army].n;
                        menu.appendChild(a);
                    }

                    controls.inside.appendChild(menu);
                    $(menu).fadeIn(250);
                    $(menu).mouseleave(function(){
                        func.enableScroll();
                        $(menu).fadeOut(250, function(){$(menu).remove();});
                    });
                }
                else if (arms.length) _this.clickArmy(arms[0]);
                else if (data[id].b != undefined && data[id].b.p == set.config.select.user) _this.clickBase(data[id].b.i);
            }
            else if (_this.checkHouseArea(c.x, c.y) && data[id] === undefined) {
                document.getElementById('firstbase_x').value = c.x;
                document.getElementById('firstbase_y').value = c.y;
            }
        }


        _this.showArea = function(area) {
            set.show.area = area;
            _this.showCell(hc[area].x, hc[area].y);
            _this.refresh();
        }


        _this.refresh = function(){
            data = {};
            armies = {};
            bases = {};
            nub = {};
            elements = {};
            controls.inside.innerHTML = '';
            set.show.waypoint = false;
            _this.reload();
        }


        _this.clickArmy = function(army){
            if (set.config.select) {
                if (set.show.waypoint != false && armies[set.show.waypoint] != undefined) {
                    var el = elements[armies[set.show.waypoint].x + 'x' + armies[set.show.waypoint].y];
                    if (el != null) _this.cellBlur(el);
                    _this.deleteWP(true);
                    set.show.waypoint = false;
                }

                else {
                    set.show.waypoint = army.i;
                    var el = elements[army.x + 'x' + army.y];
                    if (el != null) _this.cellFocus(el);
                    _this.reload();
                }
            }
        }


        _this.deleteWP = function(blur){
            if (armies[set.show.waypoint].wp != undefined) {
                data[armies[set.show.waypoint].wp.x+'x'+armies[set.show.waypoint].wp.y].w = undefined;
                var cell = armies[set.show.waypoint].wp;
                armies[set.show.waypoint].wp = undefined;
                if (blur != undefined) set.show.waypoint = false;
                _this.refreshCell(cell.x, cell.y);
            }
        }


        _this.getUserArmies = function(arms, login) {
          var a = new Array();
            if (arms == undefined) return a;
            for (army in arms) {
                if (arms[army].p == login) a[a.length] = arms[army];
            }
            return a;
        }


        _this.clickBase = function(id){
            var form = document.createElement('form');
            var input = document.createElement('input');
            form.action = '/game/buildings/';
            form.method = 'post';
            form.style.display = 'none';
            input.name = 'change_base';
            input.value = id;
            form.appendChild(input);
            map.appendChild(form);
            form.submit();
        }


        _this.cellBlur = function(el){
            var child = el.firstChild;

            child.style.margin = '0px';

            child.style.width = set.param.cellSize + 'px';
            child.style.height = set.param.cellSize + 'px';
            child.style.border = '0px solid #f00';
        }
    }
