	var eventId;
	var currentEvent;
	var currentEventDate;
	var sportId;
	var msgList = new Array();
	var author = new Array();

	function openCommentBox() {
		$('comment_box').style.display = 'block';
	}
	function closeCommentBox() {
		$('comment_box').style.display = 'none';
	}
	function loginFocus() {
		closeCommentBox();
		document.getElementById('myUsername').focus();
	}
	function toggleCommentHelp() {
		if ($('comment_help_box').style.display == 'none')
			$('comment_help_box').style.display = 'block';
		else
			$('comment_help_box').style.display = 'none';
	}
	function setCurrentEvent(event_id, name, date, sport_id) {
		openCommentBox();
		eventId = event_id;
		currentEvent = name;
		currentEventDate = date;
		sportId = sport_id;

		text = '<h3>' + name + '</h3><h4>' + date + '</h4>';
		$('comment_title').update(text);
		updateCommentList();
		try {
			pageTracker._trackPageview('/comments');
		} catch(err) {}
	}
/*
	local variable for author is 'author'
	remote is 'username'
*/
	function updateCommentList() {
		$('comment_list').update('en cours...');
		var url = '/comments/getCommentList'; //events/ajax_eventComment') ?>';//comments/getCommentList');
		new Ajax.Request(url, {
			method: 'post', 
			parameters: {action: 'getCommentList', eventId: eventId},
			requestHeaders: {Accept: 'application/json'},
			onSuccess: function(transport) {
				updateCommentList_div(transport);
			},
			onFailure: function() { alert('failed'); }
		});

		return false;
	}
	function updateCommentList_div(transport) {
				var json = transport.responseText.evalJSON();

				/* update the window count */
				if (json.length > 0) {
					/* only on the bet page : title elements to show how many bets are for this event */
					if (document.getElementById('comment_count_' + eventId) != null) {
						$('comment_count_' + eventId).update(json.length);
						var pl = "";
						if (json.length > 1)
							pl = "s";
						var comment_title;
						if (json.length == 0)
							comment_title = 'Ajouter un commentaire &agrave; ce pari';
						else if (json.length == 1)
							comment_title = 'Il y a un commentaire pour ce pari';
						else {
							comment_title = 'Il y a <num> commentaires pour ce pari';
							comment_title = comment_title.replace('<num>', json.length);
						}
						$('comment_count_' + eventId).title = comment_title;
						if (document.getElementById('comment_count2_' + eventId) != null) {
							$('comment_count2_' + eventId).update('commentaire' + pl);
							$('comment_count2_' + eventId).title = comment_title;
						}

					}
				}
				var text = '';
				var comment_head = '<table class="comment_item_head thin_grey"><td class="thin_grey_headl"></td><td class="thin_grey_headc"></td><td class="thin_grey_headr"></td></table>';
				var comment_foot = '<table class="comment_item_foot thin_grey"><td class="thin_grey_footl"></td><td class="thin_grey_footc"></td><td class="thin_grey_footr"></td></table>';
				var i;
				for (i = json.length - 1; i >= 0; i--) {
					msgList[i] = json[i].comment;
					author[i] = json[i].firstname;
					quote = '';
					if (json[i].quote.length > 0)
						quote = '<p class="comment_item_quote"><span class="quote">-- ' + json[i].quote + '</span> [<span class="author">' + json[i].quote_firstname + '</span>]</p>';
					text += comment_head + '<div class="comment_item"><table class="comment_item_content"><tr class="info"><td>Post&eacute; le ' + json[i].datetime.substr(0, 10) + ', par <span class="username">' + json[i].firstname + 
						'</span> sur ' +  json[i].datetime.substr(json[i].datetime.length - 8) + 
						'</td><td class="comment_user_options">Citer | <a href="/forum/report.php?f=' + json[i].forum_id + '&p=' + json[i].post_id+ '">Rapport d\'abus</a></td></tr>';
					text += '<tr><td colspan="2"><table width="100%"><tr><td class="quote_left"><img src="/themes/bet770/css/images/comments/quote_left.gif" alt="quote" /></td><td class="comment_body">' + quote + json[i].comment + '</td><td class="quote_right"><img src="/themes/bet770/css/images/comments/quote_right.gif" alt="quote"></td></tr></table></td></tr></table></div>' + comment_foot;

				}
				if (json.length == 0)
					text = comment_head + '<div class="comment_item"><table class="comment_item_content"><tr class="info"><td>Pas de commentaire.</td></tr></table></div>' + comment_foot;
console.log(text);
				$('comment_list').update(text);
	}

	function postComment(firstname, username, user_id, comment) {
		
		var url = '/comments/postComment'; //events/ajax_eventComment');
		new Ajax.Request(url, {
			method: 'post',
			parameters: {action: 'postComment', 
					eventId: eventId, 
					eventName: currentEvent,
					eventDate: currentEventDate,
					sportId: sportId,
					firstname: firstname,
					username: username,
					userId: user_id,
					comment: escape(comment),
					quote: escape($('comment_quote_hidden').value),
					quote_firstname: $('comment_quote_author_hidden').value},
			requestHeaders: {Accept: 'application/json'},
			onSuccess: function(transport) {
				updateCommentList_div(transport);
			},
			onFailure: function() { alert('failed'); }
		});
		cancelComment();
		$('comment').value = '';
		try {
			pageTracker._trackPageview('/comments/add');
		} catch(err) {}
	}
	function leaveAComment() {
		$('comment_list_box').hide();
		$('post_comment_box').style.display = 'block';
	}
	function cancelComment() {
		$('comment_quote').update('');
		$('comment_quote_hidden').value = '';
		$('comment_quote_author_hidden').value = '';
		$('comment').value = '';
		$('post_comment_box').style.display = 'none';
		$('comment_list_box').show();
	}
	function quoteComment(i) {
		var quote = '<span class="quote"> -- "' + msgList[i] + '"</span> [ <span class="author">' + author[i] + '</span> ]';
		var quote_content = msgList[i];
		var quote_author = author[i];
		$('comment_quote').update(quote);
		$('comment_quote_hidden').value = quote_content;
		$('comment_quote_author_hidden').value = quote_author;
		leaveAComment();
	}
	function reportAbuse(commentId) {
                var url = '/comments/reportAbuse';
                new Ajax.Request(url, {
                        method: 'post',
                        parameters: {commentId: commentId}
                });
		alert('Ce commentaire a été marqué pour modération.');
	}
	/*Element.observe(document, 'dom:loaded', function(){
		new Draggable('comment_box', {handle: 'comment_box_head', starteffect: effectFunction('myimage'), endeffect: effectFunction('myimage')});
	});*/
function effectFunction(element) {
   new Effect.Opacity(element, {from:0, to:0, duration:10});
}
                function limit_text() {
                        var limit = 400;
                        if ($('comment').value.length > limit)
                                $('comment').value = $('comment').value.substring(0, limit);
                        $('char_left').value = limit - $('comment').value.length;
                }

