/**
 * jQuery扩展（弹出层）
 * 外部关闭弹出层方法：$.nav_popup.removeBox();
 */
;(function(){	   
	$.nav_popup=function(options){
		options = $.extend({
			___title: "Hello World",	//窗口标题文字
			___content: "text:内容",	//内容(可选内容为){ text | id | img | url | iframe }
			___width: "300",			//窗口宽度
			___height: "200",			//窗口离度
			___titleClass: "nav_boxTitle",	//窗口标题样式名称
			___closeID:"",				//关闭窗口ID
			___time:"",					//自动关闭等待时间
			___drag:"",					//拖动手柄ID
			___showbg:false,			//是否显示遮罩层
			___fns:function(){}			//关闭窗口后执行的函数
		},options);
		if ($('#___box').length==0)$.nav_popup.init(options);
	};
	$.extend($.nav_popup,{
		//初始化
		init: function (options){
			$.nav_popup.showBox(options);
			$("#___boxTitle>span").live("click",function(){	//默认关闭按钮
				$.nav_popup.removeBox();
			});
			if(options.___closeID != ""){
				$("#"+options.___closeID).live("click",function(){	
					if(options.___fns != "" && $.isFunction(options.___fns)) {
						options.___fns.call(this);
					}
					$.nav_popup.removeBox();
				});
			}
			if(options.___time != "") {
				setTimeout($.nav_popup.removeBox,options.___time);
			}
			if(options.___showbg != "" && options.___showbg == true){
				var $boxBgDom = "<div id=\"nav_boxBg\" style=\"position:absolute;width:100%;height:"+$(document).height()+"px;left:0;top:0;z-index: 999991\"></div>";
				$("body").append($boxBgDom);
			}
			if(options.___drag != "") {
				$.nav_popup.dragBox(options);
			};
			if(options.___showbg != true){
				$box = $("#___box");
				$box.addClass("nav_shadow");
			}
			$.nav_popup.contentBox(options);
			$.nav_popup.keyDown();
		},
		//构造并定位弹出层
		showBox: function(options) {
			var isIE6 = !-[1,] && !window.XMLHttpRequest;
			var	___width = options.___width < 100 ? 100 : options.___width,
				___height= options.___height < 50 ? 50 : options.___height;//设置最小宽高
			var $width = parseInt(options.___width) > 1000 ? parseInt(options.___width) : parseInt(options.___width),
				$height = parseInt(options.___height) > 550 ? 550 : parseInt(options.___height);//设置最大宽高
			var $boxDom = "<div id=\"___box\" class=\"nav_box\">";
				$boxDom += "<div id=\"___boxTitle\"><h3></h3><span>关闭</span></div>";
				$boxDom += "<div id=\"___boxContent\" class=\"nav_boxContent\"></div>";
				$boxDom += "</div>";
				$("body").append($boxDom);
			var $box = $("#___box"),
				$boxContent = $("#___boxContent");
				$boxContent.css({
					width:___width+"px",
					height:___height+"px"
				});
			if(!isIE6){
				$box.css({
					position:"fixed",
					left:"50%",
					top:"50%",
					width:$width+"px",
					height:$height+"px",
					marginLeft:-(parseInt($width/2))+"px",
					marginTop:-(parseInt($height/2)+5)+"px",
					zIndex: "999999"
				});
			}else{
				$box.css({
					position:"absolute",
					left:"50%",
					top:document.documentElement.offsetHeight/2-$height/2-5+"px",
					width:$width+"px",
					height:$height+"px",
					marginLeft:-(parseInt($width/2))+"px",
					zIndex: "999999"
				});
				$(window).resize(function(){//当窗口大小发生变化时修正top值
					$box.css({
						top:document.documentElement.offsetHeight/2-$height/2-5+"px"
					});
				});
				$("select").css("visibility","hidden");//IE6下隐藏下拉菜单
				$("body").css("background-attachment","fixed").css("background-image","url(about:blank)");
				var $layer = document.createElement("<div style='position:absolute;border:0;padding:0;margin:0;overflow:hidden;background:transparent;top:expression((document).documentElement.scrollTop);left:expression((document).documentElement.scrollLeft);width:expression((document).documentElement.clientWidth);height:expression((document).documentElement.clientHeight);display:block;z-index:999992'>");
				$("body").append($layer);
				$box.appendTo($layer);//IE6下Fixed定位
			}
			var $title = $("#___boxTitle>h3");
				$title.html(options.___title);
			if(options.___titleClass != ""){
				$title.parent().addClass(options.___titleClass);
				$title.parent().find("span").hover(function(){
					$(this).addClass("hover");
				},function(){
					$(this).removeClass("hover");
				});
			}
		},
		//关闭弹出层
		removeBox: function (options){
			var $box = $("#___box");
			var $boxbg = $("#nav_boxBg");
				$("select").css("visibility","visible");//关闭弹出层后显示下拉菜单
			if($box != null || $boxbg != null){
				$box.remove();
				$boxbg.remove();
			}
		},
		//健盘事件，当按Esc的时候关闭弹出层
		keyDown: function() {
			$(document).keydown(function(e) {
				e = e || event;
				if(e.keyCode == 27) $.nav_popup.removeBox();
			})
		},
		//绑定拖拽
		dragBox: function (options){
			var moveX = 0,moveY = 0,
				drag = false;
			var ___ID = document.getElementById("___box"),
				___Handle = document.getElementById(options.___drag);
			___Handle.onmouseover = function() {
				this.style.cursor = "move";
			}
			___Handle.onmousedown = function(e) {
				drag = true;
				e = window.event?window.event:e;
				moveX = e.clientX - ___ID.offsetLeft;
				moveY = e.clientY - ___ID.offsetTop;
				document.onmousemove = function(e) {
					if (drag) {
						e = window.event?window.event:e;
						window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();//阻止浏览器默认选取
						var x = e.clientX - moveX;
						var y = e.clientY - moveY;
						if ( x > 0 &&( x + ___ID.scrollWidth < document.documentElement.clientWidth) && y > 0 && y + ___ID.scrollHeight < document.documentElement.clientHeight ) {
							___ID.style.left = x + "px";
							___ID.style.top = y + "px";
							___ID.style.margin = "auto";
						}
					}
				};
				document.onmouseup = function(){
					drag = false;
				};
			};
		},
		//装载弹出层内容
		contentBox: function (options) {
			var $contentID = $("#___boxContent");
				$contentType = options.___content.substring(0,options.___content.indexOf(":"));
				$content = options.___content.substring(options.___content.indexOf(":")+1,options.___content.length);
			var isIE6 = !-[1,] && !window.XMLHttpRequest;
			switch($contentType) {
				case "text":
					$contentID.html($content);
				break;
				case "id":
					$contentID.html($("#"+$content).html());
				break;
				case "img":
				
				$.ajax({
					error:function(){
						$contentID.html("<p class='nav_boxError'>加载数据出错...</p>");
					},
					success:function(html){
						$contentID.html("<img src="+$content+" alt='' />");
					}
				});
				break;
				case "url":
				var contentDate=$content.split("?");
				
				$.ajax({
					type:contentDate[0],
					url:contentDate[1],
					data:contentDate[2],
					error:function(){
						$contentID.html("<p class='nav_boxError'>加载数据出错...</p>");
					},
					success:function(html){
						$contentID.html(html);
					}
				});
				break;
				case "iframe":
				$contentID.css({overflowY:"hidden"});
				
				$.ajax({
					error:function(){
						$contentID.html("<p class='nav_boxError'>加载数据出错...</p>");
					},
					success:function(html){
						$contentID.html("<iframe src=\""+$content+"\" width=\"100%\" height=\""+parseInt(options.___height)+"px\" scrolling=\"auto\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\"></iframe>");
					}
				});
			}
		}
	});
})(jQuery);
