/**
 *  Entry point for dashboard
 */


$(function() {

  var Dashboard = cdb.core.View.extend({

    el: document.body,

    events: {},

    initialize: function() {
      this._initModels();
      this._initViews();
      this._bindEvents();
    },

    _initModels: function() {
      this.user = new cdb.admin.User(this.options.user_data);
      this.tables = new cdb.admin.Tables();
      this.tags = new cdb.admin.Tags();
      this.first_time = true;

      // The user model has to update every time the table model
      this.tables.bind('add remove reset', function(){
        if (!this.first_time)
          this.user.fetch();

        this.first_time = false;
      }, this);
      this.tables.bind('error', function(e) {
        cdb.log.info("error", e);
      });

      // remove the style from redis
      // this is a temporal hack until this was managed by 
      // the backend
      /*
      this.tables.bind('destroy', function(table) {
        var protocol = cdb.config.get("tiler_port") == 443 ? 'https': 'http';
        var url = protocol + '://' + this.user.get('name') + "." + cdb.config.get("tiler_domain") + ':' + cdb.config.get("tiler_port") + '/tiles/' + table.get('name') + '/style';
        $.ajax({
          url: url,
          type: 'DELETE',
          crossOrigin: true,
        });
      }, this);
      */
    },

    _initViews: function() {
      // Table list
      this.tableList = new cdb.admin.dashboard.TableList({
        el: this.$('#tablelist'),
        model: this.tables,
        config: this.options.config,
        user: this.user
      });

      // User data
      this.user_stats = new cdb.admin.dashboard.UserStats({
        el: this.$('div.subheader'),
        model: this.user
      });

      this.user_stats.render();

      // Choose sceneario
      this.scenario = new cdb.admin.dashboard.Scenario({
        el: this.$el,
        model: this.user
      });

      // Search form
      var search_form = this.search_form = new cdb.ui.common.SearchView({
        el: this.$('header ul li.search')
      });

      // User menu
      var user_menu = this.user_menu = new cdb.admin.DropdownMenu({
        target:         $('a.account'),
        host:           config.account_host,
        username:       username,
        template_base:  'common/views/settings_item'
      });
      cdb.god.bind("closeDialogs", user_menu.hide, user_menu);
      this.$el.append(this.user_menu.render().el);

      // Background Importer
      var bkg_importer = this.bkg_importer = new cdb.ui.common.BackgroundImporter({
        template_base: 'common/views/background_importer'
      });
      this.$el.append(this.bkg_importer.render().el);

      // Paginator
      var paginator = this.paginator = new cdb.admin.dashboard.DashboardPaginator({
        el: this.$("div.paginator"),
        model: this.tables
      });

      // Tables tags
      var tagsView = this.tagsView = new cdb.admin.dashboard.TagsView({
        el: this.$('aside div.content ul:eq(0)'),
        tables: this.tables,
        model: this.tags
      });

      // Tipsy
      this.$el.find("a.tooltip").tipsy({gravity: 's', fade:true, live:true});

      // Create new table view
      var createTable = this.createTable = new cdb.admin.dashboard.CreateTable({
        el:       this.$el,
        importer: this.bkg_importer,
        tables:   this.tables,
        model:    this.user
      });

      // global click
      enableClickOut(this.$el);
    },


    /**
     *  Calculate scroll pagination and moves the aside
     *  when it is necessary
     */
    _whenScroll: function(ev) {
      var $aside      = this.$el.find('aside')
        , $list       = this.$el.find('section.tables')
        , $warning    = this.$el.find("section.warning")
        , scrolled    = $(ev.target).scrollTop()
        , aside_h     = $aside.outerHeight()
        , list_pos    = $list.offset().top
        , list_height = $list.outerHeight();

      if ( scrolled > list_pos ) {
        if ((scrolled + aside_h) < (list_pos + list_height)) {
          $aside
            .addClass("moving")
            .css({
              marginTop: - list_pos - ($warning.hasClass("visible") ? $warning.outerHeight() : 0)
            })
            .removeClass("bottom");
        } else {
          $aside
            .addClass("bottom")
            .removeClass("moving")
            .css({
              marginTop: list_height - aside_h
            })
        }
      } else {
        $aside
          .removeClass("moving bottom")
          .css({
            marginTop: 0
          })
      }
    },    


    _bindEvents: function() {
      _.bindAll(this, "_whenScroll");

      this.paginator.bind('loadingPage', this.tableList.clear);

      // Move aside when scrolls
      $(window).scroll(this._whenScroll);
    }

  });

  var DashboardRouter = Backbone.Router.extend({

    routes: {
      '':                 'index',
      'page/:p':          'paginate',
      'tag/:tag/:p':      'searchTag',
      'search/:query/:p': 'searchQuery'
    },

    initialize: function() {
      window.dashboard.tables.options.bind("change", this.update ,this);
    },

    update: function() {
      var hash = window.location.hash.split("/");
      hash[hash.length - 1] = window.dashboard.tables.options.get("page");
      this.navigate(hash.join("/"));
    },

    index: function() {
      window.dashboard.tables.options.set({
        "tag_name"  : "",
        "q"         : "",
        "page"      : 1
      });
    },
    searchTag: function(tag,p) {
      window.dashboard.tables.options.set({
        "tag_name"  : tag,
        "page"      : p,
        "q"         : ""
      });
    },
    searchQuery: function(query,p) {
      window.dashboard.tables.options.set({
        "tag_name"  : "",
        "page"      : p,
        "q"         : query
      });
    },
    paginate: function(p) {
      window.dashboard.tables.options.set({
        "tag_name"  : "",
        "page"      : p,
        "q"         : ""
      });
    }
  });

  cdb.init(function() {
    cdb.templates.namespace = 'cartodb/';
    var dashboard = new Dashboard({
      user_data: user_data,
      config: config
    });

    // Expose to debug
    window.dashboard = dashboard;

    // Routing
    var router = new DashboardRouter();
    Backbone.history.start();

    // No route => tables fetch
    if (window.dashboard.tables.options.get("tag_name") === "" &&
      window.dashboard.tables.options.get("q") === "") {
      window.dashboard.tables.fetch();
    }
  });

});

@@ -777,573 +777,8 @@
 );%0A%0A
-      // remove the style from redis%0A      // this is a temporal hack until this was managed by %0A      // the backend%0A      /*%0A      this.tables.bind('destroy', function(table) %7B%0A        var protocol = cdb.config.get(%22tiler_port%22) == 443 ? 'https': 'http';%0A        var url = protocol + '://' + this.user.get('name') + %22.%22 + cdb.config.get(%22tiler_domain%22) + ':' + cdb.config.get(%22tiler_port%22) + '/tiles/' + table.get('name') + '/style';%0A        $.ajax(%7B%0A          url: url,%0A          type: 'DELETE',%0A          crossOrigin: true,%0A        %7D);%0A      %7D, this);%0A      */%0A
     

remove the style is done in backend