// -----------------------------------------------------------------------
// Eros Fratini - eros@recoding.it
// jqprint 0.3
//
// - 19/06/2009 - some new implementations, added Opera support
// - 11/05/2009 - first sketch
//
// Printing plug-in for jQuery, evolution of jPrintArea: http://plugins.jquery.com/project/jPrintArea
// requires jQuery 1.3.x
//
// Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
//------------------------------------------------------------------------

(function($) {
    var opt;

    $.fn.jqprint = function (options) {
        opt = $.extend({}, $.fn.jqprint.defaults, options);

        var $element = (this instanceof jQuery) ? this : $(this);
        var $ie6 = ($.browser.msie && $.browser.version == "6.0");
        
        if (opt.operaSupport && $.browser.opera) 
        { 
        	var tab = window.open(null,"jp-print-preview","height=520,width=800,status=yes,toolbar=no,menubar=no,location=no");

            if (!$ie6) {
            	tab.document.open();
            }
            var doc = tab.document;
        }
        else 
        {
            var $iframe = $("<iframe frameborder='0' />");
            $iframe.attr("name","printframe")
            var $div = $("<div></div>");
            var $overlay = $("<div></div>");
            var $innerdiv = $("<div></div>");
      

            $div.attr("id","PrintDiv");
            $overlay.attr("id","PrintOverlay");
            $innerdiv.attr("id","InnerDiv");

            $div.css({ position: "fixed", width: "100%", height: "100%", top: "0px", left: "0px", opacity: "0"});
            $div.css("z-index","9001");
            $overlay.css({ position: "fixed",display: "block", width: "100%", height: "100%", top: "0px", left: "0px", background: "#000", opacity: "0"});
            $overlay.css("z-index","1350");
            
            if (!opt.debug) { 
            	$innerdiv.css({ cursor: "pointer", position: "absolute", width: "800px", height: "530px",  overflow: "hidden", margin: "-280px 0 0 -400px", top: "50%", left: "50%" }); 
            	$innerdiv.css("background", "url('pix/closelabel.gif') 715px 505px no-repeat white");
            	$iframe.css({ width: "800px", height: "500px", border: "0px"});
            	$iframe.css("border-bottom","1px solid grey");
            }
            
            if ($ie6) {
            	$("body").css("height","100%");
            	$div.css({ position: "absolute", opacity: "1" });
            	$overlay.css({ position: "absolute", opacity: "1" });
            	$overlay.css("filter","Alpha(opacity=50, finishopacity=50, style=2) ; ");
            	$div.css("z-index","5000");
            }
            
           
            
            $iframe.appendTo($innerdiv);
            $innerdiv.appendTo($div);
            $div.appendTo("body");
            $overlay.appendTo("body");
            
            
            if (!$ie6) {
	            $overlay.fadeTo(500,0.5);
	            $div.fadeTo(500,1);
            }
            else {
            	fixPNG();
            }
            
            var closeFunction = function() {
            	if (!$ie6) {
	            	$("#PrintDiv").fadeTo(500,0,function() { $(this).remove(); });
	            	$("#PrintOverlay").fadeTo(500,0,function() { $(this).remove(); });	
            	}
            	else {
            		$("#PrintDiv").remove();
	            	$("#PrintOverlay").remove();
            	}
            }
            
            $div.click(closeFunction);
            $overlay.click(closeFunction);
            
            var doc = $iframe[0].contentWindow.document;
            
        }
        
        if (opt.importCSS)
        {
            if ($("link[media=print]").length > 0) 
            {
                $("link[media=print]").each( function() {
                    doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' />");
                });
            }
            else 
            {
                $("link").each( function() {
                    doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' />");
                });
                
                
            }
            doc.write("<style>#col3 { padding: 0; margin: 0px; } .col3_bottom {display: none} #logo { display: block; padding: 5px 5px 20px 0px; clear: both; float: none; }</style>");
        }
        
        doc.write($("#Logo").parent().html());
        
        if (opt.printContainer) { doc.write($element.outer()); }
        else { $element.each( function() { doc.write($(this).html()); }); }
        
        doc.write($(".adresse").html());
        
        doc.close();
        
        (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).focus();
        
        closePrintContainer = function() {         	
        	if (tab) {
        		tab.close();
        	}
        	else {
	        	$("#PrintDiv").fadeTo(500,0,function() { $(this).remove(); });
	         	$("#PrintOverlay").fadeTo(500,0,function() { $(this).remove(); });
        	}
         }
        
        setTimeout( function() { 
        	(opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).print();
         }, 1350);
    }
    
    $.fn.jqprint.defaults = {
		debug: false,
		importCSS: true, 
		printContainer: true,
		operaSupport: true
	};

    // Thanks to 9__, found at http://users.livejournal.com/9__/380664.html
    jQuery.fn.outer = function() {
      return $($('<div></div>').html(this.clone())).html();
    } 
})(jQuery);
