(function () {
  try {

    function checkSettings(varName, sectionName) {
      if (!app.settings.haveSetting(varName, sectionName)) {
        return false;
      }
      savedSetting = app.settings.getSetting(varName, sectionName);
      if (savedSetting == "false") {
        return false;
      } else(savedSetting == "true") {
        return true;
      }
    }



    app.beginUndoGroup("Select Odd Layers");
    var comp = app.project.activeItem;
    if (!comp || !(comp instanceof CompItem)) {
      alert("Please select a composition.");
      return;
    }
    var comp = app.project.activeItem;
    var layers = comp.layers;

    for (var i = 1; i <= layers.length; i++) { // Deselect all the current layers
      comp.layers[i].selected = false;
    }

    // Read settings
    var order = checkSettings("Odd-Even", "order");

    if (order) {
      for (var i = 1; i <= layers.length; i++) {
        if (i % 2 == 1) {
          comp.layers[i].selected = true;
        }
        app.settings.saveSetting("Odd-Even", "order", "false");
      } 
    } else {
       for (var i = 1; i <= layers.length; i++) {
        if (i % 2 == 0) {
          comp.layers[i].selected = true;
        }
        app.settings.saveSetting("Odd-Even", "order", "true");
      }   
    }
  } catch (err) {
    alert(err);
  } finally {
    app.endUndoGroup();
  }
})();