
(function($) {
    var printAreaCount = 0;

    $.fn.printArea = function() {
    	var ele = $(this);
        var idPrefix = "printArea_";
        removePrintArea(idPrefix + printAreaCount);
        printAreaCount++;

        var iframeId = idPrefix + printAreaCount;
        var iframeStyle = 'position:absolute;width:0;height:0;left:100px;top:100px;';

        iframe = document.createElement('IFRAME');

        $(iframe).attr({ style : iframeStyle,
        	id: iframeId
        }).attr("name", iframeId);

        document.body.appendChild(iframe);

        var doc = iframe.contentWindow.document;

		doc.write('<html>');
		doc.write('<head>');
        $(document).find("link")
            .filter(function(){
            	return $(this).attr("rel").toLowerCase() == "stylesheet";
            })
            .each(function(){
            	doc.write('<link type="text/css" rel="stylesheet" href="' + $(this).attr("href") + '" media="screen">');
            });
		doc.write('</head>');
		doc.write('<body>');
        doc.write($(ele).html());
		doc.write('</body>');
		doc.write('</html>');
        doc.close();
        
        var frameWindow = iframe.contentWindow;
		$("iframe#" + iframeId).focus();
		frameWindow.print();
		frameWindow.close();
    }

    var removePrintArea = function(id) {
    	$( "iframe#" + id ).remove();
    };

})(jQuery);


