﻿var printImageWidth = 660;
var printImageHeight = 756;
var printFooterPos = 880 ;
var originalSize;
var delta = 100;
var minimum = 100;
var Print = 0, Zoom = 1, ZoomToFit = 2 ;
var iIntervalID ;
var waitTick = 100 ;
var waitLimit = 30000 ;

//Changes Size of the image object.
function ChangeSize(isPrint, width, height)
{
	var imgObject;
	
	if( isPrint )
		imgObject = fraPrint.idImage ;
	else
		imgObject = fraImage.idImage ;
		
	if( (height == 0) && (width != 0) )
	{
		var ratio = originalSize.width / width ;
		imgObject.width = width ;
		imgObject.height /= ratio ;
	}

	if( (width == 0) && (height != 0) )
	{
		var ratio = originalSize.height / height ;
		imgObject.height = height ;
		imgObject.width /= ratio ;
	}
}

//Zoom in image.
function ZoomInImage()
{
	ChangeSize(false, fraImage.idImage.width + delta, 0);
	ProcessSize(Zoom);
}

//Zoom out image.
function ZoomOutImage()
{
	var changeWidth = fraImage.idImage.width - delta;

	if (changeWidth < minimum)
		changeWidth = minimum;

	ChangeSize(false, changeWidth, 0);
	ProcessSize(Zoom);
}

//Get Top Height.
function GetTopHeight()
{
	var height = 0 ;

	height += fraToolbar.document.body.clientHeight ;
	height += fraHeader.idDocument.clientHeight ;
	
	return height ;
}

//Process the height of the image.
function ProcessSize( reason )
{
	var origWidth = originalSize.width;
	var origHeight = originalSize.height;
	var currentWidth ;
	var ratio ;

	switch( reason ) {
		case Print:
			fraPrint.idImage.width = fraImage.idImage.width ;
			fraPrint.idImage.height = fraImage.idImage.height ;

			if( fraPrint.idImage.height > fraPrint.idImage.width )
			{
				ratio = fraPrint.idImage.height/printImageHeight;
				fraPrint.idImage.width /= ratio;
				fraPrint.idImage.height = printImageHeight;
			}
			else
			{
				ratio = fraPrint.idImage.width/printImageWidth;
				fraPrint.idImage.width = printImageWidth;
				fraPrint.idImage.height /= ratio;
			}
			
			break;

		case Zoom:
			currentWidth = fraImage.idImage.clientWidth;
			ratio = origWidth/currentWidth;
			fraImage.idImage.height = origHeight/ratio;
			fraImage.idImage.width = origWidth/ratio;
			
			break;
		
		case ZoomToFit:
			var enableHeight = document.body.clientHeight - GetTopHeight() - 50;

			currentWidth = fraImage.idImage.clientWidth;
			ratio = origWidth/currentWidth;
			fraImage.idImage.height = origHeight/ratio;
			fraImage.idImage.width = origWidth/ratio;

			if (enableHeight < fraImage.idImage.clientHeight)
			{
				if (enableHeight < minimum)
					enableHeight = 100;

				ratio = origHeight/enableHeight;
				fraImage.idImage.width = origWidth/ratio;
				fraImage.idImage.height = enableHeight;
			}
			
			break;
	}
}

//Zoom to fit image.
function ZoomToFitImage()
{
	var sizeFit = document.body.clientWidth - 30;
	if(sizeFit < minimum)
		sizeFit = minimum;
	ChangeSize(false, sizeFit, 0);
	ProcessSize(ZoomToFit);
}

//Print image.
function PrintImage()
{
	// Position footer (bottom, center).
	fraPrint.idFooter.style.position = "absolute";
	fraPrint.idFooter.style.top = printFooterPos;
	fraPrint.idFooter.style.left = (printImageWidth - fraPrint.idFooter.clientWidth) / 2 ;

	ChangeSize(true, printImageWidth, 0);
	ProcessSize(Print);

	fraPrint.focus();
	fraPrint.print();
}

function WaitForImageToLoad()
{
	// Call until image tag has completely loaded in order to obtain correct width & height properties.
	waitLimit -= waitTick;
	
	if( fraImage.idImage.complete )
	{
		clearInterval( iIntervalID );

		originalSize = new Object();
		originalSize.width = fraImage.idImage.width;
		originalSize.height = fraImage.idImage.height;
		
		ZoomToFitImage();
		fraImage.idImage.style.visibility = "visible";

		if (typeof(top) == "object")
			top.document.title = document.title;
	}
	else if( waitLimit <= 0 )
	{
		clearInterval( iIntervalID );
		fraImage.idImage.style.visibility = "visible";
	}
}

function DisableContextMenu()
{
	return true;
}

function document.onreadystatechange()
{
    if (document.readyState == "complete")
    {
        setupToolbar();
        enableGrabButton(false);
        enableVisibilityButton(false);
        enableWirecolorButton(false);
        enableFindButton(false);
        enableResetButton(false);
        setupHeader();
        resizeHeader();
        setupImage();
		
        fraPrint.document.body.innerHTML = 
	        '<table border="0" align="center"><tr><td>' + fraHeader.document.body.innerHTML + '</td></tr>' +
	        '<tr><td>' + fraImage.document.body.innerHTML + '</td></tr>' + 
	        '<tr><td id="idFooter" align="center">' + gMRICCopyright + '</td></tr></table>';
        fraPrint.idImage.style.visibility = "visible" ;
		
        iIntervalID = setInterval( WaitForImageToLoad, waitTick ) ;
		
        fraToolbar.document.body.oncontextmenu = DisableContextMenu;
        fraHeader.document.body.oncontextmenu = DisableContextMenu;
        fraImage.document.body.oncontextmenu = DisableContextMenu;
        fraPrint.document.body.oncontextmenu = DisableContextMenu;
		
        fraImage.document.body.onselectstart = KillSelection;
        fraImage.document.body.ondrag = KillSelection;
    }
}

function window.onresize() 
{
	resizeHeader();
}

function resizeHeader() 
{
	var headerHeight = fraHeader.idDocument.clientHeight;
    var rows = idImageFrame.rows.split(",");
    rows[1] = headerHeight;
    idImageFrame.rows = rows.join(",");
}

function KillSelection(evt)
{
	return false;
}


