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

var strDicomTreeViewObject = '<object id = "activexviewerlite"' +
									' name = "%VIEWPORTID%"' +
									' series = "%SERIESID%"' +
							        ' type = "application/x-oleobject"' +
							        ' classid = "clsid:F2ECA3B1-783D-4f90-9CA5-9498FC88C267"' +
							        ' codebase = "CdViewer.cab#version=' + strVersion + '"' +
							        ' class = "classNoFocus">' +
							 '</object>';

//
// Viewport C'tor
//
function Viewport(_strViewportId, _strSeriesId)
{
	this._nFrameIndex = 0;
	this._viewframe = parent;
	this._dicomdirframe = null;
	this._strViewportId = _strViewportId;
	this._strSeriesId = _strSeriesId;
	this._activexviewer = null;
	this._strInitWL = '';
	this._presstateidx = 0;	// no presentation state applied
}

//
// GetObjectElementString
//
function Viewport.prototype.GetObjectElementString()
{ with(this) {
	var re = /%VIEWPORTID%/g;
	var strElement = strDicomTreeViewObject.replace(re, _strViewportId);
	re = /%SERIESID%/g;
	return strElement.replace(re, _strSeriesId);
}}

//
// Init
//
function Viewport.prototype.Init(oActiveXViewer)
{ with(this) {
	_activexviewer = oActiveXViewer;
	
	if (_activexviewer.readyState == 4)
	{
		InitImpl();
	}
	else
	{
		_activexviewer.onreadystatechange = CreateStaticFunction(this, 'InitImpl');
	}
}}

	
//
// InitImpl
//
function Viewport.prototype.InitImpl()
{ with(this) {
	if (_activexviewer.readyState != 4)	
		return;
	
	_activexviewer.onreadystatechange = null;
	_activexviewer.onfocus = OnFocusViewControl;
	_activexviewer.attachEvent('OnMouseWheel', _viewframe.OnMouseWheelViewControl);
	if (HasErrors())
	{
		return;
	}

	try
	{
		// Initialise the annotation format of the viewport control.
		_activexviewer.SetAnnotFormat("Default", 
									  "BottomLeft=C@00281050@\nW@00281051@ ## BottomMiddle=@00200020@ ## BottomRight=@00080070@ ## MiddleLeft=@00200020@ ## MiddleRight=@00200020@ ## TopLeft=@00100010@\n@00200013%i@ ## TopMiddle=@00200020@ ## TopRight=@00081030@\n@00080020@\n@00080030@\n@00080050@ ## MiddlePercentageSize=20 ## ",
									  "");
		_activexviewer.SetAnnotFormat("CT", 
									  "DerivePatientOrientation=1 ## BottomLeft=SP:@00201041%5.1f@mm\nST:@00180050%5.1f@mm\nC@00281050@\nW@00281051@ ## BottomMiddle=@00200020@ ## BottomRight=@00080070@ ## MiddleLeft=@00200020@ ## MiddleRight=@00200020@ ## TopLeft=@00100010@\n@00200013%i@ ## TopMiddle=@00200020@ ## TopRight=@00081030@\n@00080020@\n@00080030@\n@00080050@ ## MiddlePercentageSize=20 ## ",
									  "");
		_activexviewer.SetAnnotFormat("SC", 
									  "BottomLeft=C@00281050@\nW@00281051@ ## BottomMiddle=@00200020@ ## BottomRight=@00080070@ ## MiddleLeft=@00200020@ ## MiddleRight=@00200020@ ## TopLeft=@00100010@\n@00200013%i@ ## TopMiddle=@00200020@ ## TopRight=@00081030@\n@00080020@\n@00080030@\n@00080050@ ## MiddlePercentageSize=20 ## ",
									  "");
		_activexviewer.SetAnnotFormat("MR", 
									  "DerivePatientOrientation=1 ## BottomLeft=SP:@00201041%5.1f@mm\nST:@00180050%5.1f@mm\nC@00281050@\nW@00281051@ ## BottomMiddle=@00200020@ ## BottomRight=@00080070@ ## MiddleLeft=@00200020@ ## MiddleRight=@00200020@ ## TopLeft=@00100010@\n@00200013%i@ ## TopMiddle=@00200020@ ## TopRight=@00081030@\n@00080020@\n@00080030@\n@00080050@ ## MiddlePercentageSize=20 ## ",
									  "");
		_activexviewer.SetAnnotFormat("RG", 
									  "BottomLeft=C@00281050@\nW@00281051@ ## BottomMiddle=@00200020@ ## BottomRight=@00080070@ ## MiddleLeft=@00200020@ ## MiddleRight=@00200020@ ## TopLeft=@00100010@\n@00200013%i@ ## TopMiddle=@00200020@ ## TopRight=@00081030@\n@00080020@\n@00080030@\n@00080050@ ## MiddlePercentageSize=20 ## ",
									  "");
		_activexviewer.SetAnnotFormat("XA", 
									  "BottomLeft=C@00281050@\nW@00281051@ ## BottomMiddle=@00200020@ ## BottomRight=@00080070@ ## MiddleLeft=@00200020@ ## MiddleRight=@00200020@ ## TopLeft=@00100010@\n@00200013%i@ (F@frame@/@00280008@) ## TopMiddle=@00200020@ ## TopRight=@00081030@\n@00080020@\n@00080030@\n@00080050@ ## MiddlePercentageSize=20 ## ",
									  "");
		_activexviewer.SetAnnotFormat("US", 
									  "BottomLeft=C@00281050@\nW@00281051@ ## BottomMiddle=@00200020@ ## BottomRight=@00080070@ ## MiddleLeft=@00200020@ ## MiddleRight=@00200020@ ## TopLeft=@00100010@\n@00200013%i@ ## TopMiddle=@00200020@ ## TopRight=@00081030@\n@00080020@\n@00080030@\n@00080050@ ## MiddlePercentageSize=20 ## ",
									  "");
		_activexviewer.SetAnnotFormat("CR", 
									  "BottomLeft=C@00281050@\nW@00281051@ ## BottomMiddle=@00200020@ ## BottomRight=@00080070@ ## MiddleLeft=@00200020@ ## MiddleRight=@00200020@ ## TopLeft=@00100010@\n@00200013%i@ ## TopMiddle=@00200020@ ## TopRight=@00081030@\n@00080020@\n@00080030@\n@00080050@ ## MiddlePercentageSize=20 ## ",
									  "");
		_activexviewer.SetAnnotFormat("NM", 
									  "BottomLeft=C@00281050@\nW@00281051@ ## BottomMiddle=@00200020@ ## BottomRight=@00080070@ ## MiddleLeft=@00200020@ ## MiddleRight=@00200020@ ## TopLeft=@00100010@\n@00200013%i@ (F@frame@/@00280008@) ## TopMiddle=@00200020@ ## TopRight=@00081030@\n@00080020@\n@00080030@\n@00080050@ ## MiddlePercentageSize=20 ## ",
									  "");

		_activexviewer.SetViewportProperty('imageannotations', GetApp().config.ImageAnnotationOptionsString());
		_activexviewer.SetViewportProperty('viewportannotations', GetApp().config.ViewportAnnotationOptionsString());

		OnShowAnnotationsChange();
		_activexviewer.SetLocaleDbAndId(GetApp().localedb.GetXMLDb(), GetApp().config.LocaleId())

		var Index = location.href.lastIndexOf("/");
		var strApplicationDataURL = unescape(location.href.slice(0, Index));
		_activexviewer.strApplicationDataURL = strApplicationDataURL;
	}
	catch (e)
	{
	}
}}


