popBox = {
	temp:{},
	getId:function(id){
		return typeof id == 'object' ? id : document.getElementById(id);
	},
	show:function(file,callback){
		var action = file ? true : false;
		callback = callback || function(){}
		if (!action) {
			alert('参数丢失');
			return false;
		}
		
		//create mask
		if (!this.temp.popMask){
			this.createMask();
		}
		//create main
		if (!this.temp.popMain){
			this.createMain();	
		}		
		var popMain = this.temp.popMain;
		var self  = this;
		showFile();

		//show file content
		function showFile(){
			self.request(file,function(re){
				popMain.innerHTML = re.responseText;				
				callback(popMain);
				setPosition();			
			});
		}
		function setPosition(){
			self.css(popMain,{
				marginLeft:'-' + popMain.offsetWidth/2 + 'px',
				marginTop:'-' + popMain.offsetHeight/2 + 'px'
			});		
		}
		try{
			this.stopDefault(this.getEvent());
		}catch(e){
		
		}
	},
	extend:function(obj,options){
		for(var o in options){
			obj[o] = options[o];
		}
	},
	css:function(obj,style){
		for(var o in style){
			obj.style[o] = style[o];
		}
	},
	createObj:function(tag,id,style){
		var obj = document.createElement(tag);
		obj.id = id;
		this.css(obj,style);
		return obj;
	},
	createMask:function(){
		var d = document.documentElement || document.body;
		var scrollHeight = Math.max(Math.max(d.scrollHeight, d.offsetHeight), d.clientHeight);
	
		this.temp['popMask'] = this.createObj('div','popMask',{
			position:'absolute',
			left:0,
			top:0,
			zIndex:1000,
			width:'100%',
			height:scrollHeight + 'px',
			opacity:'0',
			filter:'alpha(opacity=0)',
			background:'#fff'
		});
		if (typeof d.style.maxHeight == 'undefined'){
			this.temp['popMaskIframe'] = this.createObj('iframe', 'popMaskIframe', {
				position: 'absolute',
				left: 0,
				top: 0,
				width:'100%',
				height:'100%',
				opacity: 0,
				filter: 'alpha(opacity=0)'			
			});
			this.temp['popMask'].appendChild(this.temp['popMaskIframe']);
		}
		document.body.appendChild(this.temp['popMask']);
	},
	createMain:function(){
		var d = document.documentElement || document.body;
		var that = this;
		this.temp['popMain'] = this.createObj('div','popMain',{
			position:'fixed',
			left:'50%',
			top:'50%',
			zIndex:1002,
			textAlign:'left'
		});
		if (typeof d.style.maxHeight == 'undefined') {			
			this.css(this.temp['popMain'],{
				position:'absolute'

			});

			(function(){
				if (that.temp['popMain']){
					that.css(that.temp['popMain'],{
						top: d.scrollTop + (d.clientHeight / 2) + 'px'
					});	
					setTimeout(arguments.callee, 15);
				}
			})();
		};		
		document.body.appendChild(this.temp['popMain']);
	},
	removeAll:function(){
		for (var o in this.temp){
			this.remove(this.temp[o]);	
		}
		this.temp.popMask = this.temp.popMain = null;
		this.stopDefault(this.getEvent());
	},
	remove:function(obj){
		obj.parentNode.removeChild(obj);
	},
	addEvent:function( obj, type, fn ) {
        if (obj.addEventListener)
                obj.addEventListener(type, fn, false);
        else if (obj.attachEvent) 
                obj.attachEvent('on' + type, function() { return fn.apply(obj, new Array(window.event));});
	},
	request:function(url,func,post){
		var thisSelf = this;
		var req = createXMLHTTPObject();
		if (!req) return;
		var method = (post) ? "POST" : "GET";
		req.open(method,url,true);
		req.setRequestHeader('User-Agent','XMLHTTP/1.0');

		if (post){
			//req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=gb2312');
			req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		}
		req.onreadystatechange = function () {
			if (req.readyState == 4) {
			 if (req.status == 200) {
				func(req);
			 }
			}else{
				thisSelf.temp['popMain'].innerHTML = "<font color='#ffffff'><strong>正在加载loading...</strong></font><br /><br />";
				//alert('error');
			}			
		}
		req.send(post);
		function XMLHttpFactories() {
			return [
				function () {return new XMLHttpRequest()},
				function () {return new ActiveXObject("Msxml2.XMLHTTP")},
				function () {return new ActiveXObject("Msxml3.XMLHTTP")},
				function () {return new ActiveXObject("Microsoft.XMLHTTP")}
			];
		};		 
		function createXMLHTTPObject() {
			var xmlhttp = false;
			var factories = XMLHttpFactories();
			for (var i=0;i<factories.length;i++) {
				try {
					xmlhttp = factories[i]();
				}catch (e) {
					continue;
				}
			   break;
			}
			return xmlhttp;
		}
	},
	stopDefault:function( e ) {
		if ( e && e.preventDefault )
			e.preventDefault();
		else
			window.event.returnValue = false;
		return false;
	},
	getEvent:function(){
        if(document.all) return window.event;
        func=this.getEvent.caller;
        while(func!=null){
            var arg0=func.arguments[0];
            if(arg0){
                if((arg0.constructor==Event || arg0.constructor ==MouseEvent) || (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)){    
                    return arg0;
                }
            }
            func=func.caller;
        }
        return null;
    }
}
