/**
 *  Version 1.2
 *      -Contributors: "mindinquiring" : filter to exclude any stylesheet other than print.
 *
 *  This is a major overhaul of the previous version.
 *  Easy to edit or alter as you please. Tested ONLY in IE 8 and FF 3.5.2. No support for other browsers.
 */
(function($) {
    var counter = 0;
    $.fn.printArea = function()
        {
            counter++;
            var idPrefix = "printArea_";
            $( "iframe[id^=" + idPrefix + "]" ).remove();
            var iframeId = idPrefix + counter;
            var ele = $(this);

            var f = new Iframe( iframeId );
            f.doc.open();
            f.doc.write("<html>"/* + getHead()*/ + getBody(ele) + "</html>" );
            f.doc.close();

            var frameWin = f.contentWindow || f;
            frameWin.focus();
            frameWin.print();
        }

    function getHead()
    {
        var head = "<head>";
        $(document).find("link")
            .filter(function(){
                    return $(this).attr("rel").toLowerCase() == "stylesheet";
                })
            .filter(function(){ // this filter contributed by "mindinquiring"
                    var media = $(this).attr("media");
                    return (media.toLowerCase() == "" || media.toLowerCase() == "print")
                })
            .each(function(){
                    head += '<link type="text/css" rel="stylesheet" href="' + $(this).attr("href") + '" >';
                });
        head += "</head>";
        return head;
    }

    function getBody( printElement )
    {
        var body = "<body>";
        //body += '<div class="' + $(printElement).attr("class") + '">' + $(printElement).html() + '</div>';
		body += $(printElement).html();
        body += "</body>";
        return body;
    }

    function Iframe( frameId )
    {
        var iframeStyle = 'border:0;position:absolute;width:0px;height:0px;left:0px;top:0px;';
        var iframe;

        try
        {
            iframe = document.createElement('iframe');
            document.body.appendChild(iframe);
            $(iframe).attr({ style: iframeStyle, id: frameId, src: "" });
            iframe.doc = null;
            iframe.doc = iframe.contentDocument ? iframe.contentDocument : ( iframe.contentWindow ? iframe.contentWindow.document : iframe.document);
        }
        catch( e ) { throw e + ". iframes may not be supported in this browser."; }

        if ( iframe.doc == null ) throw "Cannot find document.";

        return iframe;
    }
})(jQuery);