// Determine whether we are running local
var sleeper;
var walker;
var local;
if (window.location.hostname == "sleeper") {
  sleeper = true;
} else {
  sleeper = false;
}
if (window.location.hostname == "walker.local") {
  walker = true;
} else {
  walker = false;
}
if (sleeper || walker) {
  local = true;
} else {
  local = false;
}

// Set some dir paths
var rootDir;
var formDir;
var imgDir;
var tmpDir;
if (sleeper) {
  rootDir = "/therwil-flyers.ch/";
} else if (walker) {
  rootDir = "/~laurent/therwil-flyers.ch/";
} else {
  rootDir = "/";
}
formDir = rootDir + "qcubed/forms/";
imgDir = rootDir + "img/";
jsDir = rootDir + "js/";
jsLib = jsDir + "lib/";
tmpDir = rootDir + "tmp/";

// Default editor pop up size
var editorWidth = 600;
var editorHeight = 700;

/**
 * Close a pop up window and reload its opener
 */
function closeWindow() {
  opener.location.reload();
  window.close();
}

/**
 * Returns the drop down value of the currently selected item in the given DIV
 */
function getSelectedItem(divId) {
  var index = document.getElementById(divId).value;
  return document.getElementById(divId).options[index].text;
}

/**
 * Opens an editor window to create a new DB object
 */
function openCreator(objectTypeName, parameters, height) {
  var url = formDir + objectTypeName + "_edit.php";
  openEditorPopUp(url, parameters, height);
  return;
}

/**
 * Opens an editor window to edit the given DB object
 */
function openEditor(objectTypeName, objectId, parameters, height) {
  // The object ID must be greater than 0, else do nothing
  if (objectId < 0) {
    return;
  }
  var url = formDir + objectTypeName + "_edit.php/" + objectId;
  openEditorPopUp(url, parameters, height);
  return;
}

/**
 * Opens an editor pop up with the given height pointing to the given URL with the given paramters
 */
function openEditorPopUp(url, parameters, height) {
  // The parameters argument must be a non-empty array to be considered
  if (parameters != null) {
    var vars = "?";
    for (var parameterName in parameters) {
      vars += parameterName + "=" + parameters[parameterName] + "&";
    }
    // Append the vars to the url
    url += vars.substring(0, vars.length - 1);
  }
  
  if (height == null) {
    height = editorHeight;
  }
  
  // If the script was called in the "editor" pop up, open a new pop up, else reuse the editor pop up
  if (window.name == "editor") {
    var editor = window.open(url, "subeditor", "width=" + editorWidth + ", height=" + height + ", scrollbars=yes");
  } else {
    var editor = window.open(url, "editor", "width=" + editorWidth + ", height=" + height + ", scrollbars=yes");
  }
  
  // Resize the pop up for the case it was already open
  editor.resizeTo(editorWidth, height);
  
  // And finally focus on the pop up
  editor.focus();
  
  return;
}

/**
 * Opens a redirect to an editor window to edit the given DB object
 */
function redirectToEditor(objectTypeName, item, height) {
  var url = formDir + "form_redirect.php";
  var parameters = new Array();
  parameters["form"] = objectTypeName;
  parameters["item"] = item;
  if (item != "" && item.substring(0, 1) != "-" && item.substring(-1, 1) != "-") {
    openEditorPopUp(url, parameters, height);
  }
  return;
}

/**
 * Show the next photo member position input
 */
function showPhotoMemberPosition() {
  var next = $('#nextPhotoMemberPosition').val();
  $('#photoMemberPositionMember' + next).show();
  $('#photoMemberPositionPosition' + next).show();
  $('#nextPhotoMemberPosition').val(parseInt(next) + 1);
}

