var $_Global_Mask={
	level:0,
	show:function(fuckflash)
	{
		if(fuckflash)
		{
			jQuery('embed,object,select').css('visibility','hidden');
		}
		this.getMask().css('visibility','visible');
		this.level++;
		this.resize();
	},
	hide:function()
	{
		this.level--;
		if(this.level<1)
		{
			jQuery('.g_layer_mask').css('visibility','hidden');
			jQuery('.g_layer_mask').width(1);
			jQuery('.g_layer_mask').height(1);
			jQuery('embed,object,select').css('visibility','visible');
			this.level=0;
		}
	},
	close:function()
	{
		this.level=0;
		this.hide();
	},
	resize:function()
	{
		if($_Global_Mask.level<1)return ;
		var root=document.documentElement;
		jQuery('.g_layer_mask').css(
		{
			'width':Math.max(root.scrollWidth,root.clientWidth,root.scrollWidth||0)-1+'px',
			'height':Math.max(root.scrollHeight,root.clientHeight,root.scrollHeight||0)-1+'px'
		});
		
	},
	getBody:function()
	{
		if(!document.body)document.write('<body></body>');
		return document.body;
	},
	getMask:function()
	{
		if(jQuery('.g_layer_mask').length<1)
		{
			jQuery(this.getBody()).append(jQuery('<div class="g_layer_mask"></div>'));
		}
		return jQuery('.g_layer_mask');
	},
	setStyle:function(s)
	{
		if(typeof s=='number')
		{	
			this.getMask()[0].style.cssText='filter:alpha(opacity='+s+'); -moz-opacity:'+(s/100)+';opacity:'+(s/100)+';'
			return;
		}
		if(/\#[a-f|A-F|0-9]{6}/.test(s))
			this.getMask().css('background',s);
	}
}
jQuery(window).resize($_Global_Mask.resize);

var $_Global_Layer=function(options)
{
	return new $_Global_Layer.prototype.Init(options);
}
$_Global_Layer.instances=[];
$_Global_Layer.zindex=90001;
$_Global_Layer.prototype={
	Init:function(options)
	{
		//登记实例
		var _id=this.id=$_Global_Layer.instances.length;
		$_Global_Layer.instances[this.id]=this;
		
		//建立窗口节点
		if(jQuery('.g_layer').length<1)
		{
			$_Global_Mask.getBody();
			this.dom=jQuery('<div style="visibility:hidden;" class="g_layer"><div class="g_layer_wrap"><div class="g_layer_head"><div class="g_layer_corner g_layer_corner_top1"></div><div class="g_layer_corner g_layer_corner_top2"><h3>备&nbsp;注</h3><a href="javascript:void(0);" class="g_layer_close" title="关闭窗口">关闭</a></div><div class="g_layer_corner g_layer_corner_top3"></div></div><div class="g_layer_body"">内容</div><div class="g_layer_foot"><div class="g_layer_corner g_layer_corner_bottom1"></div><div class="g_layer_corner g_layer_corner_bottom2"></div><div class="g_layer_corner g_layer_corner_bottom3"></div></div></div><iframe class="g_layer_background" scrolling="no" frameborder="0" src="about:blank"></iframe></div>');
		}else{
			this.dom=jQuery('.g_layer').eq(0).clone();
		}
		this.dom.attr('id','___layer'+this.id+'___');
		jQuery('.g_layer_head',this.dom).css('cursor','');
		this.dom.mousedown(function(){
			$_Global_Layer.instances[_id].dom.css('zIndex',$_Global_Layer.zindex++);							
		})
		jQuery(document.body).append(this.dom);
		
		//返回初始化后实例
		return this.reset(options);
	},
	reset:function(options)
	{
		options=jQuery.extend(
		{
			top:0,
			left:0,
			width:360,
			height:200,
			title:'窗口标题',
			content:'窗口内容',
			center:false,
			fuckflash:false,
			showCloseButton:true,
			showMask:false,
			style:{},
			drag:{handler:'g_layer_head',effect:'g_layer_move'},
			showHook:function(e){},
			hideHook:function(e){}
		},options);
		//回调函数
		this.showHook=options.showHook;
		this.hideHook=options.hideHook;
		this.center=options.center;
		this.fuckflash=options.fuckflash;
		//宽高定位
		this.top(options.top);
		this.left(options.left);
		this.width(options.width);
		this.height(options.height);
		this.setTitle(options.title);
		this.setContent(options.content);
		this.setStyle(options.style);
		//关闭事件
		var _id=this.id;
		jQuery('.g_layer_close',this.dom).unbind('click');
		jQuery('.g_layer_close',this.dom).click(function(event){
			event.preventDefault();
			$_Global_Layer.instances[_id].hide('clickCloseButton');
		}).focus(function(){this.blur();}).mousedown(function(e){e.stopPropagation();});
		if(options.showCloseButton)
			jQuery('.g_layer_close',this.dom).show();
		else
			jQuery('.g_layer_close',this.dom).hide();
			
		//遮蔽层事件
		if(options.showMask&&$_Global_Mask)
		{
			this.showMask=true;
		}
		
		if(options.drag&&this.dom.Drag&&!this.drag)
		{
			this.dom.Drag(options.drag);
			this.drag=true;
		}
	},
	show:function(e)
	{
		this.showHook(e)
		if(this.center)
		{
			var _t = 0; 
			var _l = 0;
			if(/*@cc_on!@*/false && top.document.body && top.document.getElementById("oMain")) {
				_t=top.document.body.scrollTop+((top.document.body.clientHeight-this.height())/2);
			} else {
				_t=document.documentElement.scrollTop+((document.documentElement.clientHeight-this.height())/2);
			}
			var _l=document.documentElement.scrollLeft+((document.documentElement.clientWidth-this.width())/2);
			//top.document.getElementById("oMain").height = document.documentElement.offsetHeight);
			this.top(_t);
			this.left(_l);
		}
		if(this.showMask&&this.dom.css('visibility')!='visible')
		{
			this.dom.css('zIndex',$_Global_Layer.zindex++);
			$_Global_Mask.show(this.fuckflash);
		}
		this.dom.css('visibility','visible');
		jQuery('embed,object,select',this.dom).show();
		return this;
	},
	hide:function(e)
	{
		if(this.dom.css('visibility')=='hidden')return;
		jQuery('embed,object,select',this.dom).hide();
		this.dom.css('visibility','hidden');
		if(this.showMask)
		{
			$_Global_Mask.hide();
		}
		this.hideHook(e);
		return this;
	},
	top:function(n)
	{
		if(n)jQuery(this.dom).css('top',n);
		return parseInt(jQuery(this.dom).css('top'));
	},
	left:function(n)
	{
		if(n)jQuery(this.dom).css('left',n);
		return parseInt(jQuery(this.dom).css('left'));
	},
	width:function(n)
	{
		if(n)
		{
			n=parseInt(n);
			jQuery(this.dom).width(n+2);
			jQuery('.g_layer_background',this.dom).width(n+2);
			jQuery('.g_layer_wrap',this.dom).width(n);
			var _corner=n-(document.all?2:6)-jQuery('.g_layer_corner_top1',this.dom).width()-jQuery('.g_layer_corner_top3',this.dom).width();
			jQuery('.g_layer_corner_top2',this.dom).width(_corner);
			jQuery('.g_layer_corner_bottom2',this.dom).width(_corner);
		}
		return parseInt(jQuery('.g_layer_wrap',this.dom).width());
	},
	height:function(n)
	{
		if(n)
		{
			n=parseInt(n);
			jQuery(this.dom).height(n+2);
			jQuery('.g_layer_background',this.dom).height(n+2);
			jQuery('.g_layer_wrap',this.dom).height(n);
			jQuery('.g_layer_body',this.dom).height(n-2-jQuery('.g_layer_head',this.dom).height()-jQuery('.g_layer_foot',this.dom).height());
		}
		return parseInt(jQuery('.g_layer_wrap',this.dom).height());
	},
	setStyle:function(o)
	{
		if(typeof o=='object')jQuery('.g_layer_body',this.dom).css(o);
	},
	setTitle:function(o)
	{
		jQuery('.g_layer_head h3',this.dom).html(o);
		return this;
	},
	setContent:function(o)
	{
		var _element=jQuery('.g_layer_body',this.dom);
		switch(typeof o)
		{
				case 'object':
					jQuery('.g_layer_body',this.dom).html('').append(jQuery(o).show());
					break;
				case 'string':
					if(/^http:\/\//.test(o))
						jQuery('.g_layer_body',this.dom).html('<iframe class="g_layer_iframe" scrolling="no" width="'+_element.width()+'" height="'+_element.height()+'" frameborder="0" src="'+o+'">');
					else
						jQuery('.g_layer_body',this.dom).html(o);
		}
		return this;
	}
}
$_Global_Layer.prototype.Init.prototype=$_Global_Layer.prototype;

/*兼容壳*/
var ___win_mask___=$_Global_Mask.getMask();
var ___win_pack___=$_Global_Layer();

var Alert=function(options)
{
	if(typeof options=='string')
	{
		options={msg:options};
	}
/*
 *	参数width：窗口宽度
 *	参数height：窗口高度 
 *	参数title：显示窗口标题
 *	参数msg：是必须的其他四个可以没有，如果没有按照默认的
 *	参数enter：返回确认函数
 *	参数cancel：返回取消函数
 *	参数drag：是否拖动默认拖动(false:禁止拖动)
 */
	options=jQuery.extend({
			flg:26,
			width:360,
			height:150,
			showMask:true,
			title:'警告',
			msg:'提示内容',
			center:true,
			enter:function(e){},
			cancel:function(e){}
		},options);
	if(/^#/.test(options.msg))
	{
		options.content=jQuery(options.msg);	
	}else{
		options.content='<div class="g_layer_alert_logo"><div class="g_layer_alert_ico"></div></div><div style="height:'+options.flg+'px;line-height:'+options.flg+'px;"></div>'+options.msg+'<div class="g_layer_alert_buttonbox"><span onfocus="this.blur();" class="g_layer_alert_btn" onclick="___win_pack___.hide(0);">确 定</span>';
	}
	___win_pack___.enter=options.enter;
	___win_pack___.cancel=options.cancel;
	jQuery('.g_layer_body',___win_pack___.dom)[0].style.cssText='padding:10px;line-height:130%;word-break:break-all;word-wrap:break-word';
	options.hideHook=function(s)
	{
		if(s===0)
			___win_pack___.enter(true);
		else
			___win_pack___.cancel(false);
	}
	options.fuckflash=true;
	___win_pack___.reset(options);
	___win_pack___.show();
	if(typeof options.timeout=='number')
		setTimeout(function(){___win_pack___.hide();},options.timeout);
}

var Confirm=function(options)
{
	if(typeof options=='string')
	{
		options={msg:options};
	}
/*
 *	确认框使用说明，参数顺序可以任意，参数个数任意
 *	参数msg：是必须的其他四个可以没有，如果没有按照默认的
 *	参数title：显示窗口标题
 *	参数enter：返回确认函数
 *	参数width：窗口宽度
 *	参数height：窗口高度
 *	参数cancel：返回取消函数
 *	参数drag：是否拖动默认拖动(true:拖动;false:禁止拖动)
 */
	options=jQuery.extend({
			flg:26,
			width:360,
			height:150,
			showMask:true,
			title:'确认',
			msg:'提示内容',
			center:true,
			enter:function(e){},
			cancel:function(e){}
		},options);
	if(/^#/.test(options.msg))
	{
		options.content=jQuery(options.msg);	
	}else{
		options.content='<div class="g_layer_alert_logo"><div class="g_layer_alert_ico"></div></div><div style="height:'+options.flg+'px;line-height:'+options.flg+'px;"></div>'+options.msg+'<div class="g_layer_alert_buttonbox"><span onfocus="this.blur();" class="g_layer_alert_btn" onclick="___win_pack___.hide(0);">确 定</span><span  class="g_layer_alert_btn" onclick="___win_pack___.hide(1);">取 消</span></div>';
	}
	___win_pack___.enter=options.enter;
	___win_pack___.cancel=options.cancel;
	jQuery('.g_layer_body',___win_pack___.dom)[0].style.cssText='padding:10px;line-height:130%;word-break:break-all;word-wrap:break-word';
	options.hideHook=function(s)
	{
		if(s===0)
			___win_pack___.enter(true);
		else
			___win_pack___.cancel(false);
	}
	options.fuckflash=true;
	___win_pack___.reset(options);
	___win_pack___.show();
	if(typeof options.timeout=='number')
		setTimeout(function(){___win_pack___.hide();},options.timeout);
}

var winOpen=winIframe=function(options)
{
/*
 *	Iframe框使用说明(如果想在iframle里边关闭图层调用方法：parent.hidePopWin(false))
 *	参数url：是必须的其他四个可以没有，如果没有按照默认的
 *	参数title：显示窗口标题
 *	参数width：窗口宽度
 *	参数height：窗口高度
 *	参数drag：是否拖动默认拖动(true:拖动;false:禁止拖动)
 */
	if(typeof options=='string')
	{
		options={msg:options};
	}
	options=jQuery.extend({
			width:360,
			height:130,
			title:'系统提示',
			msg:'内容',
			center:true,
			showMask:true
	},options);
	if(/^#/.test(options.msg))
	{
		options.content=jQuery(options.msg);
	}else
	{
		options.content=options.msg;
	}
	jQuery('.g_layer_body',___win_pack___.dom)[0].style.cssText='';
	___win_pack___.reset(options);
	___win_pack___.show();
	if(typeof options.timeout=='number')
		setTimeout(function(){___win_pack___.hide();},options.timeout);
}
function hidePopWin(e)
{
	___win_pack___.hide(e);	
}