var m_fMouseDownSideBox = false;	// mouse down flag
var m_fMouseDownServiceTab = false;	// mouse down flag
var m_fMouseMovedSideBox = false;	// mouse moved flag
var m_fMouseMovedServiceTab = false;	// mouse moved flag
var m_cxOffset = 0;			// x offset of mouse from left of drag part
var m_cyOffset = 0;			// y offset of mouse from top of drag part
var m_prtSrc = null;		// ref to source of the part drag
var m_prtDrag = null;		// ref to drag part
var m_zoneLast = null;		// last active zone
var m_idxInsert = 0;		// position at which to insert dropped part

function Document_OnMouseDown()
{
	if ((window.event.srcElement.className == 'LayoutWebPartSideBox') || (window.event.srcElement.className == 'LayoutWebPartServiceTab'))
	{
		if (window.event.srcElement.className == 'LayoutWebPartSideBox')
		{
			m_fMouseDownSideBox = true;
		}
		else
		{
			m_fMouseDownServiceTab = true;
		}

		m_cxOffset = window.event.clientX - GetAbsoluteLeft(window.event.srcElement);
		m_cyOffset = window.event.clientY - GetAbsoluteTop(window.event.srcElement);

		m_prtSrc = GetPartElement(window.event.srcElement);
		m_prtDrag = m_prtSrc.cloneNode(true);

		m_prtDrag.style.position = 'absolute';
		m_prtDrag.style.filter = 'alpha(opacity=50)';
		m_prtDrag.style.left = (window.event.x - m_cxOffset) + 'px';
		m_prtDrag.style.top = (window.event.y - m_cyOffset) + 'px';
		m_prtDrag.style.width = m_prtSrc.offsetWidth;

		document.body.insertAdjacentElement('beforeEnd', m_prtDrag);

		divMouse.setCapture();
	}
}

function DivMouse_OnMouseMove()
{
	// for debug
	// divposition.innerText = "x = " + window.event.x + " : y = " + window.event.y;

	if (m_fMouseDownSideBox || m_fMouseDownServiceTab)
	{
		var zoneTarget;

		if (m_fMouseDownSideBox)
		{
			m_fMouseMovedSideBox = true;
		}
		else
		{
			m_fMouseMovedServiceTab = true;
		}
		
		m_prtDrag.style.left = window.event.x - m_cxOffset + 'px';
		m_prtDrag.style.top = window.event.y - m_cyOffset + 'px';

		zoneTarget = GetZoneFromPoint(window.event.x, window.event.y, m_prtDrag, tblIBeam);

		if (FIsZone(zoneTarget))
		{
			// We are over a zone
			// If the class name is just "Zone" this is a new zone
			// and we need to switch the last drop zone
			if (m_zoneLast)
			{
				SetAsDropTarget(m_zoneLast, false);
			}
	
			SetAsDropTarget(zoneTarget, true);
			
			PositionIBeam(window.event.x, window.event.y, tblIBeam, zoneTarget);
		}
		else
		{
			// Not over a zone at all. Reset last zone and clear it
			if (m_zoneLast)
			{
				SetAsDropTarget(m_zoneLast, false);
			}
			tblIBeam.style.display = 'none';
		}
	}        
}

