var isInitCom=false;
var intervalCom;
var nbCom=0;

//Variable gestion du fonds
var intervalFond;
var timerFond = 150;
var offset_fond=0;
var switch_value_fond = 128;
var tabimages = new Array();
var conteneur1="";
var conteneur2="";
var fond_repertoire="";

//Outils Divers
function trim(myString)
{
	return myString.replace(/^\s+/g,'').replace(/\s+$/g,'');
}
function DecaleFond()
{
	if(offset_fond<switch_value_fond)
	{
		//Decalage
		conteneur2.style.backgroundPosition=offset_fond + "px 0px";
		offset_fond++;
	}
	else
	{
		//Switch des deux images
		conteneur1.style.backgroundImage = conteneur2.style.backgroundImage;

		//Chargement d'une image au hasard
		var index = Math.floor(Math.random() * tabimages.length);
		var imagefond = tabimages[index].value;
		conteneur2.style.backgroundImage = "url(" + fond_repertoire + trim(imagefond) + ")" 
		
		//Reinit des variables
		conteneur2.style.backgroundPosition="0px 0px";
		offset_fond=0;
	}
}

//Initialize les fonds de site
function initFonds()
{
	tabimages = $$('.fichier_image');
	fond_repertoire = 	$('fonds_rep').value;
	conteneur1 = $('conteneur_1');
	conteneur2 = $('conteneur_2');
	if(tabimages.length==1)
	{
		var imagefond = tabimages[0].value;
		conteneur1.style.backgroundImage = "url(" + fond_repertoire + trim(imagefond) + ")" 
	}
	else if(tabimages.length==2)
	{
		var imagefond = tabimages[1].value;
		conteneur1.style.backgroundImage = "url(" + fond_repertoire + trim(imagefond) + ")" 
		conteneur2.style.backgroundImage = "url(" + fond_repertoire + trim(imagefond) + ")" 
	}
	else if(tabimages.length>2)
	{
		var index = Math.floor(Math.random() * tabimages.length);
		var imagefond = tabimages[index].value;
		conteneur1.style.backgroundImage = "url(" + fond_repertoire + trim(imagefond) + ")" 
		conteneur2.style.backgroundImage = "url(" + fond_repertoire + trim(imagefond) + ")" 
		intervalFond = setInterval("DecaleFond()",timerFond);
	}
}

//Affiche la liste des articles
function listAffiche(offset)
{
	var conteneur = $('col_droite');
	var typearticle = $('typearticle').value;
	var url = 'site/article/articleliste&offset='+offset+'&typearticle='+typearticle;
	new Ajax.Request(urlaff, {
		asynchronous: 'true', 
		postBody: 'tpl='+url,
		onSuccess: function(zhr)
		{
			conteneur.innerHTML = zhr.responseText;
			new Effect.Appear(conteneur, {queue:'end', duration:0.2, afterFinish:function(){Behaviour.apply();}});
		}
	});	
}

//Affiche la liste des agendas
function listAgendaAffiche(offset)
{
	var conteneur = $('col_droite');
	var url = 'site/agenda/agendaliste&offset='+offset;
	new Ajax.Request(urlaff, {
		asynchronous: 'true', 
		postBody: 'tpl='+url,
		onSuccess: function(zhr)
		{
			conteneur.innerHTML = zhr.responseText;
			new Effect.Appear(conteneur, {queue:'end', duration:0.2, afterFinish:function(){Behaviour.apply();}});
		}
	});	
}

//Affiche un article
function articleAffiche(idArticle)
{
	var conteneur = $('col_droite');
	var typearticle = $('typearticle').value;
	var url = 'site/article/article&typearticle='+typearticle+'&article='+idArticle;
	new Ajax.Request(urlaff, {
		asynchronous: 'true', 
		postBody: 'tpl='+url,
		onSuccess: function(zhr)
		{
			conteneur.innerHTML = zhr.responseText;
			new Effect.Appear(conteneur, {queue:'end', duration:0.2, afterFinish:function(){Behaviour.apply();/*initCom()*/}});
		}
	});	
}

