$(document).ready(function(){
    
    $(".openModalWindow").css("z-index", 10000);
    
});

//$(".rich_text").live("click", $(".rich_text").ckeditor());



$(".openModalWindow").live("click",
	function(){
	    
	    
		// Recherche du formulaire
	    form = $(this).closest("form");
	    div = form.parent();
	    lstChamps = new Array();
	    data = "";
		// recherche des champs du formulaire
	    form.find("input, textarea, select").each(function(){
		
		
		if ($(this).attr("name"))
		{
		
		    if ($(this).attr("value"))
			valeur = $(this).attr("value");
		    else
			valeur = $(this).html();
		    
		    lstChamps[$(this).attr("name")] = valeur;
		    data += $(this).attr("name") + "=" + valeur + "&";		
		}
		
	    
	    });
	    

		// Préparation des variables pour la requête Ajax
	    methode = lstChamps["methode"];
	    url = lstChamps["url"];
	    asynchrone = true;
	    if (lstChamps["asynchrone"] == "false")
		asynchrone = false;


	    
	    $.ajax({
                type:methode,
                url: url ,
                dataType: "html",
                async: asynchrone,
                data:data,
                success : function (requete)
		    { 
		    
		    //$("#test").append(requete);
		
		
		    
		    
		
		    div.after(requete);
		    div.next(".modalWindow").hide();
		    //div.next(".modalWindow").css("z-index", zindexMW++);
		    div.next(".modalWindow").css("left", parseInt($("body").width())/2-parseInt(div.next(".modalWindow").css("width"))/2+"px");
		    //div.next(".modalWindow").css("top", parseInt($("body").height())/2-parseInt(div.next(".modalWindow").css("height"))+"px");
		    div.next(".modalWindow").fadeIn("slow");
		    
		    div.after("<div class='shadow'></div>");
		    div.next(".shadow").hide();
		    //div.next(".shadow").css("z-index", zindexMW-1);
		    div.next(".shadow").fadeIn("slow");
		    //zindexMW+=2;
		    
		    
		    }
            }).responseText; 
	    
	    return false;
	}
    );
    
 $(".updateModalWindow").live("click",
	function(){
	    
	    
	    
		// Recherche du formulaire
	    modalWindow = $(this).closest(".modalWindow");
	    form = $(this).closest("form");
	    
	//    $("#test").append(form.html());

	    lstChamps = new Array();
	    data = "";
		// recherche des champs du formulaire
	    form.find("input, textarea, select").each(function(){
		
		
		if ($(this).attr("name"))
		{
		
		    if ($(this).attr("value"))
			valeur = $(this).attr("value");
		    else
			valeur = $(this).html();
		    
		    lstChamps[$(this).attr("name")] = valeur;
		    data += $(this).attr("name") + "=" + valeur + "&";		
		}
		

	    });

		// Préparation des variables pour la requête Ajax
	    methode = lstChamps["methode"];
	    url = lstChamps["url"];
	    asynchrone = true;
	    if (lstChamps["asynchrone"] == "false")
		asynchrone = false;

		// le cas tinymce
	    /*if(document.getElementById('tiny_mce_textarea'))
		{
		    
		    var ed = tinyMCE.get('tiny_mce_textarea');
		    if (null != ed)
		    {
			var tiny_mce = ed.getContent();
			if (tiny_mce != null)
			    data += "content_tiny_mce="+ tiny_mce;
		    }
		
		}*/
	    $.ajax({
                type:methode,
                url: url ,
                dataType: "html",
                async: asynchrone,
                data:data,
                success : function (requete)
		    { 
		    
		    //$("#test").append("go !" +data);
		    modalWindow.after(requete);	// On ajoute un nouveau formulaire
		    modalWindow.remove();	// on enlève celui en cours
		    
		    //setup_tiny_mce();
		    
		    }
            }).responseText;   
	    
	    return false;
	}
    );
    
    
$(".isok").live("click",
	function(){
	    
		// Last() ne fonctionne pas sur JQuery 1.3.x
		// on fait donc de la sorte :
	    modalWindows = $(".modalWindow");

	    modalWindowToClose = modalWindows.eq(modalWindows.length-1);//$(this).closest(".modalWindow");
	    modalWindow = modalWindows.eq(modalWindows.length-2);
	    
		// Recherche du formulaire
	    shadow = $(this).closest(".modalWindow").prev();
	    form = $(this).closest("form");

	    modalWindowToClose.fadeOut("slow", function(){$(this).remove();});
	    shadow.fadeOut("slow", function(){$(this).remove();});
	    
 	    lstChamps = new Array();
	    data = "";
		// recherche des champs du formulaire
	    form.find("input, textarea, select").each(function(){
		
		
		if ($(this).attr("name"))
		{
		
		    if ($(this).attr("value"))
			valeur = $(this).attr("value");
		    else
			valeur = $(this).html();
		    
		    lstChamps[$(this).attr("name")] = valeur;
		    data += $(this).attr("name") + "=" + valeur + "&";		
		}
		
	    
	    });

		// Préparation des variables pour la requête Ajax
	    methode = lstChamps["methode"];
	    url = lstChamps["url"];
	    asynchrone = true;
	    if (lstChamps["asynchrone"] == "false")
		asynchrone = false;


	    
	    $.ajax({
                type:methode,
                url: url ,
                dataType: "html",
                async: asynchrone,
                data:data,
                success : function (requete)
		    { 
		    
		    //$("#test").append("go !" +data);
		    modalWindow.after(requete);	// On ajoute un nouveau formulaire
		    modalWindow.remove();	// on enlève celui en cours
		    
		    }
            }).responseText;    
	    return false;
	}
    );
    
    
    
    
 $(".closeModalWindow").live("click",
	function(){

	    modalWindow = $(this).closest(".modalWindow");
	    shadow = $(this).closest(".modalWindow").prev();
	    modalWindow.fadeOut("slow", function(){$(this).remove();});
	    shadow.fadeOut("slow", function(){$(this).remove();});
	    return false;
	    
	});
	
	
$(".shadow").live("click",
    function(){
		
    modalWindow = $(this).next(".modalWindow");
    shadow = $(this);
    modalWindow.fadeOut("slow", function(){$(this).remove();});
    shadow.fadeOut("slow", function(){$(this).remove();});
    return false;

    });   
    
    
    
    
