//author    ：  陈进福/cjinfu/梦乐标本
//function  ：  弹窗拖动
//first time：  10:14 2009-8-2
//last time ：  10:14 2009-8-2
(function($){
	$.fn.TM_UE_AlertDrag=function(opt){
		//settings
		var settings=jQuery.extend(
			{
				limit:false,//是否做特定范围限定定
				mask_on_off:true,//是否启用遮罩
				mask_alpha:50,//遮罩半透膜
				mask_id:"TM_UE_AlertMask",//遮罩默认id名，正常情况下不要去设置该id除非页面上有该id产生了冲突
				close_handle:false,//关闭手柄
				move_handle:false,//移动手柄
				alert_top:false,//初始定位坐标值
				alert_left:false//初始定位坐标值
			},
			$.fn.TM_UE_AlertDrag.defaults,
			opt
		);
		//settings reset
		if(settings.limit){
			settings.mask_on_off=false;
		}else{
			if(settings.mask_on_off){
				settings.limit="#"+settings.mask_id;
			}
		}
		//out interface
		$.fn.TM_UE_AlertDrag.$this=$(this);
		$.fn.TM_UE_AlertDrag.settings=settings;
		//each play
		return $(this).each(function(){
			AutoPlay($(this),settings);
		});
    };
    //autoplay
	var AutoPlay=function($this,settings){
		//this x and y
		var tx=0,ty=0;
		//currently x and y
		var cx=0,cy=0;
		//drop x and y
		var dx=0,dy=0;
		//now x and y
		var nx=0,ny=0;
		
		//start
		var Start=function(e){			
			cx=e.pageX;
			cy=e.pageY;
			tx=$this.offset().left,
			ty=$this.offset().top;
			dx=cx-tx;
			dy=cy-ty;
			//
			$(document).mousemove(Move);
			$(document).mouseup(Stop);
			//
			if ($.browser.msie){$this[0].setCapture();}
			return false;
		};
		//move
		var Move=function(e){
			cx=e.pageX;
			cy=e.pageY;
			nx=cx-dx;
			ny=cy-dy;
			var limit=settings.limit;
			if(limit){
				//box left top right bottom
				var L=$(limit).offset().left;
				    T=$(limit).offset().top;
				    R=L+$(limit).width();
				    B=T+$(limit).height();
				//overflow box
				var oRight=nx+parseInt($this.outerWidth())-R,
					oBottom=ny+parseInt($this.outerHeight())-B;
				//overflow position
				if(oRight>0)nx-=oRight;					
				if(oBottom>0)ny-=oBottom;
				if(L>nx)nx=L;
				if(T>ny)ny=T;
			}
			$this.css({left:nx,top:ny});
			return false;
		};
		//stop
		var Stop=function(e){
			$(document).unbind('mousemove');
			$(document).unbind('mouseup');
			if($.browser.msie){$this[0].releaseCapture();}
			return false;
		};
		//
		var move_handle=settings.move_handle;
		if(move_handle){
			$this.css({cursor:"default"});
			$this.find(move_handle).css({cursor:"move"});
			$this.unbind("mousedown");
			$this.find(move_handle).unbind("mousedown");
			$this.find(move_handle).mousedown(Start);
		}else{
			$this.css({cursor:"move"});
			$this.unbind("mousedown");
			$this.mousedown(Start);
		}
		return false;
	};
	//open alert
	$.fn.TM_UE_AlertDrag.Open=function(){
		//init
		var $this=this.$this,
			settings=this.settings;
		var alert_top=settings.alert_top,
			alert_left=settings.alert_left,
			top=alert_top,left=alert_left;
		//not limit
		var mask_id=settings.mask_id;
		if(!settings.limit || settings.limit=="#"+mask_id){
			//num
			var body=(document.compatMode.toLowerCase()=="css1compat")?document.documentElement:document.body;
			var bodyoffsetwidth=body.offsetWidth,
				bodyoffsetheight=body.offsetHeight,
				bodyscrollwidth=body.scrollWidth,
				bodyscrollheight=body.scrollHeight,
				bodyclientwidth=body.clientWidth,
				bodyclientheight=body.clientHeight,
				bodyscrolltop=body.scrollTop,
				bodyscrollLeft=body.scrollLeft;
				var thiswidth=$this.width(),
				thisheight=$this.height();
			//create mask
			var mask_alpha=settings.mask_alpha;
			if(settings.mask_on_off){
				$("body").append('<div id="'+mask_id+'" style="position:absolute;width:100%;height:'+Math.max(bodyoffsetheight,bodyscrollheight,bodyclientheight)+'px;left:0;top:0;right:0;bottom:0;background:#333;filter:alpha(opacity='+mask_alpha+');opacity:0.'+mask_alpha+';-moz-user-select:none;z-index:10000;display:none;"></div>');
				$("#"+mask_id).click(function(){
					$this.animate({borderWidth:"5px"},40);
					$this.animate({borderWidth:"1px"},40);
					$this.animate({borderWidth:"5px"},40);
					$this.animate({borderWidth:"1px"},40);
				});
				$("#"+mask_id).fadeIn("fast");
			}
			//
			if(!alert_top)top=bodyclientheight/2-thisheight/2+bodyscrolltop;
			if(!alert_left)left=bodyoffsetwidth/2-thiswidth/2+bodyscrollLeft;
			//window scroll
			if(!alert_top && !alert_left){
				$(window).scroll(function(){
					$this.css({left:left+"px",top:(bodyclientheight/2-thisheight/2+body.scrollTop)+"px"});
				});
			}
		}else{
			if(!alert_top) top=0;
			if(!alert_left) left=0;
		}
		$this.css({left:left+"px",top:top+"px",zIndex:10001});
		//
		$this.fadeIn("fast");
		//close alert
		var close_handle=settings.close_handle;
		if(close_handle){
			$this.find(close_handle).click(function(){
				$.fn.TM_UE_AlertDrag.Close();
			});
		}
	}
	//close alert
	$.fn.TM_UE_AlertDrag.Close=function(){
		//init
		var $this=this.$this,
			settings=this.settings;
		//
		if(settings.mask_on_off){
			$("#"+settings.mask_id).fadeOut("fast");
			//一定要remove否则开关多次后将产生n个遮罩层，如果遮罩层有动作，将产生n个动作脚本。
			$("#"+settings.mask_id).remove();
		}
		$this.fadeOut("fast");
		//这里到时可以做一个固定位置的按钮
		//unbind mousedown侦听
		//取消滚动侦听
		$(window).unbind("scroll");
	}
})(jQuery);