Document.extend({

	// css files
	CSS: {
		print: $$('link[media=print]'),
		noprint: $$('link[rel=stylesheet][media!=print]')
	},


	// buttons in preview state
	buttons: {
		print: new Element('input', {
			'type': 'submit',
			'value': _['print'],
			'class': 'nav',
			events: {
				'click': function(){
					window.print();
					return false;
				}
			}
		}),
		close: new Element('input', {
			'type': 'reset',
			'value': _['close'],
			'class': 'nav',
			events: {
				'click': function(){
					Document.closePreview(false);
					}
				}
		})
	},


	// state indicator
	preview: false,

	
	// method shows document print version
	showPreview: function(){
		this.preview = true;

		if ( !Browser.Engine.ie6 ) {
			this.buttons.print.inject(document.body, 'top');
		}
		this.buttons.close.inject(document.body, 'top');

		this.CSS.noprint.dispose();
		this.CSS.print.set('media', 'all');
		if ( Browser.Engine.name === 'gecko' && Browser.Engine.version <= 18 ) {
			this.CSS.print.dispose().inject(document.head);
		}

		return this;
	},


	// method close document print version
	closePreview: function(){
		this.preview = false;

		this.buttons.close.dispose();
		this.buttons.print.dispose();

		this.CSS.noprint.inject(this.CSS.print[0], 'before');
		this.CSS.print.set('media', 'print');
		if ( Browser.Engine.name === 'gecko' && Browser.Engine.version <= 18 ) {
			this.CSS.print.dispose().inject(document.head);
		}

		return this;
	},


	// callable method
	print: function(selector){
		if ( this.CSS.print.length > 0 ) {
			if (this.preview) {
				this.closePreview();
			} else {
				if ( Browser.Engine.trident || (Browser.Engine.presto && Browser.Engine.version <= 925) ) {
					this.showPreview();
				} else {
					$$(selector).fade('out');
					(function(){
						this.showPreview();
						$$(selector).fade('in');
					}).delay(1000, this);
				}
			}
		}
		return this;
	}
});
