
function TellAFriendObject(linkTag) {

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

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

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

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

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

}

$(function() {
	window.TellAFriend = new TellAFriendObject($('a#tafShowFormLink'));
});

