/**
 * set some variables
 */
 
// today's date
var today = new Date();

// number of days since 1/1/1970
var t = Math.floor(today.getTime()/(1000 * 60 * 60 * 24));

// date of the end of the exhibition
var endofex = new Date();
endofex.setMonth(3);
endofex.setDate(13);
endofex.setYear(2005);
endofex.setHours(0, 0, 0);

// popup window object
var popup = null;

// opens the popup
function popit()
{
    popup = window.open('popup.html','popup','width=300,height=360');
    if (popup.focus) {
        popup.focus();
    }
}

/**
 * top level menu items
 */
var menus = new Array("turksMenu", "ticketsMenu", "eventsMenu", "raMenu", "educationMenu", "sponsorsMenu", "shopMenu", "pressMenu");
/**
 * switch to provide different behaviours for menus
 */
setCookie("hideOnClick", "false");
/**
 * write a style in the document to hide all submenus for CSS capable browsers
 */
if (document.getElementById || document.all || document.layers) {
    document.write('<style type="text/css">ul.ex {display:none}</style>');
}
/**
 * load and save states when document is loaded/unloaded
 */
window.onload = loadState;
window.onunload = saveState;
/**
 * rollover functions
 */
var imgOver = new Object();
imgOver.turks = new Image(175,14);
imgOver.turks.src = 'images/nav/turks_on.gif';
imgOver.events = new Image(175,14);
imgOver.events.src = 'images/nav/events_on.gif';
imgOver.tickets = new Image(175,14);
imgOver.tickets.src = 'images/nav/tickets_on.gif';
imgOver.ra = new Image(175,30);
imgOver.ra.src = 'images/nav/ra_on.gif';
imgOver.education = new Image(175,14);
imgOver.education.src = 'images/nav/education_on.gif';
imgOver.sponsors = new Image(175,14);
imgOver.sponsors.src = 'images/nav/sponsors_on.gif';
var imgOut = new Object();
imgOut.turks = new Image(175,14);
imgOut.turks.src = 'images/nav/turks_off.gif';
imgOut.events = new Image(175,14);
imgOut.events.src = 'images/nav/events_off.gif';
imgOut.tickets = new Image(175,14);
imgOut.tickets.src = 'images/nav/tickets_off.gif';
imgOut.ra = new Image(175,30);
imgOut.ra.src = 'images/nav/ra_off.gif';
imgOut.education = new Image(175,14);
imgOut.education.src = 'images/nav/education_off.gif';
imgOut.sponsors = new Image(175,14);
imgOut.sponsors.src = 'images/nav/sponsors_off.gif';
//image swapping function
function swapImage(id,oo){
    if(document.images){
	      var targetImg;
		    //set the targetImg variable to point to the image.
		    //This is done differently in Netscape 4.x
	      if (document.layers) {
		        targetImg = document.layers["menu"].document.images["btn"+id];
		    } else {
		        targetImg = document.images["btn"+id];
		    }
		    //make sure we have a valid reference before swapping it
		    if (typeof targetImg == "object") {
		        targetImg.src = oo?imgOver[id].src:imgOut[id].src;
		    }
    }
};
/**
 * expands a submenu
 */
function expand(which) {
    if (menuObj = xGetElementById(which)) {
        if (menuObj.style && xDef(menuObj.style.display)) {
            if (getCookie("hideOnClick") == "true") {
                var d = (menuObj.style.display == 'none' || menuObj.style.display == '')? 'block': 'none';
                if (which.indexOf('.') == -1) {
                    hideAll();
                }
                menuObj.style.display = d;
            } else {
                if (menuObj.style.display == 'none' || menuObj.style.display == '') {
                    menuObj.style.display = 'block';
                } else {
                    menuObj.style.display = 'none';
                }
            }
        }
    }
}
/**
 * hides all submenus
 */
function hideAll() {
    if (arguments.length != 1) {
        for (var i = 0; i < menus.length; i++) {
            if (menuObj = xGetElementById(menus[i])) {
                if (menuObj.style && xDef(menuObj.style.display)) {
                    menuObj.style.display = 'none';
                    hideRecursive(i, '', true);
                }
            }
        }
    }
}
/**
 * recursive funtion to hide all submenus and sub-submenus etc.
 */
function hideRecursive(idx, lvl, hide) {
    var d = hide? 'none': 'block';
    for (var l = 97; l < 123; l++) {
        if (menuObj = xGetElementById(menus[idx] + '.' + lvl + String.fromCharCode(l))) {
            if (menuObj.style && xDef(menuObj.style.display)) {
                menuObj.style.display = d;
                hideRecursive(idx, lvl + String.fromCharCode(l), hide);
            }
        }
    }
}
/**
 * expand all menus
 */