//Record
function record(type,idsup,origine)
{
	var type= type||"";
	var idsup= idsup||"";
	var chPost="";
	var origine=origine||""
	var conteneur = "conteneur_"+type;
	var dispmsg = $("msg");

	//Construction de la chaine des posts
	chPost = FORM_buildpoststring(conteneur,"record","");
	if(idsup!="")
	{
		chPost += "&idsup=" + idsup;
	}
	new Ajax.Request(urltask, {
		asynchronous: 'true',
		postBody: 'tache=enregistre/'+type+"_enreg"+chPost,
		onSuccess: function(xhr)
		{
			var result = treatTaskMsg(xhr.responseText);
			if(result == true)
			{//Traitement ok
				if(origine=="com")
				{
					//On recharge la page article
					window.location = window.location;
				}
				else
				{
					if(dispmsg)
					{
						dispmsg.style.display = 'none';
					}
					
					//Traitement du message de retour
					var response = xhr.responseText.substr(13);
					
					//Masque le formualire
					new Effect.SlideUp(conteneur, { duration: 0.5 });
					window.scrollTo(0,0);
	
					//Affichage du message de reussite
					if(dispmsg && response)
					{
						dispmsg.innerHTML = response;
						new Effect.Appear(dispmsg, { duration: 0.5 });
					}
				}
			}
			else if(result == false)
			{
				//Masque le formualire
				new Effect.SlideUp(conteneur, { duration: 0.5 });
				window.scrollTo(0,0);
				//Affichage du message d'erreur
				if(dispmsg)
				{
					dispmsg.innerHTML = xhr.responseText.substr(11);
					new Effect.Appear(dispmsg, { duration: 0.5 });
				}
			}
		}
	});

}

//Gestion des commentaires
function initCom()
{
	isInitCom=true;
	divs = $$('.com')
	nbCom = divs.length;
	if(nbCom>=1)
	{
		//On affiche le premier commentaire
		$('com_en_cours').value='com_1';
		$('com_1').style.display='';
	}
	if(nbCom>=2)
	{
		intervalCom = setInterval("nextCom()",5000);
	}
}

function nextCom()
{
	var tabencours = $('com_en_cours').value.split("_");
	if(tabencours.length<2 || tabencours[1]== nbCom)
	{
		tabencours[1]=1;
	}
	else
	{
		tabencours[1]++;
	}
	new Effect.Fade($($('com_en_cours').value), {queue:'front',duration: 1.0,afterFinish: function(){$('com_en_cours').value = tabencours.join("_");new Effect.Appear($(tabencours.join("_")), {queue:'front',duration: 1.0});}});
}

function ajustePolice()
{
	zones = $$('.liste_commentaire_detail');
	if(zones.length>0)
	{
		var taillepolice = $('entete_article').style.fontSize;
		$('liste_commentaire_titre').style.fontSize = taillepolice;
		zones = $$('.liste_commentaire_detail');
		for(var i=0 ;zones.length;i++)
		{
			zones[i].style.fontSize = taillepolice;
		}
	}
}

