(function($) {
	
	$.fn.printPreview = function() {
		
		// Scope
		var elements = this;
		var args = arguments;
		var c = 0;
		
		return(
			elements.each(function(i) {				
				
				// Default values within scope
				var printLink = $(this);
				var screenSheet = $("link[media='screen']");
				var printSheet = $("link[media='print']").clone();
				printSheet.each(function() {
					$("head").append(this);
					this.media = "screen";
					this.disabled = true;
				});
				var printPreviewMessage = $('<div id="preview-message" style="margin: 10px 10px 20px 10px; padding: 10px; border: solid 1px #666; background-color: #f6f6f6;"><h1>Print preview</h1><a href="#" id="preview-print">Print this page</a> | <a href="#" id="turnoff-print">Return to the normal view</a></div>');
				$("head").append('<style type="text/css" media="print" title="Hide print preview message on print"> #preview-message { display: none !important; } </style>');
				
				// Print Preview
				printLink.click( function() {
					// Switch to print
					$("body").fadeOut("fast", function() {
						screenSheet.each(function() {
							this.disabled = true;
						});
						printSheet.each(function() {
							this.disabled = false;
						});
						$(this).fadeIn("slow");
						
						$('html, body').animate({scrollTop:0}, 'fast');
			
						// Create Print Preview Heading
						$("body").prepend(printPreviewMessage);
						$("#preview-message").hide().slideDown("slow");
						
						// Switch Back
						$("a#turnoff-print").bind("click", function(){
							$("body").fadeOut("fast", function() {
								$("#preview-message").remove();
								screenSheet.each(function() {
									this.disabled = false;
								});
								printSheet.each(function() {
									this.disabled = true;
								});
								$(this).fadeIn("slow");
							});
							return false;
						});
						
						// Print this page
						$("a#preview-print").bind("click", function(){
							window.print();
							return false;
						});
					});
					
					return false;
				});
				
			})
		);
	}
})(jQuery)