ajax = new ajax();
var backup = new Array();

function quote_comment_handler(result) {
	if (result.hasValue("content")) {
		comment  = "[quote]";
		comment += "[i]Door " + result.getValue("author") + ": [/i]";
		comment += result.getValue("content");
		comment += "[/quote]\n";

		textarea = document.getElementById("message");
		textarea.value = comment;

		textarea.focus();
	}
}

function respond() {
	textarea = document.getElementById("message");
	textarea.value = '';

	textarea.focus();
	document.location = '#respond';
}

function quote_article(article_id) {
	ajax.get("artikel", "article_id=" + article_id, quote_comment_handler);
}

function quote_comment(comment_id) {
	ajax.get("artikel", "comment_id=" + comment_id, quote_comment_handler);
}

function edit_comment_handler(result) {
	if (result.hasValue("content")) {
		id = result.getValue("id");
		comment = result.getValue("content");

		textarea = document.getElementById("comment_" + id);
		textarea.value += comment;

		textarea.focus();
	}
}

function edit_comment(comment_id) {
	if (edit[comment_id] == 0) {
		edit[comment_id] = 1;
		span = document.getElementById("edit_" + comment_id);

		backup[comment_id] = span.innerHTML;
		ajax.get("artikel", "comment_id=" + comment_id, edit_comment_handler);

		form  = "<form id=\"commentform_" + comment_id + "\">\n";
		form += "<input type=\"hidden\" name=\"id\" value=\"" + comment_id + "\">\n";
		form += "<textarea name=\"comment\" id=\"comment_" + comment_id +"\" class=\"text\"></textarea>\n";
		form += "</form>\n";
		form += "<input type=\"submit\" value=\"Opslaan\" class=\"button\" onClick=\"javascript:save_comment(" + comment_id + ")\"> ";
		form += "<input type=\"button\" value=\"Afbreken\" class=\"button\" onClick=\"javascript:close_form(" + comment_id + ")\">\n";

		span.innerHTML = form;
	}
}

function result_handler(result) {
	answer = result.getValue("result");
	if (answer != "oke") {
		alert(answer);
	} else {
		comment_id = result.getValue("id");
		
		comment = result.getValue("comment");
		textarea = document.getElementById("comment_" + comment_id);
		lastChange = document.getElementById("last-change_" + comment_id);

		backup[comment_id] = comment;

		if(!lastChange) {
			lastChange = document.createElement('DIV');
			lastChange.className = "last-change";
			lastChange.id = "last-change_"+ comment_id;
			lastChange.innerHTML = "Laatst gewijzigd: "+ result.getValue("last_change");
			document.getElementById('comment-'+ comment_id).firstChild.appendChild(lastChange);
		}

		close_form(comment_id);
	}
}

function save_comment(comment_id) {
		ajax.post("artikel&comment_id=" + comment_id, "commentform_" + comment_id, result_handler);
}

function close_form(comment_id) {
		span = document.getElementById("edit_" + comment_id);
		span.innerHTML = backup[comment_id];
		edit[comment_id] = 0;
}

function open_image(image) {
		window.open('/popup/'+ image, 'image_window', 'scrollbars=yes,location=no,status=no,menubar=no,width=200,height=200');
}

function quickpoll_handler(result) {
   document.getElementById('quickpoll').innerHTML = result.getValue("content"); 
}

function get_quickpoll(article_id) {
	ajax.get("quickpoll", "article_id=" + article_id, quickpoll_handler);
}

function submit_quickpoll(article_id, form_id) {
	ajax.post("quickpoll&article_id="+ article_id, form_id, quickpoll_handler); //"article_id="+article_id+"&answer_id="+answer_id, quickpoll_handler);
}

function toggle_bbcodes() {
	var bbcode = document.getElementById('bbcodes');
	bbcode.style.display = bbcode.style.display == 'block' ? 'none':'block';
}

function preview_comment(form) {
	var old_target = form.target;
	var old_action = form.action;

	window.open('', 'preview', 'width=740,height=600,toolbar=no,statusbar=no,location=no');
	form.target = 'preview';
	form.action = '/preview_comment';
	form.submit();

	form.target = old_target;
	form.action = old_action;
	return false;
}

