// <!-- Forum routines

function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
   return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}

var Forum = new Object();

// Toolbar rendering
Forum.CreateToolbar = function(threadID) {
    ///<summary>Displays the user's toolbar that appears above the list of posts.</summary>
    ///<param name="threadID">The thread</param>
    var target = 'commentsToolbar';
    var url = '/forum/login.ashx';
    new Ajax.Request(url, {
        method: 'get',
        parameters: { 'm': 'toolbar', 'threadID': threadID, 'dMode': 'true', 'c' : guid() },
        onSuccess: function(transport) {
            $('commentsToolbar').innerHTML = transport.responseText;
        },
        onFailure: function(transport) {
            alert('Det har oppstått en feil.');
        }
    });
}

// Forum threads handling
Forum.Thread = new Object();
Forum.Thread.Display = function(thread) {
	///<summary>Displays the thread</summary>
	///<param name="thread">The thread</param>
	$("forum").update(Forum.Thread.Render(thread));
	Forum.CreateToolbar(thread.Id);
};
Forum.Thread.Load = function(id) {
	///<summary>Loads a forum thread</summary>
	///<param name="id" type="Number" integer="true">Thread id</param>
	if (!$("forum")) {
		window.setTimeout("Forum.Thread.Load(" + id + ")", 500);
		return;
	}
	var forumSource = "/forum/forumJSON.aspx?thread=" + id;
	new Ajax.Request(forumSource, { onComplete:function (orgRequest) {
		var thread = orgRequest.responseText.evalJSON(false);
		Forum.Thread.Display(thread);
	}});
};
Forum.Thread.Render = function(thread) {
	///<summary>Renders a thread</summary>
	///<param name="thread">The thread</param>
	var res = "";
	if (thread.Posts) {
		thread.Posts.each (function (post) {
			res += Forum.Post.Render(post, true);
		});
	}
	return res;
};

// Forum post handling
Forum.Post = new Object();
Forum.Post.Render = function(post, top) {
    ///<summary>Renders a post</summary>
    ///<param name="post">The post</summary>
    ///<param name="top">True if the post is at the top level</param>
    var cls = "post";
    if (!top)
        cls += " child collapsed";
    var res = "<div class=\"" + cls + "\" id=\"post" + post.Id + "\">";
    res += Forum.Post.RenderTitle(post);
    res += Forum.Post.RenderHeader(post);

    if (post.Inappropriate) {
        res += "<div class='inappropriateContent'>";
        res += "Innlegget er fjernet av redaksjonen.";
        res += "</div>";
    } else res += "<div class=\"content\">" + post.Text + Forum.Post.RenderLinks(post) + "</div>";

    res += "<div style=\"padding-bottom: 8px;\" id=\"postReport" + post.Id + "\"></div>";
    if (post.Replies) {
        post.Replies.each(function(reply) {
            res += Forum.Post.Render(reply, true);
        });
    }
    res += "</div>";
    return res;
};
Forum.Post.RenderLinks = function(post) {
	///<summary>Renders the post links</summary>
	///<parma name="post">The post</param>
	var reply = "<a class=\"forumButton\" href=\"/forum/newpost.aspx?replyto=" + post.Id + "\">Svar</a>";
	var report = "<a class=\"forumButton\" href=\"javascript:Forum.Report.Show(" + post.Id + ");\">Upassende?</a>";
	return "<div style=\"margin-top: 5px;\">" + reply + "&nbsp;" + report + "</div>";
};
Forum.Post.RenderHeader = function(post) {
	///<summary>Renders the post header</summary>
	///<param name="post">The post</param>
	
	var user = "";
	if (post.UserName == "") user = "Anonym";
	else user = post.UserName;
	
	return "<div class=\"postdate\">Av: " + user + ", " + Forum.Utility.FormatDate(post.PostDate) + "</div>";
};
Forum.Post.RenderTitle = function(post) {
    ///<summary>Renders a post title</summary>
    ///<param name="post">The post</param>
    var heading = "<h2><a href=\"" + Forum.Post.RenderToggleLink(post) + "\">" +
		"<img class=\"toggle\" src=\"/images/gfx/netmeeting/comment.gif\" alt=\"Skjul/vis innlegg\" />" +
		post.Title + "</a></h2>";
    return heading;
};
Forum.Post.RenderToggleLink = function(post) {
	///<summary>Renders a post show/hide toggle button</summary>
	///<param name="post">The post</param>
	return "javascript:Forum.Post.Toggle(" + post.Id + ");";
};
Forum.Post.Toggle = function(id) {
	///<summary>Toggles post display</summary>
	///<param name="id" type="Number" integer="true">Post id</param>
	var post = $("post" + id);
	if (post) post.toggleClassName("collapsed");
}

// ** Utility functions **
Forum.Utility = new Object();
Forum.Utility.FormatDate = function(date) {
	///<summary>Renders a date</summary>
	///<param name="date" type="DateTime">The date</param>
	var today = new Date();
	var time = Forum.Utility.FormatTime(date);
	if (date.getFullYear() == today.getFullYear() && date.getMonth() == today.getMonth()) {
		switch (today.getDate() - date.getDate()) {
			case 0 : return "i dag " + time;
			case 1 : return "i g&aring;r " + time;
			default : return date.toLocaleDateString() + " " + time;
		}
	}
	return date.toLocaleDateString() + " " + time;
};
Forum.Utility.FormatTime = function(date) {
	///<summary>Formats the time portion of the date</summary>
	///<param name="date" type="DateTime">The date</param>
	return	Forum.Utility.DoubleDigits(date.getHours()) + ":" + 
			Forum.Utility.DoubleDigits(date.getMinutes());
};
Forum.Utility.DoubleDigits = function(val) {
	///<summary>Formats a number with at least two digits</summary>
	///<param name="val" type="Number" integer="true">The value</param>
	return (val < 10) ? "0" + val : val;
};

// ** Forum abuse reporting **
Forum.Report = new Object();
Forum.Report.Id = 0; // Tracks the post id to report
Forum.Report.Send = function(reason) {
	///<summary>Reports forum abuse</summary>
	///<param name="reason" type="String">The reason for reporting</param>
	new Ajax.Request("/forum/report.aspx", { method: "post", parameters: { "post": Forum.Report.Id, "reason": reason } });
	Forum.Report.Hide();
};
Forum.Report.Hide = function() {
	///<summary>Hides the report form</summary>
	$("reportForm").hide();
	$("txtReportReason").value = "";
	Forum.Report.Id = 0;
};
Forum.Report.Show = function(postId) {
	///<summary>Shows the report form</summary>
	///<param name="postId" type="Number" integer="true">The post id to report</param>
	Forum.Report.Hide();
	Forum.Report.Id = postId;
	var reportForm = $("reportForm");
	$("postReport" + postId).update(reportForm);
	reportForm.show();
}
Forum.Report.Cancel = function() {
	///<summary>Cancels the report</summary>
	Forum.Report.Hide();
};

// -->

