// sjcPrint.js
// Copyright (C) 2005 San Joaquin County Community Development Geographic Information Systems, ALL RIGHTS RESERVED
// initial creation: 01/14/2004 David Bollinger
//

var printTitle = '';
var printMapURL="";
var printOVURL="";
var printLegURL="";
var printPageSize = "A";         // A,B,C,D : this is set by the input form and used to calc others
var printOrientation = "P";      // L,P     : this is set by the input form and used to calc others
var printResolution = "Lo";      // Lo,Hi   : this is set by the input form and used to calc others
var printWantText = false;       // true,false : this is set by the input form
var printResolutionMultiplier;
var printPageWidth = 650;
var printPageHeight = 450;
var printMapWidth;
var printMapHeight;
var printLogoWidth;
var printLogoHeight;
var printOverviewWidth;
var printOverviewHeight;
var printTitleWidth;
var printTitleHeight;
var printLegendWidth;
var printLegendHeight;
var printAddressWidth;
var printAddressHeight;
var printTextWidth;

function printConfigurePage(ttl,pgsz,ori,res,txt) {
	printTitle = ttl;
	printPageSize = pgsz;
	printOrientation = ori;
	printResolution = res;
	printWantText = txt;
	printCalcDimensions();
}

function printCalcDimensions() {
  switch (printPageSize) {
    case "D" :
      printPageWidth = 1900;
      printPageHeight = 3000;
      break;
    case "C" :
      printPageWidth = 1400;
      printPageHeight = 1900;
      break;
    case "B" :
      printPageWidth = 900;
      printPageHeight = 1400;
      break;
    default : // "A" 
      printPageWidth = 700;
      printPageHeight = 900;
      break;
  }
  if (printOrientation == "L") {
    var temp = printPageWidth;
    printPageWidth = printPageHeight;
    printPageHeight = temp;
  }

  printPageHeight -= 50;  // ENOUGH ROOM SO THAT IE CAN DISPLAY HEADER/FOOTER WITHOUT PAGE FEED

  printResolutionMultiplier = 1.5;
  if (printResolution == "Hi") {
    printResolutionMultiplier = 2.5;
  }
  var bottomHeight = parseInt(printPageHeight * 0.1);

  printMapWidth = printPageWidth - bottomHeight;
  printMapHeight = parseInt(printPageHeight * 0.9);

  printLogoWidth = bottomHeight;
  printLogoHeight = bottomHeight;

  printOverviewWidth = bottomHeight;
  printOverviewHeight = bottomHeight;

  printLegendWidth = printOverviewWidth;
  printLegendHeight = printMapHeight;

  printTitleWidth = printPageWidth - bottomHeight * 2;
  printTitleHeight = parseInt(bottomHeight * 0.4);

  printAddressWidth = printTitleWidth;
  printAddressHeight = parseInt(bottomHeight * 0.6);

  if (printWantText) {
    printTextWidth = 140;
    printMapWidth -= printTextWidth;
    printTitleWidth -= printTextWidth;
    printAddressWidth -= printTextWidth;
  }
}

function requestPrintMap() {
	showRetrieveMap();
	var tempWidth = iWidth;
	var tempHeight = iHeight;
	iWidth = parseInt(printMapWidth * printResolutionMultiplier);
	iHeight= parseInt(printMapHeight * printResolutionMultiplier);
	legend.whitebkgnd();
	var sXML = map.asXML();
	legend.colorbkgnd();
	iWidth=tempWidth;
	iHeight = tempHeight;
	com.send(imsURL,sXML,receivePrintMap);
}

function receivePrintMap(sXML) {
	printMapURL = getTagAttValue(sXML, 'OUTPUT', 'url', '');
	printLegURL = getTagAttValue(sXML, 'LEGEND', 'url', '');
	requestPrintOV();
}

function requestPrintOV() {
	var tempWidth = i2Width;
	var tempHeight = i2Height;
	i2Width = printOverviewWidth * printResolutionMultiplier;
	i2Height = printOverviewHeight * printResolutionMultiplier;
	var sXML = ovmap.asXML(true);
	i2Width=tempWidth;
	i2Height = tempHeight;
	com.send(imsOVURL,sXML,receivePrintOV);
}

function receivePrintOV(sXML) {
	printOVURL = getTagAttValue(sXML, 'OUTPUT', 'url', '');
	displayPrintPage();
}

