// Category := top level menu item
// Page     := page item either on top level or sublevel belonging to one category

var currentCategory;
var storeAcrossPages; 
var storeAcrossSessions;

var menuState = new Array(6);
menuState["Startseite"]          = "none";
menuState["Auftrag"]             = "close";
menuState["Shop"]                = "none";
menuState["Kooperationspartner"] = "none";
menuState["Uns"]                 = "none";
menuState["Kontakt"]             = "close";

function setCurrentCategory(value)
{
	currentCategory = value;
}

function setStoreAcrossPages(value)
{
	storeAcrossPages = value;
}

function setStoreAcrossSessions(value)
{
	storeAcrossSessions = value;
}

function switchlayer(Layer_Name)
{
  var GECKO = document.getElementById? 1:0 ;
  var NS = document.layers? 1:0 ;
  var IE = document.all? 1:0 ;
 
  if (GECKO)
       {document.getElementById(Layer_Name).style.display=
	   (document.getElementById(Layer_Name).style.display=='block') ? 'none' : 'block';}
  else if (NS)
       {document.layers[Layer_Name].display=(document.layers[Layer_Name].display==
	   'block') ? 'none' : 'block';}
  else if (IE)
       {document.all[Layer_Name].style.display=(document.all[Layer_Name].style.display==
	   'block') ? 'none' : 'block';}
}

function TimeStringFix(date)
{
    var origin=new Date(1970,0,1,12);
    if (origin.toGMTString().indexOf("02")>0)
    {
        date.setTime(date.getTime()-1000*60*60*24);
    }
    return date.toGMTString();
}

function writeCookie(name,value,expires,path,domain,secure) {
    var theItem = name + "=" + escape(value);
    if (expires)
    {
        theItem += "; expires=" + fixedGMTString(expires);
    }
    if (path)
    {
        theItem += "; path=" + path;
    }
    if (domain)
    {
        theItem += "; domain=" + domain;
    }
    if (secure)
    {
        theItem += "; secure";
    }
    document.cookie = theItem;
}

function readCookie(name) {
    var theItem = document.cookie;

    // search name-vlaue-pair
    var posName = theItem.indexOf("; " + name + "=");
    if (posName == -1) {
        if (theItem.indexOf(name + "=") == 0)
        {
            posName = 0;
        }
        else
        {
            return null;
        }
    }

    // get start- and end-index of value
    var startPos = theItem.indexOf("=", posName)+1;
    var endPos   = theItem.indexOf(";", posName+1);
    if (endPos == -1)
    {
        endPos = theItem.length;
    }

    // read value
    var value = theItem.substring(startPos, endPos);
    return unescape(value);
}

function checkCookie() {
    document.cookie = "testCookie=test";
    var testCookie = document.cookie

    if (testCookie && testCookie.indexOf("testCookie") > -1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function storeMenu()
{
    if(checkCookie())
    {
        // for keeping menu state across sessions
        var currentDate = new Date();
        var expDate;
        if( storeAcrossSessions )
        {
            // store for a thousand years
            expDate = new Date(currentDate.getTime() + 1000*60*60*24*365);
        }
        else
        {
            expDate = null;
        }

        writeCookie("Startseite",          menuState["Startseite"],          expDate,"/",null,null);
        writeCookie("Auftrag",             menuState["Auftrag"],             expDate,"/",null,null);
        writeCookie("Shop",                menuState["Shop"],                expDate,"/",null,null);
        writeCookie("Kooperationspartner", menuState["Kooperationspartner"], expDate,"/",null,null);
        writeCookie("Uns",                 menuState["Uns"],                 expDate,"/",null,null);
        writeCookie("Kontakt",             menuState["Kontakt"],             expDate,"/",null,null);
    }
}

function readMenu()
{
    if( checkCookie() && storeAcrossPages )
    {
        if( readCookie("Startseite") )
        {
            menuState["Startseite"]          = readCookie("Startseite");
        }
        if( readCookie("Auftrag") )
        {
            menuState["Auftrag"]             = readCookie("Auftrag");
        }
        if( readCookie("Shop") )
        {
            menuState["Shop"]                = readCookie("Shop");
        }
        if( readCookie("Kooperationspartner") )
        {
            menuState["Kooperationspartner"] = readCookie("Kooperationspartner");
        }
        if( readCookie("Uns") )
        {
            menuState["Uns"]                 = readCookie("Uns");
        }
        if( readCookie("Kontakt") )
        {
            menuState["Kontakt"]             = readCookie("Kontakt");
        }
    }

    if( "none" != menuState[currentCategory] )
    {
        menuState[currentCategory] = "open";
    }

    storeMenu();
}

function switchmenu(theMenu,theState)
{
    menuState[theMenu]=theState;
    storeMenu();
    switchlayer(theMenu + "_open");
    switchlayer(theMenu + "_close");
}


function initMenu()
{
    // initially, everything is open

    // switch layers that shall be closed
    readMenu();

    if( "close" == menuState["Auftrag"] )
    {
        switchlayer("Auftrag_open");
        switchlayer("Auftrag_close");
    }

    if( "close" == menuState["Kontakt"] )
    {
        switchlayer("Kontakt_open");
        switchlayer("Kontakt_close");
    }

    return true;
}

