/*<--- --------------------------------------------------------------------------------------- ----
	
	Blog Entry:
	Ask Ben: Print Part Of A Web Page With jQuery
	
	Author:
	Ben Nadel / Kinky Solutions
	
	Link:
	http://www.bennadel.com/index.cfm?dax=blog:1591.view
	
	Date Posted:
	May 21, 2009 at 9:10 PM
	
---- --------------------------------------------------------------------------------------- --->*/


// Create a jquery plugin that prints the given element.
jQuery.fn.print = function(){
	// NOTE: We are trimming the jQuery collection down to the
	// first element in the collection.
	if (this.size() > 1){
		this.eq( 0 ).print();
		return;
	} else if (!this.size()){
		return;
	}
 
	// ASSERT: At this point, we know that the current jQuery
	// collection (as defined by THIS), contains only one
	// printable element.
 
	// 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" )
        /*.css( "width", "700px" )
		.css( "height", "400px" )*/
		.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;
 
	// Grab all the style tags and copy to the new
	// document so that we capture look and feel of
	// the current document.
 
	// Create a temp document DIV to hold the style tags.
	// This is the only way I could find to get the style
	// tags into IE.
	var jStyleDiv = $( "<div>" ).append(
		$( "style" ).clone()
		);

    var styles = '<style type="text/css">';
    styles += 'body {font-family:Arial,Helvetica,sans-serif;font-size:11px;}';
    //styles += 'table td {border: 1px;}';
    styles += '</style>';

    var my_date = new Date();
    var mese = my_date.getMonth()+1;
    //alert(mese);
    //alert((mese+'').length);
    if ((mese+'').length==1) mese = '0' + mese;
    var giorno = my_date.getDate();
    if ((''+giorno).length==1) giorno = '0' + giorno;
    var anno = my_date.getYear()+1900;
    var data = giorno + '/' + mese + '/' + anno;

    var body = this.html();
 
	// Write the HTML for the document. In this, we will
	// write out the HTML of the current element.
	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( "<head>" );
	objDoc.write( "<title>" );
	objDoc.write( document.title );
	objDoc.write( "</title>" );
    objDoc.write( styles );
	objDoc.write( jStyleDiv.html() );
	objDoc.write( "</head>" );
    objDoc.write( "<body>" );
    objDoc.write( "<p>" + data + "</p>" );
    var datada = '';
    var adata = '';
    if ($('#artNov_cerca', this).length>0) {
        datada = $('#artNov_cerca input[name=dag]', this).val() +
            "/" + $('#artNov_cerca input[name=dam]', this).val() +
            "/" + $('#artNov_cerca input[name=daa]', this).val();
        adata = $('#artNov_cerca input[name=ag]', this).val() +
            "/" + $('#artNov_cerca input[name=am]', this).val() +
            "/" + $('#artNov_cerca input[name=aa]', this).val();
        objDoc.write( '<p>da data ' + datada + ' a data ' + adata + '</p>');
        objDoc.write( '<p>Categorie: ' + $('#artNov_cerca select[name=fam] :selected', this).text() + '</p>');
    }
    if ($('#artSost_cerca', this).length>0) {
        datada = $('#artSost_cerca input[name=dag]', this).val() +
            "/" + $('#artSost_cerca input[name=dam]', this).val() +
            "/" + $('#artSost_cerca input[name=daa]', this).val();
        adata = $('#artSost_cerca input[name=ag]', this).val() +
            "/" + $('#artSost_cerca input[name=am]', this).val() +
            "/" + $('#artSost_cerca input[name=aa]', this).val();
        objDoc.write( '<p>da data ' + datada + ' a data ' + adata + '</p>');
        objDoc.write( '<p>Categorie: ' + $('#artSost_cerca select[name=fam] :selected', this).text() + '</p>');
    }
    $('script', this).remove();
	objDoc.write( this.html() );
	objDoc.write( "</body>" );
	objDoc.write( "</html>" );
	objDoc.close();

    $('script', objDoc).remove();

    $('img[alt=semaforo]', objDoc).remove();
    $('img[alt=descrizione]', objDoc).remove();
    $('table', objDoc).attr('border', '1')
                      .attr('cellspacing', '0');
    $('.testo_bianco', objDoc).css('font-weight', 'bold')
                              .css('font-size', '12px');
    $('table#mastertab', objDoc).attr('border', '0');
    $('table#mastertab table', objDoc).attr('border', '0');

    // solo per dettart
    $('table#dettArt_applicazioni tr:first', objDoc).remove();
    $('table#dettArt_applicazioni tr', objDoc).each(function() {
        $('td:first', this).remove();
        $('td:last', this).remove();
        $('th:first', this).remove();
        $('th:last', this).remove();
    });

    $('#artNov_cerca', objDoc).remove();
    $('#artSost_cerca', objDoc).remove();
 
	// Print the document.
	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)
		);
}

