// Generated by CoffeeScript 1.10.0

/*
LemonLDAP::NG Portal jQuery scripts

Used variables:
 - displaytab
 - choicetab
 - login
 - newwindow
 - antiframe
 */

(function() {
  var activeTimer, antiframe, getOrder, isHiddenFormValueSet, newwindow, ping, restoreOrder, setSelector, translate, translatePage, translationFields,
    indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

  translationFields = {};

  translatePage = function(lang) {
    return $.getJSON(window.staticPrefix + "languages/" + lang + ".json", function(data) {
      translationFields = data;
      $("[trspan]").each(function() {
        return $(this).text(translate($(this).attr('trspan')));
      });
      return $("[trmsg]").each(function() {
        return $(this).text(translate("PE" + ($(this).attr('trmsg'))));
      });
    });
  };

  translate = function(str) {
    if (translationFields[str]) {
      return translationFields[str];
    } else {
      return str;
    }
  };

  newwindow = window.newwindow.match('1') ? true : false;

  antiframe = window.antiframe.match('0') ? false : true;

  activeTimer = window.activeTimer.match('0') ? false : true;

  setSelector = "#appslist";

  getOrder = function() {
    return $.ajax({
      type: "POST",
      url: scriptname,
      data: {
        storeAppsListOrder: $(setSelector).sortable("toArray").join()
      },
      dataType: 'json'
    });
  };

  restoreOrder = function() {
    var IDs, child, i, item, itemID, items, j, len, len1, list, rebuild, savedOrd, v;
    list = $(setSelector);
    if (!((list != null) && appslistorder)) {
      return null;
    }
    IDs = appslistorder.split(',');
    items = list.sortable("toArray");
    rebuild = [];
    for (i = 0, len = items.length; i < len; i++) {
      v = items[i];
      rebuild[v] = v;
    }
    for (j = 0, len1 = IDs.length; j < len1; j++) {
      itemID = IDs[j];
      if (indexOf.call(rebuild, itemID) >= 0) {
        item = rebuild[itemID];
        child = $(setSelector + ".ui-sortable").children("#" + item);
        savedOrd = $(setSelector + ".ui-sortable").children("#" + itemID);
        child.remove();
        $(setSelector + ".ui-sortable").filter(":first").append(savedOrd);
      }
    }
    return 1;
  };

  isHiddenFormValueSet = function(option) {
    return $('#lmhidden_' + option).length;
  };

  ping = function() {
    return $.ajax({
      type: "POST",
      url: scriptname,
      data: {
        ping: 1
      },
      dataType: 'json',
      success: function(data) {
        if (data.auth) {
          return setTimeout('ping();', pingInterval);
        } else {
          return location.reload(true);
        }
      }
    });
  };

  window.ping = ping;

  $(document).ready(function() {
    var action, al, authMenuTabs, back_url, i, j, lang, langs, langs2, len, len1, link, menuIndex, menuTabs, method, nl, nlangs, ref;
    if (antiframe && top !== self) {
      top.location.href = location.href;
    }
    $("#appslist").sortable({
      axis: "y",
      cursor: "move",
      opacity: 0.5,
      revert: true,
      items: "> div.category",
      update: function() {
        return getOrder();
      }
    });
    restoreOrder();
    $("div.message").fadeIn('slow');
    $("input[name=timezone]").val(-(new Date().getTimezoneOffset() / 60));
    menuTabs = $("#menu").tabs({
      active: 0
    });
    menuIndex = $('#menu a[href="#' + displaytab + '"]').parent().index();
    if (menuIndex < 0) {
      menuIndex = 0;
    }
    menuTabs.tabs("option", "active", menuIndex);
    authMenuTabs = $("#authMenu").tabs({
      active: 0
    });
    if (choicetab) {
      authMenuTabs.tabs("option", "active", $('#authMenu a[href="#' + choicetab + '"]').parent().index());
    }
    if (login) {
      $("input[type=password]:first").focus();
    } else {
      $("input[type!=hidden]:first").focus();
    }
    if (newwindow) {
      $('#appslist a').attr("target", "_blank");
    }
    if ($("p.removeOther").length) {
      action = $("form.login").attr("action");
      method = $("form.login").attr("method");
      back_url = "";
      if (action.indexOf("?") !== -1) {
        action.substring(0, action.indexOf("?")) + "?";
      } else {
        back_url = action + "?";
      }
      $("form.login input[type=hidden]").each(function(index) {
        return back_url += "&" + $(this).attr("name") + "=" + $(this).val();
      });
      link = $("p.removeOther a").attr("href") + "&method=" + method + "&url=" + btoa(back_url);
      $("p.removeOther a").attr("href", link);
    }
    if (navigator) {
      langs = [];
      langs2 = [];
      nlangs = [navigator.language];
      if (navigator.languages) {
        nlangs = navigator.languages;
      }
      for (i = 0, len = nlangs.length; i < len; i++) {
        nl = nlangs[i];
        ref = window.availableLanguages;
        for (j = 0, len1 = ref.length; j < len1; j++) {
          al = ref[j];
          if (al === nl) {
            langs.push(al);
          } else if (al.substring(0, 1) === nl.substring(0, 1)) {
            langs2.push(al);
          }
        }
      }
      lang = langs[0] ? langs[0] : langs2[0] ? langs2[0] : 'en';
    } else {
      lang = 'en';
    }
    return translatePage(lang);
  });

}).call(this);