function email_notification_handler(result) {
	answer = result.getValue("result");
	if(answer != 'oke') {
		alert(answer);
		return;
	}

	form = document.getElementById('email_notification_form');
	link = document.getElementById('email_notification_link');
	checkbox = document.getElementById('enable_email_notification');

	if(form.email_notification.value == 'enable') {
		form.email_notification.value = 'disable';
		link.innerHTML = 'E-mail notificatie stopzetten';
		checkbox.checked = true;
		alert('U zult in het vervolg een e-mail krijgen wanneer er een reactie is geplaatst op dit bericht.');
	} else {
		form.email_notification.value = 'enable';
		link.innerHTML = 'E-mail notificatie';
		checkbox.checked = false;
		alert('E-mail notificatie stopgezet.');
	}
	
}

function email_notification() {
	ajax.post("artikel", "email_notification_form", email_notification_handler);
}

function rate_comment_handler(result) {
	answer = result.getValue("result");
	
	// Bepaal de nieuwe style
	if(answer == '-') {
		className = 'score neutral';
	} else if( parseInt(answer) == NaN) {
		alert(answer);
		return;
	} else if(answer >= 0) {
		answer = '+'+ answer;
		className = 'score positive';
	} else {
		className = 'score negative';
	}

	// Voer de nieuwe score door
	comment_id = result.getValue("comment_id");
	document.getElementById('score-'+ comment_id).innerHTML = answer;
	document.getElementById('score-'+ comment_id).className = className;

	// Maak de rating links niet meer klikbaar
	var thumbs = document.getElementById('thumbs-'+ comment_id);
	thumbs.innerHTML =
		'<div class="down"></div><div class="up"></div>'+
		'<div class="rate-tooltip">Je hebt deze reactie al eens beoordeeld.</div>';

	if(answer == '-') {
		alert('De beoordeling van de reactie zal pas door worden gevoerd wanneer er meer dan 5 stemmen zijn.');
	}
}

function rate_comment(form, rating) {
	document.forms[form].elements['rate'].value = rating;
	ajax.post("artikel", form, rate_comment_handler);
}

function change_comment_filter(new_value) {
	var commentsNode = document.getElementById('commentsNode');
	var commentIds = new Array();
	
	for(var i=0; i<commentsNode.childNodes.length; i++) {
		var comment = commentsNode.childNodes[i];
		if(comment.tagName != 'DIV' || comment.className != 'comment discussie') {
			continue;
		}

		var score = getElementByClassName(comment, 'score').innerHTML;
		
		if(new_value == 0){
			expand_comment(comment);
		} else {
			collapse_comment(comment);
			if(new_value == -1 && parseInt(score) < 0){
				expand_comment(comment);
			}
			if(new_value == 1 && (score == '-' || parseInt(score) >= 0)){
				expand_comment(comment);
			}
		}

	}
}

function expand_comment(comment) {
	var content = getElementByClassName(comment, 'content');
	var lastChange = getElementByClassName(comment, 'last-change');
	var actions = getElementByClassName(comment, 'actions');
	var thumbs = getElementByClassName(comment, 'thumbs');
	var teaser = getElementByClassName(comment, 'teaser');
	var info = getElementByClassName(comment, 'info');
	
	content.style.display = 'block';

	if(lastChange)
		lastChange.style.display = 'block';

	actions.style.display = 'block';
	thumbs.style.visibility = 'visible';
	teaser.style.display = 'none';
	info.className = 'info';
	info.title = 'Reactie inklappen';
}

function collapse_comment(comment) {
	var content = getElementByClassName(comment, 'content');
	var lastChange = getElementByClassName(comment, 'last-change');
	var actions = getElementByClassName(comment, 'actions');
	var thumbs = getElementByClassName(comment, 'thumbs');
	var teaser = getElementByClassName(comment, 'teaser');
	var info = getElementByClassName(comment, 'info');

	content.style.display = 'none';

	if(lastChange)
		lastChange.style.display = 'none';

	actions.style.display = 'none';
	thumbs.style.visibility = 'hidden';
	teaser.style.display = 'block';
	info.className = 'info collapsed';
	info.title = 'Reactie uitklappen';
}

function toggle_comment(e, comment) {
	if(e.srcElement) {
		var target = e.srcElement;
	} else {
		var target = e.target;
	}

	if(target.tagName == 'A')
		return;

	var content = getElementByClassName(comment, 'content');
	if(content.style.display == 'none') {
		expand_comment(comment);
	} else {
		collapse_comment(comment);
	}

}

function getElementByClassName(root, className) {
	if((' '+ root.className +' ').indexOf(' '+ className +' ') >= 0) {
		return root;
	}

	for(var i=0; i<root.childNodes.length; i++) {
		node = root.childNodes[i];

		if(node.nodeType != 1) {
			continue;
		}

		if( (node = getElementByClassName(node, className)) != null ) {
			return node;
		}
	}

	return null;
}

