/*  $Id: selection.js 11436 2009-10-14 20:21:10Z rallj $
 * ===========================================================================
 *
 *                            PUBLIC DOMAIN NOTICE
 *               National Center for Biotechnology Information
 *
 *  This software/database is a "United States Government Work" under the
 *  terms of the United States Copyright Act.  It was written as part of
 *  the author's official duties as a United States Government employee and
 *  thus cannot be copyrighted.  This software/database is freely available
 *  to the public for use. The National Library of Medicine and the U.S.
 *  Government have not placed any restriction on its use or reproduction.
 *
 *  Although all reasonable efforts have been taken to ensure the accuracy
 *  and reliability of the software and data, the NLM and the U.S.
 *  Government do not and cannot warrant the performance or results that
 *  may be obtained by using this software or data. The NLM and the U.S.
 *  Government disclaim all warranties, express or implied, including
 *  warranties of performance, merchantability or fitness for any particular
 *  purpose.
 *
 *  Please cite the author in any work or product based on this material.
 *
 * ===========================================================================
 *
 * Authors:  Vlad Lebedev
 *
 * File Description: Mouse-over selection
 *
 */
 

/* Selections */
function Selection(idx, areas) {
   this.delayTimeout = null;
   this.saved_event = null;

   config = m_GViews[idx];
   this.area = areas[ areas.length-1 ];//area;
   this.idx = idx; // view index
   this.signatures = [];
   
   for (i = 0;  i != areas.length;  i++) this.signatures.push( areas[i]['signature'] );
   
   this.parent_id = config['the_id'];
   this.parent_elem = Ext.get(this.parent_id);
   
   var tpl = new Ext.Template('<div id="selection_id_{idx}" class="over_selection"/>');
   
   this.element = tpl.append(this.parent_elem, {idx:idx}, true);
   
   bounds = this.area['bounds'];
   

   left  = (config['flip'] ? bounds['r'] : bounds['l']) + 1;
   right = (config['flip'] ? bounds['l'] : bounds['r']) + 1;
   the_top = bounds['t'] - 3 + config['top_offset'];
   
   the_left  = left + config['scroll_pix'] - 2;
   the_width = right - left + 3;

   if (the_left < -200) { // no more that 200 pix left overhang (IE unhappy)
       adj = -200 - the_left;
       the_left = -200;
       the_width = the_width - adj;
   }
   
   if (the_width - the_left > 2000) { // no more that 2000 pix right overhang (IE even more unhappy)
       the_width = 2000;
   }
   
   this.element.setLeft(the_left); // the output from cgi seems a bit off. That's why this offsets. Or is it browser-specific? :)
   this.element.setTop(the_top); 
   this.element.setHeight(bounds['b']-bounds['t']);
   this.element.setWidth(the_width);

   this.element.on('click', this.clickTest, this);
   this.element.on('mousedown', this.clickTest, this);
   this.element.on('mouseup', this.clickTest, this);
   this.element.on('mousemove', this.clickTest, this);
   
   this.element.on({
        //'click' : this.clickTest,
        //'click' : this.fireClick,//SeqApp.onGViewClick,
        //'dblclick' : SeqApp.onGViewDblClick,
        //'mousedown' : SeqApp.onGViewMouseDown,
        //'mouseup' : SeqApp.onGViewMouseUp,
        //'mousemove' : SeqApp.onGViewMouseMove,
        'contextmenu' : SeqApp.onGViewContextMenu
   });
   
   if (this.area['tooltip']) { // use from cache
      this.element.dom.setAttribute('ext:qtip', this.area['tooltip']);
   } else {
      url = theObjInfoCGI+'?id='+this.signatures.join(',');
      if (m_Key) url += '&key=' + m_Key;
      if (m_NAA) url += '&naa='+m_NAA;
      if (m_Tracks) url += '&tracks='+m_Tracks;
      
      this.qtip = new Ext.ToolTip({
           target: this.element, trackMouse:false, 
           autoWidth:true, autoHide:true,
           autoLoad: {url:url, scope:this, callback:function(el, success, res, options) {
              objs = Ext.decode(res.responseText);
              if (objs.length > 1) { // multiple features
                  obj1 = objs[0];
                  obj2 = objs[1];
                  tip1 = (obj1['text'] ? obj1['text'] : obj1['error']);
                  tip2 = (obj2['text'] ? obj2['text'] : obj2['error']);
                  tip = tip1 + '<br><br>' + tip2;
              } else {
                  obj = objs[0];
                  tip = (obj['text'] ? obj['text'] : obj['error']);
              }
              
              
              
              tip += "<br><br><div style='text-align:right;'><i>Right-click for more options...</i></div>";
              this.area['tooltip'] = tip; // cache it
              el.update(tip);
           }},
           dismissDelay:5000
      });
   } // if
}


Selection.prototype = {
  movePix: function(delta) {
     with (this) {
       new_left = element.getLeft(true) - delta;
       element.setLeft(new_left);
     }
  },
  getElement: function() { return this.element; },

  fireClick: function(e) {    
     clearTimeout(this.delayTimeout);
     this.delayTimeout = null;
     SeqApp.onGViewClick(this.saved_event);
     this.saved_event = null;
  },

  clickTest: function(e,scope) {
     if (this.delayTimeout) {
        //console.log(e);
        if (e.type == 'click') {
          clearTimeout(this.delayTimeout);
          this.delayTimeout = null;
          this.saved_event.type = 'dblclick';
          this.saved_event.browserEvent = 'Event dblclick';
          SeqApp.onGViewDblClick(this.saved_event);
          this.saved_event = null;
        }
    } else {
        if (e.type == 'click' ){
           this.saved_event = new Ext.EventObjectImpl(e);
           var that = this;
           this.delayTimeout = setTimeout(function() {that.fireClick(e);}, 200);
        } else if (e.type == 'mouseup' ) { SeqApp.onGViewMouseUp(e); }
          else if (e.type == 'mousedown' ) { SeqApp.onGViewMouseDown(e); }
          else if (e.type == 'mousemove' ) { SeqApp.onGViewMouseMove(e); }
    }
  }
};


/* Align Selection */
function AlignSelection(idx, area) {
   config = m_GViews[idx];
   this.area = area;
   this.idx = idx; // view index
   //this.signature = area['signature'];
   this.parent_id = config['the_id'];
   this.parent_elem = Ext.get(this.parent_id);
   
   var tpl = new Ext.Template('<div id="align_selection_id_{idx}" class="over_selection"/>');
   
   this.element = tpl.append(this.parent_elem, {idx:idx}, true);
   this.element.setLeft(area['x']); // the output from cgi seems a bit off. That's why this offsets. Or is it browser-specific? :)
   this.element.setTop(area['y']); 
   this.element.setHeight(area['h']);
   this.element.setWidth(area['w']);
   
   this.element.on({
        //'dblclick' : SeqApp.onGViewDblClick,
        'click' : SeqApp.onAlignViewClick
        //'mousedown' : SeqApp.onGViewMouseDown,
        //'mouseup' : SeqApp.onGViewMouseUp,
        //'mousemove' : SeqApp.onGViewMouseMove,
        //'contextmenu' : SeqApp.onGViewContextMenu
   });
   
   this.qtip = new Ext.ToolTip({target: this.element, trackMouse:false, autoWidth:true, autoHide:true, html:area['descr'], dismissDelay:5000});
}


AlignSelection.prototype.movePix = function(delta) {
   with (this) {
      new_left = element.getLeft(true) - delta;
      element.setLeft(new_left);
   }
}
AlignSelection.prototype.getElement = function() { return this.element; }
/** End **/
