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

var _oWLPresetList = null;
var oPopup = window.createPopup();

//
// onload
//
function window.onload()
{
	checkboxAnnotations.checked = parent.GetShowAnnotations();
	OnCineSpeedChange();

	var settings = GetISettings();
	if (settings == null)
		return;

	Feed2PresentationStateListbox(GetApp().GetLocaleString('strEmptyPresentationState'), 0);

	_oWLPresetList = new WLPresetListControl(listWindowPreset);
	_oWLPresetList.Initialize(GetApp().config.GetWindowLevelPresetNode());
}

//
// onresize
//
function window.onresize()
{
	parent.frmsetViewing.rows = (document.body.scrollHeight + 2).toString() + ', *';
}

//
// OnViewportLayoutChange
//
function OnViewportLayoutChange(value)
{
	oPopup.hide();
	var nRows = parseInt(value.substr(0,1));
	var nColumns = parseInt(value.substr(2,1));
	parent.SetViewportLayout(nRows, nColumns);
}

//
// OnSeriesLayoutChange
//
function OnSeriesLayoutChange(value)
{
	oPopup.hide();
	var nRows = parseInt(value.substr(0,1));
	var nColumns = parseInt(value.substr(2,1));
	parent.SetSeriesLayout(nRows, nColumns);
}

//
// HasErrors
//
function HasErrors()
{
	return false;
}


//
// GetErrorText
//
function GetErrorText()
{
	return '';
}


//
// OnPageBack
//
function OnPageBack()
{
	parent.PageBack();
}


//
// OnPageForward
//
function OnPageForward()
{
	parent.PageForward();
}

//
// OnImageBack
//
function OnImageBack()
{
	parent.ImageBack();
}


//
// OnImageForward
//
function OnImageForward()
{
	parent.ImageForward();
}


//
// OnSeriesBack
//
function OnSeriesBack()
{
	parent.SeriesBack();
}


//
// OnSeriesForward
//
function OnSeriesForward()
{
	parent.SeriesForward();
}


//
// OnCineSpeedDown
//
function OnCineSpeedDown()
{
	parent.CineSpeedDecrease();
}


//
// OnCineSpeedUp
//
function OnCineSpeedUp()
{
	parent.CineSpeedIncrease();
}


//
// OnButtonPlayStop
//
// Purpose: Forward a click on the 'play/stop' button to our parent.
//
function OnButtonPlayStop()
{
	parent.ImagePlayStop();
}


//
// OnButtonPropagateClick
//
function OnButtonPropagateClick()
{
	parent.OnPropagateClicked();
}


//
// OnClickCheckboxAnnotations
//
function OnClickCheckboxAnnotations()
{
	parent.SetShowAnnotations(checkboxAnnotations.checked);
}


//
// NotifyPlayMode
//
// Purpose: Handle the 'play mode' event by disabling/enabling buttons
//          that can/cannot be used in play/stop mode.
//
function NotifyPlayMode(bPlaying)
{
	btnPrevImage.Enable(!bPlaying);
	btnNextImage.Enable(!bPlaying);
	btnPrevSeries.Enable(!bPlaying);
	btnNextSeries.Enable(!bPlaying);
	btnPropagate.Enable(!bPlaying);

	// Change the image on the button to indicate the play state.
	if (bPlaying)
	{
		divPlayStopImage.className = 'classStopImage';
	}
	else
	{
		divPlayStopImage.className = 'classPlayImage';
	}
}


//
// OnCineSpeedChange
//
function OnCineSpeedChange(s_nCineSpeedMin, nCineSpeed, s_nCineSpeedMax)
{
	if (nCineSpeed == s_nCineSpeedMin)
	{
		btnSpeedDown.Enable(false);
	}
	else if (nCineSpeed == s_nCineSpeedMax)
	{
		btnSpeedUp.Enable(false);
	}
	else
	{
		btnSpeedDown.Enable(true);
		btnSpeedUp.Enable(true);
	}
	spanSpeedValue.innerText = nCineSpeed;
}


