(function($) {
	
	/**
	 * plug addcommentalbum : profile/album member/album
	 */
	$.fn.addcommentalbum = function(options) {

		// build main options before element iteration
		var opts = $.extend({}, $.fn.addcommentalbum.defaults, options);

		/**
		 * envoi le commentaire vers le systeme
		 */
		function send(_this) {
			var idDiv = _this.find(opts.divAdd).attr('id').split('_');
			var typeID = idDiv[1];
			var relatedObjectID = idDiv[2];
			var threadID = null;
			var body = _this.find('textarea[name="newcomment"]').val();
			var relatedObjectUserId =  _this.find(opts.divAdd).find('input[name="albumUserId"]').val();
			var peexcomUserId = $('#peexcom_userid').text();
			if( body == '' || body == ' ') {
				return false;
			}
			if( typeID ==  '3' ) { // photo / album
				if( peexcomUserId != relatedObjectUserId) {
					peexCom.photoComment(relatedObjectUserId, body, threadID, typeID, relatedObjectID, callbackSendComment(_this));
				} else {
					peexCom.myPhotoComment(body, threadID, typeID, relatedObjectID, callbackSendComment(_this));
				}
				

				$.ajax({
					url: encodeURI( '/ajax/addcomment/type/1/relatedobjectid/'+relatedObjectID+'/content/'+body),
					dataType: "xml",
					success: function(data){
						peexCom.debug.add('success');
					},
					error: function(obj){
						peexCom.debug.add('error');
					}
				});
				
				displayCommentAlbum(_this, body, relatedObjectID);
			}
			hideForm(_this);
		};
		
		/**
		 * fonction de callbackl lors de l envoi du commentaire
		 */
		function callbackSendComment(_this) {
			//_this.find(opts.divAdd).hide(); // cache le formulaire
			_this.find('textarea').val(''); // remet a zero le textarea
		};
		
		/**
		 * affiche le formulaire textarea[name="newcomment"]
		 */
		function displayForm(_this) {
			// init à vide le form
			_this.find('textarea[name="newcomment"]').val('');
			// TODO verifier si l utilisateur est connecté
			_this.find(opts.divAdd).show();
		};
		
		/**
		 * cache le formulaire textarea[name="newcomment"]
		 */
		function hideForm(_this) {
			_this.find(opts.divAdd).hide();
		}
		
		/**
		 * affiche le commentaire dans la liste des commentaires 
		 */
		function displayCommentAlbum(_this, body, relatedObjectUserId) {
			var userName = $('#userprofile_username').text();
			var userId = $('#peexcom_userid').text();
			var userThumb1 = $('#userprofile_thumb1').text();
			var commentHTML = 	'<div class="commentAndSub"><li>' +
									'<div class="avatar"><img src="' + userThumb1 + '"/>' +
									'<a href="/member/index/userid/' + userId + '">' + userName + '</a><span>( maintenant )</span></div>' +
									'<div class="infos"><p>" ' + body + ' "</p><div id="comment_'+relatedObjectUserId+'_'+userId+'_0" class="hideme link"/></div>' +
									''
								'</li></div><br class="clear"/>';
			// TODO varialiser le maintenant
			_this.find('ul').prepend(commentHTML);
		};
		
		/**
		 * bind les boutons d envoi et d affichage du formulaire
		 */
		function bind(_this) {
			if( _this.find(opts.buttonSend) ) {
				_this.find(opts.buttonSend).click(function(){
					userIdToTest=($('#member_userid').text()!="") ? $('#member_userid').text() : $('#peexcom_userid').text();
					if(testIsNotConnected())
					{
						dispLayerNotConnected();
						$('#layer').jqmShow();
					}
					else if(testIsPeexOut(userIdToTest))
					{
						dispLayerCommWaitForFriends($('#member_pseudo').html());
						$('#layer').jqmShow();
					}
					else if(testIsPeexIn(userIdToTest) || testIsPeexNone(userIdToTest))
					{
						dispLayerCommNotFriends($('#member_pseudo').html());
						$('#layer').jqmShow();
					}
					else
						send(_this);
				});
			}
			if( _this.find(opts.buttonAdd) ) {
				_this.find(opts.buttonAdd).click(function(){
					displayForm(_this);
				});
			}
		};
		
		// iterate each matched element
		return this.each(function() {
			bind($(this));
		});
	};
	
	/**
	 * plugin defaults
	 *  buttonSend		: '', le bouton qui envoi le commentaire
	 *	buttonAdd			: '', // le bouton qui affiche le formulaire d ajout
	 *	divAdd				: '', // la div qui contient le formualire d ajout
	 *	divComment		: '', // la div qui contient les commentaires
	 *	textError			: '' // le message d erreur
	 */
	$.fn.addcommentalbum.defaults = {
		buttonSend		: '',
		buttonAdd			: '',
		divAdd				: '',
		divComment		: '',
		textError			: ''
	};
	
/**********************************************************/
	
	/**
	 * plug in addcommentphotos album/mylist album/list
	 */
	$.fn.addcommentphotos = function(options) {

		// build main options before element iteration
		var opts = $.extend({}, $.fn.addcommentphotos.defaults, options);

		/**
		 * envoi le commentaire vers le systeme
		 */
		function send(_this) {
			// recuperation des donnees _this.find(opts.divAdd).data('comment', { relatedObjectUserId : tInfoComment[2], typeID : 2, relatedObjectID : tInfoComment[1], threadID : ''}
			var data = _this.find(opts.divAdd).data('comment');
			var typeID = data.typeID;
			var relatedObjectID = data.relatedObjectID;
			var threadID = data.threadID;
			var body = _this.find('textarea[name="newcomment"]').val();
			//var relatedObjectUserId = data.relatedObjectUserId;
			var relatedObjectUserId = $("#member_userid").text();
			var peexcomUserId = $('#peexcom_userid').text();
			if( body == '' || body == ' ') {
				return false;
			}
			if( threadID == '') {
				threadID = relatedObjectID;
			}
debug.log('relatedObjectUserId: ' +relatedObjectUserId);
debug.log('peexcomUserId: '+ peexcomUserId);
			if( typeID ==  '3' ) { // photo = 3, comment = 2
				if( relatedObjectUserId != '' && peexcomUserId != relatedObjectUserId) {
					//peexCom.comComment(to, body, threadID, typeID, elemID, callback)
					peexCom.comComment(relatedObjectUserId, body, threadID, typeID, relatedObjectID, callbackSendComment(_this));
				} else {
					//peexCom.myComComment(body, threadID, typeID, elemID, callback)
					peexCom.myComComment(body, threadID, typeID, relatedObjectID, callbackSendComment(_this));
				}
				displayCommentPhotos(_this, body);
			}
			hideForm(_this);
		};
		
		/**
		 * fonction de callbackl lors de l envoi du commentaire
		 */
		function callbackSendComment(_this) {
			//_this.find(opts.divAdd).hide(); // cache le formulaire
			_this.find('textarea').val(''); // remet a zero le textarea
		};
		
		/**
		 * affiche le formulaire textarea[name="newcomment"]
		 */
		function displayForm(_this) {
			// ferme les autres fenetres
			hideForm(_this);
			// TODO verifier si l utilisateur est connecté
			// id du .link comment_{commentId}_{userId}
			var tInfoComment = _this.attr('id').split('_');
			// clone le form
			/*if( $('#commentcommform_2_' + tInfoComment[1]).size() ) {
				$('#commentcommform_2_' + tInfoComment[1]).show().find('textarea[name="newcomment"]').val('');
			} else {
			*/
				var clone = $(opts.divCloneAdd).clone().appendTo($(_this)).attr('id', 'commentcommform_2_' + tInfoComment[3]).show();
				clone.find('textarea[name="newcomment"]').val(''); // clone, puis affiche, init à vide le form
			//}
			// infos ajouté au data de _this.find(opts.divAdd) : commentUserId (relatedObjectUserId), typeID, relatedObjectID, threadID
			_this.find(opts.divAdd).data('comment', { relatedObjectUserId : tInfoComment[2], typeID : 3, relatedObjectID : tInfoComment[1], threadID : tInfoComment[3]});
			// bind le bouton d envoi
			bindSend(_this);
		};
		
		/**
		 * cache le formulaire textarea[name="newcomment"]
		 */
		function hideForm(_this) {
			//$(opts.divAdd).remove();
			$('.link>' + opts.divAdd).remove();
		}
		
		/**
		 * affiche le commentaire dans la liste des commentaires 
		 */
		function displayCommentPhotos(_this, body) {
			var userName = $('#userprofile_username').text();
			var userId = $('#peexcom_userid').text();
			var userThumb1 = $('#userprofile_thumb1').text();
			var commentHTML = 	'<li class="subline"><div class="avatar">' +
									'<a href="/member/index/userid/' + userId + '"><img height="28" width="28" src="' + userThumb1 + '"/></a>' +
									'<a href="/member/index/userid/' + userId + '" class="pseudo">' + userName + '</a>' +
									'<span>( maintenant )</span>' +
								'</div>' + 
								'<div class="infos">' +
									'<p> " ' + body + ' " </p>'
									'<span> - ' + body + '</span>' +
								'</div><br class="clear"/></li><br class="clear"/>';
			// TODO varialiser le maintenant
			//_this.parents(opts.divComment).append(commentHTML);
			_this.parents('.rf_commentandsub').append(commentHTML);
		};
		
		/**
		 * bind les boutons d envoi et d affichage du formulaire
		 */
		function bind(_this) {
			if( _this.find(opts.buttonAdd) ) {
				_this.find(opts.buttonAdd).click(function(){
					displayForm(_this);
				});
			}
		};
		function bindSend(_this) {
			if( _this.find(opts.buttonSend) ) {
				_this.find(opts.buttonSend).click(function(){
					userIdToTest=($('#member_userid').text()!="") ? $('#member_userid').text() : $('#peexcom_userid').text();
					if(testIsNotConnected())
					{
						dispLayerNotConnected();
						$('#layer').jqmShow();
					}
					else if(testIsPeexOut(userIdToTest))
					{
						dispLayerCommWaitForFriends($('#member_pseudo').html());
						$('#layer').jqmShow();
					}
					else if(testIsPeexIn(userIdToTest) || testIsPeexNone(userIdToTest))
					{
						dispLayerCommNotFriends($('#member_pseudo').html());
						$('#layer').jqmShow();
					}
					else
						send(_this);
				});
			}
		};
		
		// iterate each matched element
		return this.each(function() {
			bind($(this));
		});
	};
	
	/**
	 * plugin defaults
	 *  buttonSend		: '', le bouton qui envoi le commentaire
	 *	buttonAdd			: '', // le bouton qui affiche le formulaire d ajout
	 *	divAdd				: '', // la div qui contient le formualire d ajout
	 *	divComment		: '', // la div qui contient les commentaires
	 *	textError			: '' // le message d erreur
	 */
	$.fn.addcommentphotos.defaults = {
		buttonSend		: '',
		buttonAdd			: '',
		divAdd				: '',
		divCloneAdd		: '',
		divComment		: '',
		textError			: ''
	};
	
/**********************************************************/
	
	/**
	 * plug in addcommentonephotos photo/mylist photo/list
	 */
	$.fn.addcommentonephoto = function(options) {
		
		// build main options before element iteration
		var opts = $.extend({}, $.fn.addcommentonephoto.defaults, options);
		
		/**
		 * envoi le commentaire vers le systeme
		 */
		function sendCommentResponse(_this) {
debug.log("sendCommentResponse(_this)");
			// recuperation des donnees _this.find(opts.divAdd).data('comment', { relatedObjectUserId : tInfoComment[2], typeID : 2, relatedObjectID : tInfoComment[1], threadID : ''}
			var data = _this.find(opts.divAdd).data('comment');
			var typeID = data.typeID;
			var relatedObjectID = data.relatedObjectID;
			var threadID = data.threadID;
			var body = _this.find('textarea[name="newcomment"]').val();
			var peexcomUserId = $('#peexcom_userid').text();
			var relatedObjectUserId = data.relatedObjectUserId;
			if( body == '' || body == ' ') {
				return false;
			}
			if(relatedObjectUserId == "" || relatedObjectUserId == undefined){
				relatedObjectUserId = peexcomUserId;
			}
debug.log(
	"peexcomUserId = " + peexcomUserId + "\n" +
	"relatedObjectUserId = " + relatedObjectUserId
);
			if( threadID == '') {
				threadID = relatedObjectID;
			}
			if( typeID ==  '3' ) { // photo = 3, comment = 2
				if( peexcomUserId != relatedObjectUserId) {
					//peexCom.comComment(to, body, threadID, typeID, elemID, callback)
					peexCom.comComment(relatedObjectUserId, body, threadID, typeID, relatedObjectID, callbackSendComment(_this));
				} else {
					//peexCom.myComComment(body, threadID, typeID, elemID, callback)
					peexCom.myComComment(body, threadID, typeID, relatedObjectID, callbackSendComment(_this));
				}
			displayCommentPhotos(_this, body);
			}
			hideForm(_this);
		};
		
		/**
		 * envoi le commentaire vers le systeme
		 */
		function sendPhotoComment(_this) {
debug.log("sendPhotoComment(_this)");
			var idDiv = _this.attr('id').split('_');
			var typeID = idDiv[1];
			var relatedObjectID = idDiv[2];
			var threadID = null;
			var body = _this.find('textarea[name="newcomment"]').val();
			var peexcomUserId = $('#peexcom_userid').text();
			var relatedObjectUserId = _this.find('input[name="albumUserId"]').val();
			if( body == '' || body == ' ') {
				return false;
			}
			if(relatedObjectUserId == "" || relatedObjectUserId == undefined){
				relatedObjectUserId = peexcomUserId;
			}
debug.log(
	"peexcomUserId = " + peexcomUserId + "\n" +
	"relatedObjectUserId = " + relatedObjectUserId
)
			if( typeID ==  '3' ) { // photo / album
				if( peexcomUserId != relatedObjectUserId) {
					peexCom.photoComment(relatedObjectUserId, body, threadID, typeID, relatedObjectID, callbackSendComment(_this));
				} else {
					peexCom.myPhotoComment(body, threadID, typeID, relatedObjectID, callbackSendComment(_this));
				}

				$.ajax({
					url: encodeURI( '/ajax/addcomment/type/1/relatedobjectid/'+relatedObjectID+'/content/'+body),
					dataType: "xml",
					success: function(data){
						peexCom.debug.add('success');
					},
					error: function(obj){
						peexCom.debug.add('error');
					}
				});
				
				displayCommentPhotos(_this, body);
			}
		};
		
		/**
		 * fonction de callbackl lors de l envoi du commentaire
		 */
		function callbackSendComment(_this) {
			//_this.find(opts.divAdd).hide(); // cache le formulaire
			_this.find('textarea').val(''); // remet a zero le textarea
		};
		
		/**
		 * affiche le formulaire textarea[name="newcomment"]
		 */
		function displayForm(_this) {
			// ferme les autres fenetres
			hideForm(_this);
			// TODO verifier si l utilisateur est connecté
			// id du .link comment_{commentId}_{userId}
			var tInfoComment = _this.attr('id').split('_');
			// clone le form
			var clone = $(opts.divCloneAdd).clone().appendTo($(_this)).attr('id', 'commentcommform_2_' + tInfoComment[3]).show();
			// infos ajouté au data de _this.find(opts.divAdd) : commentUserId (relatedObjectUserId), typeID, relatedObjectID, threadID
			_this.find(opts.divAdd).data('comment', { relatedObjectUserId : tInfoComment[2], typeID : 3, relatedObjectID : tInfoComment[1], threadID : tInfoComment[3]});
			clone.find('textarea[name="newcomment"]').val(''); // clone, puis affiche, init à vide le form
			// bind le bouton d envoi
			bindSend(_this);
		};
		
		/**
		 * cache le formulaire textarea[name="newcomment"]
		 */
		function hideForm(_this) {
			$('.link>' + opts.divAdd).remove();
		}
		
		/**
		 * affiche le lien "commenter" du commentaire dans la liste des commentaires 
		 */
		function displayCommentPhotoLink(_this, threadID){
debug.log("displayCommentPhotoLink()");
debug.log("threadID = " + threadID);
			var userId = $('#peexcom_userid').text();
debug.log("$(opts.divAdd).attr('id') = " + $(opts.divAdd).attr("id"));
			var photoId = $(opts.divAdd).attr("id").split("_")[2];
			var commentButtonHTML = '<div id="comment_' + photoId + '_' + userId + '_'+ threadID + '" class="link">' +
				'<a class="see_all addcom" >' +
					'<em class="remonte">></em><span id="soulignemoi">Commenter</span>' +
				'</a>' +
			'</div>';
debug.log("commentButtonHTML = " + commentButtonHTML);
			var commentText = _this.find('div.infos:first');
			commentText.append(commentButtonHTML);
			//bindFirst(_this.find('.link'));
////////////////////////////////
			var options = {
				buttonSend		: '.validerbtn',
				buttonAdd			: '.addcom',
				divAdd				: '.commentcommform',
				divCloneAdd		: '#commentcommformtoclone',
				divComment		: 'ul.putitafter>li',
				textError			: 'envoi echoué'
				};
			//comment a photo comment
			$('.link').addcommentonephotocomment( options );
////////////////////////////////
		}
		
		/**
		 * affiche le commentaire dans la liste des commentaires 
		 */
		function displayCommentPhotos(_this, body) {
			var userName = $('#userprofile_username').text();
			var userId = $('#peexcom_userid').text();
			var userThumb1 = $('#userprofile_thumb1').text();
			var commentHTML = '<div class="commentAndSub"><li><div class="avatar">' +
			'<img height="28" width="28" src="' + userThumb1 + '"/>' +
			'<a href="/member/index/userid/' + userId + '">' + userName + '</a>' +
			'<span>( maintenant )</span>' +
			'</div>' + 
			'<div class="infos">' +
			'<p> " ' + body + ' " </p>' +
			'</div><br class="clear"/></li><br class="clear"/></div>';
			// TODO varialiser le maintenant
			//_this.parents(opts.divComment).append(commentHTML);
			//_this.parents(opts.divComment).after(commentHTML);
			_this = _this.parents().children(opts.divComment);
debug.log("_this.attr('class') = " + _this.attr('class'));
//debug.log("_this.html() = " + _this.html());
debug.log("_this.find('textarea[name=\"newcomment\"]').val() = " + _this.find('textarea[name="newcomment"]').val());
			_this.children('div>li:first').find('textarea[name="newcomment"]').val('');
			_this.children('div>li:first').removeClass();
			_this.prepend(commentHTML);
			_this.children('div>li:first').addClass("noborder_top");
			
			/*** display the "comment" link of the comment in the comments list */
			if(peexCom._obj.header) {
debug.log("peexCom._obj.header");
debug.log("peexCom._obj.header.threadID = " + peexCom._obj.header.threadID);
				peexCom._obj = {}; //clear peexCom._obj
			}
			/* ***/
		};
		
		/**
		 * bind les premiers boutons d envoi et d affichage du formulaire
		 */
		function bindFirst(_this) {
debug.log("opts.buttonAdd = " + opts.buttonAdd);
debug.log("_this.attr('class') = " + _this.attr('class'));
			// bind les boutons ajouter un commentaire
			if( _this.find(opts.buttonAdd) ) {
				_this.find(opts.buttonAdd + ":first").click(function(){
					displayForm(_this);
				});
			}
		};
		
		/**
		 * bind le boutons d envoi et d affichage du formulaire
		 */
		function bind(_this) {
			// bind les boutons ajouter un commentaire
			if( _this.find(opts.buttonAdd) ) {
				_this.find(opts.buttonAdd).click(function(){
					displayForm(_this);
				});
			}
			// bind le bouton envoyer un commentaire sur la photo
			if( _this.find(opts.buttonSend) ) {
debug.log("opts.buttonSend = " + opts.buttonSend);
				_this.find(opts.buttonSend).click(function(){
					userIdToTest=($('#member_userid').text()!="") ? $('#member_userid').text() : $('#peexcom_userid').text();
					if(testIsNotConnected())
					{
						dispLayerNotConnected();
						$('#layer').jqmShow();
					}
					else if(testIsPeexOut(userIdToTest))
					{
						dispLayerCommWaitForFriends($('#member_pseudo').html());
						$('#layer').jqmShow();
					}
					else if(testIsPeexIn(userIdToTest) || testIsPeexNone(userIdToTest))
					{
						dispLayerCommNotFriends($('#member_pseudo').html());
						$('#layer').jqmShow();
					}
					else
						sendPhotoComment(_this);
				});
			}
		};
		
		/**
		 * bind les boutons de commenter un commentaire
		 */
		function bindSend(_this) {
			if( _this.find(opts.buttonSend) ) {
				_this.find(opts.buttonSend).click(function(){
					userIdToTest=($('#member_userid').text()!="") ? $('#member_userid').text() : $('#peexcom_userid').text();
					if(testIsNotConnected())
					{
						dispLayerNotConnected();
						$('#layer').jqmShow();
					}
					else if(testIsPeexOut(userIdToTest))
					{
						dispLayerCommWaitForFriends($('#member_pseudo').html());
						$('#layer').jqmShow();
					}
					else if(testIsPeexIn(userIdToTest) || testIsPeexNone(userIdToTest))
					{
						dispLayerCommNotFriends($('#member_pseudo').html());
						$('#layer').jqmShow();
					}
					else
						sendCommentResponse(_this);
				});
			}
		};
		
		// iterate each matched element
		return this.each(function() {
			bind($(this));
		});
	};
	
	/**
	 * plugin defaults
	 *  buttonSend		: '', le bouton qui envoi le commentaire
	 *	buttonAdd			: '', // le bouton qui affiche le formulaire d ajout
	 *	divAdd				: '', // la div qui contient le formualire d ajout
	 *	divComment		: '', // la div qui contient les commentaires
	 *	textError			: '' // le message d erreur
	 */
	$.fn.addcommentonephoto.defaults = {
		buttonSend		: '',
		buttonAdd			: '',
		divAdd				: '',
		divCloneAdd		: '',
		divComment		: '',
		textError			: ''
	};
	
/**********************************************************/
	
	/**
	 * plug in addcommentonephotocomment photo/mylist photo/list
	 */
	$.fn.addcommentonephotocomment = function(options) {
		
		// build main options before element iteration
		var opts = $.extend({}, $.fn.addcommentonephoto.defaults, options);
		
		/**
		 * envoi le commentaire vers le systeme
		 */
		function sendCommentResponse(_this) {
debug.log("sendCommentResponse(_this)");
			// recuperation des donnees _this.find(opts.divAdd).data('comment', { relatedObjectUserId : tInfoComment[2], typeID : 2, relatedObjectID : tInfoComment[1], threadID : ''}
			var data = _this.find(opts.divAdd).data('comment');
			var typeID = data.typeID;
			var relatedObjectID = data.relatedObjectID;
			var threadID = data.threadID;
			var body = _this.find('textarea[name="newcomment"]').val();
			var peexcomUserId = $('#peexcom_userid').text();
			//var relatedObjectUserId = data.relatedObjectUserId;
			var relatedObjectUserId = $("#member_userid").text();
			if( body == '' || body == ' ') {
				return false;
			}
			if(relatedObjectUserId == "" || relatedObjectUserId == undefined){
				relatedObjectUserId = peexcomUserId;
			}
debug.log(
	"peexcomUserId = " + peexcomUserId + "\n" +
	"relatedObjectUserId = " + relatedObjectUserId
)
			if( threadID == '') {
				threadID = relatedObjectID;
			}
			if( typeID ==  '3' ) { // photo = 3, comment = 2
				if( peexcomUserId != relatedObjectUserId) {
					//peexCom.comComment(to, body, threadID, typeID, elemID, callback)
					peexCom.comComment(relatedObjectUserId, body, threadID, typeID, relatedObjectID, callbackSendComment(_this));
				} else {
					//peexCom.myComComment(body, threadID, typeID, elemID, callback)
					peexCom.myComComment(body, threadID, typeID, relatedObjectID, callbackSendComment(_this));
				}
			displayCommentPhotos(_this, body);
			}
			hideForm(_this);
		};
		
		/**
		 * envoi le commentaire vers le systeme
		 */
		function sendPhotoComment(_this) {
			var idDiv = _this.attr('id').split('_');
			var typeID = idDiv[1];
			var relatedObjectID = idDiv[2];
			var threadID = null;
			var body = _this.find('textarea[name="newcomment"]').val();
			var peexcomUserId = $('#peexcom_userid').text();
			var relatedObjectUserId =  _this.find('input[name="albumUserId"]').val();
			if( body == '' || body == ' ') {
				return false;
			}
			if(relatedObjectUserId == "" || relatedObjectUserId == undefined){
				relatedObjectUserId = peexcomUserId;
			}
debug.log(
	"peexcomUserId = " + peexcomUserId + "\n" +
	"relatedObjectUserId = " + relatedObjectUserId
)
			if( typeID ==  '3' ) { // photo / album
				if( peexcomUserId != relatedObjectUserId) {
					peexCom.photoComment(relatedObjectUserId, body, threadID, typeID, relatedObjectID, callbackSendComment(_this));
				} else {
					peexCom.myPhotoComment(body, threadID, typeID, relatedObjectID, callbackSendComment(_this));
				}

				$.ajax({
					url: encodeURI( '/ajax/addcomment/type/1/relatedobjectid/'+relatedObjectID+'/content/'+body),
					dataType: "xml",
					success: function(data){
						peexCom.debug.add('success');
					},
					error: function(obj){
						peexCom.debug.add('error');
					}
				});
				
				displayCommentPhotos(_this, body);
			}
		};
		
		/**
		 * fonction de callbackl lors de l envoi du commentaire
		 */
		function callbackSendComment(_this) {
			//_this.find(opts.divAdd).hide(); // cache le formulaire
			_this.find('textarea').val(''); // remet a zero le textarea
		};
		
		/**
		 * affiche le formulaire textarea[name="newcomment"]
		 */
		function displayForm(_this) {
debug.log("displayForm(_this)");
			// ferme les autres fenetres
			hideForm(_this);
			// TODO verifier si l utilisateur est connecté
			// id du .link comment_{commentId}_{userId}
debug.log("displayForm _this.attr('id') = " + _this.attr('id'));
			var tInfoComment = _this.attr('id').split('_');
			// clone le form
debug.log("$(opts.divCloneAdd).attr('id') = " + $(opts.divCloneAdd).attr('id'));
			var clone = $(opts.divCloneAdd).clone().appendTo($(_this)).attr('id', 'commentcommform_2_' + tInfoComment[3]).show();
			// infos ajouté au data de _this.find(opts.divAdd) : commentUserId (relatedObjectUserId), typeID, relatedObjectID, threadID
			_this.find(opts.divAdd).data('comment', { relatedObjectUserId : tInfoComment[2], typeID : 3, relatedObjectID : tInfoComment[1], threadID : tInfoComment[3]});
			clone.find('textarea[name="newcomment"]').val(''); // clone, puis affiche, init à vide le form
debug.log("clone.html() = " + clone.html());
debug.log("clone.attr('id') = " + clone.attr('id'));
			// bind le bouton d envoi
			bindSend(_this);
		};
		
		/**
		 * cache le formulaire textarea[name="newcomment"]
		 */
		function hideForm(_this) {
			$('.link>' + opts.divAdd).remove();
		}
		
		/**
		 * affiche le commentaire dans la liste des commentaires 
		 */
		function displayCommentPhotos(_this, body) {
			var userName = $('#userprofile_username').text();
			var userId = $('#peexcom_userid').text();
			var userThumb1 = $('#userprofile_thumb1').text();
			var photoId = $(opts.divAdd).attr("id").split("_")[2];
			var commentHTML = 	'<li class="subline"><div class="avatar">' +
			'<img height="28" width="28" src="' + userThumb1 + '"/>' +
			'<a href="/member/index/userid/' + userId + '">' + userName + '</a>' +
			'<span>( maintenant )</span>' +
			'</div>' + 
			'<div class="infos">' +
			'<p> " ' + body + ' " </p><div id="comment_' + photoId + '" class="link"></div>' +
			'</div><br class="clear" /></li><br class="clear"/>';
			// TODO varialiser le maintenant
			//_this.parents(opts.divComment).append(commentHTML);
debug.log("commentHTML = " + commentHTML);
			_this.parents(".commentAndSub").append(commentHTML);
		};
		
		/**
		 * bind le boutons d envoi et d affichage du formulaire
		 */
		function bind(_this) {
			// bind les boutons ajouter un commentaire
			if( _this.find(opts.buttonAdd) ) {
				_this.find(opts.buttonAdd).unbind();
				_this.find(opts.buttonAdd).click(function(){
					displayForm(_this);
				});
			}
			
			// bind le bouton envoyer un commentaire sur la photo
			if( _this.find(opts.buttonSend) ) {
				_this.find(opts.buttonSend).unbind();
				_this.find(opts.buttonSend).click(function(){
					userIdToTest=($('#member_userid').text()!="") ? $('#member_userid').text() : $('#peexcom_userid').text();
					if(testIsNotConnected())
					{
						dispLayerNotConnected();
						$('#layer').jqmShow();
					}
					else if(testIsPeexOut(userIdToTest))
					{
						dispLayerCommWaitForFriends($('#member_pseudo').html());
						$('#layer').jqmShow();
					}
					else if(testIsPeexIn(userIdToTest) || testIsPeexNone(userIdToTest))
					{
						dispLayerCommNotFriends($('#member_pseudo').html());
						$('#layer').jqmShow();
					}
					else
						sendPhotoComment(_this);
				});
			}
		};
		
		/**
		 * bind les boutons de commenter un commentaire
		 */
		function bindSend(_this) {
			if( _this.find(opts.buttonSend) ) {
				_this.find(opts.buttonSend).unbind();
				_this.find(opts.buttonSend).click(function(){
					userIdToTest=($('#member_userid').text()!="") ? $('#member_userid').text() : $('#peexcom_userid').text();
					if(testIsNotConnected())
					{
						dispLayerNotConnected();
						$('#layer').jqmShow();
					}
					else if(testIsPeexOut(userIdToTest))
					{
						dispLayerCommWaitForFriends($('#member_pseudo').html());
						$('#layer').jqmShow();
					}
					else if(testIsPeexIn(userIdToTest) || testIsPeexNone(userIdToTest))
					{
						dispLayerCommNotFriends($('#member_pseudo').html());
						$('#layer').jqmShow();
					}
					else
						sendCommentResponse(_this);
				});
			}
		};
		
		// iterate each matched element
		return this.each(function() {
			bind($(this));
		});
	};
	
	/**
	 * plugin defaults
	 *  buttonSend		: '', le bouton qui envoi le commentaire
	 *	buttonAdd			: '', // le bouton qui affiche le formulaire d ajout
	 *	divAdd				: '', // la div qui contient le formualire d ajout
	 *	divComment		: '', // la div qui contient les commentaires
	 *	textError			: '' // le message d erreur
	 */
	$.fn.addcommentonephotocomment.defaults = {
		buttonSend		: '',
		buttonAdd			: '',
		divAdd				: '',
		divCloneAdd		: '',
		divComment		: '',
		textError			: ''
	};
	
})(jQuery);