/*  $Id: range_reflector.js 10274 2009-08-25 13:01:36Z 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: Visible range reflections in the viewer
 *
 */
 
 
var m_ReflectionActive = false;
var m_ReflectionActiveIdx;
var m_ReflectionsIdx = 0;
var m_AllReflections = [];
/* view reflections */
function Reflection(this_config, show_config) {
   this.idx = this_config['idx']; // view index
   this.show_idx = show_config ? show_config['idx'] : null; // view index
   
   color = show_config ? show_config['color'] : '000000';
   
   this.parent_id = this_config['the_id'];
   this.parent_elem = Ext.get(this.parent_id);

   vis_range = show_config ? SeqApp.toSeqG(show_config) : [m_FromSequence, m_ToSequence, m_ToSequence - m_FromSequence];
   qtip = Math.round(vis_range[0]) + ' : ' + Math.round(vis_range[1]);
   if (!show_config) qtip = 'Sequence View: ' + qtip;
   
   var tpl = new Ext.Template('<div id="reflection_id_{idx}" ext:qtip="{qtip}" class="reflection" style="z-index:10;background-color:#{color};"/>');
   this.element = tpl.append(this.parent_elem, {idx:m_ReflectionsIdx, qtip:qtip, color:color}, true);
   
   f = SeqApp.seq2PixG(this_config, vis_range[0]); // in pixels
   t = SeqApp.seq2PixG(this_config, vis_range[1]);
   
   if (this_config['flip']) {
      this.element.setLeft( t+this_config['scroll_pix'] ); // mind the scroll offset!
      this.element.setWidth( f-t );
   } else {
      this.element.setLeft( f+this_config['scroll_pix'] ); // mind the scroll offset!
      this.element.setWidth( t-f );
   }
   
   this.element.on({
        'mousedown' : SeqApp.onGViewMouseDown,
        'mouseup' : SeqApp.onGViewMouseUp,
        'mousemove' : SeqApp.onGViewMouseMove
   });
}


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


Reflection.reCreateReflections = function() {
   Reflection.deleteAll();

   for (i = 0; i != m_GViews.length; i++) { // create new ones
      this_config = m_GViews[i];
      if (this_config  &&  this_config['type'] != 'panorama'  &&  this_config['type'] != 'alignment') {
         for (j = 0; j != m_GViews.length; j++) { // create new ones
            show_config = m_GViews[j];
            if (show_config  &&  show_config['type'] != 'panorama' && 
                    show_config['type'] != 'alignment'  &&  show_config['idx']!=this_config['idx']) {
               m_AllReflections[m_ReflectionsIdx] = new Reflection(this_config, show_config);
               m_ReflectionsIdx++;
            }
         }

         if (m_TextLocator) { // sequence text  view shown
            m_AllReflections[m_ReflectionsIdx] = new Reflection(this_config, null);
            m_ReflectionsIdx++;
         }
      }
   } // done reflections
   
}


Reflection.deleteAll = function() {
   for (i = 0; i != m_AllReflections.length; i++) m_AllReflections[i].getElement().remove();
   m_AllReflections = [];
   m_ReflectionsIdx = 0;
}

Reflection.prototype.scrollPix = function(config, delta) {
   if (this.idx == config['idx']) { // scroll as one (locked)
      reflection = this.getElement();
      new_left = reflection.getLeft(true) - delta;
      reflection.setLeft(new_left);
   } else if (this.show_idx && this.show_idx == config['idx']) {
      vis_range = SeqApp.toSeqG(config);
      this_config = m_GViews[this.idx];
      f = SeqApp.seq2PixG(this_config, vis_range[0]); // in pixels
      this.element.setLeft( f+this_config['scroll_pix'] ); // mind the scroll offset!
   }
}