
var ie4 = (document.all && !document.getElementById) ? true : false;
var ie5 = (document.all && document.getElementById) ? true : false;
var ns6 = (!document.all && document.getElementById) ? true : false;
var ns4 = (document.layers) ? true : false;

// Default for papir orientering er liggende og alle er i millimeter
var A4_WIDTH = 297, A4_HEIGHT = 210, 
	A3_WIDTH = 420, A3_HEIGHT = 297,
	A2_WIDTH = 594, A2_HEIGHT = 420,
	A1_WIDTH = 840, A1_HEIGHT = 594,
	A0_WIDTH = 1188, A0_HEIGHT = 840;
	
var A4_MAP_WIDTH = 252, A4_MAP_HEIGHT = 170, 
	A3_MAP_WIDTH = 380, A3_MAP_HEIGHT = 257,
	A2_MAP_WIDTH = 544, A2_MAP_HEIGHT = 370,
	A1_MAP_WIDTH = 790, A1_MAP_HEIGHT = 544,
	A0_MAP_WIDTH = 1138, A0_MAP_HEIGHT = 790;

var printSetupDialog, printPage;
var printPageInfoObj;
var prt_unselectFunction = null;
var prt_mapCenterX = -1, mapCenterY = -1;


function PrintTool (unselectFunctionName) {
	printSetupDialog = null;
	
	if (unselectFunctionName != null || unselectFunctionName != '') {
		prt_unselectFunction = unselectFunctionName;
		if (prt_unselectFunction.lastIndexOf(')') == -1) {
			prt_unselectFunction += '()';
		} 
	}
	
	this.start = printtoolStart;
	this.stop = printtoolStop;
	this.setMapCenter = printtoolSetMapCenter;
	this.openSetupDialog = printtoolOpenSetupDialog;
	this.closeSetupDialog = printtoolCloseSetupDialog;
	this.getPixelsPrMillimeter = printtoolGetPixelsPrMillimeter;
	this.getMapWidth = printtoolGetMapWidth
}
function printtoolStart () {
	if (prt_mapCenterX == -1 || prt_mapCenterY == -1) {
		alert('Fejl center-punktet er ugyldigt!');
		return;
	}
	this.openSetupDialog ();
}
function printtoolStop () {
	this.closeSetupDialog ();
	prt_mapCenterX = -1;
	prt_mapCenterY = -1;
}
function printtoolSetMapCenter (x, y) {
	prt_mapCenterX = x;
	prt_mapCenterY = y;
}
function printtoolOpenSetupDialog () {
	printSetupDialog = window.open(maptoolURL + 'print_setup_dlg.html', 'PrintSetupWin', 
				'top=200,left=350,toolbar=no,status=no,menubar=no,width=240,height=284');
}
function printtoolCloseSetupDialog () {
	if (printSetupDialog.document) {
		printSetupDialog.close();
	}
	printSetupDialog = null;
	printtoolUnselectPrintTool();
}
function printtoolOpenPrintPage () {
	var brugerIDtilprint = document.frames.requestFrame.document.Form1.BrugerIDTilPrint.value
	var LagStr = getTaendteLag()
	
	
	printPage = window.open(maptoolURL + 'printpage.asp?BrugerID=' + brugerIDtilprint + "&LagStr=" + LagStr, '', 
				'resizeable=yes, toolbar=no, status=no, menubar=yes, scrollbars=yes, top=2, left=2');
//	printPage = top.open('printpage.html', '');
	printPage.moveTo(0,0);
	printPage.resizeTo(screen.availWidth, screen.availHeight)

}
function printButtonClickHandler () {
	var mapZoom, pixels_pr_mm, mapWidthInPixel, mapHeightInPixel, pageWidth, pageHeight;
	var isLandscape;
	var paperType, scale;

	if (!printSetupDialog.document) {
		alert('DialogBoks er ikke åben!');
		return;
	}
	var printSetupInfoObj = printSetupDialog.getInfoObj();

	if (printSetupInfoObj == null) {
		return;
	}
	printtoolCloseSetupDialog();
	printtoolSavePageInfoObj(printSetupInfoObj);

	isLandscape = printSetupInfoObj.isLandscape();
	paperType = printSetupInfoObj.getPaperType();
	scale = printSetupInfoObj.getScale();
	
	mapZoom = printtoolGetMapZoom(paperType, isLandscape, scale);
	pixels_pr_mm = printtoolGetPixelsPrMillimeter();
	mapWidthInPixel = printtoolGetMapWidthInPixel(pixels_pr_mm, paperType, isLandscape);
	mapHeightInPixel = printtoolGetMapHeightInPixel(pixels_pr_mm, paperType, isLandscape);
	
	pageWidth = (isLandscape)? pixels_pr_mm * A4_WIDTH : A4_HEIGHT;
	pageHeight = (isLandscape)? pixels_pr_mm * A4_HEIGHT : A4_WIDTH;	

	mapLeftInPixel = (pageWidth - mapWidthInPixel)/2;
	mapTopInPixel = (pageHeight - mapHeightInPixel)/2;
	
	printPageInfoObj.setPaperType(paperType);
	printPageInfoObj.setMapWidthInPixel(mapWidthInPixel);
	printPageInfoObj.setMapHeightInPixel(mapHeightInPixel);
	printPageInfoObj.setMapTopInPixel(mapTopInPixel);
	printPageInfoObj.setMapLeftInPixel(mapLeftInPixel);
	printPageInfoObj.setMapZoom(mapZoom);
	printPageInfoObj.setMapCenter(prt_mapCenterX, prt_mapCenterY);
	
	printtoolOpenPrintPage(pageWidth, pageHeight);
}