function DivMouse_OnMouseUp()
{
	if (m_fMouseDownSideBox || m_fMouseDownServiceTab)
	{
		if (m_fMouseMovedSideBox || m_fMouseMovedServiceTab)
		{
			var zoneTarget;
			var prtTemp;

			zoneTarget = GetZoneFromPoint(window.event.x, window.event.y, m_prtDrag, tblIBeam);

			if (zoneTarget)
			{
				if (-1 != zoneTarget.className.indexOf("LayoutZoneCenter"))
				{
				//	var varWarnging = "この場所にドロップできません";
				//
					tblIBeam.style.display = 'none';
					m_prtDrag.style.position = '';
					m_prtDrag.removeNode(true);
					m_prtDrag = null;
					divMouse.releaseCapture();
					m_fMouseDownSideBox = false;
					m_fMouseMovedSideBox = false;
					return;
				}
				if (m_fMouseMovedSideBox)
				{
					if ((-1 != zoneTarget.className.indexOf("LayoutZoneServiceTabTop")) ||
					    (-1 != zoneTarget.className.indexOf("LayoutZoneServiceTabNouse")))
					{
						tblIBeam.style.display = 'none';
						m_prtDrag.style.position = '';
						m_prtDrag.removeNode(true);
						m_prtDrag = null;
						divMouse.releaseCapture();
						m_fMouseDownSideBox = false;
						m_fMouseMovedSideBox = false;
						return;
					}
				}
				else
				{
					if ((-1 != zoneTarget.className.indexOf("LayoutZoneSideBoxLeft")) ||
					    (-1 != zoneTarget.className.indexOf("LayoutZoneSideBoxRight")) ||
					    (-1 != zoneTarget.className.indexOf("LayoutZoneSideBoxNotuse")))
					{
						tblIBeam.style.display = 'none';
						m_prtDrag.style.position = '';
						m_prtDrag.removeNode(true);
						m_prtDrag = null;
						divMouse.releaseCapture();
						m_fMouseDownSideBox = false;
						m_fMouseMovedSideBox = false;
						return;
					}
				}

				switch (m_idxInsert)
				{
				case -1:
					{
					zoneTarget.insertAdjacentElement('afterBegin', m_prtSrc.removeNode(true));
					break;
					}
				case zoneTarget.children.length:
					{
						// inserting part at the end of the zone
						prtTemp = zoneTarget.children(zoneTarget.children.length - 1);
						// check to make sure the source and target are not the same part
						if (prtTemp != m_prtSrc)
						{
							prtTemp.insertAdjacentElement('afterEnd', m_prtSrc.removeNode(true));
						}
						break;
					}
				default:
					{
						// inserting part before indexed child
						prtTemp = zoneTarget.children(m_idxInsert);

						// check to make sure the source and target are not the same
						if (prtTemp != m_prtSrc)
						{
							prtTemp.insertAdjacentElement('beforeBegin', m_prtSrc.removeNode(true));
						}
					}
				}
				saveToServer.partLayout.value = '';
				AddPartsInfoForSave(tdZoneSideBoxLeft, 'left');
				AddPartsInfoForSave(tdZoneSideBoxRight, 'right');
				AddPartsInfoForSave(tdZoneSideBoxNouse, 'non');
				saveToServer.partLayout.value += '</>';

				saveToServer.partLayout2.value = '';
				AddPartsInfoForSave2(tdZoneServiceTabTop1, 'top');
				AddPartsInfoForSave2(tdZoneServiceTabTop2, 'top');
				AddPartsInfoForSave2(tdZoneServiceTabTop3, 'top');
				AddPartsInfoForSave2(tdZoneServiceTabNouse, 'non');
				saveToServer.partLayout2.value += '</>';
			}

			tblIBeam.style.display = 'none';
		}
		m_prtDrag.style.position = '';
		m_prtDrag.removeNode(true);
		m_prtDrag = null;

		if (m_zoneLast)
		{
			SetAsDropTarget(m_zoneLast, false);
		}
		
		if (m_fMouseDownSideBox)
		{
			var id = window.event.srcElement.id;
			if (id.indexOf('IMG-H') != -1)
			{
				var strNumber = id.substr(7);
				//divinfo3.innerText = "strNumber = " + strNumber;	// for debug
				//changestate2( strNumber, true );
				changestate4( strNumber, true );
			}
		}

		divMouse.releaseCapture();
		m_fMouseDownSideBox = false;
		m_fMouseMovedSideBox = false;
		m_fMouseDownServiceTab = false;
		m_fMouseMoveServiceTab = false;
	}
}

function AddPartsInfoForSave(node, zone)
{
	
	var nIndex;
	var partCount = node.children.length;
	for (nIndex = 0; nIndex < partCount; nIndex ++)
	{
		saveToServer.partLayout.value +=
			node.children[nIndex].id + ',' +
			'open' + ',' +
			zone + ',';
	}
}

