// $Id: wordcount.js 166 2007-01-05 10:31:50Z plindstrom $  

if (Drupal.jsEnabled) {

  $(document).ready(function(){
      
    // we piggyback on the tinyMCE wordpress plugin so that we can 
    // handle events occuring within the editor
    if ( 'undefined' == typeof TinyMCE_wordpressPlugin ) { return; }
    var o = $.isFunction(TinyMCE_wordpressPlugin.handleEvent) ? TinyMCE_wordpressPlugin.handleEvent : function() { return true; };
  
    TinyMCE_wordpressPlugin.handleEvent = function(e) {

  		var inst = tinyMCE.selectedInstance;
      
      switch (e.type) {
        case "keydown":
        case "keypress":

        break;

          /** 
           * this section updates the word and char count fields based on
           * certain javascript keyCodes
           * we limit the keyCodes because we don't want to overwhelm the browser
           * and/or slow things to a crawl
           */

        case "keyup":
          // 13 is enter key
          if ( 13 == e.keyCode )
            _wc_loadCounts(tinyMCE.getInstanceById(inst.editorId).getHTML(), inst.editorId);
          // 45 is insert key
          if ( 45 == e.keyCode )
            _wc_loadCounts(tinyMCE.getInstanceById(inst.editorId).getHTML(), inst.editorId);
          // 46 is delete key
          if ( 46 == e.keyCode )
            _wc_loadCounts(tinyMCE.getInstanceById(inst.editorId).getHTML(), inst.editorId);
          // 8 is backspace key
          if ( 8 == e.keyCode )
            _wc_loadCounts(tinyMCE.getInstanceById(inst.editorId).getHTML(), inst.editorId);
          if ( 17 == e.keyCode )
            _wc_loadCounts(tinyMCE.getInstanceById(inst.editorId).getHTML(), inst.editorId);
          //86 is v key
          if ( 86 == e.keyCode )
            _wc_loadCounts(tinyMCE.getInstanceById(inst.editorId).getHTML(), inst.editorId);
          //88 is x key
          if ( 88 == e.keyCode )
            _wc_loadCounts(tinyMCE.getInstanceById(inst.editorId).getHTML(), inst.editorId);
          //224 is CMD key (Mac - not reliable)
          if ( 224 == e.keyCode )
            _wc_loadCounts(tinyMCE.getInstanceById(inst.editorId).getHTML(), inst.editorId);
          // 32 is spacebar
          if ( 32 == e.keyCode )
            _wc_loadCounts(tinyMCE.getInstanceById(inst.editorId).getHTML(), inst.editorId);
        
        break;
      }

      return true;
    };
  });    // end of doc.ready

// this function parses the ajax response (xml) and updates the form fields
function addCount(xml, editorid) {
  if($("status",xml).text() == "2") return;
  timestamp = $("time",xml).text();
  $("message",xml).each(function(id) {
   
    message = $("message",xml).get(id);
    
    twc = $("wordcount",message).text();
    tcc = $("charcount",message).text();
    
    $('#mceWcount'+editorid).val(twc);   
    $('#mceCcount'+editorid).val(tcc);
  });
}

// called from plugin on page load 
// has to be external since Tiny not loaded yet
function _wc_loadCounts(text, editorid) {
  // counter with setTimeout
  // prevents multiple triggers from firing if they occur within set timeinterval
  var timeinterval = 1000; // milliseconds (1 second = 1000 milliseconds)
  var block = 0;

    if ( block ) return;
    block = 1;
    
    setTimeout( function() {
      if (text) {
        var start = new Date();
        var timestamp = start.getTime();
        $.post(Drupal.settings['wordcount_post_url'], {
              action: "postcount",
              content: text,
              time: timestamp
            }, function(xml) {
          addCount(xml, editorid);
        });
      }
      else {
        $('#mceWcount'+editorid).val(0);   
        $('#mceCcount'+editorid).val(0);
      }
      setTimeout( function() { block = 0; }, timeinterval );
    }, 1 );
};  
  
}