// Gem en kopi af objektet som hentes fra setup dialog
function printtoolSavePageInfoObj (printsetupinfo) {
	printPageInfoObj = new PrintSetup();
	printPageInfoObj.setTitle(printsetupinfo.getTitle());
	printPageInfoObj.setDateStr(printsetupinfo.getDateStr());
	printPageInfoObj.setScale(printsetupinfo.getScale());
	printPageInfoObj.setRemarkText(printsetupinfo.getRemarkText());
}
function printtoolGetPrintPageInfo () {
	return printPageInfoObj;
}
function previewButtonClickHandler () {
	alert('Preview clicked!\nDenne funktionalitet er under implimentering...')
}
function getPrintSetupInfoObj () {
	return printSetupDialog.getPrintSetupInfoObj();
}
function printtoolGetPixelsPrMillimeter () {
	var tempLyr, pixelsPrMillimeter;
	var tempImgStr = '<img id="pixelsPr100mm" src="images/spacer.gif" height="1" style="width:100mm;">';
	createLayer('tempLyr', null, -1, -1, 1, 1, tempImgStr, null, 'hidden');
	
	if (ns6) {
		tempLyr = document.getElementById('tempLyr');
		tempImg = document.getElementById('pixelsPr100mm');
	} else if (ie4 || ie5) {
		tempLyr = document.all['tempLyr'];		
		tempImg = document.all['pixelsPr100mm'];		
	}
	pixelsPrMillimeter = eval(tempImg.width / 100);
	destroyLayer('tempLyr');
	return pixelsPrMillimeter;
}
function printtoolGetMapZoom (paperType, landscape, scale) {
	var mapZoom;
	var mapWidth = printtoolGetMapWidth(paperType, landscape);	// i millimeter
	mapZoom = (mapWidth/1000) * scale;	// zoom-bredde i meter
	return mapZoom;
}
function printtoolGetMapWidth (paperType, landscape) {
	var mapWidth;	// i millimeter
	if (paperType == 'A4') {
		if (landscape) {
			mapWidth = A4_MAP_WIDTH;
		} else {
			mapWidth = A4_MAP_HEIGHT;		
		}
	} else if (paperType == 'A3') {
		if (landscape) {
			mapWidth = A3_MAP_WIDTH;
		} else {
			mapWidth = A3_MAP_HEIGHT;		
		}
	} else if (paperType == 'A2') {
		if (landscape) {
			mapWidth = A2_MAP_WIDTH;
		} else {
			mapWidth = A2_MAP_HEIGHT;		
		}
	} else if (paperType == 'A1') {
		if (landscape) {
			mapWidth = A1_MAP_WIDTH;
		} else {
			mapWidth = A1_MAP_HEIGHT;		
		}
	} else if (paperType == 'A0') {
		if (landscape) {
			mapWidth = A0_MAP_WIDTH;
		} else {
			mapWidth = A0_MAP_HEIGHT;		
		}
	}
	return mapWidth;
}
function printtoolGetMapHeight (paperType, landscape) {
	var mapHeight;	// i millimeter
	if (paperType == 'A4') {
		if (landscape) {
			mapHeight = A4_MAP_HEIGHT;
		} else {
			mapHeight = A4_MAP_WIDTH;		
		}
	} else if (paperType == 'A3') {
		if (landscape) {
			mapHeight = A3_MAP_HEIGHT;
		} else {
			mapHeight = A3_MAP_WIDTH;		
		}
	} else if (paperType == 'A2') {
		if (landscape) {
			mapHeight = A2_MAP_HEIGHT;
		} else {
			mapHeight = A2_MAP_WIDTH;		
		}
	} else if (paperType == 'A1') {
		if (landscape) {
			mapHeight = A1_MAP_HEIGHT;
		} else {
			mapHeight = A1_MAP_WIDTH;		
		}
	} else if (paperType == 'A0') {
		if (landscape) {
			mapHeight = A0_MAP_HEIGHT;
		} else {
			mapHeight = A0_MAP_WIDTH;		
		}
	}
	return mapHeight;
}
function printtoolGetMapWidthInPixel (pixelsPrMillimeter, paperType, landscape) {
	var mapWidthInMillimeter = printtoolGetMapWidth(paperType, landscape);
	return Math.round(pixelsPrMillimeter * mapWidthInMillimeter);
}
function printtoolGetMapHeightInPixel (pixelsPrMillimeter, paperType, landscape) {
	var mapHeightInMillimeter = printtoolGetMapHeight(paperType, landscape);
	return Math.round(pixelsPrMillimeter * mapHeightInMillimeter);
}
function printtoolUnselectPrintTool () {
	if (prt_unselectFunction != null) {
		eval(prt_unselectFunction);
	}
}
