(function ( $ ) {
    $.fn.gw2emblems = function( options ) {
  
        var settings = $.extend({
             limit: 10
			,bbtag: "guild"
			,jsonhost: "http://gw2.properdave.com"
			,size: 100
        }, options );
		var css_class = {
			 group: "gw2"
			,stack: "stack"
			,back: "gw2b-"
			,fore: "gw2f-"
		};
		var node = this;
		var limit = settings.limit;
		var size = settings.size;
		var regex = new RegExp("\\["+settings.bbtag+"\\](.*?)\\[\\/"+settings.bbtag+"\\]","ig");
		var content = node.html();
		var res = content.match(regex);
		if (res!=null) {
			if (res.length <= limit) { limit = res.length; }
			for (var i =0;i<limit;i++) {
				var name = res[i].replace("["+settings.bbtag+"]", "");
				name = name.replace("[/"+settings.bbtag+"]", "");
				var match = res[i];
				$.ajax({ url: settings.jsonhost+"/json/emblem/?name="+name, 
					async: false,
					dataType: 'json',
					success: function(data) {
						var replace = '<span class="'+css_class.group+'" title="'+data.guild_name+' ['+data.tag+']" style="width:'+size+'px; height:'+size+'px; font-size:'+size+'px;"><span class="'+css_class.group+' '+css_class.stack+' '+css_class.back+data.background_id+' '+data.background_props+'" style="color:'+data.background_color+'"></span><span class="'+css_class.group+' '+css_class.stack+' '+css_class.fore+data.foreground_id1+' '+data.foreground_props1+'" style="color:'+data.foreground_color1+'"></span><span class="'+css_class.group+' '+css_class.stack+' '+css_class.fore+data.foreground_id2+' '+data.foreground_props2+'" style="color:'+data.foreground_color2+'"></span></span>';
						content = content.replace(match, replace);
					}
				});
			}
		}
		regex = new RegExp("\\["+settings.bbtag+" size=([0-9]*)\\](.*?)\\[\\/"+settings.bbtag+"\\]","ig");
		res = content.match(regex);
		if (res!=null) {
			if (res.length <= limit) { limit = res.length; }
			for (var i =0;i<limit;i++) {
				var sizereg = new RegExp("size=([0-9]*)", "ig");
				var sizeres = res[i].match(sizereg);
				var name = res[i].replace("["+settings.bbtag+" "+sizeres[0]+"]", "");
				name = name.replace("[/"+settings.bbtag+"]", "");
				var match = res[i];
				size = sizeres[0].replace("size=", "");
				$.ajax({ url: settings.jsonhost+"/json/emblem/?name="+name, 
					async: false,
					dataType: 'json',
					success: function(data) {
						var replace = '<span class="'+css_class.group+'" title="'+data.guild_name+' ['+data.tag+']" style="width:'+size+'px; height:'+size+'px; font-size:'+size+'px;"><span class="'+css_class.group+' '+css_class.stack+' '+css_class.back+data.background_id+' '+data.background_props+'" style="color:'+data.background_color+'"></span><span class="'+css_class.group+' '+css_class.stack+' '+css_class.fore+data.foreground_id1+' '+data.foreground_props1+'" style="color:'+data.foreground_color1+'"></span><span class="'+css_class.group+' '+css_class.stack+' '+css_class.fore+data.foreground_id2+' '+data.foreground_props2+'" style="color:'+data.foreground_color2+'"></span></span>';
						content = content.replace(match, replace);
					}
				});
			}
		}
		node.html(content);
        return this;
    };
 
}( jQuery ));