//
// HasErrors
//
function Viewport.prototype.HasErrors()
{ with (this) {
	try
	{
		return ((typeof(_activexviewer) != 'object') ||
			    (_activexviewer.object == null) ||
				(_activexviewer.readyState != 4));
	}
	catch (e)
	{
		return true;
	}
}}


//
// GetErrorText
//
function Viewport.prototype.GetErrorText()
{ with (this) {
	return GetApp().GetLocaleString('msgCannotLoadAmiViewerLite');
}}


//
// UnlockViewport
//
function Viewport.prototype.UnlockViewport(LicenseKey)
{ with(this) {
	LicenseKey.Unlock(_activexviewer);
}}


//
// SaveImageAnnotations
//
function Viewport.prototype.SaveImageAnnotations(index, frameindex)
{ with(this) {
	if (frameindex == null)
		frameindex = 0;

	var strUID = _viewframe.GetImageUID(_strSeriesId + index.toString());
	if (strUID != null)
	{
		var strAnnot = _activexviewer.GetImageAnnotations();	// if the current image has image annotations
		if ((strAnnot != "") || (strUID != "" && _viewframe.GetImageAnnotation(_strSeriesId, strUID + frameindex) != ""))
		{
			_viewframe.AddToImageAnnotations(_strSeriesId, _viewframe.GetImageUID(_strSeriesId + index.toString()) + frameindex, strAnnot);
		}
	}
}}


