// JQuery plugin that prints overlay image
jQuery.fn.print = function(collection, image1Path, image2Path, description){
 
	// Create a random name for the print frame.
	var strFrameName = ("printer-" + (new Date()).getTime());
 
	// Create an iFrame with the new name.
	var jFrame = $( "<iframe name='" + strFrameName + "'>" );
 
	// Hide the frame (sort of) and attach to the body.
	jFrame.css( "width", "1px" ).css( "height", "1px" ).css( "position", "absolute" ).css( "left", "-9999px" ).appendTo( $( "body:first" ) );
   
  // Get a FRAMES reference to the new frame.
	var objFrame = window.frames[ strFrameName ];
 
	// Get a reference to the DOM in the new frame.
  var objDoc = objFrame.document;
 
  var imagePath = '<p><img src=' + image1Path + ' width="472" height="640"/>';
  
  if (image2Path != undefined)
      imagePath = imagePath + '<img src=' + image2Path + ' width="472" height="640"/>';
  
  imagePath = imagePath + "</p>";  
 
 
	// Write the HTML for the document. 
	objDoc.open();
	objDoc.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
	objDoc.write("<html>");
	objDoc.write("<body>");
	objDoc.write("<head>");
	objDoc.write("<title>");
	objDoc.write(collection);
	objDoc.write("</title>");
	objDoc.write('<link rel="stylesheet" href="css/main.css" type="text/css" />');
	objDoc.write("</head>");	
	objDoc.write('<br><br><br><br><h1>' + collection + '</h1>');
	objDoc.write(imagePath);
	objDoc.write(description);
	objDoc.write("</body>");
	objDoc.write("</html>");
	objDoc.close();
 
  var printed = false;
  var statusOfPrint = 'Processing print job... Please wait.';

  // Check to make sure that style image is completely loaded before printing. 
  objDoc.onreadystatechange = function() {   
    if (objDoc.readyState == 'complete') {
      objFrame.focus();
  	  objFrame.print(); 
  	  printed = true;
  	  statusOfPrint = 'Processing print job... Please wait.';
    } else
      statusOfPrint = 'Loading images... Please wait for the print dialog window.';
  }
 
 
  // Print the style image when objDoc is cached by browser, and user continues
  //    to click on "Print" to print more than one copy of the SAME style image.
  if (!printed & (objDoc.readyState == 'complete')) { 
    objFrame.focus();
  	objFrame.print(); 
  } 
 
 
  // Have the frame remove itself in about a minute so that
	// we don't build up too many of these frames.
	setTimeout(
		function(){
			jFrame.remove();
		},
		(60 * 1000)
		);
		
		return statusOfPrint;
}
