// LyDhmltSplitterScript.js
// Copyright (c) 2000-2009, Lyria-W4.
// All rights reserved.
// @version	$Id: LyDhtmlSplitterScript.js,v 1.3.4.3 2009/08/06 09:47:20 nha Exp $

var hiddenDivThickness = 8;
var divMinThickness = 40;
var firstPos = -1;
var lastPos = -1;
var movedDiv = null;
var hiddenDiv = null;
var parentPart = null;
var firstPart = null;
var secondPart = null;
var splitterCode = null;
var minPos = 0;
var maxPos = 10000;
var verticalMovement;
var hideTime = -1;

// This function starts the movement of a splitter
function startMoveSplitter(event, parentPartId, firstPartId, secondPartId, hiddenDivId, movingDivId, isVertical, splitterHashcode)
{
	if ((new Date().getTime()) - hideTime < 1000)
	{
		hideTime = -1;
		return;
	}
	
	// Firefox
	if (document.getElementById && !(document.all))
		document.onmousemove = moveSplitterWithEvent;
	// IE
	else if(document.all)
		document.onmousemove = moveSplitter;

	SetSelectionStartMode(false);
	document.onmouseup = endMoveSplitter;

	parentPart = document.getElementById(parentPartId);
	firstPart = document.getElementById(firstPartId);
	secondPart = document.getElementById(secondPartId);
	hiddenDiv = document.getElementById(hiddenDivId);
	movedDiv = document.getElementById(movingDivId);
	verticalMovement = isVertical;
	splitterCode = splitterHashcode;

	hiddenDiv.style.width = this.document.body.clientWidth;
	hiddenDiv.style.height = this.document.body.clientHeight;
	hiddenDiv.style.left = 0;
	hiddenDiv.style.top = 0;
	hiddenDiv.style.cursor = (isVertical ? "s-resize" : "w-resize");
	hiddenDiv.style.display = "block";
	
	if (isVertical)
	{
		minPos = cmGetY(parentPart);
		maxPos = cmGetY(parentPart) + cmGetHeight(parentPart);
	}
	else
	{
		minPos = cmGetX(parentPart);
		maxPos = cmGetX(parentPart) + cmGetWidth(parentPart);
	}

	if (isVertical)
	{
		firstPos = event.clientY; 
		lastPos = event.clientY; 

		movedDiv.style.height = hiddenDivThickness + "px";
		movedDiv.style.width = cmGetWidth(firstPart) + "px";
		movedDiv.style.top = (firstPos - (-document.body.scrollTop) - (hiddenDivThickness / 2)) + "px";
		movedDiv.style.left = cmGetX(firstPart);
	}
	else
	{
		firstPos = event.clientX; 
		lastPos = event.clientX; 

		movedDiv.style.width = hiddenDivThickness + "px";
		movedDiv.style.height = cmGetHeight(firstPart) + "px";
		movedDiv.style.left = (firstPos - (-document.body.scrollLeft) - (hiddenDivThickness / 2)) + "px";
		movedDiv.style.top = cmGetY(firstPart);
	}

	// Display hidden div and register events
	movedDiv.style.display = "block";
}

// This function implements the movement of a splitter
function moveSplitterWithEvent(event)
{
	moveSplitter(event);
}

// This function implements the movement of a splitter
function moveSplitter(myEvent)
{
	if (document.getElementById && !(document.all))
	{
		// Firefox
		// Nothing to do
	}
	else if(document.all)
	{
		// IE
		myEvent = event;
	}

	// Register new position and change the movedDiv position
	if (verticalMovement)
	{
		if (myEvent.clientY - minPos < divMinThickness)
			return;
	
		if (myEvent.clientY > document.body.clientHeight - divMinThickness)
			return;
	
		if (myEvent.clientY > document.body.clientHeight - (hiddenDivThickness / 2) - 5)
			return;
	
	
		var difY = myEvent.clientY - lastPos;
		var newY = parseInt(movedDiv.style.top) + difY;

		movedDiv.style.top = newY + "px";
		lastPos = myEvent.clientY;
	}
	else
	{
		if (myEvent.clientX - minPos < divMinThickness)
			return;

		if (myEvent.clientX > maxPos - divMinThickness)
			return;

		if (myEvent.clientX > maxPos - (hiddenDivThickness / 2) - 5)
			return;
	
		var difX = myEvent.clientX - lastPos;
		var newX = parseInt(movedDiv.style.left) + difX;

		movedDiv.style.left = newX + "px";
		lastPos = myEvent.clientX;
	}
}

