// ****************************
// Popup functions
// ****************************

// the functions in this file require the supplementary library lib.js
// These defaults should be changed the way it best fits your site
//var _POPUP_FEATURES = '';
var _POPUP_FEATURES = 'location=0,statusbar=1,scrollbars=1,menubar=0,width=700,height=600,resizable=1';

function raw_popup(url, target, features) {
    // pops up a window containing url optionally named target, optionally having features
    if (isUndefined(features)) features = _POPUP_FEATURES;
    if (isUndefined(target  )) target   = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    return theWindow;
}

function link_popup(src, features) {
    // to be used in an html event handler as in: <a href="..." onclick="link_popup(this,...)" ...
    // pops up a window grabbing the url from the event source's href
    return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}

function event_popup(e) {
    // to be passed as an event listener
    // pops up a window grabbing the url from the event source's href
    link_popup(e.currentTarget);
    e.preventDefault();
}

function event_popup_features(features) {
    // generates an event listener similar to event_popup, but allowing window features
    return function(e) { link_popup(e.currentTarget, features); e.preventDefault() }
}

function help_popup(from) {
    // pops up a help window at the center of the screen
    var position = "right";
    
    if (position == "center") {
            _width  = 700;
            _height = 600;
            _left   = (screen.width-_width)/2;
            _top    = (screen.height-(_height+110))/2; 
    }
    if (position == "right") {
            _width  = 500;
            _height = (screen.height);
            _left   = (screen.width - _width);
            _top    = 0;
    }
    
    features = 'location=0,statusbar=1,scrollbars=1,menubar=0,' + 
               'width=' + _width + ',height=' + _height + ',resizable=1' +
               ',left=' + _left + ',top=' + _top;
    
    window.open(from, '_blank', features);
    return false;
}

// ****************************
//  button-logic
// ****************************

function isPromenuVisible() {
    if (top.frameMain.frameProMain == null) {
            return false;
    } else {
            return true;
    }
}

function isStatsMode() {
    if (isPromenuVisible()) {
            var mainframe = top.frameMain.frameProMain;
    } else {
            var mainframe = top.frameMain;
    }
    var location = mainframe.location.href;
    
    return (location.indexOf("logs")>-1);
}

function switchPromenu() {
    // bepaal link: 
    // als we in 'stats' mode zitten, en het promenu staat aan, dan moeten we linken naar 'stats'
    // als we in 'stats' mode zitten, en het promenu staat uit, dan moeten we linken naar 'stats + promenu'
    // als we niet in 'stats' mode zitten, en het promenu staat aan, dan moeten we linken naar 'basicmenu'
    // als we niet in 'stats' mode zitten, en het promenu staat uit, dan moeten we linken naar 'promenu'
    var link = '';
    var linkname = "";
    if (isStatsMode()) {
            // we zitten in 'stats' mode
            if (isPromenuVisible()) {
                    link = "../../logs/index.php";
            } else {
                    link = "frames_stats.php"; 
            }
    } else {
            // we zitten niet in 'stats' mode
            if (isPromenuVisible()) {
                    link = "basicmenu.php";
            } else {
                    link = "frames_promenu.php"; 
            }
    }  
  
    if (isPromenuVisible()) {
      // verberg het promenu
      linkSwitch = "<a class='icon icon_switch' href='" + link + "' onclick='updateButtons()' target='frameMain' title='Open Basicmenu'>Basicmenu</a>";
    } else {
      // toon het promenu
      linkSwitch = "<a class='icon icon_switch' href='" + link + "' onclick='updateButtons()' target='frameMain' title='Open Promenu'>Promenu</a>";
    }  
    document.getElementById('switch').innerHTML = linkSwitch;
}

function updateButtons() {
        // direct uitvoeren
        switchPromenu(); 
        switchStats();
        
        // en over een tijdje eventueel corrigeren... (promenu laden kan LANG duren)
        setTimeout("switchPromenu();switchStats();", 100);
        setTimeout("switchPromenu();switchStats();", 200);
        setTimeout("switchPromenu();switchStats();", 500);
        setTimeout("switchPromenu();switchStats();", 1000);
        setTimeout("switchPromenu();switchStats();", 2000);
        setTimeout("switchPromenu();switchStats();", 5000);
        setTimeout("switchPromenu();switchStats();", 10000);
}

function switchStats() {
  
    // bepaal doel-frame: als we in 'promenu' mode zitten, dan is het doel van de stats-knop het frame 'frameProMain'
    // als we in 'basic' mode zitten, dan is het doel van de stats-knop het frame 'frameMain'
    if (isPromenuVisible()) {
            var statsTarget = 'frameProMain'; 
    } else {
            var statsTarget = 'frameMain'; 
    }
  
    if (isStatsMode()) {
      // toon knop om te schakelen van stats naar wabbit-mode
      linkSwitch = "<a class='icon icon_stats' href='basicmenu.php' onclick='updateButtons()' target='" + statsTarget + "' title='Sluit statistieken'>Stats</a>";
    } else {
      // toon knop om te schakelen van wabbit-mode naar stats
      linkSwitch = "<a class='icon icon_stats' href='../../logs/index.php' onclick='updateButtons()' target='" + statsTarget + "' title='Open statistieken'>Stats</a>";
    }  
    document.getElementById('stats').innerHTML = linkSwitch;
}


// --------------------------------------
function navigatePromenu(id) {

        if (isStatsMode()) {
                // als de statistieken open staan, dan navigeert het promenu naar een nieuwe pagina binnen de statistieken
                top.frameMain.frameProMain.location = "../../logs/index.php?id=" + id;
        } else {
                // ... en anders laat 'ie gewoon een pagina van de site zien
                top.frameMain.frameProMain.location = "../../../index.php?id=" + id;
        }
        
        // highlight geklikte item in promenu
        select(id);
}