function AddPartsInfoForSave2(node, zone)
{
	
	var nIndex;
	var partCount = node.children.length;
	for (nIndex = 0; nIndex < partCount; nIndex ++)
	{
		saveToServer.partLayout2.value +=
			node.children[nIndex].id + ',' +
			zone + ',';
	}
}

// Positions the IBeam element at the right insertion point
function PositionIBeam(x, y, elemIBeam, zoneTarget)
{
	var nParts;
	var prtFirst;
	var prtTemp;
	var idxPart;

	nParts = zoneTarget.children.length;
	if (nParts > 0)
	{
		// get the first part in the zone
		prtFirst = zoneTarget.children(0);

		// set the ibeam width to the part width
		elemIBeam.style.width = prtFirst.offsetWidth + 1;

		// get the part before which we should display the ibeam
		for(m_idxInsert = 0; m_idxInsert < nParts; m_idxInsert++)
			{
				prtTemp = zoneTarget.children(m_idxInsert);
				if (y <= (GetAbsoluteTop(prtTemp) + ((prtTemp.offsetHeight + (parseInt(prtTemp.cellSpacing) * 2)) / 2)))
				{
					break;
				}
			}

		// if the part index is beyond the last part,
		// move the ibeam to after the last part
		if (m_idxInsert >= nParts)
		{
			m_idxInsert = nParts;
			prtTemp = zoneTarget.children(m_idxInsert - 1);
			//divinfo3.innerText = "(nParts, m_idxInsert) = (" + nParts + ", " + m_idxInsert + ") : GetAbsoluteTop(" + prtTemp.className + ") = " + GetAbsoluteTop(prtTemp);	// for debug
			elemIBeam.style.top = String(GetAbsoluteTop(prtTemp) + (prtTemp.offsetHeight + (parseInt(prtTemp.cellSpacing) * 2)) - (elemIBeam.offsetHeight / 2)) + 'px';
			elemIBeam.style.left = String(GetAbsoluteLeft(prtTemp)) + 'px';
		}
		else
		{
			prtTemp = zoneTarget.children(m_idxInsert);
			//divinfo3.innerText = "(nParts, m_idxInsert) = (" + nParts + ", " + m_idxInsert + ") : GetAbsoluteTop(" + prtTemp.className + ") = " + GetAbsoluteTop(prtTemp);	// for debug
			elemIBeam.style.top = String(GetAbsoluteTop(prtTemp) - (elemIBeam.offsetHeight / 2) + 1) + 'px';
			elemIBeam.style.left = String(GetAbsoluteLeft(prtTemp)) + 'px'
		}

		// show it
		elemIBeam.style.display = 'inline';
	}
	else
	{
		elemIBeam.style.display = 'none';
		m_idxInsert = -1;
	}
}

// Navigates up from an element until it finds the element
// of type LayoutWebPartFrame (the whole table representing the WebPart)
function GetPartElement(subElem)
{
	var partFrame = subElem;
	while (partFrame.className != 'LayoutWebPartFrame')
	{
		partFrame = partFrame.parentElement;
	}
	return partFrame;
}

// Gets real Top value with respect to client area
function GetAbsoluteTop(elem)
{
	var topPosition = 0;

	while (elem)
	{
		if (elem.tagName == 'BODY')
		{
			break;
		}
		topPosition += elem.offsetTop;
		elem = elem.offsetParent;
	}
	return topPosition;
}

// Get real Left value with respect to client area
function GetAbsoluteLeft(elem)
{
	var leftPosition = 0;
		
	while (elem)
	{
		if (elem.tagName == 'BODY')
		{
			break;
		}
		leftPosition += elem.offsetLeft;
		elem = elem.offsetParent;
	}
	return leftPosition;
}

