<?php
/*
PiAnoS (Picture Annotation System), A fingerprint examiner training tool
Copyright (C) 2011-2012 University of Lausanne, Switzerland
https://ips-labs.unil.ch/pianos

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see http://www.gnu.org/licenses/.
*/


require_once("unil/Node.php");
require_once("unil/Sizer.php");

class MainFrame extends Node
{
	function render(&$rs)
	{
		// Theme-related CSS
		$rs->getPage()->addStyleSheet(Application::get('csslib_jquery_ui'));
		$rs->setJQueryStyleSheet(Application::get('jqueryui_theme'), 'theme-backend.css');
		// Theme agnostic CSS
		$rs->getPage()->addStyleSheet('css/backend.css');

		$rs->getPage()->addScript(Application::get('jslib_jquery_layout'));
		$rs->getPage()->addScript(Application::get('jslib_jquery_hashchange'));

		/*$udbPath = Application::get('microdb_path');
		if ($udbPath)
		{
			$udb = array('udb','app','canvas','crud','effects','frame','iframe','jqui-proplist','jqui-qtable','list','model','node','panel','sizeable','sizer','table','tree','utils','window');
			foreach ($udb as $script)
				$rs->getPage()->addScript($udbPath."/js/".$script.".js");
			$rs->getPage()->addStyleSheet($udbPath."/css/udb.css");
		}
		else
		{
			$rs->getPage()->addScript(Application::get('jslib_microdb'));
			$rs->getPage()->addStyleSheet(Application::get('csslib_microdb'));
		}
		*/
		
		$backendJS = Application::get('use_minified_pianos4_backend');
		if ($backendJS)
		{
			$rs->getPage()->addScript($backendJS);
		}
		else
		{
			$rs->getPage()->addScript("js/pianos4_common.js");
			$rs->getPage()->addScript("js/lab.js");
		}
		
		$rs->getPage()->addScript(Application::get('jslib_ajaxupload'));
			
		$rs->getPage()->addStyleSheet(Application::get('csslib_jquery_layout'));

		$rs->getPage()->addScript('/cdn/jtable.2.2.1/jquery.jtable.min.js');
		$rs->getPage()->addStyleSheet('/cdn/jtable.2.2.1/themes/lightcolor/gray/jtable.min.css');
		
		$rs->getPage()->addScript('/cdn/jQuery-Validation-Engine-master/js/jquery.validationEngine.js');
		$rs->getPage()->addScript('/cdn/jQuery-Validation-Engine-master/js/languages/jquery.validationEngine-en.js');
		
		$rs->getPage()->addStyleSheet('/cdn/jQuery-Validation-Engine-master/css/validationEngine.jquery.css');
		
		$settings = array();
		
		// $configOverrides = Application::get('backend_app_settings');
		// if ($configOverrides && count($configOverrides))
			// $settings = array_merge($settings, $configOverrides);

		$this->domStart('div', 'pianos-app');
		$this->domClose();

		$this->domStartJS();
		echo "
		$(document).ready(function() {
			// Tool to inspect variables leaking to the global namespace (window namespace, actually)
			//window.__defineSetter__('varname', function(value) { console.trace(); });

			var settings = {};
			";
			if (count($settings))
				echo "settings = ".json_encode($settings).";\n";
			echo "
			var opt = $.extend({}, settings, {
				user: { id: ".$_SESSION['user']['id'].", name: "._J($_SESSION['user']['name']).", admin: ".($_SESSION['user']['isadmin']?'true':'false')." },
			});
			app = new p4.lab.app($('#pianos-app'), opt);
			app.run();
		});";
		$this->domClose();
	}
}
