var printScaleValue;
var printFormat;
var printOrientation;
var printFactor;
var printDragX1, printDragY1, printDragX2, printDragY2, oldPrintTop, oldPrintLeft;

// generate the print rectangle
function generatePrintRect() {
	var printRect = document.getElementById('printrectlayer');
	var top = document.getElementById('printrectlayer').style.top;
	var left = document.getElementById('printrectlayer').style.left;
	var legend = document.getElementById('printlegendcheck').checked;

	try {
		printScaleValue = document.getElementById('printscale').value;

		printFormat = document.getElementById('formato').value;
		printOrientation = document.getElementById('orientamento').value;

		var pageSize = getPageSize();

		if (printFormat == 'A4' && printOrientation == 'portrait') {
			printFactorX = (mbrMaxX - mbrMinX) / pageSize[0];
			printFactorY = (mbrMaxY - mbrMinY) / pageSize[1];
			printRect.style.width = ((0.174 * printScaleValue) / printFactorX) + 'px';
			if (legend) {
				printRect.style.height = ((0.120 * printScaleValue) / printFactorY) + 'px';
			} else {
				printRect.style.height = ((0.200 * printScaleValue) / printFactorY) + 'px';
			}
			if (top != "" && left != "") {
				printRect.style.left = left;
				printRect.style.top = top;
			} else {
				printRect.style.left = (pageSize[0] / 2) - (parseInt(printRect.style.width) / 2);
				printRect.style.top = (pageSize[1] / 2) - (parseInt(printRect.style.height) / 2);
			}

			printRect.style.visibility = 'visible';
		} else if (printFormat == "A4" && printOrientation == 'landscape') {
			printFactorX = (mbrMaxX - mbrMinX) / pageSize[0];
			printFactorY = (mbrMaxY - mbrMinY) / pageSize[1];
			if (legend) {
				printRect.style.width = ((0.165 * printScaleValue) / printFactorX) + 'px';
			} else {
				printRect.style.width = ((0.238 * printScaleValue) / printFactorX) + 'px';
			}
			printRect.style.height = ((0.135 * printScaleValue) / printFactorY) + 'px';
			if (top != "" && left != "") {
				printRect.style.left = left;
				printRect.style.top = top;
			} else {
				printRect.style.left = (pageSize[0] / 2) - (parseInt(printRect.style.width) / 2);
				printRect.style.top = (pageSize[1] / 2) - (parseInt(printRect.style.height) / 2);
			}

			printRect.style.visibility = 'visible';
		} else if (printFormat == "A3" && printOrientation == 'portrait') {
			printFactorX = (mbrMaxX - mbrMinX) / pageSize[0];
			printFactorY = (mbrMaxY - mbrMinY) / pageSize[1];
			printRect.style.width = ((0.257 * printScaleValue) / printFactorX) + 'px';
			if (legend) {
				printRect.style.height = ((0.185 * printScaleValue) / printFactorY) + 'px';
			} else {
				printRect.style.height = ((0.250 * printScaleValue) / printFactorY) + 'px';
			}
			if (top != "" && left != "") {
				printRect.style.left = left;
				printRect.style.top = top;
			} else {
				printRect.style.left = (pageSize[0] / 2) - (parseInt(printRect.style.width) / 2);
				printRect.style.top = (pageSize[1] / 2) - (parseInt(printRect.style.height) / 2);
			}
			printRect.style.visibility = 'visible';

		} else if (printFormat == "A3" && printOrientation == 'landscape') {
			printFactorX = (mbrMaxX - mbrMinX) / pageSize[0];
			printFactorY = (mbrMaxY - mbrMinY) / pageSize[1];
			if (legend) {
				printRect.style.width = ((0.240 * printScaleValue) / printFactorX) + 'px';
			} else {
				printRect.style.width = ((0.316 * printScaleValue) / printFactorX) + 'px';
			}
			printRect.style.height = ((0.226 * printScaleValue) / printFactorY) + 'px';
			if (top != "" && left != "") {
				printRect.style.left = left;
				printRect.style.top = top;
			} else {
				printRect.style.left = (pageSize[0] / 2) - (parseInt(printRect.style.width) / 2);
				printRect.style.top = (pageSize[1] / 2) - (parseInt(printRect.style.height) / 2);
			}

			printRect.style.visibility = 'visible';
		}

		printRect.onmousedown = startPrintDrag;
		// printRect.onmouseup = stopPrintDrag;
		document.getElementById("printhiddenlayer").onmouseup = stopPrintDrag;
	} catch (err) {
	}
}

