
module.exports = new Class({
	Extends: Vn.Object

	,initialize(moduleInfo, gui) {
		this.info = moduleInfo;
		this.gui = gui;
		this.conn = gui.conn;
		Vn.Object.prototype.initialize.call(this);
	}

	,open(lot) {
		this.lot = lot;
		this.createWindow();
	}
	
	,print() {
		this.window.print();
	}

	,includeCss(path) {
		var basePath = location.protocol +'//'+ location.host;
		basePath += location.port ? ':'+ location.port : '';
		basePath += location.pathname.substring(0,
			location.pathname.lastIndexOf('/'));
	
		var link = this.doc.createElement('link');
		link.rel = 'stylesheet';
		link.type = 'text/css';
		link.href = basePath +'/'+ path + Vn.getVersion();
		
		var head = this.doc.getElementsByTagName('head')[0];
		head.appendChild(link);
	}

	,createWindow() {
		var reportWindow = window.open(
			'js/hedera/report.html', '_blank',
			'height=650, width=950, resizable=yes, fullscreen=no,'+
			'titlebar=no, menubar=no, toolbar=no, location=no, scrollbars=yes'
		);
		
		if (!reportWindow) {
			Htk.Toast.showError(
				_('Please unlock popups and try again'));
			return false;
		}
		
		reportWindow.addEventListener('load', 
			this._onWindowLoad.bind(this));		
		this.window = reportWindow;
		return true;
	}
	
	,_onWindowLoad() {
		this.doc = this.window.document

		this.includeCss('reports/'+ this.info.path +'/style.css');
		
		var printButton = this.doc.getElementById('print');
		printButton.addEventListener('click', this.print.bind(this));
		Vn.Node.setText(printButton, _('Print'));
		
		this.onWindowCreate();
	}
	
	,onWindowCreate() {
		var builder = new Vn.Builder();
		builder.compileFile('reports/'+ this.info.path +'/ui.xml');

		var scope = this.scope = builder.load(this.doc, this);
		scope.link({
			lot: this.lot,
			conn: this.conn
		});
		this.$ = scope.$;

		this.doc.body.appendChild(scope.$.report);
	}

	,_destroy() {
		if (this.scope) this.scope._destroy();
		Vn.Object.prototype._destroy.call(this);
	}
});

