var XHR = null;
function getServeur(){
	if(window.XMLHttpRequest){ // Firefox
		XHR = new XMLHttpRequest();
	} else if(window.ActiveXObject){ // Internet Explorer
		XHR = new ActiveXObject("Microsoft.XMLHTTP");
	} else { // XMLHttpRequest non supporté par le navigateur
		alert("Ton navigateur n'est pas compatible. Contact le webmaster pour lui signaler.");
		return;
	}
	return XHR;
}
function del_pj(id){
    var ARQ = getServeur();
    ARQ.open('GET',"del_pj.php?url_pj="+id,true);
    ARQ.onreadystatechange = function(){
        if (ARQ.readyState == 4){
            $a(id).innerHTML = ARQ.responseText;
        }
    }
    ARQ.send(null);
}
function identifie(login,mdp){
    var ARQ = getServeur();
    ARQ.onreadystatechange = function(){
        if (ARQ.readyState == 4){
            if(ARQ.responseText == 'ok'){
                $a('iden').innerHTML = 'Tu es connect\351 en tant qu\'administrateur';
            } else if(ARQ.responseText == 'erreur'){
                alert('V\351rifie tes identifiants');
            } else {
                url = window.document.location.href;
                window.location = url;
            }
        }
    }
    ARQ.open('POST',"connect.php?nocacha="+Math.random(),true);
    ARQ.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    ARQ.send('login='+login+'&mdp='+mdp);
}
function $a(id){
    return document.getElementById(id);
}
var oldCadre;
function modifie(cadre){
    if(oldCadre){
        $a(oldCadre).innerHTML = tinyMCE.get('elm'+oldCadre).getContent();
        tinyMCE.get('elm'+oldCadre).hide();
        $a('pj'+oldCadre).style.display = 'none';
    }
    var contenu = $a(cadre).innerHTML;
    $a(cadre).innerHTML = '<textarea id="elm'+cadre+'" name="elm'+cadre+'" rows="15" cols="80" style="width: 80%">'+contenu+'</textarea>';
    $a('imgEdit'+cadre).style.display = 'none';
    oldCadre = cadre;
    initialise();
    $a('pj'+cadre).style.display = 'block';
}
function initialise(){
	tinyMCE.init({
		// General options
		mode : "textareas",
		theme : "advanced",
		plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave",

		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,restoredraft",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,

		// Example content CSS (should be your site CSS)
		content_css : "TinyMCE.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js",

		// Style formats
		style_formats : [
			{title : 'Bold text', inline : 'b'},
			{title : 'Red text', inline : 'span', styles : {color : '#ff0000'}},
			{title : 'Red header', block : 'h1', styles : {color : '#ff0000'}},
			{title : 'Example 1', inline : 'span', classes : 'example1'},
			{title : 'Example 2', inline : 'span', classes : 'example2'},
			{title : 'Table styles'},
			{title : 'Table row 1', selector : 'tr', classes : 'tablerow1'}
		],

		// Replace values for the template plugin
		template_replace_values : {
			username : "COIKE",
			staffid : "118218"
		}
	});
}
function upload(etape,cadre){
    if(etape == 0){
        $a('pjart'+cadre).innerHTML = '<form id="up_file'+cadre+'" action="pj.php" method="post" enctype="multipart/form-data" target="frame_atoufaire"><div id="file_load"></div>Joindre un fichier : <input type="file" name="pj" onchange="upload(1,\''+cadre+'\');" /><input type="hidden" name="MAX_FILE_SIZE" value="20000000" /><div id="file_load_error" style="color:red;"></div><input type="hidden" name="nbr_file" value="0" /><input type="hidden" name="cadre" value="'+cadre+'" /></form>';
    	alert('up_file'+cadre);
	} else if(etape == 1){
    	alert('up_file'+cadre);
        $a('up_file'+cadre).submit();
    }
}
function delArticle(id,etape,section){
	if(etape == 1){
		if($a('del'+id)){
			$a('del'+id).style.display = 'block';
		} else {
			$a('imgAdmin'+id).innerHTML += '<div id="del'+id+'" style="position:absolute;left:10px;" class="module"><div class="article">Veux-tu vraiment supprimer cet article?<br /><br /><a href="#" class="lien_art" onclick="delArticle(\''+id+'\',2,'+section+');return(false);">Oui</a> ou <a href="#" class="lien_art" onclick="$a(\'del'+id+'\').style.display=\'none\';return(false);">Non</a></div></div>';
		}
	} else if(etape == 2){
		var ARQ = getServeur();
	    ARQ.open('GET',"del_article.php?section="+section+"&article="+id+"&nocache="+Math.random(),true);
	    ARQ.onreadystatechange = function(){
	        if (ARQ.readyState == 4){
	            $a(id).innerHTML = ARQ.responseText;
				$a('del'+id).style.display = 'none';
	        }
	    }
	    ARQ.send(null);
	}
}
function cacheArt(id,section){
	if($a('visible'+id).checked == true){
		etat = 1;
	} else {
		etat = 0;
	}
	var ARQ = getServeur();
    ARQ.open('GET',"cache_article.php?etat="+etat+"&section="+section+"&article="+id+"&nocache="+Math.random(),true);
    ARQ.onreadystatechange = function(){
        if (ARQ.readyState == 4){
            $a('cadre'+id).style.backgroundColor = ARQ.responseText;
        }
    }
    ARQ.send(null);
}
listeArt = new Array();
function addArticle(id){
	listeArt.push(id);
}
var onMove = 0;
function moveArt(id,position,section){
	if(onMove == 0){
		for(i=0;i<listeArt.length;i++){
			$a('contenuArt'+listeArt[i]).style.display = 'none';
			$a('interdiv'+position).style.display = 'none';
		}
		onMove = id;
	} else {
		for(i=0;i<listeArt.length;i++){
			$a('contenuArt'+listeArt[i]).style.display = 'block';
		}
		onMove = 0;
		for(i=1;i<1000;i++){
    		if($a('interdiv'+i)){
	    		if($a('interdiv'+i).style.backgroundColor == 'rgb(0, 0, 0)'){
		    		var ARQ = getServeur();
				    ARQ.open('GET',"move_article.php?pos="+i+"&section="+section+"&article="+id+"&nocache="+Math.random(),true);
				    ARQ.onreadystatechange = function(){
				        if (ARQ.readyState == 4){
				            if(ARQ.responseText == 1){
				            	url = window.parent.document.location.href;
								window.parent.location = url;
				            } else {
				            	alert('Une erreur est survenue, merci de r\351\351ssayer plus tard.');
				            }
				        }
				    }
				    ARQ.send(null);
		    	}
    		} else {
    			i = 2000;
    		}
    	}
		$a('interdiv'+position).style.display = 'block';
	}
}
var Mouse_X; // Variable globale Position X de la Mouse
var Mouse_Y; // Variable globale Position Y de la Mouse

