msTinyMCE = Class.create({
  tinyOffsets: {},
  activeEditor: false,

  initialize: function () {
  },
   
  moveToRibbon: function()
  {
    var container = $('tinymce-container');
    
    container.parentNode.style.width = '610px';
    
    for (i in tinyMCEFields ) {
      if (typeof(tinyMCEFields[i]) == 'string') {
        editor = tinyMCEFields[i];

        o_x = o_y = 0;
        
        if ($(editor + '_parent')) {
          o_y = Position.cumulativeOffset($(editor + '_parent')).top;
          o_x = Position.cumulativeOffset($(editor + '_parent')).left;
        
          
          $(editor + '_parent').style.position = 'fixed';
          $(editor + '_parent').style.left = (Position.cumulativeOffset(container).left + 5) + 'px';
          $(editor + '_parent').style.top = (Position.cumulativeOffset(container).top + 5) + 'px';
          $(editor + '_parent').style.zIndex = '2';
          $(editor + '_parent').style.height = '60px';
          $(editor + '_parent').style.width = '600px';
        }
        
        $$('#' + editor + '_parent #' + editor + '_tbl td.mceStatusbar').each(function(s, i) {
          s.style.display = 'none';
        });
        
        $$('#' + editor + '_parent #' + editor + '_tbl td.mceIframeContainer').each(function(s, i) {
          s.style.border = 'none';
          s.style.height = '0';
        });
        
        $$('#' + editor + '_parent .mceToolbar').each(function(s, i) {
          s.style.background = 'none';
          s.style.border = 'none';
          s.style.zIndex = '2';
        });
        
        if ($(editor + '_tbl')) {
          $(editor + '_tbl').style.background = 'none';
          $(editor + '_tbl').style.border = 'none';
          $(editor + '_tbl').style.height = '60px';
          $(editor + '_tbl').style.width = '600px';
        }
        
        
        cl = Position.cumulativeOffset(container).left;
        
        if ($(editor + '_ifr')) {
          // reposition the iframe
          $(editor + '_ifr').style.position = 'absolute';
          $(editor + '_ifr').style.overflow = 'hidden';
          $(editor + '_ifr').style.zIndex = 0;
          $(editor + '_ifr').style.top =  (o_y - 54)+'px';
          $(editor + '_ifr').style.left = (165 - cl) + 'px';
          $(editor + '_ifr').style.border = '#cecece 1px solid';
          $(editor + '_ifr').style.height = '170px';
        }
      
        this.tinyOffsets[editor] = o_y;
        
        div = new Element('div');
        div.style.height = (Element.getDimensions($(editor + '_ifr')).height + 5 ) + 'px';
        
        div.addClassName('tabbed');
        
        $(editor).parentNode.appendChild(div);
        
        if ($(editor).nextSibling) {
          $(editor).parentNode.insertBefore(div, $(editor).nextSibling);
        } 
        else {
          $(editor).parentNode.appendChild(div);
        }
      }
    }
    
    // handle the scroll event
    // since the ribbon is positioned fixed, and the iframe inside this fixed element should be moved
    // we need this fix
    Event.observe(window, 'scroll', function(e) {
      scroll_top = typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop: document.body.scrollTop ? document.body.scrollTop : 0;

      
      for (i in myTinyMCE.tinyOffsets) {
        $(i + '_ifr').style.zIndex = 0;
        t = (myTinyMCE.tinyOffsets[i] - 54 - scroll_top);
        
        if (t < 100 && $(i + '_ifr').style.display == 'block') {
          $(i + '_ifr').style.display = 'none';
          $(i + '_ifr').style.top =  t + 'px';
          
          // scroll down to next field
          th = $(i + '_ifr').style.height.substr(0, $(i + '_ifr').style.height.length -2);
          
          self.scrollTo(0, scroll_top + ( 170 -t ));
          
        }
        else if (t < 100) {
          $(i + '_ifr').style.display = 'none';
          $(i + '_ifr').style.top =  t + 'px';
        }
        else {
          $(i + '_ifr').style.display = 'block';
          $(i + '_ifr').style.top =  t + 'px';
        }
        
      }
    });
    
    // set the hook for deactivate of TinyMCE
    document.observe('TinyMCE:deactivate', function(ed) {
      myTinyMCE.handleTinyMCELayer(false);
    });
    
    // set the hook for activate of TinyMCE
    document.observe('TinyMCE:activate', function(ed) {
      myTinyMCE.activeEditor = ed.target.id;
      myTinyMCE.handleTinyMCELayer(true);
    });
    
    // make sure all elements of the form will disable the TinyMCEs
    all = Form.getElements('form');
    for ( i in all) {
      if (typeof(all[i]) != 'function') {
        Event.observe(all[i], 'click', function() { 
          myTinyMCE.handleTinyMCELayer(false);
        });
      }
    }
    
    // disable the TinyMCEs
    myTinyMCE.handleTinyMCELayer(false);
  },
  
  handleTinyMCELayer: function(active) {
    
    // determine which editor to show
    for (i in tinyMCEFields ) {
      if (typeof(tinyMCEFields[i]) == 'string') {
        editor = tinyMCEFields[i];
        
        if (editor == this.activeEditor) {
          $(editor + '_parent').style.zIndex = 4;
        }
        else {
          $(editor + '_parent').style.zIndex = 2;
        }
      }
    }
  
    for (i in tinyMCEFields ) {
      if (typeof(tinyMCEFields[i]) == 'string') {
        editor = tinyMCEFields[i];
        
        for ( i in tinyMCE.get(editor).controlManager.controls) {
          if (active) {
            tinyMCE.get(editor).controlManager.setDisabled(i.substr(i.indexOf('_') + 1), false);
            //console.log('enable %s', i.substr(i.indexOf('_') + 1) );
          }
          else {
            tinyMCE.get(editor).controlManager.setDisabled(i.substr(i.indexOf('_') + 1), true);
            //console.log('disable %s', i.substr(i.indexOf('_') + 1) );
          }
        }
      }
    }
  }
});

myTinyMCE = new msTinyMCE();

