// ==UserScript==
// @name           zAjaxEdit
// @description    Edition Ajax pour les forums
// @include        http://*.siteduzero.com/*
// @author         delphiki
// @version	0.1	
// ==/UserScript==

if(navigator.appName != "Opera"){
	var GM_JQ = document.createElement('script');
	GM_JQ.src = 'http://jquery.com/src/jquery-latest.js';
	GM_JQ.type = 'text/javascript';
	document.getElementsByTagName('head')[0].appendChild(GM_JQ);

	function GM_wait() {
		if(typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); }
		else { $ = unsafeWindow.jQuery; }
	}
	GM_wait();
}

$(function(){
	$('td.dates a img[alt="Éditer"]').parent().click(function(){
		var url = $(this).attr('href');
		
		var repRapide = $('fieldset[id="rep_rapide"]').parent();
		var repRapideContent = repRapide.html();
		repRapide.remove();
		
		var msgBox = $(this).parent().parent().next().find('.boite_message');
		var msgBoxHtmlContent = msgBox.html();
		msgBox.empty();		
		
		msgBox.append('<p id="loader" style="text-align:center">Chargement...</p>');
		
		$('<div id="ajaxedit" />').load(url + ' form[action="'+url+'"]', function(){
			$('#loader').fadeOut();
			$(this).appendTo(msgBox);
						
			$('input[name="send"]').click(function(){
				var newMsg = $('#ajaxedit textarea').val();
				var ch = $('#ajaxedit input[name="challenge"]').val();
				var sign = $('#ajaxedit input[name="sig"]').val();
				var target = $('#ajaxedit form').attr('action');
						
				$.post(target,
					{
						texte: newMsg,
						challenge: ch,
						send: 'true',
						sig: sign 
					},
					function(data){
						msgBox.empty();
						msgBox.append('<p id="loader" style="text-align:center">Chargement...</p>');
						var postId = target.split('-')[2].split('.')[0];
						$('<div id="newmess" />').load(location.href + ' .liste_messages', function(){
							var sentMsg = $(this).find('#r'+postId).parent().parent().next().find('.boite_message').html();
							$('#loader').fadeOut();
							msgBox.html(sentMsg);
						});
					}
				);
				return false;
			});
		
		});
		
		return false;
	});
	
});