// layers.js
var g_OO = null; // g_OperatingObject
var g_oldX, g_oldY;
var g_OT= 0; // g_OT
var g_Direction = null;
var g_CtrlPressed= true;


/** 鼠标移入层的事件回调
  */
function LayerOnMouseOver(obj) {
	if (g_OT!=0) return;

	g_OO= obj;
	if(g_OO.resiziable==true) {
		g_Direction= getDirection(obj);
		if (g_Direction != "") {
			g_OO.style.cursor= g_Direction + "-resize";
		}
	}
	g_OO.style.cursor="default";
	document.onmousemove= LayerOnMouseMove;
	top.g_type=obj.type;
	top.g_itemno=obj.itemno;
}

/** @note 鼠标移出层的事件回调
  */
function LayerOnMouseOut (obj) {
	if (g_OT!=0) return;
	document.oncontextmenu = function() {return false;}
	g_OO= null;
	document.onmousemove= null;
	document.oncontextmenu = function() {return false;}
	}

/** @note 鼠标在层上移动的事件回调
  */
function LayerOnMouseMove () {
	g_Direction= getDirection(g_OO);
	if (g_Direction != "") {
		g_OO.style.cursor= g_Direction + "-resize";
	}
	else {
		g_OO.style.cursor="default";
	}
}

function getDirection (el) {
	dir = "";
	xPos = event.x; yPos = event.y;
	objPosx = initd.style.pixelLeft - initd.scrollLeft - body.scrollLeft + el.style.pixelLeft + el.style.pixelWidth;
	objPosy = initd.style.pixelTop - initd.scrollTop - body.scrollTop + el.style.pixelTop + el.style.pixelHeight;

	offset = 5; //The distance from the edge in pixels
	if (Math.abs(xPos-objPosx) < offset) {
		dir= "e";
	} else if(Math.abs(yPos-objPosy) < offset) {
		dir= "s";
	}
	return dir;
}

/*  @note 开始对一个层的操作
	@note g_OT   == 1  移动
						    == 2  改变大小
						    == 3  删除 (在按住CTRL时进行)
 */
function LayerStartOperate()
{	if(event.button==1) {
		if (g_OO == null) return;
		g_OO.style.zIndex=100; // 把当前操作的层提到最上层
		/// 判断操作类型
		g_Direction= getDirection(g_OO);
		if (g_Direction != "") {
			g_OT= 2;
			if(g_OO.resiziable==true){
				document.onmousemove= LayerResize;
			}
		}
		else {
			g_OT= 1;
			document.onmousemove= LayerMoving;
			g_OO.style.cursor="move";
		}
		//-

		g_oldX= event.x;
		g_oldY= event.y;
		document.onmouseup= LayerEndOperate;
		// 禁止文字选择
		document.body.onselectstart = new Function("return false");
		return false;
	}
	else if (event.button==2&&g_type==2||event.button==2&&g_type==3||event.button==2&&g_type==12) {
		top.document.oncontextmenu = showmenuie5;
		top.document.onclick = hidemenuie5;
	}else {
		return false;
	}
}

/** @note 停止对一个层的操作
 */
function LayerEndOperate() {
	if (g_OT == 0) return false;
	g_OT= 0;
	document.onmousemove= null;

	g_OO.style.zIndex = 0;
	g_OO.style.cursor= "default";
	document.onmouseup= null;
	g_Direction= null;
	// 恢复文字选择
	document.body.onselectstart = null;
	return false;
}

/* @note 对层的点击
 */
function LayerOnClick() {
	if (g_CtrlPressed == true) {
		var len= g_WndArray.length;
		for (var i=0; i < len; i++)
		{
			if ((g_WndArray[i].type == 99)&&(g_WndArray[i].div.id == g_OO.id)) {
				g_EditFlag = 1;
				deleteWindowByTypeAndItemno(99, g_WndArray[i].itemno);
				return false;
			}
		}
	}
	return false;
}

/*  @note 一个层的拖动
 */
function LayerMoving()
{
	if (g_OT != 1) return;
	g_OO.style.pixelLeft = g_OO.style.pixelLeft + (event.x- g_oldX);
	g_OO.style.pixelTop = g_OO.style.pixelTop + (event.y- g_oldY);
	g_oldX= event.x;
	g_oldY= event.y;
	top.g_EditFlag= 1;
}

/*  @note 一个层的改变大小
 */

function LayerResize()
{
	if (g_OT != 2) return;
	if (g_Direction == null) {
		window.status= "direction is null";
		return;
	}
	var  el,xPos, yPos, str, xMin, yMin, dir;
	xMin = 80; //The smallest width possible
	yMin = 80; //             height

	if (g_Direction.indexOf("e") != -1) {
		g_OO.style.pixelWidth= Math.max(xMin, g_OO.style.pixelWidth + event.x - g_oldX);
	}
	if (g_Direction.indexOf("s") != -1) {
		g_OO.style.pixelHeight = Math.max(yMin, g_OO.style.pixelHeight + event.y - g_oldY);
	}
	if (g_Direction.indexOf("w") != -1) {
		var temp= g_OO.style.pixelWidth-(event.x - g_oldX);
		if (temp>xMin) {
			g_OO.style.pixelLeft = g_OO.style.pixelLeft+event.x-g_oldX;
			g_OO.style.pixelWidth= Math.max(xMin, g_OO.style.pixelWidth-(event.x - g_oldX));
		}
	}
	if (g_Direction.indexOf("n") != -1) {
		var temp= g_OO.style.pixelHeight-(event.y - g_oldY);

		if (temp>yMin) {
			g_OO.style.pixelTop = g_OO.style.pixelTop+event.y-g_oldY;
			g_OO.style.pixelHeight = Math.max(yMin, g_OO.style.pixelHeight-(event.y - g_oldY));
		}
	}
	g_oldX= event.x;
	g_oldY= event.y;

	window.event.returnValue = false;
	window.event.cancelBubble = true;
	top.g_EditFlag= 1;
}



/** @note 键盘按下的事件
  */
function DocumentOnKeyDown() {
	switch (event.keyCode) {
		case 17: // CTRL
			if (cookie_iuin == top.g_iUin) g_CtrlPressed= true;
		break;
		case 74: // 'J'
			if (g_CtrlPressed) { // 进入编辑模式
				enterEditMode();
			}
		break;
		case 83: // 'S'
			if (g_EditMode==0) saveScenario();
		break;
		case 66: // 'b'
		guideButton(2)
		break;
		default: return true;
	}
	if (g_OO == null) return false;
	switch (event.keyCode) {
		case 39: // right arrow
			if (g_OT == 1)
				g_OO.style.pixelLeft = g_OO.style.pixelLeft + 1;
			else if (g_OT == 2)
				g_OO.style.pixelWidth = g_OO.style.pixelWidth + 1;
		break;
		case 37: // left arrow
			if (g_OT == 1)
				g_OO.style.pixelLeft = g_OO.style.pixelLeft - 1;
			else if (g_OT == 2)
				g_OO.style.pixelWidth = g_OO.style.pixelWidth - 1;
		break;
		case 38: // up arrow
			if (g_OT == 1)
				g_OO.style.pixelTop = g_OO.style.pixelTop - 1;
		break;
		case 40: // down arrow
			if (g_OT == 1)
				g_OO.style.pixelTop = g_OO.style.pixelTop + 1;
		break;
	}
	if (top.g_OT != 0) return false;
}

/** @note 键盘释放事件
  */
function DocumentOnKeyUp() {
	if (event.keyCode == 17) {
		g_CtrlPressed= false;
	}
	if (g_EditMode==0) return false;
}