// when changing print scale, format or orientation, redraw the print rectangle
function resizePrintRect() {
	var printRect = document.getElementById('printrectlayer');
	var top = document.getElementById('printrectlayer').style.offsetTop;
	var left = document.getElementById('printrectlayer').style.offsetLeft;
	var legend = document.getElementById('printlegendcheck').checked;

	try {
		printScaleValue = parseInt(document.getElementById('printscale').value);

		printFormat = document.getElementById('formato').value;
		printOrientation = document.getElementById('orientamento').value;
		var mapScale = parseInt(document.getElementById('scaletext').value);

		var pageSize = getPageSize();

		if (printScaleValue >= mapScale) {
			document.getElementById("printscale").style.backgroundColor = '#FF0033'; // red
			if (_isKHTML) {
				document.getElementById("printscale").style.color = '#FF0033'; // red
			}
			return;
		} else {
			document.getElementById("printscale").style.backgroundColor = 'white'; // default color
			if (_isKHTML) {
				document.getElementById("printscale").style.color = 'black'; // default color
			}
		}

		if (printFormat == 'A4' && printOrientation == 'portrait') {
			printFactorX = (mbrMaxX - mbrMinX) / pageSize[0];
			printFactorY = (mbrMaxY - mbrMinY) / pageSize[1];
			printRect.style.width = ((0.174 * printScaleValue) / printFactorX) + 'px';
			if (legend) {
				printRect.style.height = ((0.120 * printScaleValue) / printFactorY) + 'px';
			} else {
				printRect.style.height = ((0.220 * printScaleValue) / printFactorY) + 'px';
			}

			printRect.style.left = left;// (pageSize[0] / 2) - (parseInt(printRect.style.width) / 2);
			printRect.style.top = top;// (pageSize[1] / 2) - (parseInt(printRect.style.height) / 2);

			printRect.style.visibility = 'visible';
		} else if (printFormat == "A4" && printOrientation == 'landscape') {
			printFactorX = (mbrMaxX - mbrMinX) / pageSize[0];
			printFactorY = (mbrMaxY - mbrMinY) / pageSize[1];
			if (legend) {
				printRect.style.width = ((0.165 * printScaleValue) / printFactorX) + 'px';
			} else {
				printRect.style.width = ((0.258 * printScaleValue) / printFactorX) + 'px';
			}
			printRect.style.height = ((0.135 * printScaleValue) / printFactorY) + 'px';

			printRect.style.left = left;// (pageSize[0] / 2)- (parseInt(printRect.style.width) / 2);
			printRect.style.top = top;// (pageSize[1] / 2)- (parseInt(printRect.style.height) / 2);

			printRect.style.visibility = 'visible';
		} else if (printFormat == "A3" && printOrientation == 'portrait') {
			printFactorX = (mbrMaxX - mbrMinX) / pageSize[0];
			printFactorY = (mbrMaxY - mbrMinY) / pageSize[1];
			printRect.style.width = ((0.257 * printScaleValue) / printFactorX) + 'px';
			if (legend) {
				printRect.style.height = ((0.185 * printScaleValue) / printFactorY) + 'px';
			} else {
				printRect.style.height = ((0.250 * printScaleValue) / printFactorY) + 'px';
			}

			printRect.style.left = left;// (pageSize[0] / 2)- (parseInt(printRect.style.width) / 2);
			printRect.style.top = top;// (pageSize[1] / 2)- (parseInt(printRect.style.height) / 2);

			printRect.style.visibility = 'visible';
		} else if (printFormat == "A3" && printOrientation == 'landscape') {
			printFactorX = (mbrMaxX - mbrMinX) / pageSize[0];
			printFactorY = (mbrMaxY - mbrMinY) / pageSize[1];
			if (legend) {
				printRect.style.width = ((0.240 * printScaleValue) / printFactorX) + 'px';
			} else {
				printRect.style.width = ((0.38 * printScaleValue) / printFactorX) + 'px';
			}
			printRect.style.height = ((0.226 * printScaleValue) / printFactorY) + 'px';

			printRect.style.left = left;// (pageSize[0] / 2)- (parseInt(printRect.style.width) / 2);
			printRect.style.top = left;// (pageSize[1] / 2)- (parseInt(printRect.style.height) / 2);

			printRect.style.visibility = 'visible';
		}
	} catch (err) {
	}
}

