//
//	Copyright (c) 2001 General Electric Company
//
//	Version control:
//		$Archive: /releases/FreeViewer_2.0/src/WebViewer/FreeViewer/HTML/vlayout.js $
//		$Revision: 22 $
//		$Date: 3/11/03 9:26a $
//		$Author: Marceld $
//
//	Helper comment to enable syntax coloring in InterDev: <script>

var _serieslayoutman = null;
var _bUpdateViewports = true;

//
// HasErrors
//
function HasErrors()
{
	var nSeries = _serieslayoutman.GetSeriesCount();
	for (var j = 0; j < nSeries; j++)
	{
		var oSeries = _serieslayoutman.GetSeries(j);
		var nViewports = oSeries.GetViewportCount();
		for (var i = 0; i < nViewports; i++)
		{
			// Ask all the viewports if they have an error.
			if (oSeries.GetViewport(i).HasErrors())
			{
				return true;
			}
		}
	}

	return false;
}


//
// GetErrorText
//
function GetErrorText()
{
	var nSeries = _serieslayoutman.GetSeriesCount();
	for (var j = 0; j < nSeries; j++)
	{
		var oSeries = _serieslayoutman.GetSeries(j);
		var nViewports = oSeries.GetViewportCount();
		for (var i = 0; i < nViewports; i++)
		{
			// Ask all the viewports if they have an error.
			if (oSeries.GetViewport(i).HasErrors())
			{
				return oSeries.GetViewport(i).GetErrorText();
			}
		}
	}

	return '';
}


//
// UpdateAllViewports
//
// update all viewport in the current series
function UpdateAllViewports(IDicomDirView, attributes)
{
	if (!_bUpdateViewports)
		return;

	if (!parent.LayoutHasChanged())
	{
		SaveImageAnnotations();
	}

	var oSeries = _serieslayoutman.GetActiveSeries();
	for (var i = 0; i < oSeries.GetViewportCount(); i++)
	{
		// Forward the update event to all viewports.
		oSeries.GetViewport(i).UpdateViewport(IDicomDirView, i, attributes);
	}
}


//
// UnlockViewports
//
function UnlockViewports(LicenseKey)
{
	for (var j = 0; j < _serieslayoutman.GetSeriesCount(); j++)
	{
		var oSeries = _serieslayoutman.GetSeries(j);
		for (var i = 0; i < oSeries.GetViewportCount(); i++)
		{
			// Set all license keys of the viewports.
			oSeries.GetViewport(i).UnlockViewport(LicenseKey);
		}
	}
}


//
// OnShowAnnotationsChange
//
function OnShowAnnotationsChange()
{
	for (var j = 0; j < _serieslayoutman.GetSeriesCount(); j++)
	{
		var oSeries = _serieslayoutman.GetSeries(j);
		for (var i = 0; i < oSeries.GetViewportCount(); i++)
		{
			// Forward the 'OnShowAnnotationsChange' event to all viewports.
			oSeries.GetViewport(i).OnShowAnnotationsChange();
		}
	}
}


//
// NotifyPlayMode
//
function NotifyPlayMode(bCineMode)
{
	var oSeries = _serieslayoutman.GetActiveSeries();
	for (var i = 0; i < oSeries.GetViewportCount(); i++)
	{
		oSeries.GetViewport(i).SetCineMode(bCineMode);
	}
}


//
// GetViewportCount
//
function GetViewportCount()
{
	return GetActiveSeries().GetViewportCount();
}


//
// IsSelectedImageMultiFrame
//
function IsSelectedImageMultiFrame()
{
	return (GetActiveViewport().GetFrameCount() > 1);
}


//
// SelectPreviousFrame
//
function SelectPreviousFrame()
{
	var oActiveViewport = GetActiveViewport();
	oActiveViewport.SelectPreviousFrame(oActiveViewport.GetViewportIndex());
}


//
// SelectNextFrame
//
function SelectNextFrame()
{
	var oActiveViewport = GetActiveViewport();
	oActiveViewport.SelectNextFrame(oActiveViewport.GetViewportIndex());
}


