diff --git a/apps/user_openid/appinfo/info.xml b/apps/user_openid/appinfo/info.xml
index 268af23973837f9faba0cec72fa6180a20702617..7aae4271fa8b80a4e78b05e5fc897bb3f793a7ab 100644
--- a/apps/user_openid/appinfo/info.xml
+++ b/apps/user_openid/appinfo/info.xml
@@ -8,6 +8,7 @@
 	<require>4</require>
 	<shipped>true</shipped>
 	<types>
+		<prelogin/>
 		<authentication/>
 	</types>
 </info>
diff --git a/core/minimizer.php b/core/minimizer.php
index 47e3d855e7b31857fa754adb4e826c81a8e8423c..0abbca7502723aa962a664785068978ae73ba2df 100644
--- a/core/minimizer.php
+++ b/core/minimizer.php
@@ -5,11 +5,11 @@ OC_App::loadApps();
 
 if ($service == 'core.css'){
 	$minimizer = new OC_Minimizer_CSS();
-	$files = $minimizer->findFiles(OC_Util::$core_styles);
+	$files = OC_TemplateLayout::findStylesheetFiles(OC_Util::$core_styles);
 	$minimizer->output($files, $service);
 }
 else if ($service == 'core.js'){
 	$minimizer = new OC_Minimizer_JS();
-	$files = $minimizer->findFiles(OC_Util::$core_scripts);
+	$files = OC_TemplateLayout::findJavascriptFiles(OC_Util::$core_scripts);
 	$minimizer->output($files, $service);
 }
