// default page with frames
var indexHTML = "index.html";

// common pages and styles
var topHTML          = "top.html";

//
// Grundlegende Idee:
//
// - es werden immer nur links zu Dateien im Frame unten rechts gemacht
// - die Einzelnen Seiten kümmern sich selber darum, dass die Navigation angepasst wird
//   deshalb ist am Anfang die id angegeben, was oben und links hervorgehoben werden soll!
//


//
// Formatierung der selektierten Werte bei der Navigation
//

function FormatLeftObject(obj)
{
    if( obj.outerHTML )
    {
        // IE
        obj.outerHTML = "--&gt; <strong>" + obj.innerText + "<\/strong>";
     }
     else if( obj.innerHTML )
     {
        // NN
        obj.innerHTML = "--&gt; " + obj.innerHTML;
      }
}

function FormatTopObject(obj)
{
    // in top.html ist der DOM so aufgebaut:
    // 
    // <TD>             obj.parentNode.parentNode
    //  +---<P>         obj.parentNode 
    //       +--<A>     obj
    //

    //obj.parentNode.parentNode.style.backgroundColor  = "gainsboro";
    obj.parentNode.parentNode.style.fontSize         = "medium";
}

// ------------------------------------------------------------------------------------------
//
// Frames
//
// ------------------------------------------------------------------------------------------

// Bettet die Seite in die Frames ein, falls noch nicht passiert
function InitFrames()
{
    // "../../"
    var prefix = "";
    for(var i = 0; i < subdirCounter ; i++)
        prefix = prefix + "../";
    
    // ggf. in Frame einbetten (nur wenn nicht geprinted werden soll)
    var args = getArgs(self); // Get arguments.
    if( args.print == null && top.topFrame == null )
    {                        
        top.location.replace(prefix + indexHTML + "?open=" + self.location.href);
        //top.location.reload(true);
    }
}

// passt die Navigationsseiten oben und links an, sodass
// das markierte Element anders erscheint
function AdaptNavigationPages()
{
    // beim Drucken nichts zu machen
    var args = getArgs(self);
    if( args.print != null )
        return;

    var prefix    = "";
    var prefixTop = "";
    for(var i = 0; i < subdirCounter ; i++)
        prefixTop = prefixTop + "../";

    // Titel anpassen    
    if( parent.mainFrame != null )
        parent.document.title = parent.mainFrame.document.title;

    // Top-Navigtionsleite anpassen
    args = getArgs(top.topFrame);
    if( top_id==null || top_id=="" )
        ReplaceURL(top.topFrame, prefixTop + topHTML);
    else if( args.open==null || args.open!=top_id )
        ReplaceURL(top.topFrame, prefixTop + topHTML + "?open=" + top_id);
    
    // Linke Navigationsseite anpassen
    var args = getArgs(top.leftFrame);
    if( left_id==null || left_id=="" )
        ReplaceURL(top.leftFrame, prefix + left_url);
    else if( args.open==null || args.open!=left_id  )
        ReplaceURL(top.leftFrame, prefix + left_url + "?open=" + left_id);
}

function ReplaceURL(frame, url)
{
//    if( frame.location.href != url )
    frame.location.replace(url)
}

function AdaptTopFrame()
{
    var args = getArgs(self); // Get arguments.
    if( args.open == null )
        return;
    
    obj = document.getElementById(args.open)
    if( obj == null )
        return;
    
    FormatTopObject(obj);
}

function AdaptLeftFrame()
{
    var args = getArgs(self); // Get arguments.
    if( args.open == null )
        return;
    
    obj = document.getElementById(args.open)
    if( obj == null )
        return;
    
    FormatLeftObject(obj);
}

// ------------------------------------------------------------------------------------------
//
// Page queries and parmeters
//
// ------------------------------------------------------------------------------------------

//
// wenn QueryString dabei, dannn wird diese Seite unten rechts geladen! 
//
function OpenFileFromQuery()
{
    var args = getArgs(top); // Get arguments.
    if( args.open )
            return args.open;
    else    return "welcome.html";
}
/*
 * This function parses comma-separated name=value argument pairs from
 * the query string of the URL. It stores the name=value pairs in 
 * properties of an object and returns that object.
 */
function getArgs(wnd) 
{
    var args = new Object();
    var count = 0;
    var query = wnd.location.search.substring(1); // Get query string.
    var pairs = query.split(",");                 // Break at comma.
    for(var i = 0; i < pairs.length; i++) 
    {
	    var pos = pairs[i].indexOf('=');          // Look for "name=value".
	    if (pos == -1) continue;                  // If not found, skip.
	    var argname = pairs[i].substring(0,pos);  // Extract the name.
	    var value = pairs[i].substring(pos+1);    // Extract the value.
	    args[argname] = unescape(value);          // Store as a property.
	    count = count + 1;
    }
    args["length"] = count;
    return args;                                  // Return the object.
}

// ------------------------------------------------------------------------------------------
//
// Printing
//
// ------------------------------------------------------------------------------------------
function PrintCurrentPage()
{
    if( top.mainFrame == null  || top.mainFrame.location.href == null )
        return; 
        
    if( top.topFrame == null  || top.topFrame.location.href == null )
        return; 

    obj = top.topFrame.document.getElementById("print");        
    if( obj == null )
        return;

    var args = getArgs(top.mainFrame);
    if( args.length==0  )
            obj.href = top.mainFrame.location.href + "?print=true";
    else    obj.href = top.mainFrame.location.href + "&print=true";
}

