Index: trunk/phpgwapi/inc/class.preferences.inc.php
===================================================================
--- trunk/phpgwapi/inc/class.preferences.inc.php	(revision 2002)
+++ trunk/phpgwapi/inc/class.preferences.inc.php	(revision 2549)
@@ -174,5 +174,5 @@
 						foreach($val as $k => $v)
 						{
-							if (!is_array($v) && strstr($val,'$$') !== False)
+							if (!is_array($v) && is_string( $val ) && strstr($val,'$$') !== False)
 							{
 								$this->data[$app][$key][$k] = $this->parse_notify($v);
Index: trunk/phpgwapi/inc/functions.inc.php
===================================================================
--- trunk/phpgwapi/inc/functions.inc.php	(revision 2419)
+++ trunk/phpgwapi/inc/functions.inc.php	(revision 2549)
@@ -30,5 +30,5 @@
 	error_reporting(error_reporting() & ~E_NOTICE);
 
-	include(PHPGW_API_INC.'/common_functions.inc.php');
+	include_once(PHPGW_API_INC.'/common_functions.inc.php');
 	
 	/*!
@@ -234,4 +234,5 @@
 	$GLOBALS['phpgw']->preferences  = CreateObject('phpgwapi.preferences');
 	$GLOBALS['phpgw']->applications = CreateObject('phpgwapi.applications');
+	$GLOBALS['phpgw']->js           = CreateObject('phpgwapi.javascript');
 	print_debug('main class loaded', 'messageonly','api');
 	if (! isset($GLOBALS['phpgw_info']['flags']['included_classes']['error']) ||
@@ -403,5 +404,5 @@
 				! $GLOBALS['phpgw_info']['user']['apps']['admin']))
 			{
-				$GLOBALS['phpgw']->common->phpgw_header();
+				//$GLOBALS['phpgw']->common->phpgw_header();
 
 				$GLOBALS['phpgw']->log->write(array('text'=>'W-Permissions, Attempted to access %1','p1'=>$GLOBALS['phpgw_info']['flags']['currentapp']));
@@ -416,19 +417,5 @@
 			$GLOBALS['phpgw']->datetime = CreateObject('phpgwapi.date_time');
 		}
+
 		$GLOBALS['phpgw']->applications->read_installed_apps();	// to get translated app-titles
-		
-		/*************************************************************************\
-		* Load the header unless the developer turns it off                       *
-		\*************************************************************************/
-		if ( array_key_exists( 'HTTP_BACKGROUNDREQUEST', $_SERVER ) || array_key_exists( 'BackgroundRequest', $_GET ) )
-		{
-			$GLOBALS['phpgw_info']['flags'] = array(
-				'noheader'   => true,
-				'nonavbar'   => true
-			);
-		}
-		if (!@$GLOBALS['phpgw_info']['flags']['noheader'])
-		{
-			$GLOBALS['phpgw']->common->phpgw_header();
-		}
-	}
+	}
Index: trunk/phpgwapi/inc/class.javascript.inc.php
===================================================================
--- trunk/phpgwapi/inc/class.javascript.inc.php	(revision 2547)
+++ trunk/phpgwapi/inc/class.javascript.inc.php	(revision 2549)
@@ -49,5 +49,5 @@
 		* @var array list of validated files to be included in the head section of a page
 		*/
-		var $files;
+		private $files = array( );
 
 		/**
@@ -153,7 +153,9 @@
 										$links .= '<script type="text/javascript" src="'
 											. $GLOBALS['phpgw_info']['server']['webserver_url']
-											. "/$app/js/$pkg/$file" . '.js">'
+											. "/$app/js/$pkg/$file" . '.js?' . microtime( true ) . '">'
 											. "</script>\n";
 									}
+
+									$this -> unset_script_link( $app, $pkg, $file );
 								}
 							}
@@ -223,20 +225,28 @@
 		* @returns bool was the file found?
 		*/