//
// ApplyPresentationState
//
function Viewport.prototype.ApplyPresentationState(uid)
{ with(this) {
	var index = _viewframe.GetPresentationStateIndex();
	// apply presentation state
	var fname = _dicomdirframe.GetPresentationStateFilename(index, uid);
	if (fname != "")
	{
		_presstateidx = index;
		SetPresentationState(fname);
	}
}}


//
// ApplyImageAnnotations
//
function Viewport.prototype.ApplyImageAnnotations(strUID, frameindex)
{ with(this) {
	if (frameindex == null)
		frameindex = 0;

	var strAnnot = _viewframe.GetImageAnnotation(_strSeriesId, strUID + frameindex);
	if (strAnnot != null && strAnnot != "")
	{
		_activexviewer.SetImageAnnotations(frameindex, strAnnot);
	}
}}


//
// ApplyImageAtributes
//
function Viewport.prototype.ApplyImageAtributes(attributes)
{ with(this) {
	if (attributes != null)
	{
		SetAttributes(attributes);
	}
}}

//
// UpdateViewport
//
// Purpose: Updates the image in the viewport control.
//
function Viewport.prototype.UpdateViewport(IDicomDirView, index, attributes)
{ with(this) {
	_dicomdirframe = IDicomDirView;
	//SaveImageAnnotations(index);

	var strUID = _dicomdirframe.GetImageInstanceUID(index);	// get instance UID of the image to be shown

	_viewframe.SetImageUID(_strSeriesId + index.toString(), strUID);
	_viewframe.SetSeriesIndex(_strSeriesId, _dicomdirframe.GetActiveSeriesIndex());

	// Display the current selected image.
	_activexviewer.strDataPath = _dicomdirframe.GetImagePath(index);

	_strInitWL = _activexviewer.GetViewportProperty('windowlevel');
	_viewframe.viewcontrol.AddDefaultPreset(_strInitWL);

	ApplyPresentationState(strUID);
	ApplyImageAnnotations(strUID, 0);
	ApplyImageAtributes(attributes);

	_nFrameIndex = 0;
}}


//
// SelectPreviousImage
//
function Viewport.prototype.SelectPreviousFrame(index)
{ with(this) {
	SaveImageAnnotations(index, _nFrameIndex);
	_nFrameIndex--;
	if (_nFrameIndex < 0)
	{
		_nFrameIndex = GetFrameCount() - 1;
	}
	_activexviewer.SetFrame(_nFrameIndex);

	var strUID = _viewframe.GetImageUID(_strSeriesId + index.toString());
	var attributes = _viewframe.GetActiveSeriesAttributes();

	ApplyPresentationState(strUID);
	ApplyImageAnnotations(strUID, _nFrameIndex);
	ApplyImageAtributes(attributes);
}}


