function Quote(sText, sUsername, sTimestamp){
	if (!$('commentMessage')){
		return false;
	}
	
	// prepare text
	sText = sText
		.replace(/<span(.+)<\/span>/, '')
		.replace(/<([^>]+)>/g, '')
		.replace(/\s+/, ' ')
		.trim();
	var s_quote = "[quote=" + sUsername + "," + sTimestamp + "]" + sText + "[/quote]\n\n";
	
	// set quote
	$('commentMessage').set('value', s_quote);
	
	// focus quote
	var oFx =new Fx.Scroll('commentMessage', {'duration': 50});
	$('commentMessage').focus();
	oFx.toBottom();
}

window.addEvent('domready', function(oEvent) {
	// article overview
	(new Ticker('archiveTicker', '/json/articles/overview.json'));
	
	// quote buttons
	$$('a.commentQuote').each(function(oEl){
		oEl.addEvent('click', function(oEvent){
			oEvent.stop();
			
			// get parent
			o_parent = oEvent.target.getParent('div.commentContent');
			// get content
			o_content = o_parent.getChildren('p');
			if (o_content.length === 0){
				return false;
			}
			// get meta
			o_meta = o_parent.getChildren('span.commentTitle');
			if (o_meta.length === 0){
				return false;
			}
			o_timestamp = o_meta[0].getChildren('strong');
			if (o_timestamp.length === 0){
				return false;
			}
			o_username = o_meta[0].getChildren('a');
			if (o_username.length === 0){
				return false;
			}
			
			// set quote
			Quote(o_content[0].get('text'), o_username[0].get('text'), o_timestamp[0].get('text'));
		});
	});
	
	// close reaction button
	$$('a.closeReaction').each(function(oEl){
		oEl.addEvent('click', function(oEvent){
			oEvent.stop();
			if (!confirm('Weet je het zeker?')){
				return;
			}
			var o_req = new Request({
				'url': '/ajax/close_article_reaction/',
				'method': 'post',
				'data': $H({
					'reactionid': $(oEvent.target).get('title')
				}).toQueryString(),
				'onComplete': function(){
					location.reload(true);
				}
			});
			o_req.send();
		});
	});
	
	// facebook button
	if ($('facebookShare')){
		$('facebookShare').addEvent('click', function(oEvent){
			oEvent.stop();

			var s_url = this.get('href');
			var s_title = this.get('title');
			window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(s_url) + '&t=' + encodeURIComponent(s_title), 'sharer', 'toolbar=0,status=0,width=626,height=436');
			return false;
		});
	}
	
	// print buttons
	$('printButton').addEvent('click', function(){
		window.print();
	});
});
