(function(jQuery) {																	// Compliant with jquery.noConflict()

jQuery.fn.mcVideoComments = function(o) {

	jQuery.require("js/v2/template/jquery.template-min.js");

	// Default values
	o = jQuery.extend(jQuery.fn.mcVideoComments.defaults, o || {});

	// Local data
	var context = {};
	var mcCont;
	
	var t_channel = (o.tplSelector) ? jQuery.template(jQuery(o.tplSelector).html) : jQuery.template(o.tpl);

	return this.each(function() {												// Returns the element collection. Chainable.

		mcCont = this;

		jQuery(document).unbind("load.comments").bind("load.comments", function(e,data) {  

			context = jQuery.extend(data, context || {});

//DP_Debug.dump(context, 'mcVideoComments: context', true, 3);
			// Load JSON Data as String
			jQuery.ajax({
				type: "GET",
//				url: o.url + context.vid,
				url: o.url + context.video.guid,
				dataType: "json",
				timeout: 30000,
				success: function (data) {
					makeList(data);
					if (o.onComplete) o.onComplete(context);
				},																// close Success

				error: function (XMLHttpRequest, textStatus, errorThrown) {
					alert(
						'Error loading videos: '+textStatus + '\n' + errorThrown + '\n'
						+ unescape( o.url)
					);
				}																// close Error
			});																	// close $.ajax

		});																		// close Bind

    });																			// close Each

/* ------------------------------- makeList --------------------------------- */
	function makeList(data) {
//DP_Debug.dump(data, 'Load Coments', true, 3);
		jQuery(mcCont).empty();

		// Add every channel to the list
		if (data) {
//			jQuery(mcCont).show();
			jQuery.each(data, function(i, item) {
//DP_Debug.dump(item, 'Coment Item', true, 3);
				item.index = i;
//				item.id_contenu = o.vid;
//				item.id_contenu = context.video.guid;

				var toto = jQuery(t_channel.apply(item));
				jQuery(mcCont).append(toto);

				jQuery(".tv_alert_link", toto).toggle(
					function () {
						$parent = jQuery(this).parent(".tv_alert");
						$parent.addClass("open");
						jQuery('.tv_alert_corps', $parent).show();
					},
					function () {
						$parent = jQuery(this).parent(".tv_alert");
						$parent.removeClass("open");
						jQuery('.tv_alert_corps', $parent).hide();
					}
				);

/*
item.
	id_commentaire
	id_contenu
	commentaire
	id_membre
	date
	pseudo
	id_contenu *
*/
			});																		// close each
			if (jQuery(mcCont).height()>o.height) {
				jQuery(mcCont).height(o.height);
			}
		} else {
//DP_Debug.logInfo("Hide Comments");
//			jQuery(mcCont).hide();
			jQuery(mcCont).html(o.emptyMsg);
		}
	}																			// close makeList()

/* ============================ Helper functions ============================ */

};

/* ============================= Default Values ============================= */
jQuery.fn.mcVideoComments.defaults = {
	url		: "getCommentsJson.php?id=",

	tplSelector	: null,
	tpl			: '<div class="tv_comment_txt">par <a href="/communaute/membre/${pseudo}" target="_blank" title="Voir sa fiche" class="lien6">${pseudo}</a>, le ${date}</div><div class="tv_comment"><div class="tv_comment_top"><img src="images/v2/corner-tr.gif" alt="" width="9" height="9" border="0" align="right" style="margin:0" /><img src="images/v2/corner-tl2.gif" alt="" width="20" height="9" border="0" /></div><div class="tv_comment_corps">${commentaire}<div class="tv_alert"><a class="tv_alert_link" href="#">alerter l\'&eacute;quipe de mod&eacute;ration <img src="images/v2/icn-alert6.png" alt="" width="13" height="16" border="0" hspace="1" align="top" /></a><div class="tv_alert_corps" id="alerte_subject_${id_commentaire}" style="display:none"><div>Veuillez indiquer le motif de votre alerte:</div><div><textarea name="alerte_message_${id_commentaire}" rows="3" cols="25"id="alerte_message_${id_commentaire}"></textarea></div><div><input type="image" src="images/v2/btn-submit.gif" name="submit3" value="Commenter" class="btn" onclick="javascript:alerte_confirm(\'/dynamic/web.php?q=sendAlerte&amp;id_type=4&amp;type=video&amp;id_art=${id_contenu}&amp;titre=${titre_contenu}&amp;id_com=${id_commentaire}&amp;date=${date}&amp;auteur=mechl&amp;message=\'+document.getElementById(\'alerte_message_${id_commentaire}\').value,\'alerte_subject_${id_commentaire}\',\'alerte_message_${id_commentaire}\');" /></div></div></div></div><div class="tv_comment_bas"><img src="images/v2/corner-br.gif" alt="" width="9" height="9" border="0" align="right" style="margin:0" /><img src="images/v2/corner-bl.gif" alt="" width="9" height="9" border="0" /></div></div>',
	ZZZ_tpl			: '<div class="tv_comment_txt">par <a href="/communaute/membre/${pseudo}" target="_blank" title="Voir sa fiche" class="lien6">${pseudo}</a>, le ${date}</div><div class="tv_comment"><div class="tv_comment_top"><img src="images/v2/corner-tr.gif" alt="" width="9" height="9" border="0" align="right" style="margin:0" /><img src="images/v2/corner-tl2.gif" alt="" width="20" height="9" border="0" /></div><div class="tv_comment_corps">${commentaire}</div><div class="tv_comment_bas"><img src="images/v2/corner-br.gif" alt="" width="9" height="9" border="0" align="right" style="margin:0" /><img src="images/v2/corner-bl.gif" alt="" width="9" height="9" border="0" /></div></div><div class="tv_alert"><a class="tv_alert_link" href="#">alerter l\'&eacute;quipe de mod&eacute;ration <img src="images/v2/icn-alert6.png" alt="" width="13" height="16" border="0" hspace="1" align="top" /></a><div class="tv_alert_corps" id="alerte_subject_${id_commentaire}" style="display:none"><div>Veuillez indiquer le motif de votre alerte:</div><div><textarea name="alerte_message_${id_commentaire}" rows="3" cols="25"id="alerte_message_${id_commentaire}"></textarea></div><div><input type="image" src="images/v2/btn-submit.gif" name="submit3" value="Commenter" class="btn" onclick="javascript:alerte_confirm(\'/dynamic/web.php?q=sendAlerte&amp;id_type=4&amp;type=video&amp;id_art=${id_contenu}&amp;titre=${titre_contenu}&amp;id_com=${id_commentaire}&amp;date=${date}&amp;auteur=mechl&amp;message=\'+document.getElementById(\'alerte_message_${id_commentaire}\').value,\'alerte_subject_${id_commentaire}\',\'alerte_message_${id_commentaire}\');" /></div></div></div>',

	items		: {},
	height		: 250,	// in pixels
	emptyMsg	: "Soyez le premier &agrave; donner votre avis",

	onLoading	: null,
	onComplete	: null
};

})(jQuery);