//
// UpdateControls
//
// Purpose: Update the state of the controls to reflect what 
//          the user is allowed to do.
//
function UpdateControls(IDicomDirView)
{
	var bSingleImage = (IDicomDirView.GetActiveSeriesIndex() == -1);

	btnSpeedDown.Enable(!bSingleImage);
	btnSpeedUp.Enable(!bSingleImage);

	if (IDicomDirView.IsImageSelected())
	{
		if (parent.IsSelectedImageMultiFrame())
		{
			btnPrevImage.Enable(true);
			btnNextImage.Enable(true);
			btnPrevPage.Enable(false);
			btnNextPage.Enable(false);
			btnPlayStop.Enable(true);
		}
		else
		{
			var bHasPrevImages = IDicomDirView.HasPreviousImageInSeries();
			var bHasNextImages = IDicomDirView.HasNextImageInSeries();
			var bMultipleViewports = (parent.viewlayout.GetLayout() != '1x1');
			btnPrevImage.Enable(bHasPrevImages);
			btnNextImage.Enable(bHasNextImages);
			btnPlayStop.Enable(bHasPrevImages || bHasNextImages);
			btnPrevPage.Enable(bHasPrevImages && bMultipleViewports);
			btnNextPage.Enable(bHasNextImages && bMultipleViewports);
		}
		btnNextSeries.Enable(IDicomDirView.HasNextSeries());
		btnPrevSeries.Enable(IDicomDirView.HasPreviousSeries());
		if (!bSingleImage)
		{
			btnPropagate.Enable(true);
			btnViewportLayout.Enable(true);
			btnSeriesLayout.Enable(true);
			listPresentationStates.disabled = false;
			listWindowPreset.disabled = false;
		}
	}
	else
	{
		// No image is selected: disable all image control buttons.
		btnPrevImage.Enable(false);
		btnNextImage.Enable(false);
		btnPrevPage.Enable(false);
		btnNextPage.Enable(false);
		btnNextSeries.Enable(false);
		btnPrevSeries.Enable(false);
		btnPlayStop.Enable(false);
		btnPropagate.Enable(false);
		btnViewportLayout.Enable(false);
		btnSeriesLayout.Enable(false);
		listPresentationStates.disabled = true;
		listWindowPreset.disabled = true;
	}
}


//
// Feed2PresentationStateListbox
//
function Feed2PresentationStateListbox(str, val)
{
	var oOption = document.createElement("OPTION");
	oOption.text = str;
	oOption.value = new String(val);
	listPresentationStates.options.add(oOption);
}


//
// ClearPresentationStateListbox
function ClearPresentationStateListbox()
{
	var len = listPresentationStates.options.length;
	for (var i = 0; i < len; i++)
	{
		listPresentationStates.options.remove(0);
	}
}


//
// OnPresentationStateChange
//
function OnPresentationStateChange(value)
{
	parent.OnPresentationStateChange(value);
	parent.p_dicomdirview.onselectionchanged();	// force viewport update
}


//
// SelectPresentationState
//
function SelectPresentationState(cnt)
{
	listPresentationStates.selectedIndex = cnt;
}

//
// RefreshWLPresetList
function RefreshWLPresetList(strMod)
{
	_oWLPresetList.OnModalityChange(strMod);
}

//
// OnSeriesLayout
//
function OnSeriesLayout()
{
    oPopup.document.createStyleSheet('popup.css');
	oPopup.document.body.innerHTML = divSerieslayoutPopup.innerHTML; 
	//oPopup.document.body.onload = test;
	oPopup.show(2, btnSeriesLayout.offsetHeight, 105, 38, btnSeriesLayout);
}

//
// OnViewportLayout
//
function OnViewportLayout()
{
    oPopup.document.createStyleSheet('popup.css');
	oPopup.document.body.innerHTML = divViewportLayoutPopup.innerHTML; 
	oPopup.show(2, btnViewportLayout.offsetHeight, 140, 105, btnViewportLayout);
}

//
// AddDefaultPreset
//
function AddDefaultPreset(strValue)
{
	_oWLPresetList.AddOption(strValue, GetApp().GetLocaleString('strNoWindowLevel'), 0);
}
