function trimStr(str) {
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

var transparentImgSrc = "/ahlens-pharmacy/export/system/modules/com.gridnine.opencms.modules.ahlens_pharmacy/resources/pics/x.gif";
var badBrowser = /MSIE ((5\\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32";

function replaceHeaders() {
	$("h1").each(
		function(i){
			var $t = $(this);
			var img = new Image();
			img.$t = $t;
			img.t = this;
			if(badBrowser) {
				img.onload = function() {
					this.$t.html("<img class=\"replaced\" alt=\"" + escapeString(this.$t.text()) + "\" style=\"width:" + this.width + ";height:" + this.height + ";filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/headers.jsp?text=" + encode(encode(this.$t.text(), 'utf8'), 'utf8') + "&w=" + this.t.offsetWidth + "&h=" + this.t.offsetHeight + "&t=1&c=" + getHex(jQuery.curCSS(this.t, "color")) + "', sizingMethod='crop')\" src=\"" + transparentImgSrc + "\"/>");
				};
			}
			img.src = "/headers.jsp?text=" + encode($t.text(), 'utf8') + "&w=" + this.offsetWidth + "&h=" + this.offsetHeight + "&t=1&c=" + getHex(jQuery.curCSS(this, "color"));
			if(!badBrowser) {
				$t.html("<img class=\"replaced\" alt=\"" + escapeString($t.text()) + "\" src=\"" + img.src + "\"/>");
			}
			$t.css("padding", "0");
		}
	);

	$("#bonusList h2").each(
		function(i){
			var clazz = this.className;
			var $t = $(this);
			var img = new Image();
			img.$t = $t;
			img.t = this;
			if(badBrowser) {
				img.onload = function() {
					var h1 = this.height;
					var h2 = this.t.offsetHeight;
					this.$t.html("<img class=\"replaced" + (clazz && clazz != "" ? " " + clazz : "") + "\" alt=\"" + escapeString(this.$t.text()) + "\" style=\"width:" + this.width + ";height:" + this.height + ";filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/headers.jsp?text=" + encode(encode(this.$t.text(), 'utf8'), 'utf8') + "&w=" + this.t.offsetWidth + "&h=" + this.height + "&t=3&c=" + getHex(jQuery.curCSS(this.t, "color")) + "', sizingMethod='crop')\" src=\"" + transparentImgSrc + "\"/>");
				};
			}
			img.src = "/headers.jsp?text=" + encode($t.text(), 'utf8') + "&w=" + this.offsetWidth + "&h=" + this.offsetHeight + "&t=3&c=" + getHex(jQuery.curCSS(this, "color"));
			if(!badBrowser) {
				$t.html("<img class=\"replaced" + (clazz && clazz != "" ? " " + clazz : "") + "\" alt=\"" + escapeString($t.text()) + "\" src=\"" + img.src + "\"/>");
			}
			$(this).css("padding", "0");
		}
	);

	$("#menuLeft > ul > li > a").each(
		function(i){
			var $t = $(this);
			var img = new Image();
			img.$t = $t;
			img.t = this;
			if(badBrowser) {
				img.onload = function() {
					this.$t.html("<img class=\"replaced\" alt=\"" + escapeString(this.$t.text()) + "\" style=\"height:" + this.height + ";width:" + this.width + ";filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/headers.jsp?text=" + encode(encode(this.$t.text(), 'utf8'), 'utf8') + "&w=" + (this.t.offsetWidth + 2) + "&h=" + this.t.offsetHeight + "&t=4&c=" + getHex(jQuery.curCSS(this.t, "color")) + "', sizingMethod='crop')\" src=\"" + transparentImgSrc + "\"/>");
				};
			}
			img.src = "/headers.jsp?text=" + encode($t.text(), 'utf8') + "&w=" + (this.offsetWidth + 5) + "&h=" + this.offsetHeight + "&t=4&c=" + getHex(jQuery.curCSS(this, "color"));
			if(!badBrowser) {
				$t.html("<img class=\"replaced\" alt=\"" + escapeString($t.text()) + "\" src=\"" + img.src + "\"/>");
			}
		}
	);

	$("#menuTop a").each(
		function(i){
			if (this.id != 'homepageLink') {
				var $t = $(this);
				var img = new Image();
				img.$t = $t;
				img.t = this;
				if(badBrowser) {
					img.onload = function() {
						this.$t.html("<img class=\"replaced\" alt=\"" + escapeString(this.$t.text()) + "\" style=\"height:" + this.height + ";width:" + this.width + ";filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/headers.jsp?text=" + encode(encode(this.$t.text().trim(), 'utf8'), 'utf8') + "&w=" + (this.t.offsetWidth + 7) + "&h=" + this.t.offsetHeight + "&t=5&c=" + getHex(jQuery.curCSS(this.t, "color")) + "', sizingMethod='crop')\" src=\"" + transparentImgSrc + "\"/>");
					};
				}
				img.src = "/headers.jsp?text=" + encode($t.text().trim(), 'utf8') + "&w=" + (this.offsetWidth + 7) + "&h=" + (this.offsetHeight + 3) + "&t=5&c=" + getHex(jQuery.curCSS(this, "color"));
				if(!badBrowser) {
					$t.html("<img class=\"replaced\" alt=\"" + escapeString($t.text()) + "\" src=\"" + img.src + "\"/>");
				}
				$(this).css("padding", "0");
			}
		}
	);
}

function getHex (str) {
	if(str.indexOf("#") == 0) {
		return str.replace("#", "");
	}
	try {
		var parts = str.replace("rgb(", "").replace(")", "").split(",");
		return RGBtoHex (trimStr(parts[0]),trimStr(parts[1]),trimStr(parts[2]));
	} catch (e) {
		return false;
	}
}

function RGBtoHex(R,G,B) {
	return toHex(R)+toHex(G)+toHex(B)
}

function toHex(N) {
	 if (N==null) return "00";
	 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
	 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
	 return "0123456789ABCDEF".charAt((N-N%16)/16)
	  + "0123456789ABCDEF".charAt(N%16);
}