// $Revision$

Ensembl.LayoutManager = new Base();

Ensembl.LayoutManager.extend({
  constructor: null,
  
  /**
   * Creates events on elements outside of the domain of panels
   */
  initialize: function () {
    this.id = 'LayoutManager';
    
    Ensembl.EventManager.register('reloadPage',    this, this.reloadPage);
    Ensembl.EventManager.register('validateForms', this, this.validateForms);
    Ensembl.EventManager.register('makeZMenu',     this, this.makeZMenu);
    Ensembl.EventManager.register('relocateTools', this, this.relocateTools);
    Ensembl.EventManager.register('hashChange',    this, this.hashChange);
    
    $('#local-tools > p').show();
    
    $('#header a:not(#tabs a)').addClass('constant');
    
    $('.modal_link').show().live('click', function () {
      if (Ensembl.EventManager.trigger('modalOpen', this)) {
        return false;
      }
    });
    
    $('.popup').live('click', function () {
      if (window.name.match(/^popup_/)) {
        return true;
      }
      
      window.open(this.href, 'popup_' + window.name, 'width=950,height=500,resizable,scrollbars');
      return false;
    });
    
    $('a[rel="external"]').live('click', function () { 
      this.target = '_blank';
    });
    
    $('form.check').validate().live('submit', function () {
      return $(this).parents('#modal_panel').length ? Ensembl.EventManager.trigger('modalFormSubmit', $(this)) : true;      
    });
    
    // Close modal window if the escape key is pressed
    $(document).bind({
      keyup: function (event) {
        if (event.keyCode == 27) {
          Ensembl.EventManager.trigger('modalClose', true);
        }
      },
      mouseup: function (e) {
        Ensembl.EventManager.trigger('dragStop', e);
      }
    });
    
    var userMessage = unescape(Ensembl.cookie.get('user_message'));
    
    if (userMessage) {
      userMessage = userMessage.split('\n');
      
      $([
        '<div class="hint" style="margin: 10px 25%;">',
        ' <h3><img src="/i/close.gif" alt="Hide hint panel" title="Hide hint panel" />', userMessage[0], '</h3>',
        ' <p>', userMessage[1], '</p>',
        '</div>'
      ].join('')).prependTo('#main').find('h3 img, a').click(function () {
        $(this).parents('div.hint').remove();
        Ensembl.cookie.set('user_message', '', -1);
      });
    }
  },
  
  reloadPage: function (args) {
    if (typeof args == 'string') {
      Ensembl.EventManager.triggerSpecific('updatePanel', args);
    } else if (typeof args == 'object') {
      for (var i in args) {
        Ensembl.EventManager.triggerSpecific('updatePanel', i);
      }
    } else {
      Ensembl.redirect();
    }
  },
  
  validateForms: function (context) {
    $('form.check', context).validate();
  },
  
  makeZMenu: function (id, params) {
    if (!$('#' + id).length) {
      $([
        '<table class="zmenu" id="' + id + '" style="display:none">',
        '  <thead>', 
        '    <tr><th class="caption" colspan="2"><span class="close">X</span><span class="title"></span></th></tr>',
        '  </thead>', 
        '  <tbody></tbody>',
        '</table>'
      ].join('')).draggable({ handle: 'thead' }).appendTo('body');
    }
    
    Ensembl.EventManager.trigger('addPanel', id, 'ZMenu', undefined, undefined, params, 'showExistingZMenu');
  },
  
  relocateTools: function (tools) {
    var localTools = $('#local-tools');
    
    tools.each(function () {
      localTools.append($(this).children().addClass('additional')).children().show();
    }).remove();
    
    $('a.seq_blast', localTools).click(function () {
      $('form.seq_blast', localTools).submit();
      return false;
    });
  },
  
  hashChange: function (loc) {
    function addCommas(str) {
      var rgx = /(\d+)(\d{3})/;
      
      str += '';
      x    = str.split('.');
      x1   = x[0];
      x2   = x.length > 1 ? '.' + x[1] : '';
      
      while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
      }
      
      return x1 + x2;
    }
    
    $('a:not(.constant)').attr('href', function () {
      return Ensembl.urlFromHash(this.href);
    });
    
    $('input[name=r]', 'form:not(#core_params)').val(loc);
    
    loc = loc.split(/\W/);
    
    loc[1] = addCommas(loc[1]);
    loc[2] = addCommas(loc[2]);
    
    $('a', '#tab_location').html('Location: ' + loc[0] + ':' + loc[1] + '-' + loc[2]);
    
    $('h2.caption').html(function (i, html) {
      return html.replace(/^(Chromosome ).+/, '$1' + loc[0] + ': ' + loc[1] + '-' + loc[2]);
    });
  }
});


@@ -620,24 +620,28 @@
 hange);%0A    
+    
 %0A    $('#loc
@@ -719,32 +719,147 @@
 onstant');%0A    %0A
+    if (window.location.hash) %7B%0A      this.hashChange(Ensembl.urlFromHash(window.location.href, true));%0A    %7D%0A    %0A
     $('.modal_li

fix initial content if hash exists