-		function validate_file($package, $file, $app='phpgwapi')
-		{
-			if(is_readable(PHPGW_INCLUDE_ROOT .SEP .$app .SEP .'js' .SEP . $package .SEP. $file . '.js'))
-			{
-				$this->files[$app][$package][$file] = $file;
-				return True;
-			}
-			elseif($app != 'phpgwapi')
-			{
-				if(is_readable(PHPGW_INCLUDE_ROOT .SEP .'phpgwapi' .SEP .'js' .SEP . $package .SEP . $file . '.js'))
-				{
-					$this->files['phpgwapi'][$package][$file] = $file;
-					return True;
-				}
-				return False;
-			}
+		function validate_file( $package, $file, $app = NULL, $stack = false )
+		{
+			if ( $app == NULL )
+				$app = 'phpgwapi';
+
+			$_file = PHPGW_INCLUDE_ROOT . SEP . $app . SEP . 'js' . SEP . $package . SEP. $file . '.js';
+			if ( is_readable( $_file ) )
+			{
+				unset( $_file );
+
+				if ( ! array_key_exists( $app, $this -> files ) )
+					$this -> files[ $app ] = array( );
+
+				if ( ! array_key_exists( $package, $this -> files[ $app ] ) )
+					$this -> files[ $app ][ $package ] = array( );
+
+				if ( $stack )
+					$this -> files[ $app ][ $package ] = array_merge( array( $file => $file ), $this -> files[ $app ][ $package ] );
+				else
+					$this -> files[ $app ][ $package ][ $file ] = $file;
+
+				return true;
+			}
+			return false;
 		}
 	}
Index: trunk/phpgwapi/inc/class.common.inc.php
===================================================================
--- trunk/phpgwapi/inc/class.common.inc.php	(revision 2547)
+++ trunk/phpgwapi/inc/class.common.inc.php	(revision 2549)
@@ -368,8 +368,44 @@
 				define('PHPGW_FINAL',True);
 
+				$module_content = ob_get_contents( );
+
+				ob_clean( );
+
+				/*************************************************************************\
+				* Load the header unless the developer turns it off                       *
+				\*************************************************************************/
+				if ( array_key_exists( 'HTTP_BACKGROUNDREQUEST', $_SERVER ) || array_key_exists( 'BackgroundRequest', $_GET ) )
+					$GLOBALS['phpgw_info']['flags'] = array_merge( $GLOBALS['phpgw_info']['flags'], array(
+						'noheader'   => true,
+						'nonavbar'   => true
+					) );
+
+				if ( ! @$GLOBALS['phpgw_info']['flags']['noheader'] )
+				{
+					if ( ! isset( $GLOBALS[ 'phpgw_info' ][ 'flags' ][ 'java_script_thirst' ] ) )
+						$GLOBALS['phpgw_info']['flags']['java_script_thirst'] = '';
+
+					$GLOBALS['phpgw_info']['flags']['java_script_thirst'] .= '<script type="text/javascript">var URL_SERVER = "'
+						. $GLOBALS[ 'phpgw_info' ][ 'server' ][ 'webserver_url' ] . '";'
+						. 'var template = "' . $GLOBALS[ 'phpgw_info' ][ 'server' ][ 'template_set' ] . '";</script>';
+
+					$GLOBALS[ 'phpgw' ] -> js -> validate_file( 'wz_dragdrop', 'wz_dragdrop', NULL, true );
+					$GLOBALS[ 'phpgw' ] -> js -> validate_file( 'expressoAjax', 'expressoAjax', NULL, true );
+					$GLOBALS[ 'phpgw' ] -> js -> validate_file( 'tools','xlink', NULL, true );
+					$GLOBALS[ 'phpgw' ] -> js -> validate_file( 'tools','xconnector', NULL, true );
+					$GLOBALS[ 'phpgw' ] -> js -> validate_file( 'tools','xevent', NULL, true );
+
+					$this -> phpgw_header( );
+				}
+				else
+					echo $this -> get_java_script( );
+
+				echo $module_content;
+
 				/*if (is_object($GLOBALS['phpgw']->accounts))
 				{
 					$GLOBALS['phpgw']->accounts->save_session_cache();
 				}*/
+
 				// call the asyncservice check_run function if it is not explicitly set to cron-only
 				//
@@ -378,4 +414,5 @@
 					ExecMethod('phpgwapi.asyncservice.check_run','fallback');
 				}
+
 				/* Clean up mcrypt */
 				if (@is_object($GLOBALS['phpgw']->crypto))
@@ -384,4 +421,5 @@
 					unset($GLOBALS['phpgw']->crypto);
 				}
+
 				$GLOBALS['phpgw']->db->disconnect();
 			}
