/*! $Id: article-comments-interface.min.js 8481 2010-02-16 14:03:10Z kschaefer $ */
/*global $:false, jQuery:false */

//noinspection BadExpressionStatementJS
"use strict";
/**
 * "Media-Box"
 * Ignition-Groupe-Interactive
 * @author	K. Schaefer <karsten.schaefer@de.ignitiongroupe.com>
 * @version	2010-01-28-16-00-00
 */

function commentsBox() {
	//this.htmlElement;
	//this.commentsSource;
	var to = this;

	this.init = function () {
		var copyCommentElement, commentElement;
		jQuery.ajax({
			url: this.commentsSource,
			type: "GET",
			dataType: "json",
			success: function (result) {
				jQuery(to.htmlElement).addClass("activity_status_loading");
				jQuery(to.htmlElement).children(".commentsHeadline").children("h3").find(".number").replaceWith((jQuery(result.comments).length));
				commentElement = jQuery(to.htmlElement).find(".commentList").find(".commentItem");
				jQuery(to.htmlElement).find(".commentList").find("div.commentItem").remove();
				jQuery.each(result.comments, function (i, item) {
					copyCommentElement = jQuery(commentElement).clone();
					if (0 === i % 2) {
						jQuery(copyCommentElement).addClass("oddJS");
					} else {
						jQuery(copyCommentElement).addClass("evenJS");
					}
					jQuery(copyCommentElement).find(".commentCopy").append(item.commentText);
					jQuery(copyCommentElement).find(".commentName").append(item.authorName);
					jQuery(copyCommentElement).find(".commentDate").append(new Date(item.commentDate).toLocaleDateString() + "<br />" + new Date(item.commentDate).toLocaleTimeString());
					if (item.authorWebAddress && '' !== item.authorWebAddress) {
						var linkElement = jQuery("<a href='" + item.authorWebAddress + "'/>");
						jQuery(copyCommentElement).find(".commentLink").append(linkElement);
						jQuery(copyCommentElement).find(".commentLink").find("a").append(item.authorWebAddress);
					}
					jQuery(to.htmlElement).find(".commentList").append(copyCommentElement);
				});
				jQuery(to.htmlElement).find(".commentList").css("display", "block");
			},
			error: function (msg) {
				jQuery(to.htmlElement).append("<div class='error'>An error occoured. Please try later! <br />(Couldn't load data)</div>");
			}
		});
		jQuery(document).ready(function () {
			jQuery(to.htmlElement).removeClass("activity_status_loading");
		});
	};
}