diff --git a/index.php b/index.php
index 32b3c88df1dd4b0b74664d243e547512e7d75cf8..1171c0fe0cf32cc1d9bfeefd8a82a7ddc34a4aa1 100755
--- a/index.php
+++ b/index.php
@@ -69,6 +69,7 @@ elseif(OC_User::isLoggedIn()) {
 
 // For all others cases, we display the guest page :
 } else {
+	OC_App::loadApps(array('prelogin'));
 	$error = false;
 	// remember was checked after last login
 	if(isset($_COOKIE["oc_remember_login"]) && isset($_COOKIE["oc_token"]) && isset($_COOKIE["oc_username"]) && $_COOKIE["oc_remember_login"]) {
diff --git a/lib/base.php b/lib/base.php
index 94ae26c4d1ba347628aacde021054fc06b6a9221..db55504117d4b1df4752e07107e5fa647c05ed63 100644
--- a/lib/base.php
+++ b/lib/base.php
@@ -95,31 +95,6 @@ class OC{
 		}
 	}
 
-	/**
-	 * autodetects the formfactor of the used device
-	 * default -> the normal desktop browser interface
-	 * mobile -> interface for smartphones
-	 * tablet -> interface for tablets
-	 * standalone -> the default interface but without header, footer and sidebar. just the application. useful to ue just a specific app on the desktop in a standalone window.
-	 */
-	public static function detectFormfactor(){
-		// please add more useragent strings for other devices
-		if(isset($_SERVER['HTTP_USER_AGENT'])){
-			if(stripos($_SERVER['HTTP_USER_AGENT'],'ipad')>0) {
-				$mode='tablet';
-			}elseif(stripos($_SERVER['HTTP_USER_AGENT'],'iphone')>0){
-				$mode='mobile';
-			}elseif((stripos($_SERVER['HTTP_USER_AGENT'],'N9')>0) and (stripos($_SERVER['HTTP_USER_AGENT'],'nokia')>0)){
-				$mode='mobile';
-			}else{
-				$mode='default';
-			}
-		}else{
-			$mode='default';
-		}
-		return($mode);
-	}
-
 	public static function initPaths(){
 		// calculate the root directories
 		OC::$SERVERROOT=str_replace("\\",'/',substr(__FILE__,0,-13));
@@ -237,15 +212,6 @@ class OC{
 	}
 
 	public static function initTemplateEngine() {
-		// if the formfactor is not yet autodetected do the autodetection now. For possible forfactors check the detectFormfactor documentation
-		if(!isset($_SESSION['formfactor'])){
-			$_SESSION['formfactor']=OC::detectFormfactor();
-		}
-		// allow manual override via GET parameter
-		if(isset($_GET['formfactor'])){
-			$_SESSION['formfactor']=$_GET['formfactor'];
-		}
-
 		// Add the stuff we need always
 		OC_Util::addScript( "jquery-1.7.2.min" );
 		OC_Util::addScript( "jquery-ui-1.8.16.custom.min" );
diff --git a/lib/minimizer.php b/lib/minimizer.php
index 428fa477f77b4ee8b89d2baa3239a19e0c17662d..e17c114f0650fa69d257cf425216176c2a9b7887 100644
--- a/lib/minimizer.php
+++ b/lib/minimizer.php
@@ -1,17 +1,6 @@
 <?php
 
-abstract class OC_Minimizer
-{
-	protected $files = array();
-
-	protected function appendIfExist($root, $webroot, $file) {
-                if (is_file($root.'/'.$file)) {
-			$this->files[] = array($root, $webroot, $file);
-                        return true;
-                }
-                return false;
-	}
-
+abstract class OC_Minimizer {
 	public function getLastModified($files) {
 		$last_modified = 0;
 		foreach($files as $file_info) {
diff --git a/lib/minimizer/css.php b/lib/minimizer/css.php
index 09a0efdc3a73a4792d6718aa975cb972870d86d5..da502bfa9e8ce9a5fe81288f4f5f73b75bec98be 100644
--- a/lib/minimizer/css.php
+++ b/lib/minimizer/css.php
@@ -6,50 +6,6 @@ class OC_Minimizer_CSS extends OC_Minimizer
 {
 	protected $contentType = 'text/css';
 
-	public function findFiles($styles) {
-		// Read the selected theme from the config file
-		$theme=OC_Config::getValue( "theme" );
-
-		// Read the detected formfactor and use the right file name.
-		$fext = OC_Template::getFormFactorExtension();
-		foreach($styles as $style){
-			// is it in 3rdparty?
-			if($this->appendIfExist(OC::$THIRDPARTYROOT, OC::$THIRDPARTYWEBROOT, $style.'.css')) {
-
-			// or in apps?
-			}elseif($this->appendIfExist(OC::$APPSROOT, OC::$APPSWEBROOT, "apps/$style$fext.css" )) {
-			}elseif($this->appendIfExist(OC::$APPSROOT, OC::$APPSWEBROOT, "apps/$style.css" )) {
-
-			// or in the owncloud root?
-			}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "$style$fext.css" )) {
-			}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "$style.css" )) {
-
-			// or in core ?
-			}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "core/$style$fext.css" )) {
-			}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "core/$style.css" )) {
-
-			}else{
-				echo('css file not found: style:'.$style.' formfactor:'.$fext.' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT);
-				die();
-			}
-		}
-		// Add the theme css files. you can override the default values here
-		if(!empty($theme)) {
-			foreach($styles as $style){
-				     if($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$style$fext.css" )) {
-				}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$style.css" )) {
-
-				}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$style$fext.css" )) {
-				}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$style.css" )) {
-
-				}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$style$fext.css" )) {
-				}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$style.css" )) {
-				}
-			}
-		}
-		return $this->files;
-	}
-
 	public function minimizeFiles($files) {
 		$css_out = '';
 		$appswebroot = (string) OC::$APPSWEBROOT;
diff --git a/lib/minimizer/js.php b/lib/minimizer/js.php
index b9a023e06862c15ebb557fea4b3392335a54a2c0..0f5cb7e5577f4bec2659bda44cf1de735cc87fb5 100644
--- a/lib/minimizer/js.php
+++ b/lib/minimizer/js.php
@@ -6,49 +6,6 @@ class OC_Minimizer_JS extends OC_Minimizer
 {
 	protected $contentType = 'application/javascript';
 
-	public function findFiles($scripts) {
-		// Read the selected theme from the config file
-		$theme=OC_Config::getValue( "theme" );
-
-		// Read the detected formfactor and use the right file name.
-		$fext = OC_Template::getFormFactorExtension();
-		// Add the core js files or the js files provided by the selected theme
-		foreach($scripts as $script){
-			// Is it in 3rd party?
-			if($this->appendIfExist(OC::$THIRDPARTYROOT, OC::$THIRDPARTYWEBROOT, $script.'.js')) {
-
-			// Is it in apps and overwritten by the theme?
-			}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$script$fext.js" )) {
-			}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$script.js" )) {
-
-			// Is it part of an app?
-			}elseif($this->appendIfExist(OC::$APPSROOT, OC::$APPSWEBROOT, "apps/$script$fext.js" )) {
-			}elseif($this->appendIfExist(OC::$APPSROOT, OC::$APPSWEBROOT, "apps/$script.js" )) {
-
-			// Is it in the owncloud root but overwritten by the theme?
-			}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$script$fext.js" )) {
-			}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$script.js" )) {
-
-			// Is it in the owncloud root ?
-			}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "$script$fext.js" )) {
-			}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "$script.js" )) {
-
-			// Is in core but overwritten by a theme?
-			}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$script$fext.js" )) {
-			}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$script.js" )) {
-
-			// Is it in core?
-			}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "core/$script$fext.js" )) {
-			}elseif($this->appendIfExist(OC::$SERVERROOT, OC::$WEBROOT, "core/$script.js" )) {
-
-			}else{
-				echo('js file not found: script:'.$script.' formfactor:'.$fext.' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT);
-				die();
-			}
-		}
-		return $this->files;
-	}
-
 	public function minimizeFiles($files) {
 		$js_out = '';
 		foreach($files as $file_info) {
diff --git a/lib/template.php b/lib/template.php
index a3700e133e79786ecab01400d1ed432488b90350..a5d10c45d230c36fde286708ec8f86baaa39654a 100644
--- a/lib/template.php
+++ b/lib/template.php
@@ -166,11 +166,48 @@ class OC_Template{
 		$this->findTemplate($name);
 	}
 
+	/**
+	 * autodetects the formfactor of the used device
+	 * default -> the normal desktop browser interface
+	 * mobile -> interface for smartphones
+	 * tablet -> interface for tablets
+	 * standalone -> the default interface but without header, footer and
+	 * 	sidebar, just the application. Useful to use just a specific
+	 * 	app on the desktop in a standalone window.
+	 */
+	public static function detectFormfactor(){
+		// please add more useragent strings for other devices
+		if(isset($_SERVER['HTTP_USER_AGENT'])){
+			if(stripos($_SERVER['HTTP_USER_AGENT'],'ipad')>0) {
+				$mode='tablet';
+			}elseif(stripos($_SERVER['HTTP_USER_AGENT'],'iphone')>0){
+				$mode='mobile';
+			}elseif((stripos($_SERVER['HTTP_USER_AGENT'],'N9')>0) and (stripos($_SERVER['HTTP_USER_AGENT'],'nokia')>0)){
+				$mode='mobile';
+			}else{
+				$mode='default';
+			}
+		}else{
+			$mode='default';
+		}
+		return($mode);
+	}
+
 	/**
 	 * @brief Returns the formfactor extension for current formfactor
 	 */
 	static public function getFormFactorExtension()
 	{
+		// if the formfactor is not yet autodetected do the
+		// autodetection now. For possible formfactors check the
+		// detectFormfactor documentation
+		if(!isset($_SESSION['formfactor'])){
+			$_SESSION['formfactor'] = self::detectFormfactor();
+		}
+		// allow manual override via GET parameter
+		if(isset($_GET['formfactor'])){
+			$_SESSION['formfactor']=$_GET['formfactor'];
+		}
 		$formfactor=$_SESSION['formfactor'];
 		if($formfactor=='default') { 
 			$fext='';
@@ -270,7 +307,7 @@ class OC_Template{
 	 *
 	 * If the key existed before, it will be overwritten
 	 */
-		public function assign( $key, $value, $sanitizeHTML=true ){
+	public function assign( $key, $value, $sanitizeHTML=true ){
 		if($sanitizeHTML == true) {
 			if(is_array($value)) {
 				array_walk_recursive($value,'OC_Template::sanitizeHTML');
@@ -339,29 +376,6 @@ class OC_Template{
 		}
 	}
 
-	/*
-	 * @brief append the $file-url if exist at $root
-	 * @param $type of collection to use when appending
-	 * @param $root path to check
-	 * @param $web base for path
-	 * @param $file the filename
-	 */
-        public function appendIfExist($type, $root, $web, $file) {
-                if (is_file($root.'/'.$file)) {
-                		$pathes = explode('/', $file);
-                		if($type == 'cssfiles' && $root == OC::$APPSROOT && $pathes[0] == 'apps'){
-                				$app = $pathes[1];
-                				unset($pathes[0]);
-                				unset($pathes[1]);
-                				$path = implode('/', $pathes);
-                				$this->append( $type, OC_Helper::linkTo($app, $path));
-                		}else{
-                				$this->append( $type, $web.'/'.$file);
-                		}
-                        return true;
-                }
-                return false;
-        }
 	/**
 	 * @brief Proceeds the template
 	 * @returns content
@@ -373,109 +387,9 @@ class OC_Template{
 		$data = $this->_fetch();
 
 		if( $this->renderas ){
-			// Decide which page we show
-			if( $this->renderas == "user" ){
-				$page = new OC_Template( "core", "layout.user" );
-				$page->assign('searchurl',OC_Helper::linkTo( 'search', 'index.php' ), false);
+			$page = new OC_TemplateLayout($this->renderas);
+			if($this->renderas == 'user') {
 				$page->assign('requesttoken', $this->vars['requesttoken']);
-				if(array_search(OC_APP::getCurrentApp(),array('settings','admin','help'))!==false){
-					$page->assign('bodyid','body-settings', false);
-				}else{
-					$page->assign('bodyid','body-user', false);
-				}
-
-				// Add navigation entry
-				$navigation = OC_App::getNavigation();
-				$page->assign( "navigation", $navigation, false);
-				$page->assign( "settingsnavigation", OC_App::getSettingsNavigation(), false);
-				foreach($navigation as $entry) {
-					if ($entry['active']) {
-						$page->assign( 'application', $entry['name'], false );
-						break;
-					}
-				}
-			}else{
-				$page = new OC_Template( "core", "layout.guest" );
-			}
-
-			// Read the selected theme from the config file
-			$theme=OC_Config::getValue( "theme" );
-
-			// Read the detected formfactor and use the right file name.
-			$fext = self::getFormFactorExtension();
-
-			$page->assign('jsfiles', array(), false);
-			// Add the core js files or the js files provided by the selected theme
-			foreach(OC_Util::$scripts as $script){
-				// Is it in 3rd party?
-                                if($page->appendIfExist('jsfiles', OC::$THIRDPARTYROOT, OC::$THIRDPARTYWEBROOT, $script.'.js')) {
-
-				// Is it in apps and overwritten by the theme?
-				}elseif($page->appendIfExist('jsfiles', OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$script$fext.js" )) {
-				}elseif($page->appendIfExist('jsfiles', OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$script.js" )) {
-
-				// Is it part of an app?
-				}elseif($page->appendIfExist('jsfiles', OC::$APPSROOT, OC::$APPSWEBROOT, "apps/$script$fext.js" )) {
-				}elseif($page->appendIfExist('jsfiles', OC::$APPSROOT, OC::$APPSWEBROOT, "apps/$script.js" )) {
-
-				// Is it in the owncloud root but overwritten by the theme?
-				}elseif($page->appendIfExist('jsfiles', OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$script$fext.js" )) {
-				}elseif($page->appendIfExist('jsfiles', OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$script.js" )) {
-
-				// Is it in the owncloud root ?
-				}elseif($page->appendIfExist('jsfiles', OC::$SERVERROOT, OC::$WEBROOT, "$script$fext.js" )) {
-				}elseif($page->appendIfExist('jsfiles', OC::$SERVERROOT, OC::$WEBROOT, "$script.js" )) {
-
-				// Is in core but overwritten by a theme?
-				}elseif($page->appendIfExist('jsfiles', OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$script$fext.js" )) {
-				}elseif($page->appendIfExist('jsfiles', OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$script.js" )) {
-
-				// Is it in core?
-				}elseif($page->appendIfExist('jsfiles', OC::$SERVERROOT, OC::$WEBROOT, "core/$script$fext.js" )) {
-				}elseif($page->appendIfExist('jsfiles', OC::$SERVERROOT, OC::$WEBROOT, "core/$script.js" )) {
-
-				}else{
-					echo('js file not found: script:'.$script.' formfactor:'.$fext.' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT);
-					die();
-
-				}
-			}
-			// Add the css files
-			$page->assign('cssfiles', array());
-			foreach(OC_Util::$styles as $style){
-				// is it in 3rdparty?
-                                if($page->appendIfExist('cssfiles', OC::$THIRDPARTYROOT, OC::$THIRDPARTYWEBROOT, $style.'.css')) {
-
-				// or in apps?
-				}elseif($page->appendIfExist('cssfiles', OC::$APPSROOT, OC::$APPSWEBROOT, "apps/$style$fext.css" )) {
-				}elseif($page->appendIfExist('cssfiles', OC::$APPSROOT, OC::$APPSWEBROOT, "apps/$style.css" )) {
-
-				// or in the owncloud root?
-				}elseif($page->appendIfExist('cssfiles', OC::$SERVERROOT, OC::$WEBROOT, "$style$fext.css" )) {
-				}elseif($page->appendIfExist('cssfiles', OC::$SERVERROOT, OC::$WEBROOT, "$style.css" )) {
-
-				// or in core ?
-				}elseif($page->appendIfExist('cssfiles', OC::$SERVERROOT, OC::$WEBROOT, "core/$style$fext.css" )) {
-				}elseif($page->appendIfExist('cssfiles', OC::$SERVERROOT, OC::$WEBROOT, "core/$style.css" )) {
-
-				}else{
-					echo('css file not found: style:'.$style.' formfactor:'.$fext.' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT);
-					die();
-				}
-			}
-			// Add the theme css files. you can override the default values here
-			if(!empty($theme)) {
-				foreach(OC_Util::$styles as $style){
-                                             if($page->appendIfExist('cssfiles', OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$style$fext.css" )) {
-                                        }elseif($page->appendIfExist('cssfiles', OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$style.css" )) {
-
-                                        }elseif($page->appendIfExist('cssfiles', OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$style$fext.css" )) {
-                                        }elseif($page->appendIfExist('cssfiles', OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$style.css" )) {
-
-                                        }elseif($page->appendIfExist('cssfiles', OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$style$fext.css" )) {
-                                        }elseif($page->appendIfExist('cssfiles', OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$style.css" )) {
-                                        }
-				}
 			}
 
 			// Add custom headers
@@ -484,7 +398,6 @@ class OC_Template{
 				$page->append('headers',$header);
 			}
 
-			// Add css files and js files
 			$page->assign( "content", $data, false );
 			return $page->fetchPage();
 		}
diff --git a/lib/templatelayout.php b/lib/templatelayout.php
new file mode 100644
index 0000000000000000000000000000000000000000..1f82e82be74851c969835798753ac8e0f54fee5e
--- /dev/null
+++ b/lib/templatelayout.php
@@ -0,0 +1,170 @@
+<?php
+/**
+ * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+class OC_TemplateLayout extends OC_Template {
+	public function __construct( $renderas ){
+		// Decide which page we show
+		if( $renderas == 'user' ){
+			parent::__construct( 'core', 'layout.user' );
+			$this->assign('searchurl',OC_Helper::linkTo( 'search', 'index.php' ), false);
+			if(array_search(OC_APP::getCurrentApp(),array('settings','admin','help'))!==false){
+				$this->assign('bodyid','body-settings', false);
+			}else{
+				$this->assign('bodyid','body-user', false);
+			}
+
+			// Add navigation entry
+			$navigation = OC_App::getNavigation();
+			$this->assign( 'navigation', $navigation, false);
+			$this->assign( 'settingsnavigation', OC_App::getSettingsNavigation(), false);
+			foreach($navigation as $entry) {
+				if ($entry['active']) {
+					$this->assign( 'application', $entry['name'], false );
+					break;
+				}
+			}
+		}else{
+			parent::__construct( 'core', 'layout.guest' );
+		}
+
+		// Add the js files
+		$jsfiles = self::findJavascriptFiles(OC_Util::$scripts);
+		$this->assign('jsfiles', array(), false);
+		foreach($jsfiles as $info) {
+			$root = $info[0];
+			$web = $info[1];
+			$file = $info[2];
+			$this->append( 'jsfiles', $web.'/'.$file);
+		}
+
+		// Add the css files
+		$cssfiles = self::findStylesheetFiles(OC_Util::$styles);
+		$this->assign('cssfiles', array());
+		foreach($cssfiles as $info) {
+			$root = $info[0];
+			$web = $info[1];
+			$file = $info[2];
+			$paths = explode('/', $file);
+			if($root == OC::$APPSROOT && $paths[0] == 'apps'){
+				$app = $paths[1];
+				unset($paths[0]);
+				unset($paths[1]);
+				$path = implode('/', $paths);
+				$this->append( 'cssfiles', OC_Helper::linkTo($app, $path));
+			}else{
+				$this->append( 'cssfiles', $web.'/'.$file);
+			}
+		}
+	}
+
+	/*
+	 * @brief append the $file-url if exist at $root
+	 * @param $files array to append file info to
+	 * @param $root path to check
+	 * @param $web base for path
+	 * @param $file the filename
+	 */
+        static public function appendIfExist(&$files, $root, $webroot, $file) {
+                if (is_file($root.'/'.$file)) {
+			$files[] = array($root, $webroot, $file);
+			return true;
+                }
+                return false;
+        }
+
+	static public function findStylesheetFiles($styles){
+		// Read the selected theme from the config file
+		$theme=OC_Config::getValue( 'theme' );
+
+		// Read the detected formfactor and use the right file name.
+		$fext = self::getFormFactorExtension();
+
+		$files = array();
+		foreach($styles as $style){
+			// is it in 3rdparty?
+			if(self::appendIfExist($files, OC::$THIRDPARTYROOT, OC::$THIRDPARTYWEBROOT, $style.'.css')) {
+
+			// or in apps?
+			}elseif(self::appendIfExist($files, OC::$APPSROOT, OC::$APPSWEBROOT, "apps/$style$fext.css" )) {
+			}elseif(self::appendIfExist($files, OC::$APPSROOT, OC::$APPSWEBROOT, "apps/$style.css" )) {
+
+			// or in the owncloud root?
+			}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "$style$fext.css" )) {
+			}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "$style.css" )) {
+
+			// or in core ?
+			}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "core/$style$fext.css" )) {
+			}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "core/$style.css" )) {
+
+			}else{
+				echo('css file not found: style:'.$style.' formfactor:'.$fext.' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT);
+				die();
+			}
+		}
+		// Add the theme css files. you can override the default values here
+		if(!empty($theme)) {
+			foreach($styles as $style){
+				     if(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$style$fext.css" )) {
+				}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$style.css" )) {
+
+				}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$style$fext.css" )) {
+				}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$style.css" )) {
+
+				}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$style$fext.css" )) {
+				}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$style.css" )) {
+				}
+			}
+		}
+		return $files;
+	}
+
+	static public function findJavascriptFiles($scripts){
+		// Read the selected theme from the config file
+		$theme=OC_Config::getValue( 'theme' );
+
+		// Read the detected formfactor and use the right file name.
+		$fext = self::getFormFactorExtension();
+
+		$files = array();
+		foreach($scripts as $script){
+			// Is it in 3rd party?
+			if(self::appendIfExist($files, OC::$THIRDPARTYROOT, OC::$THIRDPARTYWEBROOT, $script.'.js')) {
+
+			// Is it in apps and overwritten by the theme?
+			}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$script$fext.js" )) {
+			}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$script.js" )) {
+
+			// Is it part of an app?
+			}elseif(self::appendIfExist($files, OC::$APPSROOT, OC::$APPSWEBROOT, "apps/$script$fext.js" )) {
+			}elseif(self::appendIfExist($files, OC::$APPSROOT, OC::$APPSWEBROOT, "apps/$script.js" )) {
+
+			// Is it in the owncloud root but overwritten by the theme?
+			}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$script$fext.js" )) {
+			}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$script.js" )) {
+
+			// Is it in the owncloud root ?
+			}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "$script$fext.js" )) {
+			}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "$script.js" )) {
+
+			// Is in core but overwritten by a theme?
+			}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$script$fext.js" )) {
+			}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$script.js" )) {
+
+			// Is it in core?
+			}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "core/$script$fext.js" )) {
+			}elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "core/$script.js" )) {
+
+			}else{
+				echo('js file not found: script:'.$script.' formfactor:'.$fext.' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT);
+				die();
+
+			}
+		}
+		return $files;
+	}
+}