@@ -1213,39 +1251,13 @@
 			// add a content-type header to overwrite an existing default charset in apache (AddDefaultCharset directiv)
 			header('Content-type: text/html; charset='.$GLOBALS['phpgw']->translation->charset());
-			include(PHPGW_INCLUDE_ROOT . '/phpgwapi/templates/' . $GLOBALS['phpgw_info']['server']['template_set']
-				. '/head.inc.php');
+
+			include(PHPGW_INCLUDE_ROOT . '/phpgwapi/templates/' . $GLOBALS['phpgw_info']['server']['template_set'] . '/head.inc.php');
+
 			$this->navbar(False);
-			include(PHPGW_INCLUDE_ROOT . '/phpgwapi/templates/' . $GLOBALS['phpgw_info']['server']['template_set']
-				. '/navbar.inc.php');
-
-			if(!@is_object($GLOBALS['phpgw']->js))
-			{
-				$GLOBALS['phpgw']->js = CreateObject('phpgwapi.javascript');
-			}
-
-			$GLOBALS['phpgw']->js->validate_file( 'tools','xevent' );
-			$GLOBALS['phpgw']->js->validate_file( 'tools','xconnector' );
-			$GLOBALS['phpgw']->js->validate_file( 'tools','xlink' );
-
-			$GLOBALS['phpgw']->js->validate_file('expressoAjax','expressoAjax');
-
-			$GLOBALS['phpgw']->js->validate_file('wz_dragdrop','wz_dragdrop');
-
-			echo "\n<script type='text/javascript'>var URL_SERVER = '".$GLOBALS['phpgw_info']['server']['webserver_url']."';</script>\n";
-			echo $GLOBALS['phpgw']->js->get_script_links();
-
-			$GLOBALS['phpgw']->js->unset_script_link( 'phpgwapi','tools' );
-
-			$GLOBALS['phpgw']->js->unset_script_link('phpgwapi','expressoAjax');
-
-			// TODO
-			// Esse javascript serÃ¡ carregado sempre pois causa grandes
-			// problemas em diversos mÃ³dulos. Algum dia serÃ¡ substituido.
-			$GLOBALS['phpgw']->js->unset_script_link('phpgwapi','wz_dragdrop');
+
+			include(PHPGW_INCLUDE_ROOT . '/phpgwapi/templates/' . $GLOBALS['phpgw_info']['server']['template_set'] . '/navbar.inc.php');
 
 			if (!@$GLOBALS['phpgw_info']['flags']['nonavbar'] && !@$GLOBALS['phpgw_info']['flags']['navbar_target'])
-			{
 				echo parse_navbar();
-			} 
 		}
 
@@ -1343,5 +1355,5 @@
 		* @return string the javascript to be included
 		*/
-		function get_java_script()
+		function get_java_script( )
 		{
 			$java_script = '';
@@ -1349,13 +1361,9 @@
 			/* this flag is for all javascript code that has to be put before other jscode. 
 			Think of conf vars etc...  (pim@lingewoud.nl) */
-			if (isset($GLOBALS['phpgw_info']['flags']['java_script_thirst']))
-			{
-				$java_script .= $GLOBALS['phpgw_info']['flags']['java_script_thirst'] . "\n";
-			}
-			
-			if(@is_object($GLOBALS['phpgw']->js))
-			{
-				$java_script .= $GLOBALS['phpgw']->js->get_script_links();
-			}
+			if ( isset( $GLOBALS[ 'phpgw_info' ][ 'flags' ][ 'java_script_thirst' ] ) )
+				$java_script .= $GLOBALS['phpgw_info']['flags']['java_script_thirst'];
+
+			if( is_object( $GLOBALS[ 'phpgw' ] -> js ) )
+				$java_script .= $GLOBALS[ 'phpgw' ] -> js -> get_script_links( );
 
 			if(@isset($_GET['menuaction']))
@@ -1368,8 +1376,8 @@
 				}
 			}
-			if (isset($GLOBALS['phpgw_info']['flags']['java_script']))
-			{
-				$java_script .= $GLOBALS['phpgw_info']['flags']['java_script'] . "\n";
-			}
+
+			if ( isset( $GLOBALS[ 'phpgw_info' ][ 'flags' ][ 'java_script' ] ) )
+				$java_script .= $GLOBALS[ 'phpgw_info' ][ 'flags' ][ 'java_script' ];
+
 			return $java_script;
 		}
