var vmap = {
    map         : null,
    init_zoom   : 15,
    auth_key    : '87f4ff9fd4a86b360c7e2adc66b293f6',
    origin      : { lng: 30.51559579, lat: 50.43914374 },
    allow_remote_address : false,

    init_VMap: function() {
        VMap.AUTH_KEY = this.auth_key;

        this.map = new VMap(document.getElementById('viewport'), {
            zoomControl : {
                smooth: true
            }
        });

        if(typeof(CURRENT_SHOP) != 'undefined' && CURRENT_SHOP != null) {
            if(this.allow_remote_address) {
                if(!vmap.find_shop(SHOPS, CURRENT_SHOP)) {
                    this.map.center(this.origin, this.init_zoom);
                }
            } else {
                if(typeof(SHOPS[CURRENT_SHOP].coords) == 'string') {
                    var coords = eval(SHOPS[CURRENT_SHOP].coords);

                    var layer = new VLayer();
                    var area = {
                        coords  : coords,
                        type    : "line",
                        style   : {},
                        childs  : []
                    };

                    layer.add(area);
                    this.origin = layer.childs(0).bounds().center()
                    
                    this.init_zoom = 18;
                    this.map.center(this.origin, this.init_zoom);
                } else {
                    this.map.center(this.origin, this.init_zoom);
                }
            }
        } else {
            this.map.center(this.origin, this.init_zoom);
        }
    },

    mark_shop: function(data) {
        if(typeof(data.coords) == 'undefined' && this.allow_remote_address) {
            return this.search_shop(data);
        } else if(typeof(data.coords) == 'string') {
            data.coords = eval(data.coords);
//            debugger;
        } else {
//            alert('No valid coords specified.');
            return false;
        }

        var layer = new VLayer();
        this.map.add(layer);

        var area = {
            coords  : data.coords,
            type    : "line",
            style   : {
                color       : "#ff0000",
                lineWidth   : 3,
                opacity     : 0.4
            },
            childs  : []
        };

        layer.add(area);

        var obj = layer.childs(0);
        var icon = null;

        if(data.icon !== undefined && data.icon.src !== undefined) {
            var w = data.icon.w !== undefined ? data.icon.w : 90;
            var h = data.icon.h !== undefined ? data.icon.h : 20;

            icon = new VMarkerIcon(w, h, data.icon.src);
        }

        var mpos = obj.bounds().center();

        if(typeof(data.offset) != 'undefined') {
            data.offset = eval(data.offset);

            mpos.lng += data.offset[0].lng;
            mpos.lat += data.offset[0].lat;
        }

        if(typeof(data.title) == 'undefined') data.title = '';
        if(typeof(data.description) == 'undefined') data.description = '';

        var marker = new VMarker(mpos, icon);
        marker.hint(data.title);
        marker.info(data.title, data.description);

        layer.add(marker);

        this.map.repaint();

        return true;
    },

    search_shop: function(data) {
        if(data.address === undefined || !data.address) return false;
        
        VRemoteCall.request("address", data.address, function callback(layer) {
            if(layer._childs[0] === undefined) {
                return false;
            }
            var _coords = layer._childs[0]._coords;

            data.coords = _coords;

            vmap.mark_shop(data);

            return true;
        });
    },

    mark_all_shops: function(data) {
        $.each(data, function(key, shop) {
            vmap.mark_shop(shop);
        });
    },

    find_shop: function(data, current) {
        if(typeof(data[current].address) != 'undefined' && data[current].address) {
            var address = data[current].address;

            VRemoteCall.request("address", address, function callback(layer) {
                if(layer.childs(0) === undefined) {
                    debugger;
                }
                var center = layer.childs(0).bounds().center()

                vmap.init_zoom = 17;
                vmap.map.center(center, vmap.init_zoom);

                return true;
            });
        }
    }
};



$(document).ready(function() {
    vmap.init_VMap();

    vmap.mark_all_shops(SHOPS);
//    vmap.search_shop(SHOPS[12])
//    vmap.search_shop(SHOPS[72])
//    vmap.search_shop(SHOPS[37])
//    vmap.search_shop(SHOPS[42])
});