/*
* jQuery Floatbox Plugin 1.0.7
* Copyright (c) 2008 Leonardo Rossetti (motw.leo@gmail.com)
* Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
*/
(function($){
	$.floatbox=function(options){
		
		var settings=$.extend(
			true,
			{
				//bg:"floatbox-background",
				box:"floatbox_box",
				content:"",
				//button:"<a role='button' href='javascript:void(0);' class='close-floatbox'>关闭</a>",
				fade:false,
				title:"新开窗口",
				bgConfig:{
					zIndex:8
				},				
				boxConfig:{
					zIndex:9,
					width:500,
					height:400
				},
				boxContentConfig:{
					zIndex:9
				}
				
			},options);
			
			var showBox=function(){
				var button="<a role='button' href='javascript:void(0);' id='close_floatbox_"+settings.box+"' class='closebtn'>关闭</a>";
				var content=typeof settings.content==="string"?settings.content:settings.content.clone();
				$("<div></div>")

				.attr("id","bg_"+settings.box).css(settings.bgConfig).addClass("bgConfig").height(($.browser.msie)?document.body.clientHeight:"100%").appendTo("body");
				
				$("<div><p class='boxfoot'></p><iframe id='"+settings.box+"_mask' style='position:absolute;z-index:-1;width:100%;height:100%;top:0;left:0;scrolling:no;' frameborder='0' src='about:blank'></iframe></div>").attr({id:settings.box,role:"alertdialog"}).append($("<div id='"+settings.box+"_title' class='boxTitle'>"+"<div style='float:left'>"+settings.title+"</div>"+"<div class='boxClose'>"+button+"</div></div>")).css(settings.boxConfig).addClass("boxConfig").appendTo("body").find("#close_floatbox_"+settings.box).bind("click",function(){closeBox();}).end();

				$("<div></div>").attr({id:settings.box+"_content"}).css(settings.boxContentConfig).addClass("boxContentConfig").appendTo("#"+settings.box).html(content).end();
				
				//左右居中
				$("#"+settings.box).css("left",($(window).width()-settings.boxConfig.width)/2+'px');
				//$("#"+settings.box).css("top",($(window).height()-settings.boxConfig.height)/2+'px');
				
				if(settings.fade){
					$("#bg_"+settings.box).fadeIn(
						200,
						function(){
							$("div#"+settings.box).fadeIn(200);
						}
					);
				}
				else{
					$("#bg_"+settings.box).show().parent().find("#"+settings.box).show();
				}
				
				};
				
				var closeBox=function(){
					if(settings.fade){
						$("#"+settings.box).fadeOut(
							200,
							function(){
								$("#bg_"+settings.box).fadeOut(
									200,
									function(){
										$("#"+settings.box+"_content").remove();
										$("#"+settings.box+"_title").remove();
										$("#"+settings.box+"_mask").remove();
										$("#"+settings.box).remove();
										$("#bg_"+settings.box).remove();
									}
								);
							}
						);
					}else{
						$("#"+settings.box+",#bg_"+settings.box).hide();
						//setTimeout(
							//function(){
								$("#"+settings.box+"_content").remove();
								$("#"+settings.box+"_title").remove();
								$("#"+settings.box+"_mask").remove();
								$("#"+settings.box).remove();
								$("#bg_"+settings.box).remove();
							//},
							//500
						//);
					}
				};
	
					var init=function(){
						//先关闭
						closeBox();
						
						showBox();
						/**
						$(document).one(
							"keypress",
							function(e){
								var escKey=$.browser.mozilla?0:27;
								if(e.which===escKey){
									closeBox();
								}
							}
						).one(
							"keydown",
							function(e){
								var escKey=$.browser.mozilla?0:27;
								if(e.which===escKey){
									closeBox();
								}
							}
						);	
						*/
						if($.browser.msie){
							$("body, html").css({height:"100%",width:"100%"});
							/**
							$(window).bind(
								"scroll",
								function(){
									$("#"+settings.box).css("top",document.documentElement.scrollTop+($(window).height()/2)+"px");
								}
							);
							*/
						}
						
						//设为可拖动
						//$("#"+settings.box).draggable({ handle: "#"+settings.box+"_title" });
						$("#"+settings.box).jqDrag("#"+settings.box+"_title");

					};
					
					init();
					
				};
			}
		)(jQuery);
