var activity_handler = new Class({
	Implements: Options,
	options: {
		url: document.location.href.replace(/#.*$/, ''),		// <3 IE
		//url: '/meineraffe/',
		category: 'list',
		offset: 0,
		paginate: true,
		timer: 0
	},
	dom: {
		container: null,
		pagination: null
	},
	data: [],
	settings: null,

	initialize: function(container, options, isTreffpunkt) {
		if(!$(container)) {
			return false;
		}
		this.dom.container = $(container);
		this.setOptions(options);

		this.settings = new activity_settings();

		if (!isTreffpunkt)
			var isTreffpunkt = 0;
		
		if(document.location.href.indexOf('meineraffe') >= 0 || document.location.href.indexOf('mitglieder') >= 0 || isTreffpunkt == 1 || document.location.href.indexOf('gruppen') >= 0){
			this.dom.pagination = new Element('a', {
				'class': 'more',
				'href': '#',
				'title': 'Weitere Einträge',
				events: {
					mouseover: function() {
						this.dom.pagination = this.dom.pagination.dispose();
						this.list();
					}.bind(this)
				}
			});
		}else{
			this.dom.pagination = new Element('a', {
				'class': 'more',
				'href': '#',
				'title': 'Weitere Einträge',
				events: {
					click: function() {
						document.location.href='/meineraffe/';
						return false;
					}.bind(this)
				}
			});
		}

		if(document.location.href.indexOf('meineraffe') >= 0 || document.location.href.indexOf('mitglieder') >= 0 || document.location.href.indexOf('treffpunkte') >= 0 || document.location.href.indexOf('gruppen') >= 0){
			(new Element('img', {
				'src': '/app/1/img/icons/arrow-down.png',
				'alt': '',
				styles: {
					'float': 'right'
				}
			})).inject(this.dom.pagination);
			(new Element('img', {
				'src': '/app/1/img/icons/arrow-down.png',
				'alt': '',
				styles: {
					'float': 'left'
				}
			})).inject(this.dom.pagination);
		}

		this.dom.pagination.appendText('Weitere Einträge');

		if (typeof activityJSON != 'undefined') {
			this.options.category = activityJSON.category;
			this.options.offset = activityJSON.offset;
			$A(activityJSON.items).each(function(item, index) {
				item.index = index;
				this.data.push(new activity_item(this, item));
			}, this);
			this.dom.container.empty();
			this.draw();
			if (activityJSON.items.length > 0) {
				this.dom.pagination.inject(this.dom.container);
			}
			if (this.data.length == 0) {
				p = new Element('p', {'text': 'Keine Einträge gefunden.'});
				//p.inject.delay(2000, p, this.dom.container);
			}
		}
	},

	// list(category, tab) optional
	list: function() {
		if (!this.dom.container || this.dom.container == null)
			return false;

		var category = this.options.category;
		if (arguments[0] && $type(arguments[0]) == 'string'){
			category = arguments[0];
		}
		this.options.offset += 25;
		if (this.options.category != category) {
			this.options.category = category;
			this.options.offset = 0;
			//argument 2 = offset
			if(arguments[2] && $type(arguments[2] == 'integer')){
				this.options.offset = arguments[2];
			}
			if (this.data.length > 0) {
				this.dom.pagination = this.dom.pagination.dispose();
			}
			this.data = [];
			this.dom.container.empty();

			if (arguments[1]) {
				var elem = $(arguments[1]);
				elem.getParent('ul').getElements('li').removeClass('active');
				elem.getParent('li').addClass('active');
			}
		}else{
			//argument 2 = offset
			if(arguments[2] && $type(arguments[2] == 'integer')){
				return;
			}
		}
		var loader = new Element('p');
		(new Element('img', {
			'src': '/app/1/img/button/load.gif',
			'styles': {
				'vertical-align': 'middle',
				'margin-left': '190px',
				'margin-top': '20px'
			}
		})).inject(loader);
		loader.inject(this.dom.container);

		var req = new Request.JSON({
			IVWClick: true,
			url: this.options.url,
			onSuccess: function(responseJSON) {
				if (loader != null) loader.destroy();
				if (responseJSON.category != this.options.category) return false;

				this.options.offset = parseInt(responseJSON.offset);
				var len = this.data.length;
				$A(responseJSON.items).each(function(item, index) {
					item.index = len + index;
					this.data.push(new activity_item(this, item));
				}, this);
				this.draw();
				if (responseJSON.items.length > 0) {
					this.dom.pagination.inject(this.dom.container);
				}
				if (this.data.length == 0) {
					p = new Element('p', {'text': 'Keine Einträge gefunden.'});
					p.inject.delay(2000, p, this.dom.container);
				}
			}.bind(this),
			onFailure: function() {
				this.dom.container.empty();
				this.dom.container.appendText("Bei der Anfrage ist ein Fehler aufgetreten! Bitte versuche es später erneut.");
			}.bind(this)
		});
		req.post({
			'ajax': 'activitylist',
			'category': this.options.category,
			'offset': this.options.offset
		});

		if(this.options.category == 'list'){
			/* Wird nicht gebraucht
			  args = [{
				'ajax': 'activitylist',
				'category': this.options.category,
				'offset': this.options.offset
			}];
			this.options.timer = req.post.periodical(45000, req, args);
			*/
			if(this.options.timer != 0){
				$clear(this.options.timer);
				this.options.timer = 0;
			}
		}
		else{
			if(this.options.timer != 0){
				$clear(this.options.timer);
				this.options.timer = 0;
			}
		}

	},

	draw: function() {
		this.data.each(function(item, index) {
			item.draw();
		}, this);
	},

	block: function(blockedItem, blockedCriteria) {

		var description = (blockedCriteria == 'type'?blockedItem.title:(blockedItem.category == 'friend'?blockedItem.source.nickname:blockedItem.source.name));

		new Request({
			url: this.options.url,
			onSuccess: function() {
				var blockedMessage = new Element('div', {
					'class': 'eventbox'
				});
				(new Element('p', {
					'text': '"' + description + '" wurde in deinen Aktivitäten verborgen.',
					styles: {
						'font-weight': 'bold',
						'margin-bottom': '10px'
					}
				})).inject(blockedMessage);
				var messageText = new Element('p', {
					'text': 'In Zukunft werden dir keine weiteren Aktivitäten von "' + description + '" angezeigt. Um dies zu ändern bearbeite deine '
				});
				(new Element('a', {
					'href': '/einstellungen/aktivitaeten/',
					'title': 'Einstellungen',
					'text': 'Einstellungen'
				})).inject(messageText);
				messageText.appendText('.');

				var div = new Element('div', {
					'class': 'clearfix'
				});

				var p = new Element('p', {
					'class': 'btn white middle right'
				});

				(new Element('a', {
					'href': '#',
					'title': 'Rückgängig',
					'text': 'Rückgängig',
					styles: {
						'font-weight': 'bold'
					},
					events: {
						click: this.blockUndo.bind(this, [ blockedMessage, blockedItem, blockedCriteria ])
					}
				})).inject(p);
				p.inject(div);
				div.inject(messageText);
				messageText.inject(blockedMessage);


				blockedMessage.inject(blockedItem.dom.container, 'after');

				this.hideBlockedMessage.delay(20000, this, blockedMessage);

				this.hideItems(blockedItem, blockedCriteria);

				//new Fx.Scroll(window).toElement(blockedMessage);
			}.bind(this),
			onFailure: function() {
				alert('Bei der Anfrage ist ein Fehler aufgetreten! Bitte versuche es später erneut.');
			}
		}).post({
			'ajax': 'activityblock',
			'id': blockedItem.id,
			'criteria': blockedCriteria
		});

		return false;
	},

	blockUndo: function(blockedMessage, blockedItem, blockedCriteria) {
		blockedMessage.hide();
		if (blockedItem == null || blockedCriteria == null) {
			return false;
		}

		new Request({
			url: this.options.url,
			onSuccess: this.showItems.bind(this, [ blockedItem, blockedCriteria ]),
			onFailure: function() {
				alert('Bei der Anfrage ist ein Fehler aufgetreten! Bitte versuche es später erneut.');
			}
		}).post({
			'ajax': 'activityunblock',
			'id': blockedItem.id,
			'criteria': blockedCriteria
		});

		return false;
	},

	hideBlockedMessage: function(elem) {
		if (!elem) return;
		new Fx.Slide(elem).slideOut().chain(function() { elem.destroy(); });
	},

	hideItems: function(blockedItem, blockedCriteria) {
		this.data.each(function(item) {
			if (blockedCriteria == 'type' && item.title == blockedItem.title && item.category == blockedItem.category) {
				item.hide();
			}
			else if (blockedCriteria == 'source' && item.category == blockedItem.category) {
				if ((item.category == 'friend' && item.source.nickname == blockedItem.source.nickname) || (item.category != 'friend' && item.source.name == blockedItem.source.name)) {
					item.hide();
				}
			}
		}, this);
	},

	showItems: function(blockedItem, blockedCriteria) {
		this.data.each(function(item) {
			if (blockedCriteria == 'type' && item.title == blockedItem.title && item.category == blockedItem.category) {
				item.show();
			}
			else if (blockedCriteria == 'source' && item.category == blockedItem.category) {
				if ((item.category == 'friend' && item.source.nickname == blockedItem.source.nickname) || (item.category != 'friend' && item.source.name == blockedItem.source.name)) {
					item.show();
				}
			}
		}, this);
	}

});


var activity_settings = new Class({
	items: [],

	initialize: function() {
		document.addEvent('click', this.hideMenus.bind(this));
	},

	createButton: function() {
		var button = new activity_settings_item();
		button.addEvent('showMenu', this.hideMenus.bind(this));
		this.items.push(button);

		return button;
	},

	hideMenus: function() {
		var dontHide = arguments[0];
		this.items.each(function(item) {
			if (dontHide && dontHide == item) {
				return;
			}
			item.hideMenu();
		});
	},

	showMenus: function() {
		var dontHide = arguments[0];
		this.items.each(function(item) {
			if (dontHide && dontHide == item) {
				return;
			}
			item.showMenu();
		});
	}

});

var activity_settings_item = new Class({
	Implements: Events,
	dom: {
		button: null,
		menu: null
	},
	menuVisible: false,
	buttonHover: false,
	parentHover: false,

	initialize: function() {
		this.dom.button = new Element('a', {
			'href': '#',
			'title': 'Ausblenden',
			'class': 'activity-option',
			styles: {
				'display': 'none'
			},
			events: {
				mouseenter: this.buttonEnter.bind(this),
				mouseleave: this.buttonLeave.bind(this),
				click: this.toggleMenu.bind(this)
			}
		});

		// menu
		this.dom.menu = new Element('ul', {
			'class': 'activity-optionsmenu',
			styles: {
				'display': 'none'
			}
		});

	},

	inject: function(elem) {
		this.dom.button.inject(elem);
		this.dom.menu.inject(elem);
		elem.addEvents({
			mouseenter: this.parentEnter.bind(this),
			mouseleave: this.parentLeave.bind(this)
		});
	},

	buttonEnter: function() {
		this.buttonHover = true;
		if (!this.menuVisible) {
			this.dom.button.addClass('activity-option-button');
		}
	},

	buttonLeave: function() {
		this.buttonHover = false;
		if (!this.menuVisible) {
			this.dom.button.removeClass('activity-option-button');
		}
	},

	parentEnter: function() {
		this.parentHover = true;
		if (!this.menuVisible) {
			this.dom.button.show();
			if (!this.buttonHover) {
				this.dom.button.removeClass('activity-option-button');
			}
		}
	},

	parentLeave: function() {
		this.parentHover = false;
		if (!this.menuVisible) {
			this.dom.button.hide();
			if (!this.buttonHover) {
				this.dom.button.removeClass('activity-option-button');
			}
		}
	},

	addMenuItem: function(title) {
		var li = new Element('li');

		var p = new Element('p', {
			'class': 'btn white '
		});



		var lianchor = new Element('a', {
			'href': '#',
			'title': '' + title + ' ausblenden',
			'text': '' + title + ' ausblenden',
			events: {
				click: this.hideMenu.bind(this)
			}
		});
		if (arguments[1] && $type(arguments[1]) == 'object') {
			lianchor.addEvents(arguments[1]);
		}

		lianchor.inject(li);
		li.inject(p);
		p.inject(this.dom.menu);
	},

	showMenu: function() {
		this.dom.menu.show();
		this.menuVisible = true;

		this.fireEvent('showMenu', this);
		return false;
	},

	hideMenu: function() {
		this.dom.menu.hide();
		this.menuVisible = false;
		if (!this.buttonHover) {
			this.dom.button.removeClass('activity-option-button');
		}
		if (!this.parentHover) {
			this.dom.button.hide();
		}

		this.fireEvent('hideMenu', this);
		return false;
	},

	toggleMenu: function() {
		if (this.menuVisible)
			return this.hideMenu();
		else
			return this.showMenu();
	}


});

var activity_item = new Class({
	id: 0,
	title: null,
	created: null,
	category: null,
	source: null,
	creator: null,
	ratings: 0,
	hasrated: false,
	content: null,
	comments: [],
	additionalclass: null,
	allowcomments: true,
	allowsettings: true,
	ratingusers: [],
	links: [],

	index: 0,
	parent: null,
	dirty: false,
	hidden: false,
	placeholderText: 'Dein Kommentar ...',

	dom: {
		parentContainer: null,
		container: null,
		optionbutton: null,
		optionmenu: null,
		commentForm: null
	},

	initialize: function(parent, data) {
		this.parent = parent;
		this.dom.parentContainer = parent.dom.container;

		this.setData(data);
	},

	getSource: function(category, data) {
		if (category == 'group' || category == 'place') {
			return {
				id: data.i,
				name: data.n,
				avatar: data.a
			}
		}
		else {
			return {
				id: data.i,
				nickname: data.n,
				avatar: data.a,
				forename: data.f,
				surname: data.s,
				age: data.ag,
				gender: data.g,
				region: data.r,
				online: data.o,
				club: data.c,
				trusted: data.t,
				links: data.l
			}
		}
	},

	setData: function(data) {
		this.id = data.i;
		this.title = data.t;
		this.created = data.cr;
		this.category = data.ca;

		this.source = this.getSource(this.category, data.s);
		if (data.c) {
			this.creator = this.getSource('', data.c);
		}
		this.ratings = data.r;
		this.hasrated = data.hr;
		this.content = data.c;
		this.comments = data.cm;
		this.additionalclass = data.adc;
		this.allowcomments = data.ac;
		this.allowsettings = data.as;
		this.ratingusers = data.ru;
		this.links = data.l;

		if (typeof data.index != 'undefined') this.index = data.index;

		this.dirty = true;
	},

	addComment: function() {
		if (!this.allowcomments) {
			return false;
		}
		var textarea = this.dom.commentForm.getElement('textarea');
		if (textarea.value.trim() == '' || textarea.value.trim() == this.placeholderText) {
			return false;
		}

//		this.dom.commentForm.getElement('input[type=submit]').set({
//			'disabled': 'disabled',
//			'value': 'Wird gespeichert...',
//			'class': 'button-cancel'
//		});

		(new Request.JSON({
			url: this.parent.options.url,
			onSuccess: (function(responseJSON) {
				this.setData(responseJSON);
				this.draw();
			}).bind(this),
			onFailure: (function() {
				alert('Dein Kommentar konnte nicht gespeichert werden! Bitte versuche es später erneut.');
				this.reload();
			}).bind(this)
		})).post(this.dom.commentForm);

		return false;
	},

	reload: function() {
		(new Request.JSON({
			url: this.parent.options.url,
			onSuccess: (function(responseJSON) {
				this.setData(responseJSON);
				this.draw();
			}).bind(this)
		})).post({
			'ajax': 'activityget',
			'id': this.id
		});
	},

	hide: function() {
		this.dom.container.hide();
		this.hidden = true;
	},

	show: function() {
		this.dom.container.show();
		this.hidden = false;
	},

	toggle: function() {
		if (this.hidden)
			this.show();
		else
			this.hide();
	},

	draw: function() {
		if (this.dirty) {
			// wenn der container bereits existiert
			if (this.dom.container != null) {
				this.dom.container.empty().removeClass('even').removeClass('odd');
			}
			else {
				this.dom.container = new Element('div', {
					'class': 'activity'
				});
				if (this.additionalclass) {
					this.dom.container.addClass(this.additionalclass);
				}
				this.dom.container.inject(this.dom.parentContainer);
			}
			this.dom.container.addClass((this.index%2==0?'even':'odd'));

			this.drawRating();
			switch (this.category) {
				case 'friend':
					this.drawFriend();
					break;

				case 'place':
					this.drawPlace();
					break;

				case 'group':
					this.drawGroup();
					break;
			}
			if (this.allowsettings) {
				this.drawOptions();
			}
			this.dirty = false;
		}
	},

	drawFriend: function() {
		// Avatar
		var avatar = new Element('a', {
			'class': 'activity-avatar',
			'href': '/mitglieder/profil/' + this.source.nickname + '/',
			'title': 'Profilseite von ' + this.source.nickname
		});
		(new Element('img', {
			'src': this.source.avatar,
			'alt': ''
		})).inject(avatar);
		avatar.inject(this.dom.container);

		// Status Icons
		var icons = new Element('div', {
			'class': 'activity-icons'
		});
		(new Element('img', {
			'src': '/app/1/img/new_layout/icon/' + (this.source.online?'online':'offline') + '.gif',
			'alt': '',
			'styles': {
				'float': 'right'
			}
		})).inject(icons);

		if (this.source.trusted) {
			(new Element('img', {
				'src': '/app/1/img/new_layout/icon/trusted.gif',
				'alt': ''
			})).inject(icons);
		}

		if (this.source.club) {
			(new Element('img', {
				'src': '/app/1/img/new_layout/icon/club.gif',
				'alt': ''
			})).inject(icons);
		}
		icons.inject(this.dom.container);
		// body
		var body = new Element('div', {
			'class': 'activity-body',
			'html': this.content
		});
		body.inject(this.dom.container);
		// headline
		(new Element('div', {
			'class': 'activity-headline2',
			'text': this.source.age + ' Jahre, ' + this.source.region
		})).inject(body, 'top');

		var headline = new Element('div', {
			'class': 'activity-headline'
		});
		headline.inject(body, 'top');

		var headlineAnchor = new Element('a', {
			'href': '/mitglieder/profil/' + this.source.nickname + '/',
			'title': 'Profilseite von ' + this.source.nickname
		});
		headlineAnchor.inject(headline);

		(new Element('b', {
			'text': this.source.nickname
		})).inject(headlineAnchor);
		headlineAnchor.appendText(' (' + this.source.forename + ' ' + this.source.surname + ') ');

		(new Element('img', {
			'src': '/app/1/img/new_layout/icon/' + (this.source.gender == 1?'male':'female') + '.gif',
			'alt': ''
		})).inject(headlineAnchor);
		// footer
		var footer = new Element('div', {
			'class': 'activity-footer',
			'text': this.created
		});
		footer.inject(body);
		// comments
		if (this.allowcomments) {
			footer.appendText(' • ');
			(new Element('a', {
				'href': '#',
				'title': 'Einen Kommentar verfassen',
				'text': 'kommentieren',
				events: {
					'click': function() {
						if (this.dom.commentForm) {
							this.dom.commentForm.show();
							this.dom.commentForm.getElement('textarea').focus();
						}
						return false;
					}.bind(this)
				}
			})).inject(footer);
		}

		if(this.links.length > 0) {
			this.links.each(function(item) {
				footer.appendText(' • ');
				(new Element('a', item)).inject(footer);
			});
		}

		this.drawComments(footer);
	},

	drawPlace: function() {
		// Avatar
		var avatar = new Element('a', {
			'class': 'activity-avatar',
			'href': '/treffpunkte/profil/id,' + this.source.id + '/',
			'title': this.source.name
		});
		(new Element('img', {
			'src': this.source.avatar,
			'alt': ''
		})).inject(avatar);
		avatar.inject(this.dom.container);

		var body = new Element('div', {
			'class': 'activity-body',
			'html': this.content
		});
		body.inject(this.dom.container);

		var headline = new Element('span', {
			'class': 'activity-headline'
		});
		headline.inject(body, 'top');

		(new Element('a', {
			'href': '/treffpunkte/profil/id,' + this.source.id + '/',
			'title': this.source.name,
			'text': this.source.name
		})).inject(headline);

		var footer = new Element('div', {
			'class': 'activity-footer',
			'text': this.created
		});
		footer.inject(body);

		if (this.allowcomments) {
			footer.appendText(' • ');
			(new Element('a', {
				'href': '#',
				'title': 'Einen Kommentar verfassen',
				'text': 'kommentieren',
				events: {
					'click': function() {
						if (this.dom.commentForm) {
							this.dom.commentForm.show();
							this.dom.commentForm.getElement('textarea').focus();
						}
						return false;
					}.bind(this)
				}
			})).inject(footer);

			if(this.links.length > 0) {
				this.links.each(function(item) {
					footer.appendText(' • ');
					(new Element('a', item)).inject(footer);
				});
			}
		}

		this.drawComments(footer);
	},

	drawGroup: function() {
		// Avatar
		var avatar = new Element('a', {
			'class': 'activity-avatar',
			'href': '/gruppen/profil/id,' + this.source.id + '/',
			'title': this.source.name
		});
		(new Element('img', {
			'src': this.source.avatar,
			'alt': ''
		})).inject(avatar);
		avatar.inject(this.dom.container);

		var body = new Element('div', {
			'class': 'activity-body',
			'html': this.content
		});
		body.inject(this.dom.container);

		var headline = new Element('span', {
			'class': 'activity-headline'
		});
		headline.inject(body, 'top');

		(new Element('a', {
			'href': '/gruppen/profil/id,' + this.source.id + '/',
			'title': this.source.name,
			'text': this.source.name
		})).inject(headline);

		var footer = new Element('div', {
			'class': 'activity-footer',
			'text': this.created
		});
		footer.inject(body);

		if (this.allowcomments) {
			footer.appendText(' • ');
			(new Element('a', {
				'href': '#',
				'title': 'Einen Kommentar verfassen',
				'text': 'kommentieren',
				events: {
					'click': function() {
						if (this.dom.commentForm) {
							this.dom.commentForm.show();
							this.dom.commentForm.getElement('textarea').focus();
						}
						return false;
					}.bind(this)
				}
			})).inject(footer);

			if(this.links.length > 0) {
				this.links.each(function(item) {
					footer.appendText(' • ');
					(new Element('a', item)).inject(footer);
				});
			}
		}

		this.drawComments(footer);
	},

	drawRating: function() {

		if (this.hasrated) {
			var body = new Element('span', {
				'id': 'rate_' + this.id,
				'class': 'news-rating-done news-rating-topright',
				'title': 'Aktivität wurde ' + this.ratings + ' mal positiv bewertet'
			});

			(new Element('a', {
				'text': this.ratings
			})).inject(body);
			body.inject(this.dom.container, 'top');
		}
		else {
			var body = new Element('span', {
				'id': 'rate_' + this.id,
				'class': 'news-rating news-rating-topright',
				'title': 'Aktivität wurde ' + this.ratings + ' mal positiv bewertet'
			});
			(new Element('a', {
				'href': '#',
				'text': this.ratings,
				events: {
					'click': function(evt) {
						oRating.rate(this.id);
						evt.target.removeEvents();
						evt.stop();
					}.bind(this)
				}
			})).inject(body);
			body.inject(this.dom.container, 'top');
		}
	},

	drawRatingUsers: function() {
		if (this.ratingusers.length == 0) return;

		var container3 = new Element('div', {
			'class': 'activity-rating' + (this.comments.length == 0?'-last':'')
		});
		if (arguments[0])
			container3.inject(arguments[0]);
		else
			container3.inject(this.dom.container);

		var ratingspan = null;
		$A(this.ratingusers).each(function(data, index) {
			data = this.getSource('', data);	// short keys auflösen

			var userlink = new Element('a', {
				'href': '/mitglieder/profil/' +data.nickname+ '/',
				'text': data.nickname,
				'onmouseover': "tt.set(this, '" +data.avatar+ "');"
			});

			if (this.ratingusers.length > 2) {
				if (index == 2) {
					ratingspan = new Element('span', {
						'text': ', ',
						styles: {
							'display': 'none'
						}
					});
					ratingspan.inject(container3);

					(new Element('a', {
						'href': '#',
						'title': (this.ratingusers.length-2==1?'Mitglied anzeigen':(this.ratingusers.length-2) + ' weitere Mitglieder anzeigen'),
						'text': (this.ratingusers.length-2==1?' und ein weiteres Mitglied':' und ' +(this.ratingusers.length-2)+ ' weitere Mitglieder'),
						styles: {
							'font-size': 'smaller',
							'font-weight': 'normal'
						},
						events: {
							click: function() {
								this.getPrevious('span').setStyle('display', 'inline');
								this.destroy();
								return false;
							}
						}
					})).inject(container3);

					userlink.inject(ratingspan);
				}
				else if (index > 2) {
					ratingspan.appendText(', ');
					userlink.inject(ratingspan);
				}
				else {
					if (index > 0 && container3) container3.appendText(', ');
					userlink.inject(container3);
				}
			}
			else {
				if (index > 0 && container3) container3.appendText(', ');			
				userlink.inject(container3);
			}

		}, this);
		if (this.ratingusers.length > 1 && container3) {
			container3.appendText(' finden das gut!');
		}
		else if (container3) {
			container3.appendText(' findet das gut!');
		}
	},

	drawComments: function() {
		var comment = new Element('div', {
			'class': 'comment'
		});

		if (arguments[0])
			comment.inject(arguments[0]);
		else
			comment.inject(this.dom.container);

		this.drawRatingUsers(comment);

		var commentspan = null;
		$A(this.comments).each(function(data, index) {

			var body = new Element('div', {
				'class': 'activity-comment-body' + (index == 0?' first':'')
			});

			if (this.comments.length > 2) {
				if (index == 1) {
					var toolbar = new Element('p', {
						'class': 'toolbar',
						styles: {
							'padding': '10px 10px 0 0',
							'border-top': '1px dotted #fff'
						}
					});
					toolbar.inject(comment);

					(new Element('a', {
						'href': '#',
						'title': 'mehr Kommentare',
						'text': 'mehr Kommentare »',
						events: {
							click: function() {
								this.getParent().getNext('span').setStyle('display', 'inline');
								this.getParent().destroy();
								return false;
							}
						}
					})).inject(toolbar);

					commentspan = new Element('span', {
						styles: {
							'display': 'none'
						}
					});
					commentspan.inject(comment);
					body.inject(commentspan);
				}
				else if (index > 1 && index < this.comments.length-1) {
					body.inject(commentspan);
				}
				else {
					body.inject(comment);
				}
			}
			else {
				body.inject(comment);
			}

			var commentitem = new Element('div', {
				styles: {
					'margin-left': '6px'
				}
			});
			commentitem.inject(body);

			(new Element('a', {
				'href': '/mitglieder/profil/' + data.c.n,
				'title': 'Profilseite von ' + data.c.n,
				'onmouseover': 'tt.set(this, "' + data.c.a + '");',
				'text': data.c.n
			})).inject(commentitem);

			(new Element('p', {
				'class': 'comment-info',
				'text': data.cr
			})).inject(commentitem);

			(new Element('div', {
				'html': data.co,
				styles: {
					'margin': '0',
					'padding': '0'
				}
			})).inject(commentitem);
		}, this);

		if (this.allowcomments) {
			this.dom.commentForm = new Element('form', {
				'class': 'commentform placeholder',
				'id': 'commentform_'+this.id,
				'events': {
					'submit': this.addComment.bind(this)
				}
			});
			this.dom.commentForm.inject(comment);

			(new Element('input', {
				'type': 'hidden',
				'name': 'id',
				'value': this.id
			})).inject(this.dom.commentForm);

			(new Element('input', {
				'type': 'hidden',
				'name': 'ajax',
				'value': 'activitycomment'
			})).inject(this.dom.commentForm);

			var commentInput = new Element('textarea', {
				'name': 'comment-text',
				'html': this.placeholderText,
				'rows': '2',
				'cols': '40',
				'events': {
					'focus': this.showCommentForm.bind(this),
					'blur': this.hideCommentForm.bind(this),
					'keydown': function(evt) {
						if (evt.key == 'enter' && (evt.shift || evt.control || evt.alt)) {
							this.addComment();
							return false;
						}
					}.bind(this)
				}
			});
			commentInput.inject(this.dom.commentForm);

			var commentDiv = new Element('div', {
				'class': 'clearfix c_toolbar'
			});
			commentDiv.inject(this.dom.commentForm);

			var commentP = new Element('p', {
				'class': 'btn middle right green'
			});
			commentP.inject(commentDiv);

			var commentA = new Element('a', {
				'href': '#',
				'name': 'commentsubmit',
				'text': 'Speichern',
				'events': {
					'click': this.saveCommentForm.bind(this)
				}
			});

			commentA.inject(commentP);

			var commentSubmit = new Element('input', {
				'type': 'hidden',
				'name': 'commentsubmit',
				'value': '1'
			});
			commentSubmit.inject(commentP);
		}

		if (this.comments.length > 0) {
			var moreComments = new Element('a', {
				'href': '/startseite/aktivitaet/id,' + this.id + '/',
				'title': 'alle Kommentare',
				'text': 'alle Kommentare »',
				styles: {
					'font-weight': 'normal',
					'text-align': 'right'
				}
			});
			if (arguments[0])
				moreComments.inject(arguments[0]);
			else
				moreComments.inject(this.dom.container);
		}
		else {
			this.dom.commentForm.hide();
		}

	},

	saveCommentForm: function() {
		this.addComment();
		// $('commentform_'+this.id).submit();
		return false;
	},

	showCommentForm: function() {
		var textarea = this.dom.commentForm.getElement('textarea');
		if (textarea.value.trim() == this.placeholderText) textarea.value = '';
		this.dom.commentForm.removeClass('placeholder');
	},


	hideCommentForm: function() {
		var textarea = this.dom.commentForm.getElement('textarea');
		if (textarea.value.trim() == '') {
			textarea.value = this.placeholderText;
			this.dom.commentForm.addClass('placeholder');
		}
	},

	drawOptions: function() {
		var button = this.parent.settings.createButton();

		button.addMenuItem( (this.category == 'friend'?this.source.nickname:this.source.name), { click: this.parent.block.bind(this.parent, [ this, 'source' ]) });
		button.addMenuItem(this.title, { click: this.parent.block.bind(this.parent, [ this, 'type' ]) });

		button.inject(this.dom.container);
	}

});

/*
var activity = null;
window.addEvent('domready', function() {
	if (typeof activity_options == 'undefined') activity_options = {};
	activity = new activity_handler('activity-container', activity_options);
});
*/

if (typeof activity_options == 'undefined') activity_options = {};

if (!isTreffpunkt)
	var isTreffpunkt = 0;

var activity = new activity_handler('activity-container-me', activity_options, isTreffpunkt);