// Returns true if the given element is one of our zones.
function FIsZone(elem)
{
	var fRet = false;
	var strClass;
	var ich;
	
	if (elem)
	{
		strClass = elem.className;
		ich = strClass.indexOf(' ');
		if (ich != -1)
		{
			strClass = strClass.substr(0, ich);
		}

		fRet = (strClass.indexOf("LayoutZone") != -1);

		if (m_fMouseDownSideBox && m_fMouseMovedSideBox && (-1 != strClass.indexOf("LayoutZoneServiceTab")))
		{
			fRet = false;
		}
		else if (m_fMouseDownServiceTab && m_fMouseMovedServiceTab && (-1 != strClass.indexOf("LayoutZoneSideBox")))
		{
			fRet = false;
		}
		else if (m_fMouseDownServiceTab && m_fMouseMovedServiceTab && (-1 != strClass.indexOf("LayoutZoneServiceTabTop")))
		{
			if (0 < elem.children.length)
			{
				fRet = false;
			}
		}


		// for debug		
		//divinfo2.innerText = "FIsZone(" + elem.className + ") = " + fRet + " : strClass = \"" + strClass + "\"";
	
	}

	return fRet;
}

// Sets or clears elem as the drop target.
function SetAsDropTarget(elem, fSet)
{
	var strClass;
	var ich;
		
	if (fSet)
	{
		strClass = elem.className;
		if ((strClass.indexOf(' LayoutDropZoneSideBox') == -1) && (strClass.indexOf(' LayoutDropZoneServiceTab') == -1))
		{
			if (m_fMouseMovedSideBox == true)
			{
				elem.className = strClass + ' LayoutDropZoneSideBox';
			}
			else
			{
				elem.className = strClass + ' LayoutDropZoneServiceTab';
			}
			m_zoneLast = elem;
		}
	}
	else
	{
		strClass = elem.className;
		if (m_fMouseMovedSideBox == true)
		{
			ich = strClass.indexOf(' LayoutDropZoneSideBox');
		}
		else
		{
			ich = strClass.indexOf(' LayoutDropZoneServiceTab');
		}
		if (ich != -1)
		{
			elem.className = strClass.substr(0, ich);
			m_zoneLast = null;
		}
	}
}


// Gets the zone underneath the x,y or returns Nothing if not over a zone
function GetZoneFromPoint(x, y, prtDrag, elemIBeam)
{
	// This is totally hoaky. Hopefully someone on DHTML talk can
	// give me a better idea. The problem is that elementFromPoint will
	// return the top-most element under the point, which is the part
	// being dragged, not the zone underneath it. Setting zIndex to -1
	// temporarily seems to work, but also seems like a lot of overhead.

	var prtDragZIndexOld = prtDrag.style.zIndex;
	var elemIBeamZIndexOld = elemIBeam.style.zIndex;

	prtDrag.style.zIndex = -1;
	elemIBeam.style.zIndex = -1;
		
	var zone = document.elementFromPoint(x, y);
	prtDrag.style.zIndex = prtDragZIndexOld;
	elemIBeam.style.zIndex = elemIBeamZIndexOld;

	// Now the element we just got might be another part sitting in
	// the zone, so check to see if the class name is "LayoutWebPartSideBox". If it
	// is then go up the parent chain until we get its zone

	if (zone.className == 'LayoutWebPartSideBox')
	{
		while ((!FIsZone(zone)) && 
				(zone.className != 'LayoutDropZoneSideBox') &&
				(zone.tagName != 'BODY'))
		{
			zone = zone.parentElement;
		}
	}
	else if (zone.className == 'LayoutWebPartServiceTab')
	{
		while ((!FIsZone(zone)) && 
				(zone.className != 'LayoutDropZoneServiceTab') &&
				(zone.tagName != 'BODY'))
		{
			zone = zone.parentElement;
		}
	}

	// Last check is that we might be over the body or something
	// else that is not a zone at all. If the current return pointer
	// is not of className = "Zone" or "LayoutDropZoneSideBox" or "LayoutDropZoneServiceTab" then return Nothing
	if ((!FIsZone(zone)) && (zone.className != 'LayoutDropZoneSideBox') && (zone.className != 'LayoutDropZoneServiceTab'))
	{
		zone = null;
	}
	// for debug
	//if (zone != null)
	//{
	//	divinfo1.innerText = "GetZoneFromPoint(" + x + ", " + y + ", " + prtDrag.className + ", " + elemIBeam.className + ") : zone.className = \"" + zone.className + "\"";
	//}
	//else
	//{
	//	divinfo1.innerText = "GetZoneFromPoint(" + x + ", " + y + ", " + prtDrag.className + ", " + elemIBeam.className + ") : zone = null";
	//}
	return zone;
}

