var planner_launch_locked = false;
var planner_lock_timeout = 5 * 1000;
var planner_last_lauch = 0;

var planner_width_fraction = 0.8;
var planner_height_fraction = 0.8;
var planner_width_min = 1024;
var planner_height_min = 620;

function open_planner(projectid, levelkey, baseurl, mode) {
  // In FF, rapid "window.open"s with same window name opens several windows.
  // We only want one (even if user double- or multi-clicks).
  // Workaround: locking flag with timeout
  now = new Date().getTime();
  if (planner_launch_locked==true){
    // A launch is already underway.
    if (now - planner_last_lauch < planner_lock_timeout){
      // -and it's not stale either.
      return; // Ignore this request.
    }
  }
  // ASSERT: no non-stale launch underway.
  // Get the lock:
  planner_launch_locked = true;
  planner_last_lauch = now;
  // All clear.
  
  if (!baseurl) {baseurl = "http://planner.viseno.com/?ctxt=viseno_no";}

  var projparam = "";
  var sep = "#?";
  if (projectid) {
    projparam = sep + "pid=" + projectid;
    sep = "&";
  } 
  if (levelkey) {
    projparam += $sep + "lkey=" + levelkey; 
    sep = "&";
  }
  if (mode) {
    projparam += sep + "mode=" + mode;
    sep = "&";
  }

  dest = baseurl + projparam;
  
  var w = screen.availWidth * planner_width_fraction;
  if (w < planner_width_min) w = planner_width_min;
  if (w > screen.availWidth) w = screen.availWidth;
  
  var h = screen.availHeight * planner_height_fraction;
  if (h < planner_height_min) h = planner_height_min;
  if (h > screen.availheight) h = screen.availHeigth;
  
  var top = (screen.availHeight-h) /2;
  var left = (screen.availWidth-w) /2;
  
  plannerwindow = window.open(dest, "visenoplanner", "resizable, width="+w+",height="+h+",left="+left+",top="+top);
  try {
    plannerwindow.focus();
  } 
  catch (e) {}

  planner_launch_locked = false; // open requests are allowed again.
}

