﻿/*
 * printHelper - simple jQuery plugin for contact forms
 * Version: 1.0 (04/14/2011)
 * Copyright (c) 2011 David Dierking
 * Requires: jQuery v1.3+
*/

/*
 * Installation Instructions
 * Install the following on your form page to initialize the plugin
    $(document).ready(function(){
        $(".printTrigger").printHelper();
    });
*/
var printInt = 0;
(function ($) {
    $.fn.printHelper = function(options)
    {
        this
        .click(function()
        {
            $this = $(this);
            $href = $this.attr("href");
            printInt++;
            $printHelperDiv = $(".printHelperDiv");
            if($printHelperDiv.length > 0){
                $printHelperDiv.remove();
            }
            
            //$("body").append('<div id="printHelperDiv" style="height: 0px; overflow: hidden;"><iframe id="printFrame" name="printFrame" src="' + $href + '"></iframe></div>');
            $("body").append('<div id="printHelperDiv' + printInt.toString() + '" class="printHelperDiv" style="height: 0px; overflow: hidden;"><iframe id="printFrame' + printInt.toString() + '" name="printFrame' + printInt.toString() + '" src=""></iframe></div>');
            $frame = $("#printFrame" + printInt.toString());
            $frame.unbind().load( 
                function() {
                    window.frames['printFrame' + printInt.toString()].focus();
                    window.frames['printFrame' + printInt.toString()].print();
                }
             );
             $frame.attr("src", $href);
            
            return false; 
        });
    }
})(jQuery);
