// Scribendi wordcount.js v1.1.2
// Copyright (c) 2007-2009 Scribendi Inc. All Rights Reserved.

Object.extend(Form.Element.Observer.prototype, {
registerCallback: function() { this.pe = new PeriodicalExecuter(this.onTimerEvent.bind(this), this.frequency);},
stop: function() {if (this.pe){this.pe.stop();}},
start: function() {this.lastValue = this.getValue();this.registerCallback();}
});

if(!window.Scribendi) {Scribendi = {};}

Scribendi.WordCountWidget = Class.create({
  initialize: function(where,toggler,targetfield) {
	try {
		this.el = $(where).insert({ after: '<div class="wc_widget" style="display:none;">'+
			'<div style="float:right; padding:7px"><a href="javascript:;" class="wc_hide" title="Close this box"><img src="/images/close_helper.gif" width="16" height="16" alt="[x]" border="0" /></a></div>'+
			'<div class="wc_wrap"><div class="wc_apphelp">In Microsoft Word, go to the <i>Review</i> tab or <i>Tools</i> menu, and select <i>Word Count</i>.<br />'+
			'In WordPerfect, right click anywhere in the document and select <i>Properties</i>, and then click the <i>Information</i> tab.</div>'+
			'<label>To <b>automatically count</b> the number of words in your document, <b>copy and paste</b> the text into the box below.<br />'+
			'<textarea name="wordtext" rows="10" cols="75" wrap="virtual"></textarea></label>'+
			'<div class="wc_sub" style="float: left">Your word count will appear here.</div>'+
			'<div style="text-align: right"><i>The text you enter above will not be stored online.</i> <a href="javascript:;" class="wc_clear" title="Clear text">Clear</a></div>'+
			'</div></div>'}).next();
	} catch (err) {
		return null;
	}
    this.sub = this.el.down('.wc_sub');
    this.textarea = this.el.down('textarea');
    this.obs = new Form.Element.Observer(this.textarea, 0.2, this.wc.bind(this) );
    this.el.down('.wc_clear').observe('click', this.clear.bindAsEventListener(this) );
    this.target = $(targetfield);
    if (!this.target) { 
      this.el.down('.wc_apphelp').remove(); 
    }
    if (!toggler) {
      this.el.down('.wc_hide').remove();
      this.el.show();
    } else {
      this.toggleL = this.toggle.bindAsEventListener(this);
      $(toggler,this.el.down('.wc_hide')).invoke('observe', 'click', this.toggleL );
      $(toggler).href = 'javascript:;';
    }
  },
  toggle: function(ev) {
    Event.stop(ev);
    if (window.Effect) {
      Effect.toggle(this.el,'blind',{duration: 0.5});
    } else {
      this.el.toggle();
    }
  },
  wc: function(e,v) {
    v = (v.match(/^<\?xml|<\/html>\s*$/i))?v.stripScripts().replace(/<\/?[^>]+>/gi, ' ') : v;
    var c = v.match(/^[\s\W]*$/)?0:$w(v.replace(/^[\s\W]+|[\s\W]+$/g,'').replace(/\s[^\w&]\s|\-{2,}/g,' ')).length;
    this.sub.innerHTML = c + ' word' + ((c!==1) ? 's' : '');
    if (this.target && this.target.value != c) {
      this.target.value = c;	  
      if (window.Effect) {
        new Effect.Highlight(this.target.up('.fr'),{ startcolor: '#ffff00', queue: {position: 'end', scope: 'wordcount', duration: 2, limit: 2 } });
      }
	  this.target.fire("change"); // fire the custom event "change" on the target
    }
  },
  clear: function(ev) {
    Event.stop(ev);
    this.obs.stop();
    this.textarea.clear();
    this.sub.innerHTML = 'Your word count will appear here.';
    this.obs.start();
  }
});
