/**
 * @author Gregor Aisch, Dave Gööck, Christoph Mewes, Zozi
 * @publisher webvariants GbR
 * @website http://www.webvariants.de/
 * @contact info - webvariants - de
 * 
 * Dieses Script benötigt entweder jQuery (http://www.jquery.com/) oder
 * Prototype (http://www.prototypejs.org/). jQuery wird bevorzugt.
 * 
 * Es werden sowohl die einfache sowie die erweiterte (mit Mail-Titel) mapson Syntax
 * unterstützt. Da das MoreCSS Hyphenation Plugin sämtliche in seinem Einflussbereich
 * befindlichen mapson Nodes ersetzt wurde das hypheneation script um ein callback erweitert,
 * welches hier benutzt wird um nach der Ersetzung die Eventhandler neu zu setzen.   
 * 
 */

function replaceMailToLink(e){
	var ext = e.childNodes[1];
	var contNode = e; 
	var link = '';
	
	if (ext.className == 'liam') {
		var title = e.firstChild;
		contNode = ext;
	}
				
	for (var c=0; c<contNode.childNodes.length; c++)
			if (contNode.childNodes[c].nodeType == 3) link += contNode.childNodes[c].nodeValue.replace(/ ?(­|&#173;|&shy;|&#x00AD;) ?/g, "");
	
	var a = document.createElement('a');
	if (title == undefined) var title = link;
	a.href = 'mailto:'+link;
	
	a.appendChild(title);
	
	e.parentNode.replaceChild(a, e);
}

function setMapsonEvent() {
	if (typeof jQuery!="undefined") {
		jQuery('span.mapson').unbind("mouseover");
		jQuery('span.mapson').mouseover(function() {
			replaceMailToLink(this);
		});
	}else if (typeof Prototype!="undefined") {
		$$('span.mapson').each(function(e) {
			e.stopObserving('mouseover');
			e.observe('mouseover', function(event) {
				replaceMailToLink(e);
			});
		});	
	}
}

function setHyphenationFunction() {
	if(typeof MoreCSS != 'undefined' && typeof MoreCSS.hyphenation == 'function'){
		var oldHyphenation = MoreCSS.hyphenation;
		MoreCSS.hyphenation = function(element, properties) {
			oldHyphenation(element, properties, function(){ setMapsonEvent(); });
		}
	}
}

if (typeof jQuery != 'undefined') {
	(function($) { /* on dom load... */ $(function() {
		setHyphenationFunction();
		setMapsonEvent();
	}); /* dom loaded */ })(jQuery);
}
else if (typeof Prototype != 'undefined') {
	document.observe('dom:loaded', function() {
		setHyphenationFunction();
		setMapsonEvent();
	});
}