// This function ends the movement of a splitter
function endMoveSplitter()
{
	// Compute the new sizes of the two parts
	var offset = lastPos - firstPos;

	// Change the sizes of the two parts
	if (verticalMovement)
	{
		var total = cmGetHeight(firstPart) + cmGetHeight(secondPart);
		var firstFrameSize = cmGetHeight(firstPart) + offset;
		var secondFrameSize = cmGetHeight(secondPart) - offset;
		var firstProp = parseInt(((firstFrameSize * 100) / total) + 0.5);

		if (firstProp < 3)
			firstProp = 3;
		else if (firstProp > 97)
			firstProp = 97;
		
		getLeonardiTopFrame().setSplitterSize(firstPart.id + "", firstProp, splitterCode, STRUTS_EVENT_URL);
	}
	else
	{
		var total = cmGetWidth(firstPart) + cmGetWidth(secondPart);
		var firstFrameSize = cmGetWidth(firstPart) + offset;
		var secondFrameSize = cmGetWidth(secondPart) - offset;
		var firstProp = parseInt(((firstFrameSize * 100) / total) + 0.5);

		if (firstProp < 3)
			firstProp = 3;
		else if (firstProp > 97)
			firstProp = 97;
		
		getLeonardiTopFrame().setSplitterSize(firstPart.id + "", firstProp, splitterCode, STRUTS_EVENT_URL);
	}
	
	// Reset default state
	minPos = 0;
	maxPos = 10000;
	hiddenDiv.style.width = 5;
	hiddenDiv.style.height = 5;
	hiddenDiv.style.left = 0;
	hiddenDiv.style.top = 0;
	hiddenDiv.style.display = "none";
	movedDiv.style.display = "none";
	parentPart = null;
	firstPart = null;
	secondPart = null;
	splitterCode = null;
	SetSelectionStartMode(true);
	document.onmousemove = null;
	document.onmouseup = null;
}

// Array of splitter ids :
//  - splitterIds[n][0] : isVertical
//  - splitterIds[n][1] : parentId
//  - splitterIds[n][2] : firstPartId
//  - splitterIds[n][3] : secondPartId
//  - splitterIds[n][4] : firstPartSize
//  - splitterIds[n][5] : set to auto overflow style of divs
var splitterIds = new Array();

function addSplitterIds(vars)
{
	var found = false;
	
	for (var i = splitterIds.length - 1; i >= 0; i--)
	{
		var oldVars = splitterIds[i];
		
		if (vars[2] == oldVars[2])
		{
			splitterIds[i] = vars;
			found = true;
		}
	}
	
	if (!found)
		splitterIds[splitterIds.length] = vars;
}

function setSplitterSize(firstPartId, firstPartSize, splitterHashcode, path)
{
	for (var i = splitterIds.length - 1; i >= 0; i--)
	{
		var vars = splitterIds[i];
		
		if (vars[2] == firstPartId)
		{
			vars[4] = firstPartSize + "";
		}
	}
	
	checkSizeMainView('true', 'false');
	
	// Send new splitter size to server
	for (var i = 0; i < ajaxEncodedChars.length; i++)
	{
		firstPartSize = ReplaceString(firstPartSize + "", ajaxChars[i], ajaxEncodedChars[i]);
	}

	SendAjaxRequestParams(_rootUrl + path, pageParam + '=' + splitterHashcode + '&' + sourceParam + '=setSize' +
		'&' + valueParam + '=' + firstPartSize);
}

function hideSplitter(firstPartId, increase, splitterHashcode)
{
	hideTime = (new Date().getTime());

	for (var i = getLeonardiTopFrame().splitterIds.length - 1; i >= 0; i--)
	{
		var vars = getLeonardiTopFrame().splitterIds[i];
		
		if (vars[2] == firstPartId)
		{
			var newSize;
			
			if (increase)
			{
				if (vars[4].indexOf('0%') == 0)
					newSize = vars[4].substring(2, vars[4].length);
				else if (vars[4].indexOf('100%') == 0)
					newSize = vars[4];
				else if ((vars[4] != "") && (vars[4] != "0"))
					newSize = '100%' + vars[4];
				else
					newSize = '50';
			}
			else
			{
				if (vars[4].indexOf('100%') == 0)
					newSize = vars[4].substring(4, vars[4].length);
				else if (vars[4].indexOf('0%') == 0)
					newSize = vars[4];
				else if ((vars[4] != "") && (vars[4] != "100"))
					newSize = '0%' + vars[4];
				else
					newSize = '50';
			}

			getLeonardiTopFrame().setSplitterSize(firstPartId, newSize, splitterHashcode, STRUTS_EVENT_URL);
			break;
		}
	}

}