/*
	Sets cookie storing state of last expanded tree view node.
	
	Parameters:
		name: id of the element as string.
		state: expansion state as integer.
*/
function fwtvjs_setExpansionCookie( name, state )
{
	var expiry = new Date( );
	var nowPlusOneWeek = expiry.getTime( ) + (7 * 24 * 60 * 60 * 1000);
	expiry.setTime( nowPlusOneWeek );
	
	document.cookie = 'fwtv_' + name + '=' + state + '; expires=' + expiry.toGMTString( );
}

/* 
	Toggles display (visibility) of an element
	
	Parameters:
		nodeId: id of the element as string
		e: event
*/
function fwtvjs_onToggle(nodeId, e)
{
	if (!e)
	{
		e = window.event;
	}

	var element = document.getElementById(nodeId);
	if (!element)
	{
		return(false);
	}
	
	var elementImage = document.getElementById(nodeId + '_img')

	if (element.style.display == 'none')
	{
		element.style.display = 'block';
		elementImage.src = '/fotoweb/rsrc/FWTV_SL_M.gif';
	}
	else
	{
		element.style.display = 'none';
		
		elementImage.src = '/fotoweb/rsrc/FWTV_SL_P.gif';
	}
	
	if (e)
	{
		// Stop the event from propagating (stops regular HREF link to be followed).
		e.cancelBubble = true;
		if (e.stopPropagation)
		{
			e.stopPropagation();
		}
	}
	
	return(true);
}


/*
	Returns the value of the specified cookie.
	
	Parameters:
		name: name of the cookie to get value of.
*/
function fw_getCookieValue(name)
{
	var cookies = document.cookie;
	var intName = name + '=';
	
	// Start position
	var startPos = cookies.indexOf(intName);
	if (startPos == -1)
	{
		return('');
	}
	
	startPos += intName.length;
	
	// End position
	var endPos = cookies.indexOf(';', startPos);
	if (endPos == -1)
	{
		endPos = cookies.length;
	}
	
	return(unescape(cookies.substring(startPos, endPos)));
}


/*
	Returns the name of the cookie for the tree view control
*/
function fw_GetCookieName(nodeId)
{
	var node = document.getElementById(nodeId);

	while(node)
	{
		if (node.getAttribute('cookieName'))
		{
			return(node.getAttribute('cookieName'));
		}
		
		node = node.parentNode;
	}
	
	return(null);
}




/*
	Sets cookie storing id of the last expanded tree view node.
	
	Parameters:
		treeViewId: id of the tree view control.
		nodeId: id of the node.
		state: expansion state of the node.
*/
function fwtvjs_setNodeIdCookie(cookieName, nodeId, state)
{
	// Get current cookie.
	var currValue = fw_getCookieValue(cookieName);
	
	if (currValue.length > 0)
	{
		var index = currValue.indexOf(nodeId + '|');
		if (index != -1 && state == 0) // id is present and we need to remove it
		{
			currValue = currValue.replace(nodeId + '|', '');
		}
		
		if (index == -1 && state == 1) // id is not present and we want to add it
		{
			currValue += nodeId + '|';
		}
	}
	else
	{
		currValue = nodeId + '|';
	}


	// Set cookie.
	var expiry = new Date();
	var nowPlusOneWeek = expiry.getTime() + (7 * 24 * 60 * 60 * 1000);
	expiry.setTime( nowPlusOneWeek );
	
	document.cookie = cookieName + '=' + currValue + '; expires=' + expiry.toGMTString();
}

/*
	Sets cookie storing path of the last expanded tree view node.
	
	Parameters:
		treeViewId: id of the tree view control.
		nodePath: path of the node.
		state: expansion state of the node.
*/
function fwtvjs_setNodePathCookie(cookieName, nodePath)
{
	var expiry = new Date();
	var nowPlusOneWeek = expiry.getTime() + (7 * 24 * 60 * 60 * 1000);
	expiry.setTime( nowPlusOneWeek );
	
	document.cookie = cookieName + '=' + nodePath + '; expires=' + expiry.toGMTString();
}


/*
	Toggles visibility of a tree view node.
	Stores the last expanded node id in a cookie.
*/
function fwtvjs_onToggleStoreId(nodeId, e)
{
	if (fwtvjs_onToggle(treeViewId, nodeId, e))
	{
		// Get cookie name
		var cookieName = fw_GetCookieName(nodeId);
		
		if (cookieName)
		{
			if (document.getElementById(nodeId).style.display == 'none')
			{
				fwtvjs_setNodeIdCookie(cookieName, nodeId, 0);
			}
			else
			{
				fwtvjs_setNodeIdCookie(cookieName, nodeId, 1);
			}
		}
		
		return(true);
	}
	
	return(false);
}

/*
	Toggles visibility of a tree view node.
	Stores the last expanded node path in a cookie.
*/
function fwtvjs_onToggleStorePath(nodeId, e)
{
	if (fwtvjs_onToggle(nodeId, e))
	{
		// Get node path.
		var node = document.getElementById(nodeId);
		var nodePath = '';
		var expansionState = node.style.display == 'none' ? 0 : 1;

		while(node.getAttribute('path'))
		{
			// If expansion state is 0, then do not add the lowest path.
			if (expansionState == 1)
			{
				// Build path string from bottom up.
				nodePath = node.getAttribute('path') + '\\' + nodePath;
			}
			else
			{
				expansionState = 1;
			}
			
			if (node.parentNode)
			{
				node = node.parentNode;
			}
			else
			{
				break;
			}
		}
		
		// Get cookie name
		var cookieName = fw_GetCookieName(nodeId);
		if (cookieName)
		{
			fwtvjs_setNodePathCookie(cookieName, nodePath);
		}
		
		return(true);
	}
	
	return(false);
}








