(function($) {
	$.fn.setBorder=function(options) {
		var settings={
			path: "",
			borders: "c.gif",
			topnbottom: "tb.gif",
			leftnright: "lr.gif",
			backcolor: "#ffffff",
			size: {
				top: 0,
				right: 0,
				bottom: 0,
				left: 0
			},
			all: {
				width: 0,
				height: 0
			},
			topleft: {
				width: 0,
				height: 0
			},
			topright: {
				width: 0,
				height: 0
			},
			bottomleft: {
				width: 0,
				height: 0
			},
			bottomright: {
				width: 0,
				height: 0
			}
		};
		var reset={
			"max-width": ["","auto","none"],
			"width": ["","auto"],
			"float": ["","none"]
		};


		$.extend(true,settings,options);
		var p=settings.path;
		if(p.length>0) {
			var at=settings.all.height;
			if(at>0) {
				settings.topleft.height=at;
				settings.topright.height=at;
				settings.bottomleft.height=at;
				settings.bottomright.height=at;
			}
			at=settings.all.width;
			if(at>0) {
				settings.topleft.width=at;
				settings.topright.width=at;
				settings.bottomleft.width=at;
				settings.bottomright.width=at;
			}
			$(this)
				.attr("a","a")
				.css("background-color",settings.backcolor)
				.css("border","0px")
				.wrap("<div><div><div><div><div></div></div></div></div></div>")
				.parent()
					.css("padding-top",settings.size.top+"px")
					.css("background","url("+settings.path+settings.topnbottom+") repeat-x right top")
					.parent()
						.css("padding-right",settings.size.right+"px")
						.css("background","url("+settings.path+settings.leftnright+") repeat-y right top")
						.parent()
							.css("padding-left",settings.size.left+"px")
							.css("background","url("+settings.path+settings.leftnright+") repeat-y left bottom")
							.parent()
								.css("padding-bottom",settings.size.bottom+"px")
								.css("background","url("+settings.path+settings.topnbottom+") repeat-x left bottom")
								.parent()
									.css("background-color",settings.backcolor)
									.each(function() {
										var bs=($(this).find("div>div>div>div>*[a=a]"));
										if(bs!=null) {
											for(var k in reset) {
												var t=bs.css(k);
												var a=new Array();
												if(t!=null&&jQuery.inArray(t,reset[k])== -1) {
													if(k=="width") {
														if(t.indexOf("px")> -1) {
															t=(parseInt(t.replace("px",""))+settings.size.left+settings.size.right)+"px";
														}
													}
													$(this).css(k,t);
												}
											}
										}
									})
								.end()
								.before("<div></div><div></div>")//ecken
								.prev()
									.css("width",settings.topright.width+"px")
									.css("height",settings.topright.height+"px")
									.css("float","right")
									.css("background","url("+settings.path+settings.borders+") no-repeat right top")
									.prev()
										.css("position","absolute")
										.css("width",settings.topleft.width+"px")
										.css("height",settings.topleft.height+"px")
										.css("float","left")
										.css("background","url("+settings.path+settings.borders+") no-repeat left top")
									.end()
								.end()
							.end()
							.after("<div></div><div></div>")
							.next()
								.css("width",settings.bottomleft.width+"px")
								.css("height",settings.bottomleft.height+"px")
								.css("float","left")
								.css("margin-top",(-settings.size.bottom)+"px")
								.css("background","url("+settings.path+settings.borders+") no-repeat left bottom")
								.next()
									.css("width",settings.bottomright.width+"px")
									.css("height",settings.bottomright.height+"px")
									.css("float","right")
									.css("margin-top",(-settings.size.bottom)+"px")
									.css("background","url("+settings.path+settings.borders+") no-repeat right bottom")
								.end()
							.end()
						.end()
					.end()
				.end()
				.removeAttr("a")
				.css("width","auto")
				.css("margin","")
				.each(function() {
					for(var k in reset) {
						$(this).css(k,reset[k][0]);
					}
					if($(this).context.tagName=="IMG") {
						$(this)
							.css("width","100%")
							.css("margin-top","-10px")
						;
					} else {
						$(this).after("<div class='clear'></div>");
					}
				})
			;
		}
	};
})(jQuery);
