// ** DRAG AND DROP JAVASCRIPT ** AUTHOR @ SO (26th May 2001)

var offsetX, offsetY;
var movetoX, movetoY;
var isNav, isIE;

function ini_base()
{
	if (!document.getElementById) top.location.href = "/browser_error.html";

	isIE = (navigator.appName.charAt(0) == "M") ? true : false;
	isNav = (navigator.appName.charAt(0) == "N") ? true : false;
	return false;
}

function elementEvent(Lay, e)
{
	if(Lay)
	{
     	offsetX = window.event.offsetX;
     	offsetY = window.event.offsetY;
	}
	return false;
}

function mmove(Lay, e, f_x, f_y, t_x, t_y) // 移動範囲 : (f_x, f_y) - (t_x, t_y)
{
	if(Lay)
	{
		yourX = document.body.scrollLeft + event.clientX;
		yourY = document.body.scrollTop + event.clientY;
		if(!isNaN(yourX) && !isNaN(yourY))
		{
			movetoX = yourX - offsetX;
			movetoY = yourY - offsetY;

			if(movetoX < f_x) movetoX = f_x;
			if(movetoX > t_x) movetoX = t_x;
			if(movetoY < f_y) movetoY = f_y;
			if(movetoY > t_y) movetoY = t_y;

			if(!isNaN(movetoX) && !isNaN(movetoY) && movetoX >= 0 && movetoY >= 0)
			{
				moveLayer(Lay, movetoX, movetoY)
			}
		}
	}
	return false;
}

function mup(Lay, e, x_math, y_math, h_x, h_y) // x_math, y_math : グリッドの大きさ、h_x, h_y : 地点判定
{
	if(Lay)
	{
		if(!isNaN(movetoX) && !isNaN(movetoY) && movetoX >= 0 && movetoY >= 0)
		{
			x = parseInt((movetoX / x_math) + 0.5) * x_math;
			y = parseInt((movetoY / y_math) + 0.5) * y_math;
			moveLayer(Lay, x, y);
			if(h_x == x && h_y == y) return true;
		}
	}
	return false;
}

function moveLayer(Lay, x, y)
{
	if(Lay)
	{
		Lay.left = x + 'px';
		Lay.top = y + 'px';
	}
	return false;
}

function getLay(layName)
{
	return document.getElementById(layName).style;
}

function outLay(layName, html)
{
	html += "\n";			// <-- これがないとMacで動かない
	document.getElementById(layName).innerHTML = html;
	return false;
}

function setClip(Lay, cx1, cy1, cx2, cy2)
{
	Lay.clip = 'rect(' + cy1 + ', ' + cx2 + ', ' + cy2 + ', ' + cx1 + ')';
	return false;
}

function setZindex(Lay, zindex)
{
	if(Lay) Lay.zIndex = zindex;
	return false;
}

function getLeft(Lay)
{
	var l = Lay.left;
	if(l.indexOf("px")) l = l.substring(0, l.indexOf("px"));
	return parseInt(l);
}

function getTop(Lay)
{
	var t = Lay.top;
	if(t.indexOf("px")) t = t.substring(0, t.indexOf("px"));
	return parseInt(t);
}

function getWidth(Lay)
{
	return Lay.pixelWidth;
}

function getHeight(Lay)
{
	return Lay.pixelHeight;
}

function isVisible(Lay)
{
	if(Lay) if(Lay.display == 'block') return true;
	return false;
}

function setVisible(Lay)
{
	Lay.display = 'block';
}

function setHidden(Lay)
{
	Lay.display = 'none';
}

function setBackGround(Lay ,imgURL)
{
	Lay.backgroundImage = 'url(' + imgURL + ')';
}

function getBrowserWidth()
{
	return document.body.clientWidth;
}

function getBrowserHeight()
{
	return document.body.clientHeight;
}

function getMouseX(e)
{
	return document.body.scrollLeft + event.clientX;
}

function getMouseY(e)
{
	return document.body.scrollTop + event.clientY;
}

function digit(str, len)
{
	var now_len = (str + "").length;
	if(now_len >= len) return str;

	for(var i = 0; i < len - now_len; i++)
	{
		str = "0" + str;
	}
	return str;
}