var myrules = {
	//Gestion des images de fonds
	'#conteneur_1': function()
	{
		//initFonds();
	},
	
	
	//Navigation liste article
	'.btn_suiv_liste' : function(bouton)
	{
		bouton.onclick = function()
		{
			var idbout = bouton.id.split("_");
			var offset = (parseInt(idbout[1])+3);
			listAffiche(offset);
			window.scrollTo(0,0);
		}
	},

	'.btn_prec_liste' : function(bouton)
	{
		bouton.onclick = function()
		{
			var idbout = bouton.id.split("_");
			var offset = (parseInt(idbout[1])-3);
			listAffiche(offset);
			window.scrollTo(0,0);
		}
	},

	//Navigation liste agenda
	'.btn_suiv_liste_agenda' : function(bouton)
	{
		bouton.onclick = function()
		{
			var idbout = bouton.id.split("_");
			var offset = (parseInt(idbout[1])+5);
			listAgendaAffiche(offset);
			window.scrollTo(0,0);
		}
	},

	'.btn_prec_liste_agenda' : function(bouton)
	{
		bouton.onclick = function()
		{
			var idbout = bouton.id.split("_");
			var offset = (parseInt(idbout[1])-5);
			listAgendaAffiche(offset);
			window.scrollTo(0,0);
		}
	},

	//Navigation article
	'.btn_nav_article' : function(bouton)
	{
		bouton.onclick = function()
		{
			var idbout = bouton.id.split("_");
			var idArticle = parseInt(idbout[1]);
			articleAffiche(idArticle);
			window.scrollTo(0,0);
		}
	},
	
	//Taille des polices
	'.btn_police_plus' : function(bouton)
	{
		bouton.onclick = function()
		{
			var tailleMax = 48;
			var taillepolice = parseInt($('entete_article').style.fontSize.substring(0,2));
			if(taillepolice<tailleMax)
			{
				taillepolice+=4;
				$('entete_article').style.fontSize = taillepolice+"px";
				$('text_article').style.fontSize = taillepolice+"px";
				$('liste_commentaire_titre').style.fontSize = taillepolice+"px";
				zones = $$('.liste_commentaire_detail');
				for(var i=0 ;zones.length;i++)
				{
					zones[i].style.fontSize = taillepolice+"px";
				}
			}
		}
	},
	
	'.btn_police_moins' : function(bouton)
	{
		bouton.onclick = function()
		{
			var tailleMin = 12;
			var taillepolice = parseInt($('entete_article').style.fontSize.substring(0,2));
			if(taillepolice>tailleMin)
			{
				taillepolice-=4;
				$('entete_article').style.fontSize = taillepolice+"px";
				$('text_article').style.fontSize = taillepolice+"px";
				$('liste_commentaire_titre').style.fontSize = taillepolice+"px";
				zones = $$('.liste_commentaire_detail');
				for(var i=0 ;zones.length;i++)
				{
					zones[i].style.fontSize = taillepolice+"px";
				}
			}
		}
	},
	//Impression
	'.btn_print' : function(bouton)
	{
		bouton.onclick = function()
		{
			var background = $('body').style.backgroundImage; 
			$('body').style.backgroundImage='';
			window.print();
			$('body').style.backgroundImage=background;
		}
	},

	//Enregistrement newsletter
	'.btn_valider_newsletter' : function(bouton)
	{
		bouton.onclick = function()
		{
			var tabType = bouton.id.split("_");
			type = tabType[1];
			var idSup = $("id_sup").value;
			record(type,idSup,"news");
		}
	},
	//Enregistrement commentaire
	'.btn_valider_commentaire' : function(bouton)
	{
		bouton.onclick = function()
		{
			if($("formtitre").value=="")
			{
				$("msg").innerHTML="Veuillez saisir un titre";
			}
			else if($("formauteur").value=="")
			{
				$("msg").innerHTML="Veuillez saisir un auteur";
			}
			else if($("formcontent").value=="")
			{
				$("msg").innerHTML="Veuillez saisir un commentaire";
			}
			else
			{
				var tabType = bouton.id.split("_");
				type = tabType[1];
				var idSup = $("id_sup").value;
				record(type,idSup,"com");
			}
		}
	},
	
	'.count_car' : function(zone)
	{
		var nbmaxchar = parseInt($("compteur").innerHTML);

		zone.onkeyup = function()
		{
			if(zone.value.length >nbmaxchar)
			{
				zone.value = zone.value.substring(0,nbmaxchar);
				zone.scrollTop = zone.scrollHeight;
			}
			var nbchar =  nbmaxchar - zone.value.length;
			$("compteur").innerHTML	= nbchar;
		}

		zone.onkeydown = function(event)
		{
			var nbchar = parseInt($("compteur").innerHTML);
			if(nbchar<=0 && event.keyCode!=8 && event.keyCode!=46 && event.keyCode!=33 && event.keyCode!=34 && event.keyCode!=35 && event.keyCode!=36 && event.keyCode!=37 && event.keyCode!=38 && event.keyCode!=39 && event.keyCode!=40)
			{
				return false;
			}
		}
	},
	
	'.liste_commentaire': function()
	{
		var bloc =  $$('.bloc_reglette');
		if(bloc.length==1 && !($('titre_reglette')))
		{
			bloc[0].innerHTML='<div id="titre_reglette" class="titre_reglette">Pages de commentaires :</div>' + bloc[0].innerHTML;
		}
	},
	
	
//	'.zone_commentaire' : function()
//	{
//		if(!isInitCom)
//		{
//			initCom();
//		}
//	},
	
	'.btn_com': function(bouton)
	{
		//Prechargement de l'image
//		Imagefond = new Image();
//		Imagefond.src = $('popup_commentaire').style.backgroundImage.replace("url(","").replace(")","");
//		alert(Imagefond.src);
		bouton.onclick = function()
		{
			new Effect.Appear($('popup_commentaire'), {queue:'end', duration:1});
		}
	},

	'.btn_fermer_popup': function(bouton)
	{
		bouton.onclick = function()
		{
			$('popup_commentaire').style.display="none";
		}
	},
	
	//Reglette resultat recherche
	'div.lien_reglette' : function(lr){
 		goReglette(lr);
 	},
	 	'div.lien_reglette_debut' : function(lr){
 		goReglette(lr);
 	},
 	
 	'div.lien_reglette_fin' : function(lr){
 		goReglette(lr);
 	},
 	
 	'div.lien_reglette_precedent' : function(lr){
 		goReglette(lr);
 	},
 	
 	'div.lien_reglette_suivant' : function(lr){
 		goReglette(lr);
 	},
};

// Enregistrement des regles definies ci dessus dans Behaviour
Behaviour.register(myrules);

