var adgeo_server="adgeo.ru";
function AdGeo(map_obj, apikey, min_zoom, max_zoom, termid, number, nozoomcontrol,w,h) {
    if(!number) number = 80;
    if(!termid) termid = 0;
    if(!w) w = '';
    if(!h) h = '';
    if(!nozoomcontrol) nozoomcontrol = 0;
    _this = this;
    _this.key = apikey;
    _this.cat_id = termid;
    _this._number = number;
    _this._map = map_obj;
    _this._min_zoom = min_zoom;
    _this._max_zoom = max_zoom;
    _this._start_zoom = 1;//_this._map.getZoom();
    _this._adgeo_layer = null;
    _this._nozoomcontrol = nozoomcontrol;
    _this._w = w;
    _this._h = h;
    _this._move_handler = function () {
	_this._refresh(0);
    }
    _this._zoom_handler = function () {
	_this._refresh(1);
    }
    
    if( typeof GMap2 != 'undefined' || ( typeof google != 'undefined' && typeof google.maps != 'undefined') ){
        _this.getZoom = function () { return _this._map.getZoom();}
        _this.getMapSizeX = function () { return _this._map.getSize().width;}
        _this.getMapSizeY = function () { return _this._map.getSize().height;}
        _this.getCenterLat = function () { return _this._map.getCenter().lat();}
        _this.getCenterLng = function () { return _this._map.getCenter().lng();}
        _this.getBoundsNELat = function () { return _this._map.getBounds().getNorthEast().lat();}
        _this.getBoundsNELng = function () { return _this._map.getBounds().getNorthEast().lng();}
        _this.getBoundsSWLat = function () { return _this._map.getBounds().getSouthWest().lat();}
        _this.getBoundsSWLng = function () { return _this._map.getBounds().getSouthWest().lng();}
        if( typeof google.maps == 'object') {
            _Event = google.maps.Event; _GeoXml = google.maps.GeoXml;
        } else {
            _Event = GEvent; _GeoXml = GGeoXml;
        }
        _this.createAdgeoLayer = function() {
            _this._current_lat = _this.getCenterLat();
            _this._current_lon = _this.getCenterLng();
            _this._current_dlat = (_this.getBoundsNELat() - _this.getBoundsSWLat())*4;
            _this._current_dlon = (_this.getBoundsNELng() - _this.getBoundsSWLng())*4;
            _adgeo_url = 'http://'+adgeo_server+'/adsuni.php?sortby=rnd&kmldetails=1&lat='+_this._current_lat+'&lon='+_this._current_lon+
                '&key='+_this.key+'&termid='+_this.cat_id+'&mw='+_this.getMapSizeX()+'&mh='+_this.getMapSizeY()+'&dlat='+_this._current_dlat+'&dlon='+_this._current_dlon+'&number='+_this._number+'&zoom='+_this.getZoom()+'&nozoomcontrol='+_this._nozoomcontrol+'&junk='+Math.random();
            if(_this._w!='') _adgeo_url+='&w='+_this._w+'&h='+_this._h;
            _old_layer = _this._adgeo_layer;
            if( _old_layer ) {
            _this._map.removeOverlay(_old_layer);
            }
            _this._adgeo_layer = new _GeoXml(_adgeo_url);
            _this._map.addOverlay(_this._adgeo_layer);
            _Event.addListener(_this._adgeo_layer, 'load', function() {
            //if(_old_layer) {
            //_this._map.removeOverlay(_old_layer);
            //}
            });
        }
        _Event.addListener(_this._map, 'moveend', _this._move_handler);
        _Event.addListener(_this._map, 'zoomend', _this._zoom_handler);
    } else if( typeof YMaps != 'undefined' ) {
        _this.getZoom = function () {
            return _this._map.getZoom();
        }
        _this.getMapSizeX = function () {
            return _this._map.getContainerSize().x;
        }
        _this.getMapSizeY = function () {
            return _this._map.getContainerSize().y;
        }
        _this.getCenterLat = function () {
            return _this._map.getCenter().getY();
        }
        _this.getCenterLng = function () {
            return _this._map.getCenter().getX();
        }
        _this.getBoundsNELat = function () {
            return _this._map.getBounds().getTop();
        }
        _this.getBoundsNELng = function () {
            return _this._map.getBounds().getRight();
        }
        _this.getBoundsSWLat = function () {
            return _this._map.getBounds().getBottom();
        }
        _this.getBoundsSWLng = function () {
            return _this._map.getBounds().getLeft();
        }

        _this.createAdgeoLayer = function() {
            _this._current_lat = _this.getCenterLat();
            _this._current_lon = _this.getCenterLng();
            _this._current_dlat = (_this.getBoundsNELat() - _this.getBoundsSWLat())*4;
            _this._current_dlon = (_this.getBoundsNELng() - _this.getBoundsSWLng())*4;
            _adgeo_url = 'http://'+adgeo_server+'/adsuni.php?sortby=rnd&format=yml&lat='+_this._current_lat+'&lon='+_this._current_lon+
                '&key='+_this.key+'&termid='+_this.cat_id+'&mw='+_this.getMapSizeX()+'&mh='+_this.getMapSizeY()+'&dlat='+_this._current_dlat+'&dlon='+_this._current_dlon+'&number='+_this._number+'&nozoomcontrol='+_this._nozoomcontrol+'&junk='+Math.random()+'&zoom='+_this.getZoom();
            _old_layer = _this._adgeo_layer;
            if( _old_layer ) {
            _this._map.removeOverlay(_old_layer);
            }
            _this._adgeo_layer = new YMaps.YMapsML(_adgeo_url);
            _this._map.addOverlay(_this._adgeo_layer);
            YMaps.Events.observe(_this._adgeo_layer, _this._adgeo_layer.Events.Load, function() {
            //if(_old_layer) {
                //_this._map.removeOverlay(_old_layer);
            //}
            });
        }
        YMaps.Events.observe(_this._map, _this._map.Events.MoveEnd, _this._move_handler);
        YMaps.Events.observe(_this._map, _this._map.Events.SmoothZoomEnd, _this._zoom_handler);
    }

    _this._current_lat = 0;//_this.getCenterLat();
    _this._current_lon = 0;//_this.getCenterLng();
    //calculate height and width in lat lon
    _this._current_dlat = 0;
    _this._current_dlon = 0;

    _this._refresh = function (forcecase) {
	if( forcecase ==1 || 
	    _this.getBoundsSWLat() < _this._current_lat - _this._current_dlat/2 ||
	    _this.getBoundsNELat() > _this._current_lat + _this._current_dlat/2 ||
	    _this.getBoundsSWLng() < _this._current_lon - _this._current_dlon/2 ||
	    _this.getBoundsSWLng() > _this._current_lon + _this._current_dlon/2 ||
	    (_this.getBoundsNELat() - _this.getBoundsSWLat())*(_this.getBoundsNELng() - _this.getBoundsSWLng()) < (_this._current_dlat*_this._current_dlon)/64
	    ) {
		_this.show();
		}
		if(!(_this._map.getZoom() >= _this._min_zoom && _this._map.getZoom() <= _this._max_zoom)) {
		_this.hide();	
		}

    }

    _this.onload = function () {}

    _this._onlayerload = function () {
	_this.show();
        _this.onload();
    }

    _this.show = function () {
	if( _this._map.getZoom() >= _this._min_zoom && _this._map.getZoom() <= _this._max_zoom) {
	    _this.createAdgeoLayer();
	} else {
	    _this.hide();
	}
    }

    _this.hide = function () {
	if(_this._adgeo_layer) {
	    _this._map.removeOverlay(_this._adgeo_layer);
	    // Force reload when unhide
	    _this._current_dlat = 0;
	    _this._current_dlon = 0;
	}
	_this._adgeo_layer = null;
    }
    
    _this.show();
};

