(function($) { 
 $.chatEditor = function(e,options) {
    this.options = options;
    this.channel = options.channel;
    this.e = e;
	this.typeTimer = 0;
	var self = this;
	e.keydown(function(evt){
       if(!self.options.typeSend){
            self.type();
            self.options.typeSend = true;
            if(chat.channel(this.channel)!=undefined)
            this.typeTimer = setTimeout(function() { self.toggleType(); },5000);
       }
	   if (evt.which == 13 && !evt.shiftKey) {
			self.send();
			return false;
		} 
	});
 };
 
 $.extend($.chatEditor, {    
    DEFAULTS: {ispopout:false,typeSend:false }
 });
 
 $.chatEditor.prototype = {
    message:function(msg) {
        var self = this;
        jQuery.post("/live.ashx",{action:"send",id: chat.account.Id, channel: self.channel, message: msg},
           function(res) {
              if(res.Status == 1){
                 //chat.onMessage(chat.account.Name,self.channel, msg);
              }
           },'json');
    },
    
    onSent:function(res){
       alert(res);
    },
    
    matchPhone:function(msg){
      return msg.match(/\(\d{3}\)\s*\d{3}(?:-|\s*)\d{4}?x/)
    },
    
    match10:function(msg){
      return msg.match(/(\s*\(?0\d{4}\)?(\s*|-)\d{3}(\s*|-)\d{3}\s*)|(\s*\(?0\d{3}\)?(\s*|-)\d{3}(\s*|-)\d{4}\s*)|(\s*(7|8)(\d{7}|\d{3}(\-|\s{1})\d{4})\s*)/)
    },
    
    match7:function(msg){
       return msg.match(/\d{7}/);
    },
    
    matchf:function(msg){
       return msg.match(/((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}/);
    },
    
    match34:function(msg){
       return msg.match(/\d{3}\s?\d{4}/);
    },
    
    matche:function(msg){
       return msg.match(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/);
    },
    
    matchit:function(msg){
       if(this.matchPhone(msg)) return true;
       if(this.match10(msg)) return true;
       if(this.match7(msg)) return true;
       if(this.matchf(msg)) return true;
       if(this.match34(msg)) return true;
       if(this.matche(msg)) return true;
    },
    
    send:function() {       
      var msgg = this.e.val();
      if( this.matchit(msgg)){
         alert("Please do not provide any personal contact such as email addresses or phone number in your message. We suggest that all communication be made exclusively through the Cellswapper messaging system to allow for maximum support and efficiency for all parties involved in the transfer.");
         return false;
      }
      
	  msgg = msgg.replace(/<br \/>/g,"\n").replace(/<br>/g,"\n").replace(/&nbsp;/g,' ');
	  var msgs = msgg.split("\n");
	  for(i=0;i<msgs.length;i++){
	      var msg = msgs[i];
		msg = msg.replace(/(<([^>]+)>)/ig,"").replace(/\n/g,'').replace(/\r/g,'');
		msg = msg.replace(/'/g,"<spc>1<spc>");
		msg = msg.replace(/\?/g,"<spc>2<spc>");
		msg = msg.replace(/#/g,"<spc>3<spc>");
		 msg = msg.replace(/=/g,"<spc>4<spc>");
		if (msg && msg != '') {
			this.message(msg);
		}
	   }
	 var self = this;
	   setTimeout(function() { self.e.val(""); }, 10);
 },

 clear:function(){
   $(this.input).val();
 },
  
 disable:function(){
  $(this.input).disabled = true;
  $(this.input).readonly = true;
 },
  
 enable:function(){
   $(this.input).disabled = false;
  $(this.input).readonly = false;
  },

 type:function(){
    var self = this;
    jQuery.post("/live.ashx",{
                action: "typing",
                id: chat.account.Id,
                channel: self.channel,
                message: ""}
    ,function(){},"json");
 },

 toggleType:function(){
   this.typeSend = false;
   clearTimeout(this.typeTimer);
 }
}

})(jQuery);