Index: code/branches/menu/data/gui/scripts/InitialiseGUI.lua
===================================================================
--- code/branches/menu/data/gui/scripts/InitialiseGUI.lua	(revision 6024)
+++ code/branches/menu/data/gui/scripts/InitialiseGUI.lua	(revision 6032)
@@ -19,4 +19,5 @@
 root = nil
 bShowsCursor = false
+bHidePrevious = {}
 
 -- loads the GUI with the specified filename
@@ -32,5 +33,4 @@
         if table.getn(loadedGUIs) == 1 then
             current = loadedGUIs[1]
-            showing = false
         end
         -- hide new GUI as we do not want to show it accidentially
@@ -40,6 +40,6 @@
 end
 
-function showGUI(filename, bCursorVisible, ptr)
-    gui = showGUI(filename, bCursorVisible)
+function showGUI(filename, hidePrevious, bCursorVisible, ptr)
+    gui = showGUI(filename, hidePrevious, bCursorVisible)
     gui.overlay = ptr
 end
@@ -47,15 +47,11 @@
 -- shows the specified and loads it if not loaded already
 -- be sure to set the global variable "filename" before calling this function
-function showGUI(filename, bCursorVisible)
---     bCursorVisibile=false
+function showGUI(filename, hidePrevious, bCursorVisible)
     if bCursorVisible == nil then
-        cursorVisibility= true
+        bCursorVisible = true
     end
 
     if root == nil then
-        root = winMgr:createWindow("TaharezLook/StaticImage", "AbsoluteRootWindow")
-        root:setProperty("Alpha", "0.0")
-        root:setSize(CEGUI.UVector2(CEGUI.UDim(1.0,0),CEGUI.UDim(1.0,0)))
-        system:setGUISheet(root)
+        setBackground("")
     end
 
@@ -65,5 +61,9 @@
     end
 
+    debug("test")
+    debug(currentGUI.window:getName())
+    debug(root:isChild(currentGUI.window))
     if(root:isChild(currentGUI.window)) then
+        debug("removing window")
         root:removeChildWindow(currentGUI.window)
     end
@@ -75,12 +75,21 @@
         hideCursor()
     end
-    cursorVisibility[filename]=bCursorVisible
     
+    if find( activeSheets, filename ) ~= nil then
+        table.remove( activeSheets, find( activeSheets, filename ) )
+        nrOfActiveSheets = nrOfActiveSheets - 1
+    end
     nrOfActiveSheets = nrOfActiveSheets + 1
     table.insert(activeSheets, filename)
     activeSheets[nrOfActiveSheets] = filename
+    bHidePrevious[filename]=hidePrevious
+    cursorVisibility[filename] = bCursorVisible
     
+    if hidePrevious == true then
+        for i=1,nrOfActiveSheets-1 do
+            loadedGUIs[ activeSheets[i] ]:hide()
+        end
+    end
     currentGUI:show()
-    showing = true
     return currentGUI
 end
@@ -106,7 +115,17 @@
     end
     currentGUI:hide()
+    if bHidePrevious[filename] == true then
+        local i = nrOfActiveSheets-1
+        while i>0 do
+            loadedGUIs[ activeSheets[i] ]:show()
+            if bHidePrevious[filename]==true then
+                break
+            else
+                i=i-1
+            end
+        end
+    end
     root:removeChildWindow(currentGUI.window)
-    showing = false
-    i=1
+    local i=1
     while activeSheets[i] do
         if activeSheets[i+1] == nil then
@@ -129,3 +148,44 @@
     end
     cursorVisibility[filename] = nil -- remove the cursor visibility of the current gui from the table
+    bHidePrevious[filename] = nil
 end
+
+function setBackground(filename)
+    local newroot
+    if root ~= nil then
+        root:rename("oldRootWindow")
+    end
+    if filename ~= "" then
+        newroot = winMgr:loadWindowLayout(filename .. ".layout")
+        newroot:rename("AbsoluteRootWindow")
+        system:setGUISheet(newroot)
+    else
+        newroot = winMgr:createWindow("DefaultWindow", "AbsoluteRootWindow")
+        newroot:setProperty("Alpha", "0.0")
+        newroot:setSize(CEGUI.UVector2(CEGUI.UDim(1.0,0),CEGUI.UDim(1.0,0)))
+        system:setGUISheet(newroot)
+    end
+    if root ~= nil then
+        local child
+        for i=0,root:getChildCount()-1 do
+            child = root:getChildAtIdx(i)
+            root:removeChildWindow(child)
+            newroot:addChildWindow(child)
+        end
+        winMgr:destroyWindow(root)
+    end
+    newroot:show()
+    root = newroot
+end
+
+function find(table, value)
+    local i=0
+    while table[i] ~= nil do
+        if table[i]==value then
+            return i
+        else
+            i=i+1
+        end
+    end
+    return nil
+end
