function rmcom (id, e) {
	var div_parent = document.getElementById(id).parentNode.id;
	var confirm = window.confirm("Effacer le commentaire ?");
	if (confirm) {
		var cid = div_parent.replace(/comment_/,"");
		new Ajax.Request('/modules/pb-comments.php', {
			method:'post',
			parameters: {cid: cid, action:'rm'},
			onSuccess: function(transport){
				var json = transport.responseText.evalJSON();
				if (json.status == 200) {
					$(div_parent).fade({ duration: 0.5, from: 1, to: 0 });
				}
			}
			//,onFailure: function(){ alert('Something went wrong...') }
		});
	}
	Event.stop(e);
}


Event.observe(window, 'load', function() {
	var comments = $$('a.lnk_delete_comment');
	comments.each(function(s, index) {
		Event.observe(s.id, 'click', function (e) {
			rmcom(s.id, e);
		});
	});
	
	$$('a.commentreport').each(function(s, index) {
		new Tip(s,'signaler ce commentaire', {
			hook: {target: 'bottomLeft', tip: 'topRight'},
			offset: {x:-5, y:0},
			//className: 'pinktip',
		});
		
		Event.observe(s, 'click', function (e) {
			if (confirm("Voulez-vous informer les modérateurs de ce commentaire ?")) {
				Event.stop(e);
				var parentid = s.up('div').id;
				var elem = parentid.split('_');
				var cid = parseInt(elem[elem.length-1]);
				
				new Ajax.Request('/modules/pb-comments-report.php',{
					method: 'post',
					contentType: 'application/x-www-form-urlencoded',
					parameters: {
						cid: cid
					},
					onSuccess: function () {
							alert('les modérateurs ont été informés.');
						}
					});
			}
		});
	});
	
	if ($('formcomment'))
		Event.observe('formcomment', 'submit', postCommentAjax);
	
	function postCommentAjax (e) {
		Event.stop(e);
		var comment = $F('formcommentarea').stripTags();
		$('formcommentarea').disabled='disabled';
		$('formcommentarea').value='en cours d\'envoi ...';
		//alert('click');
		new Ajax.Request('/modules/pb-comments.php',{
			method: 'post',
			contentType: 'application/x-www-form-urlencoded',
			parameters: {
				action: 'add',
				object_type: $F('object_type'),
				object_id: $F('object_id'),
				comment: comment
			},
			onSuccess: sendOk
			});
		//Event.stop(e);
		function sendOk (transport) {
			var json = transport.responseText.evalJSON();
			if (json.status == 200) {
				var cid = json.cid;
				var login = json.login;
				var sex = json.sex;
				var strdate = json.strdate;
				
				var div_comments = document.getElementById('comments');
				var div_root = document.createElement('div');
				
				div_root.setAttribute("id", "comment_" + cid);
				
				if (document.all) {
					//div_root.style.setAttribute("cssText","display:none;");
				} else {
					div_root.setAttribute("style","display:none;");
				}
				
				div_root.setAttribute('class','comment');
				div_root.setAttribute('className','comment');
				
				//link to remove
				var myArm = document.createElement('a');
				myArm.setAttribute("href","#");
				myArm.setAttribute('class', 'lnk_delete_comment');
				myArm.setAttribute('className', 'lnk_delete_comment');
				myArm.setAttribute("id", 'lnk_delete_comment_' + cid);
				myCross = document.createTextNode('x');
				myArm.appendChild(myCross);
				
				var div_img = document.createElement('div');
				div_img.setAttribute('class','commentimg');
				div_img.setAttribute('className','commentimg');
				
				var myA = document.createElement('a');
				var link = '/' + login;
				myA.setAttribute("href",link);
				
				//img
				var myImg = document.createElement('img');
				imgsrc = json.avatar;
				myImg.setAttribute("src", imgsrc);

				myA.appendChild(myImg);
				
				div_img.appendChild(myA);
				div_root.appendChild(myArm);
				div_root.appendChild(div_img);
				
				//div_from
				var div_from = document.createElement('div');
				div_from.setAttribute('class','commentfrom');
				div_from.setAttribute('className','commentfrom');

				myAsender = document.createElement('a');
				myAsender.setAttribute("href",link);
				// ajouter la class de sex
				myAsender.setAttribute('class', sex);
				myAsender.setAttribute('className', sex);
				
				
				mysender = document.createTextNode(login);
				myAsender.appendChild(mysender);

				mySpan = document.createElement('span');
				mySpan.setAttribute('class','commentmeta');
				mySpan.setAttribute('className','commentmeta');
				var txt = document.createTextNode(strdate);
				mySpan.appendChild(txt);

				var before = document.createTextNode('par ');

				div_from.appendChild(before);
				div_from.appendChild(myAsender);
				div_from.appendChild(mySpan);
				div_root.appendChild(div_from);
				
				var div_body = document.createElement('div');
				div_body.setAttribute('class', 'commentbody');
				div_body.setAttribute('className', 'commentbody');
				var commentarray = comment.split('\n');
				commentarray.each(function(s, index) {
					var body = document.createTextNode(s);
					div_body.appendChild(body);
					var br = document.createElement('br');
					div_body.appendChild(br);
				});
				
				div_root.appendChild(div_body);

				div_comments.appendChild(div_root); // ajouter en bas

				var formcommentarea = document.getElementById('formcommentarea');
				formcommentarea.value='';
				formcommentarea.disabled='';
				
				if (document.all) {
					//...
				} else {
					div_root.appear({ duration: 1});
				}
				
				if ($('fb_feed').checked == true) {
					var fb_img=document.getElementById("image_src");
					var fb_desc=document.getElementById("image_src");
					var fb_name=document.getElementById('pb_obj_fullname');
					var user_message_prompt = "Publier sur votre Facebook ?";
					var template_data = {
						"comment-txt":comment,
						"title-txt":fb_name.innerHTML,
						"page-url":document.URL,
						"images":[
						{"src":fb_img.href, "href":document.URL}
					]};
				var attachement = {
					'name':fb_name.innerHTML,
					'href':document.URL,
					'caption' : '{*actor*} a laissé un commentaire sur parisbouge',
					'description':comment,
					'media':
						[{'type': 'image',
							'src': fb_img.href,
							'href': document.URL }
						]
					}

					FB.Facebook.apiClient.users_hasAppPermission('publish_stream', function(result){
						if (result == 0){
                        // not work
							FB.Connect.showPermissionDialog('publish_stream', function(result){
								//alert( 'publish_stream: ' + result ); // not called
								if ( result == 'publish_stream') {
									// publish !
									 FB.Connect.streamPublish('', attachement, null, ' ', ' ', null, true);
								}
							});
						} else {
							// publish !
							 //FB.Connect.streamPublish('', template_data);
							 FB.Connect.streamPublish('', attachement, null, ' ', ' ', null, true);
						}
					} );
					//FB.Connect.showFeedDialog(62877711219, template_data);
				}
				
				Event.observe('lnk_delete_comment_' + cid, 'click', function (e) {
					rmcom('lnk_delete_comment_' + cid, e);
				});
			}
		}
	}
});

