// $Revision$

Ensembl.Panel.ModalContent = Ensembl.Panel.LocalContext.extend({
  constructor: function () {
    this.base.apply(this, arguments);
    
    Ensembl.EventManager.register('modalFormSubmit', this, this.formSubmit);
  },
  
  init: function () {
    var panel = this;
    
    this.activeLink = false;
    
    this.base();
    
    this.elLk.content = $('.modal_wrapper', this.el);
    
    $('a', this.elLk.links).bind('click', function () {
      if (!$(this).hasClass('disabled')) {
        var link = $(this).parent();
        
        if (!link.hasClass('active')) {
          panel.elLk.links.removeClass('active');
          panel.getContent(link.addClass('active'), this.href);
        }
        
        link = null;
      }
      
      return false;
    });
    
    this.elLk.links.bind('click', function (e) {
      e.stopPropagation();
      return $(this).children('a').trigger('click');
    });
    
    this.live.push(
      $('a.delete_bookmark', this.elLk.content).live('click', function () {
        Ensembl.EventManager.trigger('deleteBookmark', this.href.match(/id=(\d+)\b/)[1]);
      }),
    
      $('form div.select_all input', this.elLk.content).live('click', function () {
        $(this).parents('fieldset').find('input[type=checkbox]').prop('checked', this.checked);
      }),
      
      $('form.wizard input.back', this.elLk.content).live('click', function () {
        $(this).parents('form.wizard').append('<input type="hidden" name="wizard_back" value="1" />').submit();
      })
    );
    
    this.initialize();
  },
  
  initialize: function () {
    this.setSelectAll();
    
    this.elLk.dataTable = $('table.data_table', this.el);
    
    if (this.elLk.dataTable.length) {
      if (!this.dataTableInit) {
        $.extend(this, Ensembl.DataTable);
      }
      
      this.dataTableInit();
    }
    
    Ensembl.EventManager.trigger('validateForms', this.el);
    
    this.addSubPanel();
  },
  
  getContent: function (link, url) {
    this.elLk.content.html('<div class="panel"><div class="spinner">Loading Content</div></div>');
    
    $.ajax({
      url: Ensembl.replaceTimestamp(url),
      dataType: 'json',
      context: this,
      success: function (json) {
        if (json.redirectURL) {
          return this.getContent(link, json.redirectURL);
        }
        
        // Avoid race conditions if the user has clicked another nav link while waiting for content to load
        if (typeof link === 'undefined' || link.hasClass('active')) {
          this.updateContent(json);
        }
      },
      error: function (e) {
        if (e.status !== 0) {
          this.elLk.content.html('<p class="ajax_error>Sorry, the page request failed to load.</p>');
        }
      }
    });
  },
  
  formSubmit: function (form, data) {
    if (!form.parents('#' + this.id).length) {
      return undefined;
    }
    
    if (form.hasClass('upload')) {
      return true;
    }
    
    data = data || form.serialize();
    
    this.elLk.content.html('<div class="panel"><div class="spinner">Loading Content</div></div>');
    
    $.ajax({
      url: form.attr('action'),
      type: form.attr('method'),
      data: data,
      dataType: 'json',
      context: this,
      success: function (json) {
        if (json.redirectURL) {
          return this.getContent(undefined, json.redirectURL);
        }
        
        if (json.success === true) {
          Ensembl.EventManager.trigger('reloadPage');
        } else if (this.el.is(':visible')) {
          this.updateContent(json);
        }
      },
      error: function (e) {
        if (e.status !== 0) {
          this.elLk.content.html('<p class="ajax_error">Sorry, the page request failed to load.</p>');
        }
      }
    });
    
    return false;
  },
  
  updateContent: function (json) {
    if (json.wrapper) {
      this.elLk.content.wrapInner(json.wrapper);
    }
  
    this.elLk.content.html(json.content);
       
    if ($('.modal_reload', this.el).length) {
      Ensembl.EventManager.trigger('queuePageReload', '', false, false, $('.modal_reload', this.el).attr('href'));
    }
    
    this.initialize();
  },
  
  addSubPanel: function () {
    var panel = this;
    
    $('.ajax', this.elLk.content).each(function () {
      Ensembl.EventManager.trigger('createPanel', $(this).parents('.js_panel')[0].id, 'Content');
    });
    
    $('.js_panel', this.elLk.content).each(function () {
      var panelType = $('input.panel_type', this).val();
      
      if (panelType && !(panel instanceof Ensembl.Panel[panelType])) {
        Ensembl.EventManager.trigger('createPanel', this.id, panelType);
      }
    });
  },
  
  setSelectAll: function () {
    $('form div.select_all input', this.elLk.content).prop('checked', function () {
      return $(this).parents('fieldset').find('input[type=checkbox]:not(:checked)').length - 1 <= 0; // -1 for the select_all checkbox itself
    });
  }
});

@@ -4199,32 +4199,33 @@
   var panel 
+ 
 = this;%0A
     %0A    $('
@@ -4204,32 +4204,53 @@
  panel  = this;%0A
+    var params = %5B%5D;%0A
     %0A    $('.aja
@@ -4301,51 +4301,21 @@
     
-Ensembl.EventManager.trigger('createPanel',
+params.push(%5B
  $(t
@@ -4356,16 +4356,18 @@
 Content'
+ %5D
 );%0A    %7D
@@ -4564,24 +4564,116 @@
 Type%5D)) %7B%0A  
+      params.push(%5B this.id, panelType %5D);%0A      %7D%0A    %7D);%0A    %0A    for (var i in params) %7B%0A
       Ensemb
@@ -4700,22 +4700,23 @@
 er('
-create
+destroy
 Panel', 
 this
@@ -4715,44 +4715,117 @@
 l', 
-this.id, panelType);%0A      %7D
+params%5Bi%5D%5B0%5D, 'empty');%0A      Ensembl.EventManager.trigger('createPanel',  params%5Bi%5D%5B0%5D, params%5Bi%5D%5B1%5D);
 %0A    %7D
-);
 %0A  %7D

fix duplicate live events being bound to sub panels