
function TellAFriendObject(linkTag) {

	this.linkTag = linkTag;
	this.container = false;
	this.wrapper = false;
	
	var self = this;

	this.setupContainer = function() {
		this.linkTag.after('<div class="tellAFriendWrapper"><a href="#close" class="tafCloseMessageLink"><span>Schliessen</span></a><div class="tellAFriendContainer"></div></div>');
		this.wrapper = this.linkTag.next(); 
		this.container = this.wrapper.children(':last');
		this.wrapper.css({position: 'absolute', display: 'none'});  
	};

	this.showForm = function() {
		this.wrapper.css({position: 'absolute', display: 'none'});
		this.container.load(this.linkTag.attr('href') + ' .tellafriend.form', {}, function() {
			self.container.find('.tafForm').submit(function() { self.submitForm(); return false; });
			self.wrapper.find('.tafCloseMessageLink').click(function() { self.wrapper.fadeOut(1000, function() { self.container.html(''); }); return false; });
			self.wrapper.fadeIn(500);
		});
	};

	this.submitForm = function() {
		this.container.load(this.linkTag.attr('href') + '&' + this.container.find('.tafForm *').serialize() + ' .tellafriend.form', {}, function() {
			if (self.container.find('.tafForm').length > 0) {
				self.container.find('.tafForm').submit(function() { self.submitForm(); return false;});
			}
			self.container.find('.tafCloseMessageLink').click(function() { self.wrapper.fadeOut(500, function() { self.container.html(''); } ); return false; });
			self.wrapper.css({position: 'absolute'});
		});
	};

	this.setupContainer();
	this.linkTag.click(function() { self.showForm(); return false; });
}

$(function() {
	$('.tafShowFormLink').each(function () {
		var tellAFriend = new TellAFriendObject($(this));
	});
});


