/*
 * jQuery Print Job
 * Version: .5 (July-15-2011)
 * Requires: jQuery v1.5
 */
(function($){
	// Settings
	var AppSetting = {
		width : 600,
		height : 300,
		screenX : 50,
		screenY : 50,
		screenPadding : 30
	};
	
	// Main Class
	/*
	 * @param element - css target
	 * @param options - optional parameter
	 */
	var PrintJob = function(element, options)
	{
		var $elem = $(element);
		var html = $elem.html();
		
		AppSetting.width = $elem.width();
		// Add 150px for padding.
		AppSetting.height = $elem.height() + 150;
		
		// Create a new window.
		var doc = new Window(html);
	};
	
	/*
	 * Create a new window.
	 * 
	 * @param html Markups to be appended to the body.
	 * 
	 * @return Window object.
	 */
	var Window = function(html)
	{
		var windowAttr = "location=yes,statusbar=no,directories=no,menubar=no,titlebar=no,toolbar=no,dependent=no";
		windowAttr += ",width=" + AppSetting.width + ",height=" + (AppSetting.height + AppSetting.screenPadding);
		windowAttr += ",resizable=yes,screenX=" + AppSetting.screenX + ",screenY=" + AppSetting.screenY + ",personalbar=no,scrollbars=yes";
		
		var newWin = window.open( "", "_blank",  windowAttr );
		newWin.document.write(new DocType + "<html>");
		newWin.document.write('<head>');
		newWin.document.write('<style type="text/css">body, td{font-family:arial,sans-serif;font-size:80%} a:link, a:active, a:visited{color:#0000CC} img{border:0} pre { white-space: pre; white-space: -moz-pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word; width: 800px; overflow: auto;}</style>');
		newWin.document.write('<script>function Print(){document.body.offsetHeight;window.print()};</script>');
		newWin.document.write('</head>');
		newWin.document.write('<body onload="Print()">');
		newWin.document.write("</body></html>");
		newWin.document.body.innerHTML = html;
		newWin.document.close();
		
		// Remove the "Click to Print This Page".
		$(newWin.document).find('a.printjob-btn').remove();
		
		return newWin;
	};
	
	var DocType = function()
	{
		return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
	};
	
	// Extending the jQuery
	$.fn.printJob = function(options)
	{
        return this.each(function(key, value)
	   {
            var $element = $(this);
            // Return early if this element already has a plugin instance
            if ($element.data('printJob')) return $element.data('printJob');
            // Pass options to plugin constructor
            var printJob = new PrintJob(this, options);
            // Store plugin object in this element's data
            $element.data('printJob', printJob);
        });
	};
	
})(jQuery);