function displayPrintPage() {
	var Win1 = open("","PrintPage");
  var doc = Win1.document;

  doc.open();

  doc.writeln('<html><head>');
  doc.writeln('	<title>Map Output</title>');
  doc.writeln(' <LINK rel="stylesheet" type="text/css" href="' + appDir + 'style.css" />');

  doc.writeln('</head>');
  doc.writeln('<body class="print">');
  doc.writeln('<FONT FACE="Arial"><B>');
  doc.writeln('<TABLE WIDTH="' + printPageWidth + '" BORDER="2" CELLSPACING="0" CELLPADDING="0" NOWRAP>');

  doc.writeln('<TR>');

  // THE MAP
  doc.writeln('  <TD WIDTH="' + printMapWidth + '" HEIGHT="' + printMapHeight + '" COLSPAN="2" ALIGN="CENTER" VALIGN="TOP">');
  doc.writeln('    <IMG SRC="' + printMapURL + '" WIDTH=' + printMapWidth + ' HEIGHT=' + printMapHeight + ' HSPACE=0 VSPACE=0 BORDER=0 ALT="">');
  doc.writeln('  </TD>');

  // THE LEGEND
  doc.writeln('  <TD WIDTH="' + printLegendWidth + '" HEIGHT="' + printLegendHeight + '" ALIGN="CENTER" VALIGN="TOP">');
	var printAspect = printLegendWidth / printLegendHeight;
	var imageAspect = legend.width / legend.height;
  doc.writeln('    <IMG SRC="' + printLegURL + '" WIDTH=' + printLegendWidth + ' HSPACE=0 VSPACE=0 BORDER=0 ALT="">');
  doc.writeln('  </TD>');

  // THE TEXT AREA
  if (printWantText) {
    doc.writeln('    <TD WIDTH="' + printTextWidth + '" HEIGHT="' + printPageHeight + '" ROWSPAN="3" VALIGN="TOP">');
    doc.writeln('    <DIV class="print" id="divDetails">');
    doc.writeln( lastDetailsHTML );
    doc.writeln('    </DIV></TD>');
  }

  doc.writeln('</TR>');

  doc.writeln('<TR>');

  // THE LOGO
  doc.writeln('  <TD WIDTH=' + printLogoWidth + ' HEIGHT=' + printLogoHeight + ' ALIGN="center" VALIGN="center" ROWSPAN=2>');
  doc.writeln('    <IMG SRC="http://sjmap.org/website/common/images/sjclogo.jpg" WIDTH=' + printLogoWidth + ' HEIGHT=' + printLogoHeight + '>');
  doc.writeln('  </TD>');

  // THE TITLE
  doc.writeln('  <TD WIDTH=' + printTitleWidth + ' HEIGHT=' + printTitleHeight + ' ALIGN="center">');
  doc.writeln('    <P STYLE="{font-family: Tahoma; font-size: 7pt; margin: 0;}">');
  doc.writeln('    <B>' + printTitle + '</B>');
  doc.writeln('    </P>');
  doc.writeln('  </TD>');

  // THE OVERVIEW
  doc.writeln('  <TD WIDTH=' + printOverviewWidth + ' HEIGHT=' + printOverviewHeight + ' ALIGN="center" VALIGN="center" ROWSPAN=2>');
  doc.writeln('    <IMG SRC="' + printOVURL + '" WIDTH=' + printOverviewWidth + ' HEIGHT=' + printOverviewHeight + '>');
  doc.writeln('  </TD>');

  doc.writeln('</TR>');

  // THE ADDRESS & DISCLAIMER
  doc.writeln('<TR>');

  doc.writeln('  <TD WIDTH=' + printAddressWidth + ' HEIGHT=' + printAddressHeight + ' ALIGN="center">');
  doc.writeln('    <P STYLE="{font-family: Tahoma; font-size: 6pt; margin: 0;}">');
  doc.writeln('      <B>San Joaquin County Geographic Information Systems</B>');
  doc.writeln('    </P>');
  doc.writeln('    <P STYLE="{font-family: Tahoma; font-size: 5pt; margin: 0;}">');
  doc.writeln('      1810 East Hazelton Avenue, Stockton, California  95205');
  doc.writeln('    </P>');
  doc.writeln('    <BR STYLE="{font-family: Tahoma; font-size: 4pt; margin: 0;}">');
  doc.writeln('    <P STYLE="{font-family: Tahoma; font-size: 3pt; margin: 0;}">');
  doc.writeln('      The information on this map is based on the most current information available to San Joaquin County Geographic Information Systems.<br>');
  doc.writeln('      The County of San Joaquin does not warrant its accuracy, completeness, or suitability for any particular purpose.<br>');
  doc.writeln('      The information on this map is not intended to replace engineering, financial or primary records research.');
  doc.writeln('    </P>');
  doc.writeln('    <IMG ALIGN="right" VALIGN="bottom" SRC="/website/common/images/dbSignature.gif" WIDTH=10 HEIGHT=5>');
  doc.writeln('  </TD>');
  doc.writeln('</TR>');

	doc.writeln('</TABLE>');
	doc.writeln('</B></FONT>');
	doc.close();
	
	Win1=null;
	hideRetrieveMap();
}