// -----------------------------------------------------------------------
// 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=1350,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>");
      
            
           /* var $footerprintmegdiv = $("<div class='meggenfooter' style='font:Arial; font-size:111%; letter-spacing:0.05em; word-spacing:0.7px; color:#003366;'><div style='float:left; width:280px; padding-left:30px;'><img src='pix/tennismeggen.gif'></div><div style='float:left; width:550px; padding-top:10px;'><b>Tennisclub Meggen</b>&nbsp;&nbsp;Sentib&uuml;hlstr. 50&nbsp;&nbsp;6045&nbsp;Meggen LU&nbsp;&nbsp;Telefon 041 377 31 37</div></div>");
            */
            $div.attr("id","PrintDiv");
            $overlay.attr("id","PrintOverlay");
            $innerdiv.attr("id","InnerDiv");
          /*
            $footerprintmegdiv.attr("id","FooterprintmegDiv");
            */
            $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");
           /*
            $footerprintmegdiv.css({ width: "1350px", height: "20px", padding: "3px" });
            $footerprintmegdiv.css("display","block","z-index","93004","position","absolute");
      */
            
            if (!opt.debug) { 
            	$innerdiv.css({ background: "white", position: "absolute", width: "1350px", height: "520px",  overflow: "hidden", margin: "-280px 0 0 -675px", top: "50%", left: "50%" }); 
            	$iframe.css({ width: "1350px", 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);
          /*  $footerprintmegdiv.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") + "' />");
                });
            }
        }
        
        if (opt.printContainer) { doc.write($element.outer()); }
        else { $element.each( function() { doc.write($(this).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);
