/**
 * 
 * @deprecated : import this lib after impor jQuery and jTip
 * @author : Avenger.Bevis
 * 
 * power by :
 * 	jQuery(http://www.jquery.com)
 * 	jTip
 **/ 
(function($) {

    /**
     * 
     * String replaceAll
     */
    String.prototype.replaceAll = function ( expr , news) {
      var thiz = this;
      news = news || '';
      
      var check = function(){
        if(typeof expr == 'string')
          return thiz.indexOf(expr) != -1;
          
        else // if regex 
          return expr.test(thiz);
          
      }
      
      while(check()){
        thiz = thiz.replace(expr, news);
      }
      return thiz; 
    }

	/**
	 * 
	 * @param {Object} context : article text
	 * @param {Object} options : 
	 * 	default : {
			url : 'getDefinition.do', -- get definition path
			title : '{word}的解释', -- title
			style : 'jTip', -- class name
			param : 'key' -- parameter name
		}
	 * @param {Object} success : when success , execute this function
	 * @param {Object} link
	 * 	default : '<a href="{url}?{param}={id}" class="{style}" id="{aid}" name="{title}">{word}</a>'
	 * 	{
	 * 		'{url}' :  get definition path
	 * 		'{paramter}' : parameter name
	 * 		'{id}' : parameter value
	 * 		'{className}' : class name
	 * 		'{title}' : title
	 * 		'{word}' : key word
	 * 	}
	 */
	var F = function(context, options, success, link) {
                options = options || {};
		$.extend(options, {
			url : '/hnzfw/getDefinition.do',
			title : '{word}的解释',
			className : 'jTip',
			param : 'key',
			style : 'color:green'
		});
		link = link || '<a href="{url}?{param}={id}" class="{className}" style="{style}" id="{aid}" name="{title}">{word}</a>';
		for( var p in options) {
			link = link.replace('{' + p + '}', options[p])
		}
		var newContext = context;
        $.post( '/hnzfw/getDefinitionsDOM.do', {
			context : context
			}, function( response ) {
				$(response).find('DEF').each(function(){
					var $this = $(this);
					var result = {
						id : $this.attr('id'),
						word : $this.attr('word')
					}
					newContext = newContext.replace(result.word, 
						link.replaceAll('{id}', result.id)
							.replaceAll('{aid}', 'definition:' + Math.random())
							.replaceAll('{word}', result.word));
					
	
				});
				success(newContext);
				JT_init();
			});        
	}
	window.Deffer = F;	
})(jQuery);

