/* Author: yangzeming
 * Date: 2013-12-10
 * Desc: 遮罩层
    
 */
define("component/mapoverlay",function (require, exports, module) {

	function s(t, i) {
        var e = t.cloneNode(!0);
        document.body.appendChild(e);
        var s, a;
        return i ? (s = t.clientWidth, a = t.clientHeight) : (s = e.clientWidth, a = e.clientHeight), document.body.removeChild(e), e = null, {
            width: s,
            height: a
        }
    }
    var mapoverlay = function(point, template,style, map, target,eventList) {
        this._point = point;
        this._template = template;
        this._map = map;
        this._style = style;
        this._target = target;
        this._eventList = eventList;
        this._opts = style || {};

    };
    mapoverlay.prototype = new BMap.Overlay();
    mapoverlay.prototype.initialize = function(map) {
        this._map = map;
        var self = this;
        var _wrap = this._div = this.renderView()[0];
        this.bindEvent();
        setTimeout(function() {
            self._panBox();
        }, 100)
        this._map.getPanes().floatPane.appendChild(_wrap);
        return _wrap;
    };
    mapoverlay.prototype.renderView = function(){
    	var _temp = $(this._template);
    	
    	_temp.css(this._style);
    	this._$template = _temp;
    	return _temp;
    	
    };
    // 重新去生成内容
    mapoverlay.prototype.setData = function(content,eventList){
    	this._eventList = eventList;
    	var self = this;
    	$(this._div).html(content);
    	this.bindEvent();
    	setTimeout(function() {
            self._panBox();
        }, 100)
    	
    };
    // 重新定位
    mapoverlay.prototype.reposition = function(point,opt){
    	
            this._opts = opt || {}, this._point = point, this.show(), this.draw();
    };
    // 窗口定位让地图重新定位
    
	mapoverlay.prototype.bindEvent = function(){
	    var self = this;	
	    this._$template.find('[data-eventrole]').each(function(index,item){
	    	var role = $(item).data('eventrole');
	    	if(self._eventList[role]){
	    		
	    		self._eventList[role](self,item);
	    	}
	    	
	    	
	    });	
	};

	mapoverlay.prototype.render = function(map){
		map.addOverlay(this);
		
	};
	mapoverlay.prototype._resize = function(){
		
		 return this.size = s(this._div, true), this.size
	};
	mapoverlay.prototype._getInfoBoxSize = function(){
		
		this._boxWidth = parseInt(this._div.offsetWidth, 10), this._boxHeight = parseInt(this._div.offsetHeight, 10);
	};
	mapoverlay.prototype._panBox = function(){
		 this._getInfoBoxSize();
         var t = parseInt(this._map.getContainer().offsetHeight, 10),
             i = parseInt(this._map.getContainer().offsetWidth, 10),
             e = this._boxHeight,
             s = this._boxWidth;
         if (!(e >= t || s >= i)) {
             this._map.getBounds().containsPoint(this._point) || this._map.setCenter(this._point);
             var a, o, n, r = this._map.pointToPixel(this._point),
                 h = s - r.x,
                 d = s + r.x - i;
             if (this._marker) var c = this._marker.getIcon(); {
                 this._marker ? c.anchor.height + this._marker.getOffset().height - c.infoWindowAnchor.height : 0
             }
             a = e - r.y + 100 + 2, panX = h > 0 ? h : d > 0 ? -d : 0, n = a > 0 ? a : o > 0 ? -o : 0, this._map.panBy(panX, n)
         }
		
	};
    mapoverlay.prototype.draw = function() {
    	 var t = this._map,
         i = this._resize(),
         e = t.pointToOverlayPixel(this._point),
         s = this._opts.left || 10,
         a = this._opts.top || 0;
    	 this._div.style.left = e.x - i.width / 2 + (73 + s) + "px", this._div.style.top = e.y - i.height - (27 + a) + "px"
    };
    
    module.exports = mapoverlay;
});