// Generated by CoffeeScript 1.3.3
(function() {
  var resizeInProgress,
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  $(window).unload(function() {
    return $AL.helpWindow.close();
  });

  resizeInProgress = false;

  $(window).resize(function() {
    return $AL.resize_loop();
  });

  window.$AL.resizeRoot = function() {
    var ALRootHeight, midPanelHeight, winHeight, winWidth;
    winHeight = $(window).height();
    winWidth = $(window).width();
    ALRootHeight = winHeight * .85;
    midPanelHeight = ALRootHeight * .95;
    $('#layout-root').height(ALRootHeight);
    $('#midPanel').height(midPanelHeight);
    $('.tab-main').height(midPanelHeight * .90);
    return resizeInProgress = false;
  };

  $AL.resize_loop = function() {
    if (!resizeInProgress) {
      resizeInProgress = true;
      return _.delay(function() {
        return window.$AL.resizeRoot();
      }, 500);
    }
  };

  $AL.Deferreds = {};

  $AL.Promises = {};

  $AL.Deferreds._tab_rendered = $.Deferred();

  $AL.Promises.tab_rendered = $AL.Deferreds._tab_rendered.promise();

  $AL.Deferreds._basetabs_rendered = $.Deferred();

  $AL.Promises.basetabs_rendered = $AL.Deferreds._basetabs_rendered.promise();

  _.delay(function() {
    $('#upgradeBtn').click(function() {
      return $AL.upgrade();
    });
    $('#newsBtn').click(function() {
      return $AL.news();
    });
    $('#cdxHref').click(function() {
      return $AL.showCDX();
    });
    $('#helpAL').click(function() {
      return $AL.showHelp('AL');
    });
    return $AL.resize_loop();
  }, 1000);

  $AL.upgrade = function() {
    return false;
  };

  $AL.news = function() {
    $AL.main_tab_set.add_tab({
      tab_name: 'News',
      view: $AL.newsView,
      route: 'news'
    });
    $AL.newsView.render();
    return false;
  };

  $AL.showCDX = function() {
    if ($.isWindow($AL.cdxWindow)) {
      $AL.cdxWindow.focus();
    } else {
      $AL.cdxWindow = window.open($AL.configDict.get('CDX_URL'));
    }
  };

  $AL.showHelp = function(section) {
    $AL.helpWindow = window.open('/AL_help?section=' + section, 'Anaconda Launcher Help', 'width=600,height=600,scrollbars=yes,resizable=yes,location=no,status=yes');
    event.preventDefault();
    return false;
  };

  $AL.ConfigDict = (function(_super) {

    __extends(ConfigDict, _super);

    function ConfigDict() {
      return ConfigDict.__super__.constructor.apply(this, arguments);
    }

    ConfigDict.prototype.initialize = function(attrs, options) {
      return ConfigDict.__super__.initialize.call(this, attrs, options);
    };

    ConfigDict.prototype.defaults = {
      'def': 'val'
    };

    return ConfigDict;

  })(Backbone.Model);

  $(function() {
    var WorkspaceRouter;
    $AL.utility = {
      instantiate_main_tab_set: function() {
        if ($AL.Deferreds._tab_rendered.isResolved()) {
          return;
        }
        return $.when($AL.layout_render).then(function() {
          $("#layout-root").prepend($AL.layout_render.el);
          $AL.main_tab_set = new $AL.TabSet({
            el: $("#midPanel"),
            tab_view_objs: []
          });
          $AL.Deferreds._tab_rendered.resolve();
          return null;
        });
      },
      instantiate_base_tabs: function() {
        if ($AL.Deferreds._basetabs_rendered.isResolved()) {
          return;
        }
        this.instantiate_main_tab_set();
        $AL.main_tab_set.add_tab({
          tab_name: 'Cluster',
          view: $AL.clusterView,
          route: 'cluster'
        });
        $AL.clusterView.render();
        $AL.main_tab_set.add_tab({
          tab_name: 'Config',
          view: $AL.configView,
          route: 'config'
        });
        $AL.configView.render();
        $AL.main_tab_set.activate('cluster');
        $AL.Deferreds._basetabs_rendered.resolve();
        return null;
      }
    };
    WorkspaceRouter = Backbone.Router.extend({
      routes: {
        "launcher": "load_default_document",
        "launcher/": "load_default_document"
      },
      load_default_document: function() {
        $.ajaxSetup({
          async: false
        });
        $.when($.getJSON('/version')).then(function(data) {
          return $AL.version = data.version;
        });
        $.when($.getJSON('/config')).then(function(data) {
          return $AL.configDict.set(data, '{silent: true}');
        });
        $.ajaxSetup({
          async: true
        });
        $AL.utility.instantiate_base_tabs();
        return $("#versionLbl").prepend(' ' + $AL.version);
      }
    });
    $AL.configDict = new $AL.ConfigDict();
    $AL.router = new WorkspaceRouter();
    $AL.layout = new $AL.LayoutView();
    $AL.configView = new $AL.ConfigView();
    $AL.clusterView = new $AL.ClusterView();
    $AL.layout_render = $AL.layout.render();
    $.when($AL.layout_render).then(function() {
      $("#layout-root").prepend($AL.layout_render.el);
    });
    console.log("Backbone history start");
    return Backbone.history.start({
      pushState: true
    });
  });

  $AL.dialog = function(dialogTitle, dialogText, okFunc, cancelFunc) {
    var modalParams;
    modalParams = {
      'dialogTitle': dialogTitle,
      'dialogText': dialogText
    };
    $('#confirmModal').empty().prepend(_.template($('#modal-confirm-template').html(), modalParams));
    if (typeof okFunc === 'function') {
      $('#modalConfirmBtn').click(okFunc);
    }
    if (typeof cancelFunc === 'function') {
      $('#modalCancelBtn').click(cancelFunc);
    }
    return $('#confirmModal').modal('show');
  };

  $AL.dialog.updateTitle = function(msg) {
    if (msg === void 0) {
      msg = '';
    }
    $('#modalTitle').empty().append(msg);
  };

  $AL.dialog.dismiss = function() {
    $('#confirmModal').modal('hide');
  };

  $AL.dialog.removeFooter = function() {
    $('#modalFooter').remove();
  };

  $AL.dialog.showprogress = function(msg) {
    if (msg === void 0) {
      msg = '';
    }
    return $('#modalBody').empty().append(_.template($('#modal-progress-template').html(), {
      msg: msg
    }));
  };

}).call(this);
