// JScript File

// Global constants to store elements that may be resized
var sActiveElementType = "TD";
var sVbarId = "vBar";
var sActiveTableId = "propertygridtable";
var sActiveCobtainerId = "propertygridbox";
var iResizeThreshold = 3;
var iEdgeThreshold = 10;
var iSizeThreshold = 20;

// Global variables to store position and distance moved
var oResizeTarget = null;
var iStartX = null;
var iEndX = null;
var iSizeX = null;

// MouseMove event on resizable table. This checks if you are in an allowable 
// 'resize start' position. It also puts the vertical bar (visual feedback) 
// directly under the mouse cursor. The vertical bar might NOT be currently 
// visible, that depnds on if you're resizing or not.
function trackCursor() 
{
    // Change cursor and store cursor position for resize indicator on column
    var oTarget = event.srcElement;
    var oVbar = document.getElementById(sVbarId);

    oVbar.style.top = document.getElementById(sActiveCobtainerId).offsetTop;
    oVbar.style.height = document.getElementById(sActiveCobtainerId).offsetHeight;
	
	// We have to add the body.scrollLeft in case the table is wider than the view
	// window where it is entriely within the screen this value should be zero...
	//oVbar.style.left = window.event.clientX + document.body.scrollLeft;

//    if ((oTarget.tagName.toUpperCase() == sActiveElementType) 
//		&& (event.offsetX >= (oTarget.offsetWidth - iEdgeThreshold)) 
//        && selectColhead(oTarget.cellIndex).getAttribute("resizable")
//        ) 
//    {
//        oTarget.style.cursor = "e-resize";
//    } 
//    else 
//    {
//        oTarget.style.cursor = "";
//    }
//    // We don't want resizing to select any text elements...
//    if (oVbar.style.display == "inline") 
//    {
//        document.selection.empty();
//    }
//    return true;
}

// Find cell at column iCellIndex in the first row of the table 
// - needed because you can only resize a column from the first row.
// by using this, we can resize from any cell in the table
function selectColhead(iCellIndex) 
{
//    var oTable = document.getElementById(sActiveTableId);
//    var oHeaderCell = oTable.rows(0).cells(iCellIndex);
//    return oHeaderCell;
}

// MouseDown event. This fills the globals with tracking information, and 
// displays the vertical bar. This is only done if you are allowed to start 
// resizing.
function selectCol() 
{
//    // Record start point and show vertical bar resize indicator
//    var oTargetCell = event.srcElement;
//    var oHeaderCell = selectColhead(event.srcElement.cellIndex);
//    
//    if(oTargetCell.style.cursor == "e-resize") 
//    {
//        iStartX = event.screenX;
//        oResizeTarget = oHeaderCell;
//        var oVbar = document.getElementById(sVbarId);
//        //if the cell isn't marked as 'resizable' viz a custom attribute
//        // then we won't allow this column to resize.
//        if (oResizeTarget.getAttribute("reSizable")) 
//        {
//            oVbar.style.display = "inline";
//        }
//    }
//    return true;
}

// MouseUp event. This finishes the resize.
function resizeCol() 
{
    // Resize the column and its adjacent sibling if position and size 
    // are within threshold values
//    var oAdjacentCell = null;
//    if (iStartX != null && oResizeTarget != null) 
//    {
//        iEndX = event.screenX;
//        iSizeX = iEndX - iStartX;
//        if ((oResizeTarget.offsetWidth + iSizeX) >= iSizeThreshold) 
//        {
//            if (Math.abs(iSizeX) >= iResizeThreshold) 
//            {
//                var iAdjCellOldWidth;
//                if (oResizeTarget.nextSibling != null) 
//                {
//                    oAdjacentCell = oResizeTarget.nextSibling;
//                    iAdjCellOldWidth = (oAdjacentCell.offsetWidth);
//                } 
//                else 
//                {
//                    oAdjacentCell = null;
//                }
//				var iResizeOldWidth = (oResizeTarget.offsetWidth);
//                oResizeTarget.style.width = iResizeOldWidth + iSizeX;
//                if ((oAdjacentCell != null) && (oAdjacentCell.tagName.toUpperCase() == sActiveElementType)) 
//                {
//                    oAdjacentCell.style.width = (
//                        ((iAdjCellOldWidth - iSizeX) >= iSizeThreshold) ? 
//                        (iAdjCellOldWidth - iSizeX) : 
//                        (oAdjacentCell.style.width = iSizeThreshold))
//                }
//            }
//        }
//    } 
//    else 
//    {
//        oResizeTarget.style.width = iSizeThreshold;
//    }
}

// DoubleClick event. This pushes the current column out to a good size.
function setMaxWidth()
{
	// Try to emulate the column double-click behaviour from explorer
	// this is not complete and only approximates the correct behaviour!!!
//	var oHeaderCell = selectColhead(event.srcElement.cellIndex)
//	if (oHeaderCell.tagName.toUpperCase() == sActiveElementType) 
//	{
//		var oAdjacentCell = (oHeaderCell.nextSibling != null) ? oHeaderCell.nextSibling : null;
//		var iOldWidth = null;
//		var iNewWidth = null;
//		var iAdjWidth = null;
//		var iWidthDiff = null;
//		var oTable = document.getElementById(sActiveTableId);
//		iOldWidth = oHeaderCell.offsetWidth;
//		iAdjWidth = oAdjacentCell !=null?oAdjacentCell.offsetWidth:0;
//		oTable.style.tableLayout = "auto";
//		iNewWidth = oHeaderCell.clientWidth;
//		iWidthDiff = iNewWidth - iOldWidth;
//		oTable.style.tableLayout = "fixed";
//		oHeaderCell.style.width = (iOldWidth + iWidthDiff) <= iSizeThreshold ?
//									iSizeThreshold : (iOldWidth + iWidthDiff);
//		if (oAdjacentCell != null) 
//		{
//			oAdjacentCell.style.width = (iAdjWidth - iWidthDiff) <= iSizeThreshold ?
//										iSizeThreshold : (iAdjWidth - iWidthDiff);
//		}
//		// We don't want this particular double-click to select any header text elements...
//		document.selection.empty();
//	}
//	return true;
}

// BODY MouseUp event - clears out the tracking information
//    if we're not resizing.
function cleanUp() 
{
	// Void the Global variables and hide the vertical bar
	var oVbar = document.getElementById(sVbarId);
	oVbar.style.display = "none";
	iEndX = null;
	iSizeX = null;
	iStartX = null;
	oResizeTarget = null;
	oAdjacentCell = null;
	return true;
}