/// <reference path="http://code.jquery.com/jquery-1.4.1-vsdoc.js" />
/*
 * Print Element Plugin 1.2
 *
 * Copyright (c) 2010 Erik Zaadi
 *
 * Inspired by PrintArea (http://plugins.jquery.com/project/PrintArea) and
 * http://stackoverflow.com/questions/472951/how-do-i-print-an-iframe-from-javascript-in-safari-chrome
 *
 *  Home Page : http://projects.erikzaadi/jQueryPlugins/jQuery.printElement
 *  Issues (bug reporting) : http://github.com/erikzaadi/jQueryPlugins/issues/labels/printElement
 *  jQuery plugin page : http://plugins.jquery.com/project/printElement
 *
 *  Thanks to David B (http://github.com/ungenio) and icgJohn (http://www.blogger.com/profile/11881116857076484100)
 *  For their great contributions!
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 *   Note, Iframe Printing is not supported in Opera and Chrome 3.0, a popup window will be shown instead
 * 
 * Modification: MW, added fixes to IE, 
 * issue re when print button clicked, the content in the background parent window was printed instead of
 * in the popup window.
 */
;
(function (g) {
    function k(c) {
        c && c.printPage ? c.printPage() : setTimeout(function () {
            k(c)
        }, 50)
    }

    function l(c) {
        c = a(c);
        a(":checked", c).each(function () {
            this.setAttribute("checked", "checked")
        });
        a("input[type='text']", c).each(function () {
            this.setAttribute("value", a(this).val())
        });
        a("select", c).each(function () {
            var b = a(this);
            a("option", b).each(function () {
                b.val() == a(this).val() && this.setAttribute("selected", "selected")
            })
        });
        a("textarea", c).each(function () {
            var b = a(this).attr("value");
            if (a.browser.b && this.firstChild) this.firstChild.textContent =
                b;
            else this.innerHTML = b
        });
        return a("<div></div>").append(c.clone()).html()
    }

    function m(c, b) {
        var i = a(c);
        c = l(c);
        var d = [];
        d.push("<html><head><title>" + b.pageTitle + "</title>");
        if (b.overrideElementCSS) {
            if (b.overrideElementCSS.length > 0)
                for (var f = 0; f < b.overrideElementCSS.length; f++) {
                    var e = b.overrideElementCSS[f];
                    typeof e == "string" ? d.push('<link type="text/css" rel="stylesheet" href="' + e + '" >') : d.push('<link type="text/css" rel="stylesheet" href="' + e.href + '" media="' + e.media + '" >')
                }
        } else a("link", j).filter(function () {
            return a(this).attr("rel").toLowerCase() ==
                "stylesheet"
        }).each(function () {
            d.push('<link type="text/css" rel="stylesheet" href="' + a(this).attr("href") + '" media="' + a(this).attr("media") + '" >')
        });
        d.push('<base href="' + (g.location.protocol + "//" + g.location.hostname + (g.location.port ? ":" + g.location.port : "") + g.location.pathname) + '" />');
        d.push('</head><body style="' + b.printBodyOptions.styleToAdd + '" class="' + b.printBodyOptions.classNameToAdd + '">');
        d.push('<div class="' + i.attr("class") + '">' + c + "</div>");
        d.push('<script type="text/javascript">function printPage(){focus();print();' +
            (!a.browser.opera && !b.leaveOpen && b.printMode.toLowerCase() == "popup" ? "close();" : "") + "}<\/script>");
        d.push("</body></html>");
        return d.join("")
    }
    var j = g.document,
        a = g.jQuery;
    a.fn.printElement = function (c) {
        var b = a.extend({}, a.fn.printElement.defaults, c);
		//fixes for IE popup window print - bring the content to the popup.
		//Issues: only printed content of background parent window.
				
		var isIE = /*@cc_on!@*/!1;  // [Boolean]
		 		 
		 if (b.printMode == "iframe") {
            //if (IE7lower == 'true' || IE8 == 'true') {
			if (isIE){
			   b.printMode = "popup";
			  }
		 }
		//-- End of modification--
		
        if (b.printMode == "iframe")
            if (a.browser.opera || /chrome/.test(navigator.userAgent.toLowerCase())) b.printMode = "popup";
			
        a("[id^='printElement_']").remove();
        return this.each(function () {
            var i = a.a ? a.extend({}, b, a(this).data()) : b,
                d = a(this);
            d = m(d, i);
            var f = null,
                e = null;
            if (i.printMode.toLowerCase() == "popup") {
                f = g.open("about:blank", "printElementWindow", "width=650,height=440,scrollbars=yes");
                e = f.document
            } else {
                f = "printElement_" + Math.round(Math.random() * 99999).toString();
                var h = j.createElement("IFRAME");
                a(h).attr({
                    style: i.iframeElementOptions.styleToAdd,
                    id: f,
                    className: i.iframeElementOptions.classNameToAdd,
                    frameBorder: 0,
                    scrolling: "no",
                    src: "about:blank"
                });
                j.body.appendChild(h);
                e = h.contentWindow || h.contentDocument;
                if (e.document) e = e.document;
                h = j.frames ? j.frames[f] : j.getElementById(f);
                f = h.contentWindow || h
            }
            focus();
            e.open();
            e.write(d);
            e.close();
            k(f)
        })
    };
    a.fn.printElement.defaults = {
        printMode: "iframe",
        pageTitle: "",
        overrideElementCSS: null,
        printBodyOptions: {
            styleToAdd: "padding:10px;margin:10px;",
            classNameToAdd: ""
        },
        leaveOpen: false,
        iframeElementOptions: {
            styleToAdd: "border:none;position:absolute;width:0px;height:0px;bottom:0px;left:0px;",
            classNameToAdd: ""
        }
    };
    a.fn.printElement.cssElement = {
        href: "",
        media: ""
    }
})(window);