// print.js

// :: FUNCTIONS
// strip(filestr) - (NB: Same as function in funcs.js)
// printReady(filename,title,width)

// STRIPS OUT '/' AND '.' AND RETURNS FLAT STRING
// USED TO GENERATE WINDOW NAME FROM FILENAME
// NB: SAME AS IN funcs.js
function strip(filestr) {
  var invalid = '/._?&=';
  var flatstr = '';
  var j, qpos;
	
	// Strip everything past '?' (if exists)
	qpos = filestr.indexOf('?');
	if (qpos > -1) {
		filestr = filestr.substring(0,qpos);
	}

  // COPY VALID CHARS AND IGNORE INVALID ONES
  for (j=0; j<filestr.length; j++) {
    flatstr += (invalid.indexOf(filestr.charAt(j)) > -1) ? '' : filestr.charAt(j);
  }

  return flatstr;
}

// POPUP NEW WINDOW WITH IMAGE READY FOR PRINTING
// NB: USES SEPARATE STYLESHEET,
//     SEE WEBSITE SECURE SITE FOR ONE THAT DOESN'T
// title - HTML PAGE TITLE (OPTIONAL)
// width - WIDTH OF IMAGE IN cm (OPTIONAL) - IF NOT SPECIFIED THEN USES FULL WIDTH
// CALL: <A HREF="image.jpg" onClick="return printReady('image.jpg','Title');">
function printReady(filename,title,width) {
  var x = '';

  // TITLE SPECIFIED?
  if (title < ' ') {
    title = filename;
  }

  // APPEND 'cm' OR USE 100%
  if (width > 0) {
    width += 'cm';
  } else {
    width = '100%'; /* 92%, 19.2cm MAX */
  }

  // HEADER
	x += '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
  x += '<HTML><HEAD><TITLE>Print - ' + title + '</TITLE>\n';
  x += '<META HTTP-EQUIV="imagetoolbar" CONTENT="no" />\n';
	x += '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />';
	x += '<script language="JavaScript" type="text/javascript" src="/funcs.js"></script>';

  // STYLESHEET
  x += '<LINK REL="stylesheet" type="text/css" href="/style.css" title="Normal" />';
  x += '<STYLE TYPE="text/css">\n';
  x += 'body {margin:0;}\n';
  x += '#printimg {width:' + width + ';}\n';
  x += '#infobar {position:fixed;left:10px;top:10px;width:660px;}\n';
  x += '</STYLE>\n';
  x += '</HEAD>\n';

  // BODY
  x += '<BODY STYLE="background-color:white;">';

	x += '<!--[if IE]>\n';
  x += '<style type="text/css">\n';	
	x += '#infobar {position:absolute;left:10px;top:10px;width:660px;}\n';
  x += '</style>\n';	
	x += '<![endif]-->\n';
	
  // TITLE and CLOSE/PRINT BUTTONS
  x += '<DIV ID="infobar" CLASS="edge noprint"><DIV CLASS="banner">';

  x += '<DIV STYLE="float:left;margin-right:10px;">';
  x += '<INPUT STYLE="width:60px;margin-right:2px;font-weight:bold;" TYPE="button" VALUE="Print" onClick="javascript:window.print();">';
  x += '<INPUT STYLE="width:60px;" TYPE="button" VALUE="Close" onClick="javascript:window.close();">';
  x += '</DIV>';

  x += '<DIV STYLE="float:right;">';
  x += '<BUTTON ID="movebtn" STYLE="width:80px;background-color:#9999CC;" CLASS="smallfont" onClick="javascript:moveObj(\'infobar\');" TITLE="Move this banner out the way!">';
  x += 'Move&nbsp;this<BR>out&nbsp;the&nbsp;way!';
  x += '</BUTTON>';
  x += '</DIV>';

  x += '<SPAN CLASS="bigfont">' + title + '</SPAN><BR>';
  x += '<SPAN CLASS="smallfont">(Designed to print directly from here to A4 paper)</SPAN>';
  x += '</DIV></DIV>';

  if (width != '100%') {
    x += '<TABLE WIDTH="100%" HEIGHT="100%" CELLPADDING=0 CELLSPACING=0 BORDER=0>\n';
    x += '<TR><TD ALIGN="center" VALIGN="middle">\n';
  }
  x += '<IMG ID="printimg" SRC="' + filename + '" ALT="' + title + '" BORDER=0>\n';
  if (width != '100%') {
    x += '</TD></TR></TABLE>\n';
  }

  // FOOTER
  x += '</BODY></HTML>';

  // WINDOW NAME OF strip(filename) ENSURES THAT EACH IMG HAS THEIR OWN WINDOW
  popup = window.open('about:blank',strip(filename),'resizable=yes,scrollbars=yes,width=700,height=460');

  popup.document.open();
  popup.document.write(x);
  popup.document.title = 'Print - ' + title;	// This overrides title-tag
  popup.document.close();

  popup.focus();

  // SO THAT LINK IS NOT FOLLOWED
  return false;
}

