function envoyerProfil() {
	if($('selection-books').value=='Titre / Auteur , Titre / Auteur , ...') $('selection-books').value='';
	if($('selection-quotes').value=='Nom / Fonction , Nom / Fonction , ...') $('selection-quotes').value='';
	if($('selection-interests').value=='http://, http://, ...') $('selection-interests').value='';
	return true;
}
function deconnexion() {
	var url = location.href;
	if(url.substr(url.length-3,3).toLowerCase()=='cgi' || url.substr(url.length-5,3).toLowerCase()=='cgi')
		url = 'http://www.contre-feux.com/';
	else
		url = location.href;
	location.href='http://www.contre-feux.com/cgi/mt-comments.cgi?__mode=handle_sign_in&amp;logout=1&static='+url;
}
function formatLogin(val) {
	return val.replace(/[^a-zA-Z0-9]/g,'');
}
function getCookieVal(offset){
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
function LireCookie(nom){
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen){
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return null;
}
function EcrireCookie(nom, valeur){
	var argv=EcrireCookie.arguments;
	var argc=EcrireCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=nom+"="+escape(valeur)+
	((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
	((path==null) ? "" : ("; path="+path))+
	((domain==null) ? "" : ("; domain="+domain))+
	((secure==true) ? "; secure" : "");
}
/*
function EffaceCookie(nom){
	date=new Date;
	date.setFullYear(date.getFullYear()-1);
	EcrireCookie(nom,null,date);
}
*/
function isLogged() {
	var name = LireCookie('commenter_name');
	if(name!='' && name!=null) return true;
	else return false;
}


/* numérote les articles les plus... */
function topnotesNum(){
	var i = 1;
	$$('p.topnotes-titre strong').each(function(elt){
		$(elt).insert(i);
		i++;
	});

}

document.observe("dom:loaded",function(){

	/* TABS DE DROITE LIENS */
	$$('#tab-consultes a').each(function(elt){elt.href='http://www.contre-feux.com/plusconsultes.html'});
	$$('#tab-commentes a').each(function(elt){elt.href='http://www.contre-feux.com/pluscommentes.html'});
	$$('#tab-apprecies a').each(function(elt){elt.href='http://www.contre-feux.com/plusapprecies.html'});

	/* HEADER TABS */
	$$('#toptab-particip a').each(function(elt){elt.href='http://www.contre-feux.com/participez.html'});
	$$('#toptab-ident a').each(function(elt){elt.href='http://www.contre-feux.com/login_form.html'});
       if(isLogged()) { // chargement de l'onglet "mon compte"
		 $('toptab-particip').removeClassName('noir');
		 $('toptab-ident').down().update('MON COMPTE');
		 $('toptab-ident').down().writeAttribute('href', 'http://www.contre-feux.com/login_box.html');
		 new Ajax.Updater('topbox-texte', 'http://www.contre-feux.com/login_box.html', {
				onComplete:function() {
			$('box-moncompte-nom').update(LireCookie('commenter_name'));
			$('lien-monprofil').href = 'http://www.contre-feux.com/profile/' + LireCookie('commenter_login');
				}
			});
	}else{
		$('toptab-ident').removeClassName('noir');
		$('topbox-inner').show();
	}

	$$('#toptabs a').each(function(elt){
		$(elt).observe('click',function(e){
			Event.stop(e);
// exception double clic sur participer : envoyer vers participer.php direct!
if($(elt).up().hasClassName('noir') && $(elt).href=='http://www.contre-feux.com/participez.html') location.href='http://www.contre-feux.com/participer.php';
			$$('#toptabs li.noir').each(function(elem){
				$(elem).removeClassName('noir')
			});
			$(elt).up().addClassName('noir');
			$('topbox-texte').update('<p id="topbox-inner">&nbsp;</p>');
			new Ajax.Updater('topbox-texte', $(elt).href, {
				onComplete:function(a){
					if(elt=='http://www.contre-feux.com/login_form.html') {
						$('staticlogin').value = location.href;
					} else if(elt=='http://www.contre-feux.com/participez.html') {
						if(isLogged()) { $('topbox-inner').down().href = 'http://www.contre-feux.com/participer.php'; }
						$('topbox-inner').show();
					} else if(elt=='http://www.contre-feux.com/login_box.html') {
						$('box-moncompte-nom').update(LireCookie('commenter_name'));
						$('lien-monprofil').href = 'http://www.contre-feux.com/profile/' + LireCookie('commenter_login');
					}
				}
			});
			
		});		
	});

	/* AJAX MISE A JOUR DAU CHARGEMENT */
	var debat = ($('debat-home')) ? $('debat-home').value : '';
	var debat2 = ($('debat-home2')) ? $('debat-home2').value : '';
	new Ajax.Request('http://www.contre-feux.com/ajax_onload.php',{
		method:'post',
		parameters:{debat:debat,debat2:debat2},
		onSuccess:function(transport) {
			var retour = transport.responseText.split(":");
			if($('debat-home-nbreac') && parseInt(retour[0])==retour[0]) {
				if(retour[0]>1)
					$('debat-home-nbreac').update(retour[0]+' r&eacute;actions');
				else
					$('debat-home-nbreac').update(retour[0]+' r&eacute;action');
				$('debat-home-nbreac').show();
			}
			if($('petition-nbsign') && parseInt(retour[1])==retour[1]) {
				if(retour[1]>1)
				  $('petition-nbsign').update(retour[1]+' signatures');
				else
				  $('petition-nbsign').update(retour[1]+' signature');
				$('petition-nbsign').show();
			}
			if($('debat-home-nbreac2') && parseInt(retour[2])==retour[2]) {
				if(retour[2]>1)
					$('debat-home-nbreac2').update(retour[2]+' r&eacute;actions');
				else
					$('debat-home-nbreac2').update(retour[2]+' r&eacute;action');
				$('debat-home-nbreac2').show();
			}
		}
	});

	/* LES PLUS ... */
	topnotesNum();
	$$('#topnotes-tabs a').each(function(elt){
		$(elt).observe('click',function(e){
			Event.stop(e);
			$$('#topnotes-tabs li.active').each(function(elem){
				$(elem).removeClassName('active')
			});
			$(elt).up().addClassName('active');
			$('topnotes-tabs-texte').update('chargement en cours...');
			new Ajax.Updater('topnotes-tabs-texte', $(elt).href, {
					onComplete: function() {
						topnotesNum();
					}
				});
			
		});		
	});

});



/* PAGES ARTICLE */
var taille=12;
function AugmenterTexte() {
	taille = taille+1;
	$('entrybody').style.fontSize = taille+'px';
}
function DiminuerTexte() {
	taille = taille-1;
	$('entrybody').style.fontSize = taille+'px';
}

function laisserComm(entry) {
	if(isLogged()) {
		new Ajax.Updater('form-commentaire', 'http://www.contre-feux.com/form_commentaire.php',{
		  parameters: { url: location.href, entry:entry, blog:1 },
		  onComplete: function(){

var name = LireCookie('commenter_name');
var ids = LireCookie('commenter_id').split(':');
var id = ids[0];
var login = name;
login+= ', réagissez sur cet article :'
login+= '<br /><a href="http://www.contre-feux.com/cgi/mt-comments.cgi?__mode=handle_sign_in&amp;logout=1&static=' + location.href + '" class="lien">';
login+= "(si vous n'êtes pas "+name+", merci de vous déconnecter)</a>"; 
$('comments-login').innerHTML = login;

$('form-commentaire').style.display = 'block';
new Effect.Pulsate('form-commentaire',{pulses:3,from:0.3});
			}
		});
	} else { 
		$('form-commentaire').style.display = 'block';
		new Effect.Pulsate('form-commentaire',{pulses:3,from:0.3});
	}
}
function afficherComm(nbcomm, entry) {
//	if(nbcomm>0) {
//		$('form-commentaire').style.display = 'none';
//	} else 
	if (isLogged()) {
		laisserComm(entry);
	}
}

function caseReponseComm(entry_id, parent_id, author) {
	var p = document.getElementById(entry_id + '_comment-reply');

	var label = document.createElement('label');
	var checkbox = document.createElement('input');
	checkbox.setAttribute('type', 'checkbox');
	checkbox.setAttribute('name', 'simplythreaded_parent_id');
	checkbox.value = parent_id; 
	checkbox.setAttribute('checked', true);
	label.appendChild(checkbox);

	var reply_text = document.createTextNode(' Réponse �?  ');
	label.appendChild(reply_text);

	var anchor = document.createElement('a');
	anchor.setAttribute('href', '#comment-' + parent_id);
	anchor.innerHTML = author;
	label.appendChild(anchor);

	while (p.hasChildNodes()) {
		p.removeChild(p.firstChild);
	}

	p.appendChild(label);
}

function repondreCommentaire(entry_id, parent_id, author) {

	var p = document.getElementById(entry_id + '_comment-reply');
	if(!p) {
		new Ajax.Updater('form-commentaire', 'http://www.contre-feux.com/form_commentaire.php',{
		  parameters: { url: location.href, entry:entry_id },
		  onComplete: function(){

var name = LireCookie('commenter_name');
var ids = LireCookie('commenter_id').split(':');
var id = ids[0];
var login = name;
login+= ', réagissez sur cet article :'
login+= '<br /><a href="http://www.contre-feux.com/cgi/mt-comments.cgi?__mode=handle_sign_in&amp;logout=1&static=' + location.href + '" class="lien">';
login+= "(si vous n'�?ªtes pas "+name+", merci de vous déconnecter)</a>"; 
$('comments-login').innerHTML = login;

$('form-commentaire').style.display = 'block';
caseReponseComm(entry_id,parent_id,author);
			}
		});
		return false;
	} else
		caseReponseComm(entry_id,parent_id,author);

}

function noterArticle(entry, a, b, auteur) {
	var x;
	var y;
	x = (a+b)/2;
	y = (a-b)/2;

	// + de pour que de contre = main rouge
	if(a>0) up = 'pic_main-up-rouge.gif';
	else    up = 'pic_main-up-bleue.gif';
	
	var html;
	if( isLogged() ) {
		html = '<a href="#0" onclick="pushRating(\'entry\','+ entry +',1,1,'+ a +','+ b +','+ auteur +');return false;">';
		html+= '<img src="http://www.contre-feux.com/static/themes/contrefeux/images/'+ up +'" align="absmiddle" />';
		html+= '</a>';
		html+= ' ' + x;
		html+= '  &nbsp; &nbsp;';
		html+= '<a href="#0" onclick="pushRating(\'entry\','+ entry +',-1,1,'+ a +','+ b +','+ auteur +');return false;">';
		html+= '<img src="http://www.contre-feux.com/static/themes/contrefeux/images/pic_main-down-bleue.gif" align="absmiddle" />';
		html+= '</a>';
		html+= ' ' + y;
	} else {
		html = '<a href="#0" onclick="noterLogin('+ entry +')">';
		html+= '<img src="http://www.contre-feux.com/static/themes/contrefeux/images/'+ up +'" align="absmiddle" />';
		html+= '</a>';
		html+= ' ' + x;
		html+= '  &nbsp; &nbsp;';
		html+= '<a href="#0" onclick="noterLogin('+ entry +')">';
		html+= '<img src="http://www.contre-feux.com/static/themes/contrefeux/images/pic_main-down-bleue.gif" align="absmiddle" />';
		html+= '</a>';
		html+= ' ' + y;
	}

	$('vote-resultats').update(html);
}
function noterLogin(entry) {
//	$('login-vote-php').update('chargement...');
	$('login-vote-php').hide();
	new Ajax.Updater('login-vote-php', 'http://www.contre-feux.com/login_vote.php', 
		{	parameters: { link: location.href , entry: entry  },
			onComplete: function() {if($('login-vote-php').visible()==false) new Effect.BlindDown('login-vote-php');}
		}
	);
}


function noteComm(id, a, b, auteur) {
	var x;
	var y;
	x = (a+b)/2;
	y = (a-b)/2;

	var html;
	if(isLogged()) {
		d = $('vote-comment-'+id);

		html = '<a href="#0" onclick="pushRating(\'comment\','+ id +',1,1,'+ a +','+ b +','+ auteur +');return false;">';
		if(a>0)
		  html+= '<img src="http://www.contre-feux.com/static/themes/contrefeux/images/pic_main-up-rouge.gif" align="absmiddle" />';
		else
		  html+= '<img src="http://www.contre-feux.com/static/themes/contrefeux/images/pic_main-up-bleue.gif" align="absmiddle" />';
		html+= '</a>';
		html+= ' '+x+' &nbsp; ';
		html+= '<a href="#0" onclick="pushRating(\'comment\','+ id +',-1,1,'+ a +','+ b +','+ auteur +');return false;">';
		html+= '<img src="http://www.contre-feux.com/static/themes/contrefeux/images/pic_main-down-bleue.gif" align="absmiddle" />';
		html+= '</a>';
		html+= ' '+y;
		d.innerHTML = html;
	} else {
		d = $('vote-comment-'+id);
		if(a>0)
		  html+= '  <img src="http://www.contre-feux.com/static/themes/contrefeux/images/pic_main-up-rouge.gif" />';
		else
		  html+= '  <img src="http://www.contre-feux.com/static/themes/contrefeux/images/pic_main-up-bleue.gif" />';
		html+= '  &nbsp; &nbsp;';
		html+= '  <img src="http://www.contre-feux.com/static/themes/contrefeux/images/pic_main-down-bleue.gif" />';
		d.innerHTML = html;
	}
}


/* DIVERS */


function cacherPub(){
	$$('.pub').each( function(elt) {
		$(elt).style.visibility = 'hidden';
	});
}

function montrerPub(){
	$$('.pub').each( function(elt) {
		$(elt).style.visibility = 'visible';
	});
}

function openBox(url,titre) {
	if(titre==null) titre='';
	cacherPub();
	GB_showCenter(titre,url,540,500,function(){
		$$('.pub').each( function(elt) {
			$(elt).style.visibility = 'visible';
		});
	});
	return false;
}

function openBoxImage(url,w,h) {
	if(navigator.appName=="Microsoft Internet Explorer") {
	  w+=5;
	  h+=5;
	}
	cacherPub();
	GB_showCenter('',url,h,w,function(){
		$$('.pub').each( function(elt) {
			$(elt).style.visibility = 'visible';
		});
	});
	return false;
}

/**
*
* URL encode / decode
* http://www.webtoolkit.info/
*
**/

var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

function rechercherCF(q) {
	if($('search-cf').checked)
		location.href = '/rechercher/'+Url.encode(q);
//		location.href = 'http://www.contre-feux.com/cgi/mt-search.cgi?search='+Url.encode(q);
	else if($('search-google').checked) {
		url = 'http://www.google.com/custom?';
		url+= 'q=' + Url.encode(q) + '&sa=Recherche+Google';
		url+= '&client=pub-2649874145700019';
		url+= '&forid=1';
		url+= '&ie=ISO-8859-1';
		url+= '&oe=ISO-8859-1';
		url+= '&cof=GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:336699;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;FORID:1';
		url+= '&hl=fr';
		location.href = url;
	}
	return false;
}

function checkPetForm() {
	if($F('sign-nom')=='' || $F('sign-prenom')=='' || $F('sign-email')=='') {
	  alert('Merci de renseigner au moins vos nom, prénom et email');
	  return true;
	} else return false;
}


/* affiche l'actu suivante (home) */
function defilReuters(id) {
	if(id && $(id).next())
	  next = $(id).next();
	else 
	  next = $('fil-reuters').down();

	if(navigator.appName=='Microsoft Internet Explorer') {
	  // version prototype sans scriptaculous
	  next.show();
	  setTimeout('$(\''+ next.id +'\').hide()',4100);
	} else {
	  new Effect.Appear(next.id,{duration:0.3});
	  setTimeout('new Effect.Fade(\'' + next.id + '\',{duration:0.3});',4000);
	}

	  setTimeout("defilReuters('"+ next.id +"')",4400);
 
}

/* affiche la miniature en grand REUTERS */
function resizeThumb() {
	$$('.reuters-thb').each( function(e){ e.toggle(); });
}

/* vérifie que le formulaire de participation est bien rempli et que l'user est loggé */
/*
function checkParticiper() {
	if(!isLogged()) {
	  alert('Vous devez �?ªtre connecté pour participer!\nMerci de vous identifier ou de vous inscrire avant de valider ce formulaire');
	  return false;
	} else return true;
}
*/