(function($){
	$.alerts = {
		alert : function(o,options){
			var defaults = {
				title : '标题',
				content : '内容',
				GetType : 'string',		//controls,ajax,string,iframe
				IsDrag : true,
				IsCover: true,
				Url : '',
				Data : null,
				host:'http://files.jxxgx.com',
				skin:'',
				width:400,
				height:300,
				bgcolor:'#ffffff',
				callback : function(){},
				close : function(){}
			}
			
			var options = $.extend(defaults,options);
			if(!$("#window")[0])
			{
				var skin = options.skin==''?'':'blog/'+options.skin;
				$.alerts._createObject(options.host,skin);
			}
			
			var position = $.alerts._getPosition(o);				
			$("#windowContent").hide();			
			$("#windowContent").css({background:options.bgcolor});			
			//$.alerts._setSize(position);			
			$("#window").css(
				{
					width:position.w,
					height:position.h,
					top:position.t,
					left:position.l,
					zIndex:1002
				}
			);
			$("#windowBottom,#windowBottomContent").css(
				{
					height:options.height-30
				}
			);
			$("#windowContent").css(
				{
					height:options.height - 45,
					width:options.width - 25,
					background:options.bgcolor
				}
			);		
			$("#windowBottomContent,#windowTop,#windowBottom,#windowClose").addClass("transferer2");
			$("#windowTopContent").html(options.title);
			switch(options.GetType){
				case "string":
					$("#windowContent").html(options.content);
				break;
				case "ajax":
					if(options.Url == ''){
						alert("AjaxUrl不能为空");
						return;
					}else{
						$.ajax(
							{
								type: "POST",
								url: options.Url,
								data: options.Data,
								success: function(msg){
									$("#windowContent").html(msg);
								}

							}
						);
					}
				break;
				case "controls":
					var oID = "";
					if(options.content.substring(0,1) == "#")
						oID = options.content;
					else
						oID = "#"+options.content;
					$("#windowContent").children().hide();
					$(oID).appendTo("#windowContent");	
					$(oID).show();				
				break;
				case "iframe":
					$("#windowContent").empty();
					$("<iframe>").attr(
						{
							src : options.Url,
							width:options.width,
							height:options.height
						}
					).appendTo("#windowContent");
				break;
			}
			var winPosition = $.alerts._getWindowPosition(options);
			$("#window").animate(
				{
					left:winPosition.l,
					top:winPosition.t,
					height:winPosition.h,
					width:winPosition.w
				},500,function(){						
					//$("#windowContent").fadeIn('slow');					
					$("#windowContent").fadeIn(300,function(){
						if($.isFunction(options.callback)){
							//options.callback();							
							options.callback.apply( this, arguments );
						}
					});
					$("#windowBottomContent,#windowTop,#windowBottom,#windowClose").removeClass("transferer2");					
					if(options.IsCover){
						if($("#middleElement_bgDiv").get().length == 0){
							$("<div>").attr("id","middleElement_bgDiv").css({zIndex:"1000"}).appendTo("body");	
							$("<iframe>").attr("id","bgIframe").css({zIndex:"1001"}).appendTo("body");	
							$("#middleElement_bgDiv,#bgIframe").css(
								{
									position:"absolute",
									left:"0px",
									top:"0px",
									width:$(window).width()+"px",
									height:Math.max($("body").height(),$(window).height())+"px",
									filter:"Alpha(Opacity=40)",
									opacity:"0.4",
									backgroundColor:"#AAAAAA",									
									margin:"0px",
									padding:"0px"
								}	
							);							
						}else{
							$("#middleElement_bgDiv,#bgIframe").show();
						}
					}

					
				}
			);
			
			/*$(window).scroll(function(){
				var t = parseInt($(window).height()/6)+parseInt($(window).scrollTop());
				$("#window").animate({top:t},10);
			});	*/
			$("#windowClose").one("click",function(){
				$("#windowBottomContent,#windowTop,#windowBottom,#windowClose").addClass("transferer2");
				$("#windowContent").fadeOut(300,function(){
					
					$("#window").animate(
						{
							left:position.l,
							top:position.t,
							height:position.h,
							width:position.w
						},500,function(){
							$(this).hide();
							$("#windowBottomContent,#windowTop,#windowBottom,#windowClose").removeClass("transferer2");	
							if($("#middleElement_bgDiv").get().length > 0){
								$("#middleElement_bgDiv,#bgIframe").hide();
							}
							$("#window").css(
								{
									left:winPosition.l,
									top:winPosition.t,
									height:winPosition.h,
									width:winPosition.w
								}
							);
						}
					);

					if($.isFunction(options.close)){										
						options.callback.apply( this, arguments );
					}

				});			
				
			});

			$("#windowTop").mousedown(function(event){
				$.alerts.Drag(
					document.getElementById("window"),
					{					
						e : event,							
						Drag : options.IsDrag
					}
				);
			});
			
		},		
		_createObject : function(host,skin){			
			$("<div id=\"window\">"+
				"<div id=\"windowTop\">"+
					"<div id=\"windowTopContent\">Window example</div>"+
					//"<img src=\""+host+"/images/Alert/window_min.jpg\" id=\"windowMin\" />"+
					//"<img src=\""+host+"/images/Alert/window_max.jpg\" id=\"windowMax\" />"+
					"<img src=\""+host+"/images/"+skin+"/Alert/window_close.jpg\" id=\"windowClose\" />"+
				"</div>"+
				"<div id=\"windowBottom\"><div id=\"windowBottomContent\">&nbsp;</div></div>"+
				"<div id=\"windowContent\"></div>"+					
				//"<img src=\""+host+"/images/Alert/window_resize.gif\" id=\"windowResize\" />"+
			"</div>").appendTo("body");		
			
		},		
		_setSize : function(param){					
			$("#window").css(
				{					
					height:param.h					
				}
			);	
			$("#windowBottom,#windowBottomContent").css(
				{
					height:param.h-30
				}
			);
			$("#windowContent").css(
				{
					height:param.h - 45				
				}
			);			
			//$("#windowBottom,#windowBottomContent").height(parseInt(param.h-30));			
			//$("#windowContent").height(parseInt(param.h-45));
			//$("#windowContent").width(parseInt(param.w-25));			
				
		},
		_getWindowPosition : function(options){
			//var wPosition = $.alerts._getPosition("#window");
			var windowPosition = {};				
			windowPosition.t = parseInt($(window).height()/6)+parseInt($(window).scrollTop());			
			windowPosition.l = ($(window).width()+$(window).scrollLeft())/2 - options.width/2;				
			windowPosition.w = options.width;			
			windowPosition.h = options.height;			
			return windowPosition;
		},
		_getPosition : function(o){
			var top = $(o).offset().top;
			var left = $(o).offset().left;
			var height = $(o).height();
			var width = $(o).width();
			//alert("t:"+top+"l:"+left);
			return {t:top,l:left,h:height,w:width};
		},
		Drag : function(Obj,options){
			var pX,pY;
			var event = options.e;
			document.onmouseup = MUp;
			document.onmousemove = MMove;
			MDown(event);
			function MDown(event) {                    
				if (Obj.setCapture) 
					Obj.setCapture();
				else 
					window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
				pX = event.clientX - Obj.offsetLeft;
				pY = event.clientY - Obj.offsetTop;
			}
			function MMove(event) {
				if (window.event) event = window.event;
				if (Obj) {
					Obj.style.left = event.clientX - pX + "px";
					Obj.style.top = event.clientY - pY + "px";
				}
			}
			function MUp(event) {
				if (Obj) {
					if (Obj.releaseCapture)
							Obj.releaseCapture();
					else 
						window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
					Obj = null;
				}
			}

		}
	}
	JAlert = function(o,json){
		$.alerts.alert(o,json);
	};
})(jQuery);