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

	,initialize: function (moduleInfo, gui)
	{
		this.info = moduleInfo;
		this.gui = gui;
		this.conn = gui.conn;
		this.parent (null);
	}

	/**
	 * Gets an object from the builder associated to this report.
	 *
	 * @param {string} objectId The object identifier
	 * @return {Object} The object, or %null if not found
	 **/
	,$: function (objectId)
	{
		if (this.builderResult)
			return this.builderResult.getById (objectId);
			
		return null;
	}

	,open: function (batch)
	{
		this.batch = batch;
		this.createWindow ();
	}
	
	,print: function ()
	{
		this.window.print ();
	}

	,includeCss: function (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: function ()
	{
		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: function ()
	{
		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: function ()
	{
		var builder = new Vn.Builder ();
		builder.signalData = this;
		builder.add ('batch', this.batch);
		builder.add ('conn', this.conn);
		builder.loadXml ('reports/'+ this.info.path +'/ui.xml');

		var res = this.builderResult = builder.load ();
		res.link ();

		this.doc.body.appendChild (res.$('report'));
	}
});