// Center が img の場合の処理
function changestate( strNumber )
{
	var strSRC_L = document.all( "IMG-HL-" + strNumber ).src;
	var strSRC_C = document.all( "IMG-HC-" + strNumber ).src;
	var strSRC_R = document.all( "IMG-HR-" + strNumber ).src;
	//var strClose = "false";
	var iIndex_L = strSRC_L.indexOf( "close" );
	var iIndex_C = strSRC_C.indexOf( "close" );
	var iIndex_R = strSRC_R.indexOf( "close" );

	var bClose = (-1 == iIndex_L);
	if( bClose )
	{
		strSRC_L = strSRC_L.substring( 0, strSRC_L.length - 4 ) + "_close.jpg";
		strSRC_C = strSRC_C.substring( 0, strSRC_C.length - 4 ) + "_close.jpg";
		strSRC_R = strSRC_R.substring( 0, strSRC_R.length - 4 ) + "_close.jpg";
		//strClose = "true";
	}
	else
	{
		strSRC_L = strSRC_L.substring( 0, iIndex_L - 1 ) + ".jpg";
		strSRC_C = strSRC_C.substring( 0, iIndex_C - 1 ) + ".jpg";
		strSRC_R = strSRC_R.substring( 0, iIndex_R - 1 ) + ".jpg";
		//strClose = "false";
	}
	document.all( "IMG-HL-" + strNumber ).src = strSRC_L;
	document.all( "IMG-HC-" + strNumber ).src = strSRC_C;
	document.all( "IMG-HR-" + strNumber ).src = strSRC_R;

	boxexpand( strNumber, bClose );
}

function changestate2( strNumber, bClose )
{
	var strSRC_L = document.all( "IMG-HL-" + strNumber ).src;
	var strSRC_C = document.all( "IMG-HC-" + strNumber ).src;
	var strSRC_R = document.all( "IMG-HR-" + strNumber ).src;
	var iIndex_L = strSRC_L.indexOf( "close" );
	var iIndex_C = strSRC_C.indexOf( "close" );
	var iIndex_R = strSRC_R.indexOf( "close" );

	if( bClose )
	{
		if (iIndex_L == -1)
		{
			strSRC_L = strSRC_L.substring( 0, strSRC_L.length - 4 ) + "_close.jpg";
			strSRC_C = strSRC_C.substring( 0, strSRC_C.length - 4 ) + "_close.jpg";
			strSRC_R = strSRC_R.substring( 0, strSRC_R.length - 4 ) + "_close.jpg";
		}
	}
	else
	{
		if (iIndex_L != -1)
		{
			strSRC_L = strSRC_L.substring( 0, iIndex_L - 1 ) + ".jpg";
			strSRC_C = strSRC_C.substring( 0, iIndex_C - 1 ) + ".jpg";
			strSRC_R = strSRC_R.substring( 0, iIndex_R - 1 ) + ".jpg";
		}
	}
	document.all( "IMG-HL-" + strNumber ).src = strSRC_L;
	document.all( "IMG-HC-" + strNumber ).src = strSRC_C;
	document.all( "IMG-HR-" + strNumber ).src = strSRC_R;

	boxexpand( strNumber, bClose );
}

