(function(jQuery) {

	var self = null;
 
	 //HANDLE $.print( element, [context] )
	 //Acts like $() no parameter, gets DOCUMENT
 	jQuery.extend({
		'print':	function(e, c){
			if( $(e, c).size() > 0){
				$(e, c).jPrint();
				return true;
			} else {
				return false;
			}
		}
	});

 	//HANDLE $( ... ).jPrint( [options] )
 	jQuery.fn.extend({
		'jPrint':	function(opts) {
			//Create configuration
			var pref = $.extend({}, jQuery.fn.jPrint.defaults, opts);
			return new jQuery.jPrint(this, pref);			
		}
	});

	jQuery.fn.jPrint.defaults = {
		//'canvas': 		'<div>&nbsp;</div>',
		'autoDrawn':	true,	//Drawns elements to canvas automatically
		'autoPrint': 	true,	//Print where canvas is drawn
		'autoClean': 	true,	//Auto clean elements after drawn them to canvas
		'beforePrint':	false,
		'afterPrint':	false,
		'debug': 		false
	};

	//Class
	jQuery.jPrint = function( e, p ) {
		this.iframe = null;
		this.elements = e;
		this.pref = p;
		this.ready = false;
		this.last_ret = true;

		this.init();
	}

	//Functions
	jQuery.jPrint.fn = jQuery.jPrint.prototype = {
		'jPrint': '1.0'
	};

	//Copy the $.extend function
 	jQuery.jPrint.prototype.extend = jQuery.jPrint.extend = jQuery.extend;
	
	//Extend the functions
	jQuery.jPrint.prototype.extend({

		'init': function() {

			self = this;
			this.iframe = $('<iframe src="about:blank" name="jprint_frame" />')
			.css({'position': 'absolute', 'width': '0px', 'height': '0px', 'left': '-500px', 'top': '-500px'})
			.load(function(){
				$(this).contents().find('body').append('<div id="jprint_canvas">&nbsp;</div>');
				$(this).trigger('canvasready', self);
			})
			.bind('canvasready', function(e, jPrinter){
				jPrinter.ready = true;

				//drawn elements to canvas automatically
				if(self.pref.autoDrawn) {
					self.toCanvas();
				}

			});

			$('body').append(this.iframe);
		},


		'addElements': function(e) {
			this.elements = this.elements.add( e );			
		},
		
		'clean':	function(){
			this.elements = this.elements.not('*');		//empty set
		},

		'toCanvas': function(){

			//Trying to drawn a canvas which is not ready yet
			if(! this.ready )
				alert('Error! Tried to drawn a non-ready canvas.');
			
			//IE dont allow the insertion of nodes inside iframe BODY, then we must insert the HTML
			this.iframe.contents().find("#jprint_canvas").html(
				$(this.elements).html()
			);
			
			//Auto clean on drawn canvas
			if(this.pref.autoClean)
				this.clean();
				
			//Auto print on drawn canvas
			if(this.pref.autoPrint)
				this.print();

		},

		//Prints the window
		'print':	function(){
			self = this;
			
					//adds elements to canvas automatically
					if(self.pref.autoAdd) {
						self.addElements( self.elements );
					}

			//Error occurred in the user beforeprint callback
			if(this.last_ret == false){
				return false;
			}
			
			//Prints
			with(this.iframe.attr('contentWindow')){
				focus();
				print();
			}

			//Clean UP
			//setTimeout(function(){ $(self.iframe).remove(); }, 500);

			return this.last_ret;
		}

	 });

})(jQuery);

