// verifica se il testo è un'immagine, un link, una mail, ...
;;(function ($) {
    $.extend($, {
        chatInputVerify: function (rtm, callback) {
            var tx = rtm.data.val;
			if (tx.indexOf(" ") >= 0) {
	            var mytx = tx.split(" ");
	        } else {
	            var mytx = new Array('' + tx + '');
	        }
	        var length = mytx.length;
			var RegexUrl = /(^ftp|^http|^https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
			if (RegexUrl.test(tx) == true && length == 1) {
				var items = $.parseUri(tx);
				$.cacheImage(tx, {
				    load : function (e) {
				        $.ajaxSetup({
				            timeout: 10000
				        });
				        $.post("/image", {
				            url: tx
				        },
						function(data, status){
				            //console.log("load",data,status);
							if(status=='success'){
				            	if(data.img!="error"){
						    		rtm.data.img = data.img;
						    		rtm.data.width = data.width;
						    		rtm.data.height = data.height;
				            	}else{
						    		//rtm.data.img = tx;
								}
								callback (rtm);
				            }
				        }, "json");
				    },
				    error: function (e) {
						//console.log("error",e);
						rtm.data.img = "error";
						callback (rtm);
				    },
				    abort: function (e) {
						//console.log("abort",e);
						rtm.data.img = "abort";
						callback (rtm);
				    }
				});
			}else{
				callback (rtm);
			}
        },
		parseUri : function (str) {
			var o = {
				strictMode: false,
				key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
				q: {
					name: "queryKey",
					parser: /(?:^|&)([^&=]*)=?([^&]*)/g
				},parser: {
					strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
					loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
				}
			};
			m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
			uri = {},
			i = 14;
			
			while (i--) uri[o.key[i]] = m[i] || "";
				uri[o.q.name] = {};
				uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
				if ($1) uri[o.q.name][$1] = $2;
			});
			
			return uri;
		}
    });
})(jQuery);