// Center が TD の background の場合の処理
function changestate3( strNumber )
{
	var strSRC_L = document.all( "IMG-HL-" + strNumber ).src;
	//var strSRC_C = document.all( "IMG-HC-" + strNumber ).src;
	var strSRC_R = document.all( "IMG-HR-" + strNumber ).src;
	//var strClose = "false";
	var iIndex_L = strSRC_L.indexOf( "close" );
	//var iIndex_C = strSRC_C.indexOf( "close" );
	var iIndex_R = strSRC_R.indexOf( "close" );

	var bClose = (-1 == iIndex_L);
	if( bClose )
	{
		strSRC_L = strSRC_L.substring( 0, strSRC_L.length - 4 ) + "_close.jpg";
		//strSRC_C = strSRC_C.substring( 0, strSRC_C.length - 4 ) + "_close.jpg";
		strSRC_R = strSRC_R.substring( 0, strSRC_R.length - 4 ) + "_close.jpg";
		//strClose = "true";
	}
	else
	{
		strSRC_L = strSRC_L.substring( 0, iIndex_L - 1 ) + ".jpg";
		//strSRC_C = strSRC_C.substring( 0, iIndex_C - 1 ) + ".jpg";
		strSRC_R = strSRC_R.substring( 0, iIndex_R - 1 ) + ".jpg";
		//strClose = "false";
	}
	document.all( "IMG-HL-" + strNumber ).src = strSRC_L;
	//document.all( "IMG-HC-" + strNumber ).src = strSRC_C;
	document.all( "IMG-HR-" + strNumber ).src = strSRC_R;

	boxexpand( strNumber, bClose );
}

function changestate4( strNumber, bClose )
{
	var strSRC_L = document.all( "IMG-HL-" + strNumber ).src;
	var SRC_C = document.all( "IMG-HC-" + strNumber );
	var strSRC_C = "";
	if (SRC_C != null)
	{
		strSRC_C = SRC_C.src;
	}
	var strSRC_R = document.all( "IMG-HR-" + strNumber ).src;
	var iIndex_L = strSRC_L.indexOf( "close" );
	var iIndex_C = -1;
	if ((strSRC_C != null) && (strSRC_C != ""))
	{
		iIndex_C = strSRC_C.indexOf( "close" );
	}
	var iIndex_R = strSRC_R.indexOf( "close" );

	if( bClose )
	{
		if (iIndex_L == -1)
		{
			strSRC_L = strSRC_L.substring( 0, strSRC_L.length - 4 ) + "_close.jpg";
			if ((strSRC_C != null) && (strSRC_C != ""))
			{
				strSRC_C = strSRC_C.substring( 0, strSRC_C.length - 4 ) + "_close.jpg";
			}
			strSRC_R = strSRC_R.substring( 0, strSRC_R.length - 4 ) + "_close.jpg";
		}
	}
	else
	{
		if (iIndex_L != -1)
		{
			strSRC_L = strSRC_L.substring( 0, iIndex_L - 1 ) + ".jpg";
			if ((strSRC_C != null) && (strSRC_C != ""))
			{
				strSRC_C = strSRC_C.substring( 0, iIndex_C - 1 ) + ".jpg";
			}
			strSRC_R = strSRC_R.substring( 0, iIndex_R - 1 ) + ".jpg";
		}
	}
	document.all( "IMG-HL-" + strNumber ).src = strSRC_L;
	if ((strSRC_C != null) && (strSRC_C != ""))
	{
		document.all( "IMG-HC-" + strNumber ).src = strSRC_C;
	}
	document.all( "IMG-HR-" + strNumber ).src = strSRC_R;

	boxexpand( strNumber, bClose );
}

function boxexpand( strNumber, bClose )
{
	if( bClose )
	{
		document.all( "TBL-B-" + strNumber ).style.visibility = "hidden";
		document.all( "TBL-B-" + strNumber ).style.display = "none";
	}
	else
	{
		document.all( "TBL-B-" + strNumber ).style.visibility = "visible";
		document.all( "TBL-B-" + strNumber ).style.display = "block";
	}
}