//
// SetActiveViewport
//
function SetActiveViewport(nViewportIndex, nSeriesIndex)
{
	if (nSeriesIndex != null && _serieslayoutman.GetActiveSeriesIndex() != nSeriesIndex)
	{
		SaveImageAnnotations();
		
		var oSeries = _serieslayoutman.GetActiveSeries();
		oSeries.SetSeriesNr(parent.GetDicomDirSeriesIndex());
		oSeries.SetImageNr(parent.GetDicomDirImageIndex());

		_bUpdateViewports = false;	// ignore OnSelectionChanged event
		oSeries = _serieslayoutman.SetActiveSeries(nSeriesIndex);
		parent.SetDicomDirActiveImages(oSeries.GetSeriesNr(), oSeries.GetImageNr());
		_bUpdateViewports = true;
	}
	var oActiveViewport = GetActiveSeries().SetActiveViewport(nViewportIndex);
	parent.viewcontrol.AddDefaultPreset(oActiveViewport.GetInitialWindowLevel());
	parent.viewcontrol.listPresentationStates.value = oActiveViewport.GetPresentationStateIndex();
	parent.viewcontrol.listWindowPreset.value = oActiveViewport.GetWindowLevel();
}


//
// GetActiveSeries
//
function GetActiveSeries()
{
	return _serieslayoutman.GetActiveSeries();
}

//
// GetActiveSeriesIndex
//
function GetActiveSeriesIndex()
{
	return _serieslayoutman.GetActiveSeriesIndex();
}

//
// GetActiveViewport
//
function GetActiveViewport()
{
	return GetActiveSeries().GetActiveViewport();
}


//
// GetActiveAttributes
//
function GetActiveAttributes()
{
	return _serieslayoutman.GetActiveSeries().GetActiveViewport().GetAttributes();
}


//
// PropagateAttributes
//
function PropagateAttributes(attribs)
{
	var oSeries = _serieslayoutman.GetActiveSeries();
	for (var i = 0; i < oSeries.GetViewportCount(); i++)
	{
		var viewport = oSeries.GetViewport(i);
		if (viewport == oSeries.GetActiveViewport())
			continue;
		oSeries.GetViewport(i).SetAttributes(attribs);
	}
}


//
// SetPresentationState
//
function SetPresentationState(fname)
{
	_serieslayoutman.GetActiveSeries().GetActiveViewport().SetPresentationState(fname);
}


//
// SaveImageAnnotations
//
function SaveImageAnnotations()
{
	var oSeries = _serieslayoutman.GetActiveSeries();
	var nViewports = oSeries.GetViewportCount();
	for (var i = 0; i < nViewports; i++)
	{
		oSeries.GetViewport(i).SaveImageAnnotations(i);
	}
}


//
// SetViewportLayout
//
function SetViewportLayout(nRows, nColumns)
{
	_serieslayoutman.GetActiveSeries().SetViewportLayout(nRows, nColumns);	// change currently selected series area
}


//
// SetSeriesLayout
//
function SetSeriesLayout(nRows, nColumns)
{
	return _serieslayoutman.SetSeriesLayout(nRows, nColumns);
}


//
// GetLayout
//
function GetLayout()
{
	return _serieslayoutman.GetActiveSeries().GetLayout();
}

//
// GetSeriesLayout
//
function GetSeriesLayout()
{
	return _serieslayoutman.GetSeriesLayout();
}

//
// SelectPresentationState
//
function SelectPresentationState(cnt)
{
	var oSeries = GetActiveSeries();
	var nViewports = oSeries.GetViewportCount();
	for (var i = 0; i < nViewports; i++)
	{
		oSeries.GetViewport(i).SetPresentationStateIndex(cnt);
	}
}

//
// OnLoad
//
function OnLoad()
{
	_serieslayoutman = new SeriesLayoutManager(divViewportTable);
	_serieslayoutman.SetSeriesLayout(1, 1);
}