function WhereMouse(e){
    if(e){                     // Dans ce cas on obtient directement la position dans la page
        Mouse_X = e.pageX;
        Mouse_Y = e.pageY;
    }
    else {                      // Dans ce cas on obtient la position relative à la fenêtre d'affichage
        Mouse_X = event.clientX;
        Mouse_Y = event.clientY;
    }
    if(onMove != 0){
    	// on bouge l'article
    	$a('cadre'+onMove).style.position = 'absolute';
    	window.parent.document.body.scrollTop++
	    if (typeof(window.pageYOffset) == 'number' ) {
	        //Netscape compliant
	        scrOfY = window.pageYOffset;
	    	$a('cadre'+onMove).style.top = Mouse_Y-40+'px';
	    } else if (document.body && document.body.scrollTop) {
	        //DOM compliant
	        scrOfY = document.body.scrollTop;
	        $a('cadre'+onMove).style.top = Mouse_Y+scrOfY-40+'px';
	    } else if (document.documentElement && document.documentElement.scrollTop) {
	        //IE6 standards compliant mode
	        scrOfY = document.documentElement.scrollTop;
	        $a('cadre'+onMove).style.top = Mouse_Y+scrOfY-40+'px';
	    }
	    for(i=0;i<1000;i++){
	    	if($a('interdiv'+i)){
		        $a('interdiv'+i).style.backgroundColor = '#FDF4AC';
		        if(Mouse_Y > $a('interdiv'+i).offsetTop && Mouse_Y < $a('interdiv'+i).offsetTop+30){
		            $a('interdiv'+i).style.backgroundColor = '#000000';
		        }
		    } else {
		    	i = 2000;
		    }
	    }
    }
}
//== INITIALISATION ================
document.onmousemove = WhereMouse;

