var GAYARTICLE = new Class({
 	
	initialize : function() {
		
 	},
 	
 	DOMReady : function() {
 		var o_this = this;
		// get static url
		if ($('staticURL') && $('serverURL')){
			this.s_static_url = $('staticURL').get('text').trim();
			this.s_server_url = $('serverURL').get('text').trim();
		} else {
			this.s_static_url = '';
			this.s_server_url = '';
		}
		
		var o_visitors = new Swiff(this.s_static_url + '/assets/swf/overview.swf',{
			id:'otherSWF',
			container:$('otherSWFContainer'),
			width:264,
			height:250,
			vars: {url:this.s_server_url + '/xml/articles/overview.xml?'+Math.random()},
			properties: {name:'otherSWF'}
		});
		
		$('printButton').addEvent('click', function(){
			window.print();
		});
		
		// 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();
			});
		});
 	}
});

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();
}


/**
 * Create new class instance
 */
var moGAYARTICLE = new GAYARTICLE();

/**
 * DOM Ready callback
 *
 * @access  public
 * @param   event
 * @return  void
 */
window.addEvent('domready', function(oEvent) {
	moGAYARTICLE.DOMReady();
	
	// 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'));
		});
	});
});