var focus = false;
var timer;
var current_id = "";


function show(id) {
  // si on passe sur une autre rubrique du menu, on cache directement l'ancien
  if (id != current_id) {
    var current_element = document.getElementById(current_id);
    if (current_element) {
      current_element.style.display = "none";
    }
    focus = false;
  }
  if (!focus) {
    current_id = id;
    document.getElementById(id).style.display = "block";
    focus = true;
  }
}

function hide(id) {
  if (focus) {
    timer = setTimeout(function () {
      document.getElementById(id).style.display = "none";
      focus = false;
    }, 500);
  }
}

// cree les contenus des menus dans le dom
function createData() {
  ////////////////////////////////////
  // solution provisoire d'une navigation relative
  var url = location.href;
  var tab = url.split('/');
  var page = tab[tab.length-1];
  var path = ""; // a effacer apres
  if (page != "index.html" && page != "index.html#") {
    path += "../";
  }
  ////////////////////////////////////
  var data = {
    "pcd": {
      "A Brief Look at Corridor D": path + "project/brief.html",
      "Deployment of ERTMS": path + "project/deployment.html",
      "Quality & interoperability": path + "project/quality.html",
      "Project leadership": path + "project/leadership.html"
    },
    "ce": {
      "European policy": path + "eu_context/policy.html",
      "The corridor-based approach": path + "eu_context/corridor_approach.html",
      "European projects coordination": path + "eu_context/coordination.html"
    },
    "edp": {
      "Traffic growth & reliability": path + "challenges_project/traffic_reliability.html",
      "Sustainable mobility": path + "challenges_project/mobility.html",
      "Opening-up of the market": path + "challenges_project/openingup_market.html"
    },
    "mdtq": {
      "Publications": path + "multimedia/publications.html",
      "Newsletter": path + "multimedia/newsletter.html"
    }
  };
  
  var div = document.createElement("div");
  div.id = "contenus_menu";
  
  for (id in data) {
    var ul = document.createElement("ul");
    ul.id = id;
    for (link in data[id]) {
      var li = document.createElement("li");
      var a = document.createElement("a");
      a.href = data[id][link];
      a.appendChild(document.createTextNode(link));
      li.appendChild(a);
      ul.appendChild(li);
    }
    div.appendChild(ul);
  }
  
  document.getElementById("header").appendChild(div);
}

// initialisation des evenements
function initEvents() {
  var pcd = document.getElementById('pcd');
  pcd.onmouseover = function () {
    clearTimeout(timer);
    focus = true;
  };
  
  pcd.onmouseout = function () {
    timer = setTimeout(function () {
      pcd.style.display = "none";
      focus = false;
    }, 500);
  };
  
  var ce = document.getElementById('ce');
  ce.onmouseover = function () {
    clearTimeout(timer);
    focus = true;
  };
  
  ce.onmouseout = function () {
    timer = setTimeout(function () {
      ce.style.display = "none";
      focus = false;
    }, 500);
  };
  
  var edp = document.getElementById('edp');
  edp.onmouseover = function () {
    clearTimeout(timer);
    focus = true;
  };
  
  edp.onmouseout = function () {
    timer = setTimeout(function () {
      edp.style.display = "none";
      focus = false;
    }, 500);
  };
  
  var mdtq = document.getElementById('mdtq');
  mdtq.onmouseover = function () {
    clearTimeout(timer);
    focus = true;
  };
  
  mdtq.onmouseout = function () {
    timer = setTimeout(function () {
      mdtq.style.display = "none";
      focus = false;
    }, 500);
  };
}

window.onload = function () {
  createData();
  initEvents();
};