function expandAll() {
    for (var i = 0; i < menus.length; i++) {
        if (menuObj = xGetElementById(menus[i])) {
            if (menuObj.style && xDef(menuObj.style.display)) {
                menuObj.style.display = 'block';
                hideRecursive(i, '', false);
            }
        }
    }
}
/**
 * toggle the different menu behaviour
 */
function toggle() {
    var msg = (getCookie("hideOnClick") == "true")? 'All submenus stay open until closed': 'Any open submenus will now collapse when another is activated';
    var biscuit = (getCookie("hideOnClick") == "true")? "false": "true";
    setCookie("hideOnClick", biscuit);
    alert(msg);
}
/**
 * state of menus is stored in this variable as a comma separated list of all menus which are displayed
 */
var menuState = '';
/**
 * save state of menus
 */
function saveState()
{
    for (var i = 0; i < menus.length; i++) {
        if (menuObj = xGetElementById(menus[i])) {
            if (menuObj.style && xDef(menuObj.style.display)) {
                if (menuObj.style.display == 'block') {
                    menuState += menus[i] + ',';
                }
            }
        }
        saveRecursive(i, '');
    }
    setCookie("menuState", menuState);
}
/**
 * recursive funtion to save the state of all submenus and sub-submenus etc.
 */
function saveRecursive(idx, lvl) {
    for (var l = 97; l < 123; l++) {
        if (menuObj = xGetElementById(menus[idx] + '.' + lvl + String.fromCharCode(l))) {
            if (menuObj.style && xDef(menuObj.style.display)) {
                if (menuObj.style.display == 'block') {
                    menuState += menus[idx] + '.' + lvl + String.fromCharCode(l) + ',';
                }
                saveRecursive(idx, lvl + String.fromCharCode(l));
            } else {
                return;
            }
        }
    }
}
/**
 * load function called when document has loaded - expands the menus to the saved state
 */
function loadState()
{
    if (today.getTime() < endofex.getTime()) {
        // exhibition is still on
        var st = getCookie("popup");
        if ((st == null || st < t) && st != 'dontshowagain') {
            // no cookie set yet, or the cookie is a day old, or disabled by the user
            setCookie('popup', t, endofex);
            popit();
        }
    }
    var s = getCookie("menuState");
    if (s != null) {
        var active = s.split(",");
        for (var i = 0; i < active.length; i++) {
            expand(active[i]);
        }
    }
    deleteCookie("menuState");
}
        
/**
 * sets a cookie on the client
 *
 * an argument defaults when it is assigned null as a placeholder
 * a null placeholder is not required for trailing omitted arguments
 * @param string name - name of the cookie
 * @param string value - value of the cookie
 * @param date [expires] - expiration date of the cookie (defaults to end of current session)
 * @param string [path] - path for which the cookie is valid (defaults to path of calling document)
 * @param string [domain] - domain for which the cookie is valid (defaults to domain of calling document)
 * @param boolean [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
 */
function setCookie(name, value, expires, path, domain, secure) {
    path = "/";
    var curCookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
    document.cookie = curCookie;
}

/**
 * gets the value of a cookie saved on the client
 * @param string name - name of the desired cookie
 * @return string containing value of specified cookie or null if cookie does not exist
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) {
            return null;
        }
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}
/**
 * delete a cookie
 * 
 * path and domain default if assigned null or omitted if no explicit argument proceeds
 * @param string name - name of the cookie
 * @param string [path] - path of the cookie (must be same as path used to create cookie)
 * @param string [domain] - domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" + 
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
/**
 * fix the date given to a cookie
 *
 * hand all instances of the Date object to this function for "repairs"
 * @param string date - any instance of the Date object
 */
function fixDate(date) {
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0) {
        date.setTime(date.getTime() - skew);
    }
}
/**
 * utility functions taken from the x library (http://www.cross-browser.com)
 * Copyright (c) 2004 Michael Foster, Licensed LGPL (gnu.org)
 */
function xGetElementById(e) {
    if (typeof(e) != 'string') {
        return e;
    }
    if (document.getElementById) {
        e = document.getElementById(e);
    } else if (document.all) {
        e = document.all[e];
    } else if (document.layers) {
        e = xLayer(e);
    } else {
        e = null;
    }
    return e;
}
function xLayer(id,root) {
    var i, layer, found = null;
    if (!root) {
        root = window;
    }
    for (i = 0; i < root.document.layers.length; i++) {
        layer = root.document.layers[i];
        if (layer.id == id) {
            return layer;
        }
        if (layer.document.layers.length) {
            found = xLayer(id, layer);
        }
        if (found) {
            return found;
        }
    }
    return null;
}
function xDef() {
    for (var i=0; i < arguments.length; ++i) {
        if (typeof(arguments[i]) == 'undefined') {
            return false;
        }
    }
    return true;
}
