/**********************************************************
Author:
Adam Barry
Klestrup | partners
www.klestrup-partners.dk

Date: September 4 2007

© 2008 Adam Barry, all rights reserved
-----------------------------------------------------------

Name:
reportComments script

-----------------------------------------------------------
Description:
A script that enables dynamic insertion of a form for
reporting comments

-----------------------------------------------------------
Usage:
Simply place a link to the this script in the head-section
of the XHTML page. The script will then automatically
execute on page load.

<script type="text/javascript" src="reportComments.js"></script>

-----------------------------------------------------------
Example:
<script type="text/javascript" src="thumbnails.js"></script>

<div class="comments">

	<dl>
		<dt>List title</dt>
		<dd>
			<div class="comment">
				<h3>Comment title</h3>
				<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras elit. Proin gravida posuere nulla. Aenean ac pede eget arcu dignissim tristique. Curabitur rhoncus adipiscing elit. In hac habitasse platea dictumst. Sed diam. Fusce ac neque commodo lectus eleifend interdum. Morbi faucibus enim non erat. Nunc eu justo. Etiam eu diam. Etiam porttitor massa nec ante. Aliquam interdum dolor tempus enim. Praesent lacinia nibh nec dui. Fusce ultrices magna.</p>
			</div>

			<a class="report" href="#">Report this comment</a>

		</dd>
	</dl>

</div>

-----------------------------------------------------------
Dependencies:
windowOnLoad

**********************************************************/

var visibleComment = false;
var visibleCommentForm;

function initReportComments () {
	if (!document.getElementsByTagName) return;

	var elements = document.getElementsByTagName("a");

	for (var i = 0; i < elements.length; i++) {
		if (elements[i].className.indexOf('report')>=0) {
			var me = elements[i];

			me.onclick = function () {
				if (visibleComment == false) {
					report (this.parentNode.id);
				}
				else {
					closeReport (visibleCommentForm);
					report(this.parentNode.id)
				}
				return false;
			}
		}
	}
}
addLoadEvent(function(){initReportComments();});


function report(commentId) {
	var comment = document.getElementById(commentId);

	comment.appendChild(newReportElement(commentId));
	visibleComment = true;
	visibleCommentForm = commentId;
	addButtonFunctionality(commentId);
}

function newReportElement (commentId) {
	var listItem = document.createElement('form');

	listItem.setAttribute('method','post');
	listItem.setAttribute('action', location.href);
	listItem.setAttribute('onsubmit','return validateForm(this)');
	listItem.innerHTML = "<fieldset class=\"SendToFriend\"><legend>Send link til denne vejledning</legend><h2>Send link til denne vejledning</h2><ul><li><label for=\"name\">Dit navn:</label><input name=\"name\" id=\"name\" type=\"text required\" class=\"text\" /></li><li><label for=\"email\">Modtager e-mail:</label><input name=\"email\" id=\"email\" type=\"text required\" class=\"text\" /></li><li><label>Besked:</label><textarea name=\"comment\" id=\"comment\" type=\"textarea\" class=\"textarea\">Skriv besked.</textarea></li></ul><button type=\"submit\" class=\"send\"><span>Send</span></button><button type=\"reset\" class=\"close\"><span>Luk</span></button></fieldset>";

	return listItem;
}

function closeReport (commentId) {
	var commentForm;
	var comment = document.getElementById(commentId);
	var elements = document.getElementById(commentId).getElementsByTagName('form');

	for (var i = 0; i < elements.length; i++) {
		commentForm = elements[i];
	}

	comment.removeChild(commentForm);

	visibleComment = false;
	visibleCommentForm = null;
}

function addButtonFunctionality (commentId) {
	var comment = document.getElementById(commentId);
	var elements = document.getElementById(commentId).getElementsByTagName('button');

	for (var i = 0; i < elements.length; i++) {
		if (elements[i].getAttribute('type')=='reset') {
			var me = elements[i];

			me.onclick = function () {
				closeReport(commentId);
			}
		}
	}
}