function getReferences(commentText, parentNr) {
	var occurenceAt = commentText.indexOf('@');
	var occurenceNr = commentText.indexOf('#');
	if (occurenceAt == -1) occurenceAt = 10000;
	if (occurenceNr == -1) occurenceNr = 10000;

	var occurence = -1;
	var replaceChar = "@";
	if (occurenceAt < occurenceNr) {
		occurence = occurenceAt;
		replaceChar="@";	
	} else if (occurenceAt > occurenceNr) {
		//alert("gevonden!");
		occurence = occurenceNr;		
		replaceChar="#";
	} else {
	}
	if (occurence != -1) {
		var cont = true;
                var commentNr = "";
                var lastpos = 0;
		var extraSpace = false;
	        for (var i=(occurence+1);cont;i++) {
			compareString = commentText.charAt(i).replace(/[A-Z0-9_]*/gi,"");
			if(commentText.charAt(i) != compareString) {
				commentNr += commentText.charAt(i);
			} else {
                                var chr = commentText.charAt(i);
                                if((chr!=':')&&(chr!='.')&&(chr!=" ")&&(chr!="#")&&(chr!=",")) {
                                        extraSpace = true;
				}
				cont = false;
				lastpos =i;
			}
        	}
		var commentNrLowerCase = commentNr.toLowerCase();

                var before = commentText.substr(0,occurence);
                var after = getReferences(commentText.substr(lastpos),parentNr);
		if ((commentNr!="")&& ($('.comment_'+commentNrLowerCase).size()>0) && (isNaN(Math.floor(commentNr))||(parseInt(commentNr) < parseInt(parentNr)))) {
                        var between = "<span id = \"cb"+parentNr+"\" class=\"responseBalloon\">"+replaceChar+commentNr+"</span>";
                        if (extraSpace) between += " ";
                	return before+between+after;
		} else {
                        var between = replaceChar+commentNr;
                	return before+between+after;
		}
	} else {
		return commentText;
	}
}

$(document).ready(function() {
	var i=0;
	$('.bCommentText').each(function() {
		if (i<100) {
			var commentText = $(this).html();
			var commentNr = $(this).parent().attr('id').substr(1);
//			alert(commentNr);
			var totalText = getReferences(commentText,commentNr);
			$(this).html(totalText);
			i++;
		}
	});

	if (!$('Balloon').length) {
		$('body').append('<div id="Balloon" style="display: none"><div class="LeftTop">&nbsp;</div><div class="RightTop">&nbsp;</div><div class="CloseButton">X</div><div class="CenterTop">&nbsp;</div><div id="BalloonContent"></div><div class="LeftBottom"></div><div class="RightBottom">&nbsp;</div><div class="CenterBottom"></div></div>');
	}

	$('.responseBalloon').click(function(e) {
		var mouseX = 0;
		var mouseY = 0;
		mouseX = e.pageX;
		mouseY = e.pageY;
		var number = $(this).html().substr(1);
		number = number.toLowerCase();
		currentComment = $(this).attr("id").substr(2);
		if ( $("#c"+number).length > 0 ) {
			$('#BalloonContent').html($('#c'+number).html());
		} else if ($('.comment_'+number).size() > 0) {
			var nr = $('.comment_'+number).size()-1;
			var newHtml ="";
			$('.comment_'+number).each(function() {
				referredComment = parseInt($(this).attr('id').substr(1));
				if (currentComment > referredComment) {
					nr = parseInt($(this).attr('id').substr(1))-1;
					//alert(nr);
				}
			});
			$('#BalloonContent').html($('.bComment:eq('+nr+')').html());
		} else {
			$('#BalloonContent').html("Deze reactie bestaat niet!");
		}
		$('#Balloon').width(450+"px");
		$('#Balloon').css("left",mouseX+"px");
		$('#Balloon').css("top",mouseY+"px");
		$('#Balloon').show();
	});

	$('#Balloon').click(function() {
		$('#Balloon').hide();
	});
	
});