// start print drag
function startPrintDrag(eventObject) {

	var printRect = document.getElementById('printrectlayer');
	var hiddendiv = document.getElementById("printhiddenlayer");
	var xyArr = getPageSize();

	if (!eventObject) eventObject = window.event;

	if (_isIE || _isOpera) {
		if (_isIE) {
			printDragX1 = eventObject.clientX - 2;
			printDragY1 = eventObject.clientY - 4;
		} else {
			printDragX1 = eventObject.clientX;
			printDragY1 = eventObject.clientY - 2;
		}
	} else {
		printDragX1 = eventObject.clientX;
		printDragY1 = eventObject.clientY;
	}

	oldPrintTop = parseInt(printRect.style.top);
	oldPrintLeft = parseInt(printRect.style.left);

	hiddendiv.style.position = 'absolute'; // da controllare
	hiddendiv.style.visibility = 'visible';
	hiddendiv.style.left = 0;
	hiddendiv.style.top = 0;
	hiddendiv.style.width = xyArr[0] + 'px';
	hiddendiv.style.height = xyArr[1] + 'px';
	hiddendiv.onmousemove = movePrintDrag;
	// printRect.onmousemove = movePrintDrag;
}

// move print drag
function movePrintDrag(eventObject) {

	var printRect = document.getElementById('printrectlayer');
	var printRectWidth = parseInt(printRect.style.width);
	var printRectHeight = parseInt(printRect.style.height);
	var pageSize = getPageSize();

	if (!eventObject) eventObject = window.event;

	if (_isIE || _isOpera) {
		if (_isIE) {
			printDragX2 = eventObject.clientX - 2;
			printDragY2 = eventObject.clientY - 4;
		} else {
			printDragX2 = eventObject.clientX;
			printDragY2 = eventObject.clientY - 2;
		}
	} else {
		printDragX2 = eventObject.clientX;
		printDragY2 = eventObject.clientY;
	}

	if (((oldPrintTop + (printDragY2 - printDragY1)) >= 0) && ((oldPrintLeft + (printDragX2 - printDragX1)) >= 0)) {
		if ((((oldPrintTop + (printDragY2 - printDragY1)) + printRectHeight) <= pageSize[1]) && (((oldPrintLeft + (printDragX2 - printDragX1)) + printRectWidth) <= pageSize[0])) {
			printRect.style.top = (oldPrintTop + (printDragY2 - printDragY1)) + 'px';
			printRect.style.left = (oldPrintLeft + (printDragX2 - printDragX1)) + 'px';
		}
	}
}

// stop print drag
function stopPrintDrag(eventObject) {
	var printDragX3, printDragY3;
	var printRect = document.getElementById('printrectlayer');
	var hiddendiv = document.getElementById("printhiddenlayer");

	printDragX3 = parseInt(printRect.style.left) + (parseInt(printRect.style.width) / 2);
	printDragY3 = parseInt(printRect.style.top) + (parseInt(printRect.style.height) / 2);

	hiddendiv.style.position = 'absolute'; // da controllare
	hiddendiv.style.visibility = 'hidden';
	hiddendiv.style.left = 0;
	hiddendiv.style.top = 0;
	hiddendiv.onmousemove = null;

	printRect.onmousemove = null;
}
