//This class creates a plugin for jquery which takes all the selected
//elements, combining them into one output blob, and print them together.
var jQuery_fn_print_holder = null;
jQuery.fn.print = function(head,bodyPre,bodyPost){
//No events fire from the iframe when print is completed, therefore, to keep the print functionality from having multiple iframes,
//a tracking variable is assigned the last frame we created. This frame is automatically removed upon the next print request.
if (jQuery_fn_print_holder!=null)
{
	//remove the last frame which was used from printing.
	jQuery_fn_print_holder.remove();
	jQuery_fn_print_holder = null;
}
// Array objects are faster than appending strings in serial. All content will be appended into the Array object
// first, then pushed into the target printable frame.
var pArray = new Array();

// Begin assembling the output which will be presented in the iframe.
pArray.push("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
pArray.push("<html>");
pArray.push("<head>");
// Grab a copy of all the styles in the current page
// followed by a copy of all the content to be rendered into an Array object
// A copy of the style tags will be necessary to make sure the look and feel of the parent page match that of the 
// target frame. Keep note, however, that cascading logic will fail to apply why the dom element is no longer in the same
// parent.
var listItems = document.getElementsByTagName('head');
for (var i = 0; i < listItems.length; i++) {   
	//SCRIPTS will throw exceptions, rendering should not execute again because the HTML should be up to date. Disable script tags.
    pArray.push(listItems[i].innerHTML.replace(/<script/ig,'<!--script').replace(/\/script>/ig,'/script-->'));
}  
//Add the additional Head html requested by the print function
if (typeof head!='undefined' && head!=null)
{
pArray.push(head);
}
pArray.push("</head>");
pArray.push("<body>" );
//Add anything desired as the pre-content of the body
if (typeof bodyPre!='undefined' && bodyPre!=null)
{
pArray.push(bodyPre);
}
//Next, grab a copy of each element that is going to be printed.
for (var i = 0; i<this.size();i++)
{
	//SCRIPTS will throw exceptions, rendering should not execute again because the HTML should be up to date. Disable script tags.
	pArray.push(this.eq(i).html().replace(/<script/ig,'<!--script').replace(/\/script>/ig,'/script-->'));
}

//Add anything desired as the post-content of the body
if (typeof bodyPost!='undefined' && bodyPost!=null)
{
pArray.push(bodyPost);
}

//Finish writing the html output into the array
pArray.push("</body>" );
pArray.push("</html>" );
 
//Generate the Name of the iFrame object which will be created
//Use a random number between 0 and 10000000 to create a unique frame name
var strName = ("print" + (Math.floor(Math.random()*10000000)+''));

//Two variables are managed for this one - objFrame_jq which will reference the jQuery object for the frame and
// objFrame_dom which references the DOM object for the frame
var objFrame_jq = $( "<iframe name='" + strName + "'>" );

//Make sure the frame is hidden. We must have a width/height set to do this as a hidden frame will not be capable of
//receiving focus (thanks:microsoft ie6).
//Do this by setting the position to absolute, supplying a width and height of 1px, and far out of view!
objFrame_jq.css( "position", "absolute" ).css( "width", "1px" ).css( "height", "1px" ).css( "left", "-9999px" )

//Now once everything is set up, push the frame into your page so that it doesnt have cause any flickering or resizing
objFrame_jq.appendTo( $( "body:first" ) );

//Setup the reference to the frame object (dom)
var objFrame_dom = window.frames[strName];
 
//Setup the reference to the document object contained within the new frame
var objDoc = objFrame_dom.document;

//Write the output of the pArray into the document. Do this by performing a join on the array and passing an empty string as the delimiter
objDoc.open();
objDoc.write(pArray.join(''));

//Print the document contained in our new frame!
objFrame_dom.focus();
try 
{ objDoc.execCommand('print',false,null); }
catch(ex) 
{ objFrame_dom.print(); }

//Clean up!
pArray=null;
jQuery_fn_print_holder = objFrame_jq;

}
