/**************** Preamble ****************/
// This script is licensed under the creative commons license
// Attribution-Share Alike 2.5 Switzerland
// You are free to share and to remix this work under the 
// following conditions: Attribution and Share-Alike
// See http://creativecommons.org/licenses/by-sa/2.5/ch/deed.en
// Thus you are free to use it for commercial purposes.
//
// Dieses Script steht unter der Creative Commons-Lizenz
// Attribution-Share Alike 2.5 Switzerland
// Sie dürfen das Werk vervielfältigen, verbreiten und
// öffentlich zugänglich machen,
// sowie Bearbeitungen des Werkes anfertigen
// Zu den folgenden Bedingungen:
// Namensnennung und Weitergabe unter gleichen Bedingungen.
// Siehe http://creativecommons.org/licenses/by-sa/2.5/ch/
// Somit sind Sie frei, das Script für kommerzielle Zwecke zu nutzen.
//
// Mathias Nater, Zürich, 2009
// mathias at mnn dot ch
/** ************** Preamble *************** */
var Hyphenator = function() {
	var SUPPORTEDLANG = {
		"de" : true
	};
	var LANGUAGEHINT = "Deutsch: de";
	var PROMPTERSTRINGS = {
		"de" : "Die Sprache dieser Webseite konnte nicht automatisch bestimmt werden. Bitte Sprache angeben: \n\n" + LANGUAGEHINT
	};
	var BASEPATH = function() {
		var s = document.getElementsByTagName("script"), i = 0, p, t;
		while (!!(t = s[i++].src)) {
			p = t.indexOf("Hyphenator.js");
			if (p !== -1) {
				return t.substring(0, p);
			}
		}
		return "http://hyphenator.googlecode.com/svn/trunk/";
	}();
	var DONTHYPHENATE = {
		"script" : true,
		"code" : true,
		"pre" : true,
		"img" : true,
		"br" : true,
		"samp" : true,
		"kbd" : true,
		"var" : true,
		"abbr" : true,
		"acronym" : true,
		"sub" : true,
		"sup" : true,
		"button" : true,
		"option" : true,
		"label" : true
	};
	var exceptions = {};
	var enableCache = true;
	var cache = function() {
		if (!enableCache) {
			return undefined;
		}
		var r = {}, l;
		for (l in SUPPORTEDLANG) {
			if (SUPPORTEDLANG.hasOwnProperty(l)) {
				r[l] = {};
			}
		}
		return r;
	}();
	var enableRemoteLoading = true;
	var displayToggleBox = false;
	var hyphenateclass = "hyphenate";
	var hyphen = String.fromCharCode(173);
	var urlhyphen = "";
	var min = 6;
	var bookmarklet = false;
	var patternsloaded = {};
	var preparestate = 0;
	var mainlanguage = null;
	var url = "(\\w*://)((\\w*:)?(\\w*)@)?([\\w\\.]*)?(:\\d*)?(/[\\w#!:\\.?\\+=&%@!\\-]*)*";
	var mail = "[\\w-\\.]+@[\\w\\.]+";
	var urlRE = new RegExp(url, "i");
	var mailRE = new RegExp(mail, "i");
	var zerowidthspace = "";
	function createZeroWidthSpace() {
		var ua = navigator.userAgent.toLowerCase();
		if (ua.indexOf("msie 6") === -1 && ua.indexOf("msie 8") === -1) {
			zerowidthspace = String.fromCharCode(8203);
		} else {
			zerowidthspace = "";
		}
		return zerowidthspace;
	}
	function checkIfBookmarklet() {
		var loc = null;
		var jsArray = document.getElementsByTagName("script");
		for ( var i = 0, l = jsArray.length; i < l; i++) {
			if (!!jsArray[i].getAttribute("src")) {
				loc = jsArray[i].getAttribute("src");
			}
			if (!loc) {
				continue;
			} else {
				if (loc.indexOf("Hyphenator.js?bm=true") !== -1) {
					bookmarklet = true;
				}
			}
		}
	}
	function getLang(el, nofallback) {
		if (!!el.getAttribute("lang")) {
			return el.getAttribute("lang").substring(0, 2);
		}
		try {
			if (!!el.getAttribute("xml:lang")) {
				return el.getAttribute("xml:lang").substring(0, 2);
			}
		} catch (ex) {
		}
		if (el.tagName != "HTML" && nofallback) {
			return getLang(el.parentNode);
		}
		if (!nofallback && mainlanguage) {
			return mainlanguage;
		}
		return null;
	}
	function autoSetMainLanguage() {
		var el = document.getElementsByTagName("html")[0];
		mainlanguage = getLang(el);
		mainlanguage = "de";
		
		/*
		if (!mainlanguage) {
			var m = document.getElementsByTagName("meta");
			for ( var i = 0; i < m.length; i++) {
				if (!!m[i].getAttribute("http-equiv")
						&& (m[i].getAttribute("http-equiv") === "content-language")) {
					mainlanguage = m[i].getAttribute("content").substring(0, 2);
				}
				if (!!m[i].getAttribute("name")
						&& (m[i].getAttribute("name") === "DC.language")) {
					mainlanguage = m[i].getAttribute("content").substring(0, 2);
				}
				if (!!m[i].getAttribute("name")
						&& (m[i].getAttribute("name") === "language")) {
					mainlanguage = m[i].getAttribute("content").substring(0, 2);
				}
			}
		}
		if (!mainlanguage) {
			var text = "";
			var ul = navigator.language ? navigator.language
					: navigator.userLanguage;
			ul = ul.substring(0, 2);
			if (SUPPORTEDLANG[ul]) {
				text = PROMPTERSTRINGS[ul];
			} else {
				text = PROMPTERSTRINGS.en;
			}
			var lang = window.prompt(unescape(text), ul);
			if (SUPPORTEDLANG[lang]) {
				mainlanguage = lang;
			}
		}*/
	}
	function hideInside() {
		var elements, i, l;
		if (document.getElementsByClassName) {
			elements = document.getElementsByClassName(hyphenateclass);
			for (i = 0, l = elements.length; i < l; i++) {
				elements[i].style.visibility = "hidden";
			}
		} else {
			elements = document.getElementsByTagName("*");
			for (i = 0, l = elements.length; i < l; i++) {
				if (elements[i].className.indexOf(hyphenateclass) !== -1
						&& elements[i].className.indexOf("donthyphenate") === -1) {
					elements[i].style.visibility = "hidden";
				}
			}
		}
	}
	function switchToggleBox(s) {
		var myBox, bdy, myIdAttribute, myTextNode;
		if (s) {
			bdy = document.getElementsByTagName("body")[0];
			myBox = document.createElement("div");
			myIdAttribute = document.createAttribute("id");
			myIdAttribute.nodeValue = "HyphenatorToggleBox";
			myClassAttribute = document.createAttribute("class");
			myClassAttribute.nodeValue = "donthyphenate";
			myTextNode = document.createTextNode("Hy-phe-na-ti-on");
			myBox.appendChild(myTextNode);
			myBox.setAttributeNode(myIdAttribute);
			myBox.setAttributeNode(myClassAttribute);
			myBox.onclick = Hyphenator.toggleHyphenation;
			myBox.style.position = "absolute";
			myBox.style.top = "0px";
			myBox.style.right = "0px";
			myBox.style.margin = "0";
			myBox.style.backgroundColor = "#AAAAAA";
			myBox.style.color = "#FFFFFF";
			myBox.style.font = "6pt Arial";
			myBox.style.letterSpacing = "0.2em";
			myBox.style.padding = "3px";
			myBox.style.cursor = "pointer";
			myBox.style.WebkitBorderBottomLeftRadius = "4px";
			myBox.style.MozBorderRadiusBottomleft = "4px";
			bdy.appendChild(myBox);
		} else {
			myBox = document.getElementById("HyphenatorToggleBox");
			myBox.style.visibility = "hidden";
		}
	}
	function loadPatterns(lang) {
		if (SUPPORTEDLANG[lang] && !patternsloaded[lang]) {
			var url = BASEPATH + "patterns/" + lang + ".js";
		} else {
			return;
		}
		if (document.createElement) {
			var head = document.getElementsByTagName("head").item(0);
			var script = document.createElement("script");
			script.src = url;
			script.id = lang;
			script.type = "text/javascript";
			head.appendChild(script);
		}
	}
	function convertPatternsToObject() {
		for ( var lang in Hyphenator.patterns) {
			if (Hyphenator.patterns.hasOwnProperty(lang)) {
				var sa = Hyphenator.patterns[lang].split(" ");
				Hyphenator.patterns[lang] = {};
				var pat, key, i = 0;
				while (!!(pat = sa[i++])) {
					key = pat.replace(/\d/g, "");
					Hyphenator.patterns[lang][key] = pat;
				}
			}
		}
	}
	function runHyphenation() {
		function bind(obj, fun, args) {
			return function() {
				var f = obj[fun];
				return f.call(obj, args);
			};
		}
		var body = document.getElementsByTagName("body")[0];
		var i, elements, l;
		if (Hyphenator.isBookmarklet()) {
			Hyphenator.hyphenateElement(body);
		} else {
			if (document.getElementsByClassName) {
				elements = document.getElementsByClassName(hyphenateclass);
				for (i = 0, l = elements.length; i < l; i++) {
					window.setTimeout(bind(Hyphenator, "hyphenateElement",
							elements[i]), 0);
				}
			} else {
				elements = document.getElementsByTagName("*");
				for (i = 0, l = elements.length; i < l; i++) {
					if (elements[i].className.indexOf(hyphenateclass) !== -1) {
						window.setTimeout(bind(Hyphenator, "hyphenateElement",
								elements[i]), 0);
					}
				}
			}
		}
	}
	function removeHyphenation() {
		var body = document.getElementsByTagName("body")[0];
		var elements, i, l;
		if (Hyphenator.isBookmarklet()) {
			Hyphenator.deleteHyphenationInElement(body);
		} else {
			if (document.getElementsByClassName) {
				elements = document.getElementsByClassName(hyphenateclass);
				for (i = 0, l = elements.length; i < l; i++) {
					Hyphenator.deleteHyphenationInElement(elements[i]);
				}
			} else {
				elements = document.getElementsByTagName("*");
				for (i = 0, l = elements.length; i < l; i++) {
					if (elements[i].className.indexOf(hyphenateclass) !== -1) {
						Hyphenator.deleteHyphenationInElement(elements[i]);
					}
				}
			}
		}
	}
	function runOnContentLoaded(w, f) {
		var d = w.document, D = "DOMContentLoaded", u = w.navigator.userAgent
				.toLowerCase(), v = parseFloat(u
				.match(/.+(?:rv|it|ml|ra|ie)[\/: ]([\d.]+)/)[1]), documentloaded = false;
		function init(e) {
			if (!documentloaded) {
				documentloaded = true;
				f((e.type && e.type === D) ? e : {
					type : D,
					target : d,
					eventPhase : 0,
					currentTarget : d,
					timeStamp : new Date().getTime(),
					eventType : e.type || e
				});
			}
		}
		if (/webkit\//.test(u) && v < 525.13) {
			(function() {
				if (/complete|loaded/.test(d.readyState)) {
					init("khtml-poll");
				} else {
					setTimeout(arguments.callee, 10);
				}
			})();
		} else {
			if (/msie/.test(u) && !w.opera) {
				d.attachEvent("onreadystatechange", function(e) {
					if (d.readyState === "complete") {
						d.detachEvent("on" + e.type, arguments.callee);
						init(e);
					}
				});
				if (w == top) {
					(function() {
						try {
							d.documentElement.doScroll("left");
						} catch (e) {
							setTimeout(arguments.callee, 10);
							return;
						}
						init("msie-poll");
					})();
				}
			} else {
				if (d.addEventListener && (/opera\//.test(u) && v > 9)
						|| (/gecko\//.test(u) && v >= 1.8)
						|| (/khtml\//.test(u) && v >= 4)
						|| (/webkit\//.test(u) && v >= 525.13)) {
					d.addEventListener(D, function(e) {
						d.removeEventListener(D, arguments.callee, false);
						init(e);
					}, false);
				} else {
					var oldonload = w.onload;
					w.onload = function(e) {
						init(e || w.event);
						if (typeof oldonload === "function") {
							oldonload(e || w.event);
						}
					};
				}
			}
		}
	}
	function prepare(callback) {
		if (!enableRemoteLoading) {
			preparestate = 2;
			callback();
			return;
		}
		preparestate = 1;
		var doclanguages = {};
		doclanguages[mainlanguage] = true;
		var elements = document.getElementsByTagName("body")[0]
				.getElementsByTagName("*");
		var lang = null;
		var i, l;
		for (i = 0, l = elements.length; i < l; i++) {
			if (!!(lang = getLang(elements[i], true))) {
				if (SUPPORTEDLANG[lang]) {
					doclanguages[lang] = true;
				} else {
				}
			}
		}
		for (lang in doclanguages) {
			if (doclanguages.hasOwnProperty(lang)) {
				loadPatterns(lang);
			}
		}
		var interval = window.setInterval(function() {
			var finishedLoading = false;
			for (lang in doclanguages) {
				if (!patternsloaded[lang]) {
					finishedLoading = false;
					break;
				} else {
					finishedLoading = true;
				}
			}
			if (finishedLoading) {
				window.clearInterval(interval);
				preparestate = 2;
				callback();
			}
		}, 100);
	}
	function autoinit() {
		for ( var lang in SUPPORTEDLANG) {
			if (SUPPORTEDLANG.hasOwnProperty(lang)) {
				patternsloaded[lang] = false;
			}
		}
		autoSetMainLanguage();
		urlhyphen = createZeroWidthSpace();
		checkIfBookmarklet();
	}
	autoinit();
	return {
		leftmin : {},
		rightmin : {},
		shortestPattern : {},
		longestPattern : {},
		specialChars : {},
		patterns : {},
		init : function(obj) {
			if (obj.classname) {
				Hyphenator.setClassName(obj.classname);
			}
			if (obj.minwordlength) {
				Hyphenator.setMinWordLength(obj.minwordlength);
			}
			if (obj.hyphenchar) {
				Hyphenator.setHyphenChar(obj.hyphenchar);
			}
			if (obj.togglebox) {
				Hyphenator.setDisplayToggleBox(obj.togglebox);
			}
			if (obj.urlhyphenchar) {
				Hyphenator.setUrlHyphenChar(obj.urlhyphenchar);
			}
			if (obj.remoteloading) {
				Hyphenator.setRemoteLoading(obj.remoteloading);
			}
		},
		run : function() {
			runOnContentLoaded(window, function() {
				hideInside();
				Hyphenator.hyphenateDocument();
				if (displayToggleBox) {
					switchToggleBox(true);
				}
			});
		},
		addExceptions : function(words) {
			var w = words.split(",");
			for ( var i = 0, l = w.length; i < l; i++) {
				var key = w[i].replace(/-/g, "");
				if (!exceptions[key]) {
					exceptions[key] = w[i];
				}
			}
		},
		setClassName : function(str) {
			hyphenateclass = str || "hyphenate";
		},
		setMinWordLength : function(mymin) {
			min = mymin || 6;
		},
		setHyphenChar : function(str) {
			if (str === "&shy;") {
				str = String.fromCharCode(173);
			}
			hyphen = str || String.fromCharCode(173);
		},
		
		setDisplayToggleBox : function(bool) {
			if (bool === undefined) {
				var bool = true;
			}
			displayToggleBox = bool;
		},
		setUrlHyphenChar : function(str) {
			urlhyphen = str || createZeroWidthSpace();
		},
		setRemoteLoading : function(bool) {
			enableRemoteLoading = bool;
		},
		setEnableCache : function(bool) {
			enableCache = bool;
		},
		updatePatternsLoadState : function(lang, bool) {
			patternsloaded[lang] = bool;
		},
		isBookmarklet : function() {
			return bookmarklet;
		},
		hyphenateDocument : function() {
			function callback() {
				convertPatternsToObject();
				runHyphenation();
			}
			if (preparestate !== 2) {
				if (preparestate === 0) {
					prepare(callback);
				}
			}
		},
		hyphenateElement : function(el, lang) {
			if (el.className.indexOf("donthyphenate") !== -1) {
				return;
			}
			if (!lang) {
				lang = getLang(el, true);
			} else {
				var elemlang = getLang(el, true);
				if (elemlang !== null) {
					lang = elemlang;
				}
			}
			var wrd = "[\\w" + Hyphenator.specialChars[lang] + "@" + String.fromCharCode(173) + "-]{" + min + ",}";
			function hyphenate(word) {
				if (urlRE.test(word) || mailRE.test(word)) {
					return Hyphenator.hyphenateURL(word);
				} else {
					return Hyphenator.hyphenateWord(lang, word);
				}
			}
			var genRegExp = new RegExp("(" + url + ")|(" + mail + ")|(" + wrd
					+ ")", "gi");
			var n, i;
			for (i = 0; (n = el.childNodes[i]); i++) {
				if (n.nodeType === 3 && n.data.length >= min) {
					n.data = n.data.replace(genRegExp, hyphenate);
				} else {
					if (n.nodeType === 1
							&& !DONTHYPHENATE[n.nodeName.toLowerCase()]) {
						Hyphenator.hyphenateElement(n, lang);
					}
				}
			}
			if (el.className.indexOf(hyphenateclass) !== -1) {
				el.style.visibility = "visible";
			}
		},
		deleteHyphenationInElement : function(el) {
			var h, i, n;
			switch (hyphen) {
			case "|":
				h = "\\|";
				break;
			case "+":
				h = "\\+";
				break;
			case "*":
				h = "\\*";
				break;
			default:
				h = hyphen;
			}
			for (i = 0; (n = el.childNodes[i]); i++) {
				if (n.nodeType === 3) {
					n.data = n.data.replace(new RegExp(h, "g"), "");
				} else {
					if (n.nodeType === 1) {
						Hyphenator.deleteHyphenationInElement(n);
					}
				}
			}
		},
		hyphenateWord : function(lang, word) {
			if (word === "") {
				return "";
			}
			if (word.indexOf(String.fromCharCode(173)) !== -1) {
				return word;
			}
			if (exceptions.hasOwnProperty(word)) {
				return exceptions[word].replace(/-/g, hyphen);
			}
			if (enableCache && cache.hasOwnProperty(lang)
					&& cache[lang].hasOwnProperty(word)) {
				return cache[lang][word];
			}
			if (word.indexOf("-") !== -1) {
				var parts = word.split("-");
				for ( var i = 0, l = parts.length; i < l; i++) {
					parts[i] = Hyphenator.hyphenateWord(lang, parts[i]);
				}
				return parts.join("-" + zerowidthspace);
			}
			var w = "_" + word + "_";
			var wl = w.length;
			var s = w.split("");
			w = w.toLowerCase();
			var hypos = [];
			var p, maxwins, win, pat = false, patl, c, digits, z;
			var numb3rs = {
				"0" : true,
				"1" : true,
				"2" : true,
				"3" : true,
				"4" : true,
				"5" : true,
				"6" : true,
				"7" : true,
				"8" : true,
				"9" : true
			};
			var n = wl - Hyphenator.shortestPattern[lang];
			for (p = 0; p <= n; p++) {
				maxwins = Math.min((wl - p), Hyphenator.longestPattern[lang]);
				for (win = Hyphenator.shortestPattern[lang]; win <= maxwins; win++) {
					if (Hyphenator.patterns[lang].hasOwnProperty(w.substr(p,
							win))) {
						pat = Hyphenator.patterns[lang][w.substr(p, win)];
					} else {
						continue;
					}
					digits = 1;
					patl = pat.length;
					for (i = 0; i < patl; i++) {
						c = pat.charAt(i);
						if (numb3rs[c]) {
							if (i === 0) {
								z = p - 1;
								if (!hypos[z] || hypos[z] < c) {
									hypos[z] = c;
								}
							} else {
								z = p + i - digits;
								if (!hypos[z] || hypos[z] < c) {
									hypos[z] = c;
								}
							}
							digits++;
						}
					}
				}
			}
			var inserted = 0;
			for (i = Hyphenator.leftmin[lang]; i <= (word.length - Hyphenator.rightmin[lang]); i++) {
				if (!!(hypos[i] & 1)) {
					s.splice(i + inserted + 1, 0, hyphen);
					inserted++;
				}
			}
			var hyphenatedword = s.slice(1, -1).join("");
			if (enableCache) {
				cache[lang][word] = hyphenatedword;
			}
			return hyphenatedword;
		},
		hyphenateURL : function(url) {
			return url.replace(/([:\/\.\?#&_,;!@]+)/gi, "$&" + urlhyphen);
		},
		toggleHyphenation : function() {
			var currentText = document.getElementById("HyphenatorToggleBox").firstChild.nodeValue;
			if (currentText === "Hy-phe-na-ti-on") {
				removeHyphenation();
				document.getElementById("HyphenatorToggleBox").firstChild.nodeValue = "Hyphenation";
			} else {
				runHyphenation();
				document.getElementById("HyphenatorToggleBox").firstChild.nodeValue = "Hy-phe-na-ti-on";
			}
		}
	};
}();
if (Hyphenator.isBookmarklet()) {
	Hyphenator.hyphenateDocument();
}