//
// GetFrameCount
//
function Viewport.prototype.GetFrameCount()
{ with(this) {
	return _activexviewer.FrameCount;
}}


//
// SelectNextImage
//
function Viewport.prototype.SelectNextFrame(index)
{ with(this) {
	SaveImageAnnotations(index, _nFrameIndex);
	_nFrameIndex++;
	if (_nFrameIndex >= GetFrameCount())
	{
		_nFrameIndex = 0;
	}
	_activexviewer.SetFrame(_nFrameIndex);

	var strUID = _viewframe.GetImageUID(_strSeriesId + index.toString());
	var attributes = _viewframe.GetActiveSeriesAttributes();

	ApplyPresentationState(strUID);
	ApplyImageAnnotations(strUID, _nFrameIndex);
	ApplyImageAtributes(attributes);
}}


//
// OnFocusViewControl
//
function Viewport.prototype.OnFocusViewControl()
{ with(this) {
	SetActiveViewport(parseInt(this.name), parseInt(this.series));
}}


//
// OnShowAnnotationsChange
//
// Purpose: Updates the annotations in the viewport depending on the current state
//          of 'show-annotations' of the viewframe component.
//
function Viewport.prototype.OnShowAnnotationsChange()
{ with(this) {
	var nNone  = 0;
	var nBasic = 1;

	if (_viewframe.GetShowAnnotations())
	{
		_activexviewer.AnnotationLevel = nBasic;
	}
	else
	{
		_activexviewer.AnnotationLevel = nNone;
	}
}}


//
// SetCineMode
//
function Viewport.prototype.SetCineMode(bCineMode)
{ with(this) {
	_activexviewer.CineMode = bCineMode;
}}


//
// GetAttributes
//
function Viewport.prototype.GetAttributes()
{ with(this) {
	return _activexviewer.GetImageAttributes();
}}

//
// SetAttributes
//
function Viewport.prototype.SetAttributes(attributes)
{ with(this) {
	_activexviewer.SetImageAttributes(attributes);
}}


//
// GetPresentationStateCount
//
function Viewport.prototype.GetPresentationStateCount()
{ with(this) {
	return _activexviewer.PresentationStateCount;
}}


//
// SetPresentationState
//
function Viewport.prototype.SetPresentationState(strFilename)
{ with(this) {
	_activexviewer.strPresentationStatePath = strFilename;
}}

//
// GetViewportIndex
//
function Viewport.prototype.GetViewportIndex()
{ with(this) {
	return parseInt(_strViewportId);
}}

//
// MakeActive
//
function Viewport.prototype.MakeActive()
{ with(this) {
	_activexviewer.parentElement.className = "classFocus";
}}

//
// MakeInactive
//
function Viewport.prototype.MakeInactive()
{ with(this) {
	_activexviewer.parentElement.className = "classNoFocus";
}}

//
// SetWindowLevel
//
function Viewport.prototype.SetWindowLevel(strValue)
{ with(this) {
	_activexviewer.SetViewportProperty('windowlevel', strValue);
}}

//
// GetWindowLevel
//
function Viewport.prototype.GetWindowLevel()
{ with(this) {
	return _activexviewer.GetViewportProperty('windowlevel');
}}

//
// GetInitialWindowLevel
//
function Viewport.prototype.GetInitialWindowLevel()
{ with(this) {
	return _strInitWL;
}}

//
// IsStaticPaletteMonitor
//
function Viewport.prototype.IsStaticPaletteMonitor()
{ with(this) {
	return _activexviewer.IsStaticPaletteMonitor();
}}

//
// GetPresentationStateIndex
//
function Viewport.prototype.GetPresentationStateIndex()
{ with(this) {
	return _presstateidx;
}}

//
// SetPresentationStateIndex
//
function Viewport.prototype.SetPresentationStateIndex(nVal)
{ with(this) {
	_presstateidx = nVal;
}}