//Transform mailto-links
jQuery(document).ready(function() {
	jQuery(".mailto_link").each(function() {
		var link = jQuery(this);
		var link_address = link.attr("href");
		link_address = link_address.replace(/!/g, ".");
		link_address = link_address.replace(/\^/g, "@");
		link_address = "mailto:"+link_address;
		link.attr("href", link_address);
	});
});

//Add tooltips to .portrait_items
jQuery(document).ready(function() {
	jQuery(".list_item img").each(function() {
		new Tooltip(this, null, null, document.documentElement);
		jQuery(this).attr("title", "");
	});
});

//Classes
var Tooltip = function(opener) {
	this.opener = jQuery(opener);
	this.content = jQuery(arguments[1] || this.opener.next());
	opener.tooltip = this;
  var appender = jQuery(arguments[2] || document.body);
  this.content.appendTo(appender);
  this.offsetParent = jQuery(arguments[3] || appender);
	this.enable();
};

Tooltip.prototype.display = function() {
	this.content.fadeIn(100);
};

Tooltip.prototype.hide = function() {
	this.content.fadeOut(100);
};

Tooltip.prototype.setPosition = function(x, y) {
  var offset = this.offsetParent.offset();
	this.content.css("left", x-(offset.left)+10);
	this.content.css("top", y-(offset.top)+10);
};

Tooltip.prototype.enable = function() {
	this.opener.bind("mouseenter", this.handleMouseover);
	this.opener.bind("mouseleave", this.handleMouseout);
  this.opener.mousemove(this.handleMousemove);
	this.hide();
};

Tooltip.prototype.handleMouseover = function(event) {
	var tooltip = this.tooltip;
	tooltip.setPosition(event.pageX, event.pageY);
	tooltip.display();
};

Tooltip.prototype.handleMouseout = function(event) {
	var tooltip = this.tooltip;
	tooltip.hide();
};

Tooltip.prototype.handleMousemove = function(event) {
	var tooltip = this.tooltip;
	tooltip.setPosition(event.pageX, event.pageY);
};
