(function(){
window.uIE7=/MSIE [1-6].\d/.test(navigator.userAgent);
window.bindEvent=null;
window.removeEvent=null;
if (window.attachEvent) {
    bindEvent = function(caller, event, listener, useCapture){caller.attachEvent("on"+event, listener);};
	removeEvent = function(caller, event, listener, useCapture) {caller.detachEvent("on"+event, listener);};
} else if (window.addEventListener) {
    bindEvent = function(caller, event, listener, useCapture){caller.addEventListener(event, listener, useCapture);};
	removeEvent = function(caller, event, listener, useCapture) {caller.removeEventListener(event, listener, useCapture);};
}

var floatPanel = window.floatPanel = function(p) {
    return new floatPanel.prototype.init(p);
}
floatPanel.prototype={
    init:function(p){
        if (!p.target) throw new Error('指定的目标不存在');
        if (typeof p.autoHide == 'undefined') p.autoHide=true;
		if (typeof p.enableEsc=='undefined') p.enableEsc=false;
		if (typeof p.autoFollow=='undefined') p.autoFollow=false;
		
        this.preShowed = this.isShowed=this.selfClicked=false;
		for(var k in p)this[k]=p[k];
		var self = this;
		bindEvent(this.target, "click", function(){self.selfClicked=true;}, false);
		if (p.autoFollow && !uIE7) this.target.style.position='fixed';
        return this;
    },
    show:function(sender, evt){
        evt = evt || window.event;
		this.caller = sender;
		this.target.style.display='block';
        this.isShowed = false;
		this.preShowed = true;
		
        if(typeof this.onShow == 'function')this.onShow.call(this, sender, evt);
		
		var self=this;
       	if (this.autoHide) {
			if (!this.__click) this.__click=function(){self._click.call(self, evt);};
			bindEvent(document, "click", this.__click, false);
		}
      	if (this.enableEsc) {
			if (!this.__keyup) this.__keyup = function(evt){evt=evt||window.event;self._keyup.call(self,evt)};
			bindEvent(document, "keyup", this.__keyup, false);
		}
		
		if (uIE7 && this.autoFollow) {
			if (!this.__follow) this.__follow = function(){self._follow.call(self, evt);};
			bindEvent(window, "scroll", this.__follow, false);
		}
		this._top = document.documentElement.scrollTop || 0;
    },
    _click:function(evt){
		if (this.isShowed && !this.selfClicked) {
			this.hide();return false;
		}
		if (this.preShowed) this.isShowed=true;
		this.preShowed = false;
		this.selfClicked = false;
    },
	_keyup:function(evt){
		if (evt.keyCode=='27')this.hide();
	},
	_follow:function(evt){
		this.target.style.top = (parseInt(this.target.style.top, 10) + document.documentElement.scrollTop-this._top) + 'px';
		this._top = document.documentElement.scrollTop;
	},
    hide:function(evt){
		evt = evt || window.event;
        this.target.style.display='none';
        this.isShowed=false;
		if (this.autoHide) removeEvent(document, "click", this.__click, false);
		if (this.enableEsc) removeEvent(document,"keyup", this.__keyup, false);
		if (uIE7 && this.autoFollow) removeEvent(window, 'scroll', this.__follow, false);
		
		if (typeof this.onHide == 'function')this.onHide.call(this, this.caller, evt);
    }
};
floatPanel.prototype.init.prototype=floatPanel.prototype;

window.maskPanel = {
	_id:'__window_mask_',
	_inited:false,
	show: function(target){
		var div;
		if (!(div = $(this._id))) {
			div = document.createElement('div');
			div.id = this._id;
			div.style.cssText = 'position:absolute;z-index:50000;left:0;top:0;width:100%;background:#000;opacity:0.05;filter:Alpha(Opacity:5);display:none;';
			document.body.appendChild(div);
		}
		if (!this._inited) {
			this.resize.call(div);
			bindEvent(window, 'resize', this.resize, false);
			this._inited = true;
		}
		div.style.display = 'block';
		
		if (target) {
			target.style.zIndex = 50001;
			this._zIndex = target.style.zIndex;
		}
		this._target = target;
	},
	hide:function(){
		if ($(this._id))$(this._id).style.display = 'none';
		if (this._target) this._target.style.zIndex=this._zIndex;
		removeEvent(window, 'resize', this.resize, false);
	},
	resize:function(){
		var mask = $(maskPanel._id);
		mask.style.width = document.documentElement.scrollWidth + 'px';
		mask.style.height = document.documentElement.scrollHeight + 'px';
	}
};

})()