/**
 * fileName ： ilinju_createDiv.js
 * 自动创建div层，底层遮罩灰度效果
 * 必须提供参数
 * divH 层的高度
 * divW 层的宽度
 * i_title 空不显示标题，否则显示
 * i_content 需要填充的内容
 */

function addModalDiv(divH, divW, i_title, i_content) {
	var z = 300;
	var divH = (divH < 0)?300:divH;
	var divW = (divW < 0)?650:divW;
	var i_title = (i_title == '')?'':'<div id="div_title">' + i_title + '<div id="div_close" title="关闭"></div></div>';
	var divT = parseInt($(window).height() - divH) / 2;
	var divL = parseInt($(window).width() - divW) / 2;

	$('html,body').css( {
		height : '100%',
		width : '100%'
	});
	/* 创建灰色半透明遮罩DIV层 */
	$('<div>', {
		id : 'div_gray',
		css : {
			'width' : '100%',
			'height' : '100%',
			'z-index' : parseInt(z - 1)
		}
	}).show('slow').appendTo("body");
	
	/* 创建内容填充容器DIV层 */
	$('<div>',
		  {
			  id : 'div_body',
			  css : {
				  height : divH,
				  width : divW,
				  top : divT,
				  left : divL,
				  'z-index' : z
			  },
			  html : i_title + '<div id="div_html">' + i_content + '</div>'
		  }).show('slow').appendTo("body");
		  
	/* 销毁 半透明层，容器层 */
	$('#div_close').click(function() {
		clearDiv();
	});
	$('#div_gray').click(function() {
		clearDiv();
	});
	
	/* 内容高度 */
	$('#div_html').css( {
		height : (i_title == '')?divH:(divH - $('#div_title').height())
	});

	/* 拖拽 */
	var i_obj = $('#div_body');
	var i_ott = $('#div_title');
	var flag = false;

	i_obj.mousemove(function(event) {
		if (flag) {
			var myEvent = event || window.event;
			changeXY(i_obj, myEvent);
		}
	});
	i_obj.mouseup(function() {
		i_ott.unbind("mousemove");
		flag = false;
	});
	i_ott.mouseup(function() {
		i_ott.unbind("mousemove");
		flag = false;
	})
	i_ott.mouseout(function() {
		i_ott.unbind("mousemove");
	})

	i_ott.mousedown(function(event) {
		var t = $(this);
		var myEvent = event || window.event;
		fx = myEvent.clientX;
		fy = myEvent.clientY;
		t.mousemove(function(event) {
			var myEvent = event || window.event;
			changeXY(i_obj, myEvent);
			flag = true;
		});
	})

}

/* 移除创建的DIV */
function clearDiv(){
	$('#div_gray, #div_body').hide('slow',function(){$('#div_gray, #div_body').remove();});	
	//$('#div_gray, #div_body').remove();	
}

/* 实时更改坐标函数 */
function changeXY(i_obj, myEvent) {
	if (fx > myEvent.clientX) {
		var change_top_x = parseInt(i_obj.css("left")) - (fx - myEvent.clientX);
		// 测试是否超出左边窗口
		if (change_top_x < 5) {
			change_top_x = 5;
			// flag = false;
			// title.unbind("mousemove");
		}
		i_obj.css("left", change_top_x + "px");
		fx = myEvent.clientX;
	}
	if (fx < myEvent.clientX) {
		var change_top_x = parseInt(i_obj.css("left")) + (myEvent.clientX - fx);
		// 测试是否超出窗口右边
		if (change_top_x > ($(window).width() - parseInt(i_obj.css("width")) - 5)) {
			change_top_x = ($(window).width() - parseInt(i_obj.css("width")) - 5);
			// flag = false;
			// title.unbind("mousemove");
		}
		i_obj.css("left", change_top_x + "px");
		fx = myEvent.clientX;
	}
	if (fy > myEvent.clientY) {
		var change_top_y = parseInt(i_obj.css("top")) - (fy - myEvent.clientY)
		// 测试是否超出窗口顶部
		if (change_top_y < 5) {
			change_top_y = 5;
		}
		i_obj.css("top", change_top_y + "px");
		fy = myEvent.clientY;
	}
	if (fy < myEvent.clientY) {
		var change_top_y = parseInt(i_obj.css("top")) + (myEvent.clientY - fy);
		// 测试是否超出窗口底部
		if (change_top_y > ($(window).height() - parseInt(i_obj.css("height")) - 5)) {
			change_top_y = ($(window).height() - parseInt(i_obj.css("height")) - 5);
		}
		i_obj.css("top", change_top_y + "px");
		fy = myEvent.clientY;
	}
}

