// -----------------------------------------------------------------------
// 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;

    $.iFramePrint = function (options) {
        opt = $.extend({}, $.iFramePrint.defaults, options);

       var $ie6 = ($.browser.msie && $.browser.version == "6.0");
        
        if (opt.operaSupport && $.browser.opera) 
        { 
        	var tab = window.open(null,"jp-print-preview","height=500,width=1024,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({ background: "white", position: "absolute", width: "850px", height: "500px",  overflow: "hidden", margin: "-280px 0 0 -475px", top: "50%", left: "50%" }); 
            	$iframe.css({ width: "850px", height: "500px", border: "0px"});
            }
            
            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;
            
        }
        
        doc.write(opt.content);
        
        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);
    }
    
    $.iFramePrint.defaults = {
    	content: null,
		debug: false,
		importCSS: false, 
		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);
