/**
 * @author Intermediatic 
 * 
 */
Event.observe(window,"load",init);
function init()
{
	//Module connexion controller index 
	
		if ($("afform")) {
			Event.observe($("afform"),"click",function(e){
				$("formconnect").style.display="block";
			})
		}
	
		if ($("connect")){
			Event.observe($("connect"),"submit",connexion);
		}
		
		if ($("connect2")){
			Event.observe($("connect2"),"submit",connexion2);
		}
		
	
	//ComminView - Menu
		$$(".mymenu").each(function(elt){
			Event.observe(elt,"mouseover",function(e){
				elt.className="mymenuselect";
			})
			Event.observe(elt,"mouseout",function(e){
				elt.className="mymenu";
			})
		})
		
		
	
	//Module Articles
		if ($("myarticlebt")){
			Event.observe($("myarticlebt"),"click",rechercheNews);
		}
		
		lienDetail=$$(".mynewsdetail");
		lienDetail.each(function(elt){
			Event.observe(elt,"click",afficheDetail)
		})
	
	//Module connexion controller profile
		if ($("myprofilform")) {
			Event.observe($("myprofilform"),"submit",function(e){
				Event.stop(e);
				new Ajax.Request($("myprofilform").action,{encoding:"iso-8859-15",parameters:$("myprofilform").serialize(),onSuccess:function(transport){
					alert(transport.responseText)
				}})
			})
		}
	
	//Module connexion controller admin
		if ($("myadminajoutform")) {
			Event.observe($("myadminajoutform"),"submit",function(e){
				Event.stop(e);
				new Ajax.Request($("myadminajoutform").action,{encoding:"iso-8859-15",parameters:$("myadminajoutform").serialize(),onSuccess:function(transport){
					var arrRes=transport.responseText.split("|");
					if (arrRes[0]=="ok"){
						var href=$("myadminajoutform").action.replace("ajout","detail/LOGIN/" + arrRes[1] + "/PASS/" + arrRes[2]);
						new Effect.Opacity("framework", {
						duration: 0.2,
						from: 1,
						to: 0,
						afterFinish:function(){
							new Ajax.Updater("framework", href, {
							encoding: "iso-8859-15",
							onComplete: function(){
								new Effect.Opacity("framework", {duration: 0.2,from: 0,to: 1});
								Event.observe($("myadminform"),"submit",function(e){
									Event.stop(e);
									new Ajax.Request($("myadminform").action,{encoding:"iso-8859-15",parameters:$("myadminform").serialize(),onSuccess:function(transport){
										alert(transport.responseText)
									}})
								})
							}
						})
						}})
						
					}else{
						alert(transport.responseText)
					}
				}})
			})
		}
	
		var myAdminDetail=$$(".myadmindetail");
		myAdminDetail.each(function(elt){
			Event.observe(elt,"click",adminDetail);
		})
	
		//User dans admin liste déroulante
		var users=$$(".myuser")
		users.each(function(elt){
			Event.observe(elt,"change",function(e){
				var form=elt.up("form");
				new Ajax.Request(form.action,{encoding:"iso-8859-15",parameters:form.serialize(),onSuccess:function(transport){
					new Ajax.Updater("framework","liste",{encoding:"iso-8859-15",onComplete:function(){
						init();
					}});
					alert(transport.responseText)
					
				}})
			})
		})
	
		//Gestionnaire dans admin liste déroulante
		var gestd=$$(".mygest")
		gestd.each(function(elt){
			Event.observe(elt,"change",function(e){
				var form=elt.up("form");
				new Ajax.Request(form.action,{encoding:"iso-8859-15",parameters:form.serialize(),onSuccess:function(transport){
					alert(transport.responseText)
				}})
			})
		})
	
		//Suppression dans admin
		var mySuppProfil=$$(".myadminsupp")
		mySuppProfil.each(function(elt){
			Event.observe(elt,"click",suppProfil)
		});
	
	//Module dataex 
		var mydown=$$(".mydataexdown")
		mydown.each(function(elt){
			Event.observe(elt,"click",function(){
				window.open("dataex/index/download/id/" + elt.id);
				document.location.href="dataex";
			})
		})
	
		//Partie suppression et refresh liste dataex
		var mydataexsupp=$$(".mydataexsupp")
		mydataexsupp.each(function (elt){
			Event.observe(elt,"click",function(e){
				Event.stop(e);
				if (confirm("Voulez-vous supprimer définitivement ce fichier")){
					new Ajax.Request(elt.href,{encoding:"iso-8859-15",onSuccess:function(transport){
					new Ajax.Updater("mydataexliste","dataex/index/liste",{encoding:"iso-8859-15",onComplete:function(){
						init(); 
					}})
					}})
				}
			})
		})
	
		//Partie desc dataex via click
		var mydataexslibhref=$$(".mydataexslibhref")
		mydataexslibhref.each(function(elt){
			Event.observe(elt,"click",function(e){
				Event.stop(e);
				$("slib" + elt.id).style.display="block"; 
			})
		})
} 

/**Module connexion controller index - Connexion formulaire 
 * 
 */
function connexion(e)
{
	Event.stop(e);
	new Ajax.Request($("connect").action,{encoding:"iso-8859-15",parameters:{log:$F("clogin"),pass:$F("cpassword")},onSuccess:function(transport){
		if (transport.responseText=="connect"){
			document.location.href=$F("uri");
		}else{
			alert(transport.responseText)
		}
	}})
}

function connexion2(e)
{
	Event.stop(e);
	new Ajax.Request($("connect2").action,{encoding:"iso-8859-15",parameters:{log:$F("clogin2"),pass:$F("cpassword2")},onSuccess:function(transport){
		if (transport.responseText=="connect"){
			document.location.href=$F("uri");
		}else{
			alert(transport.responseText)
		}
	}})
}

/**Module article - Moteur de recherche de news
 * 
 */
function rechercheNews()
{
	new Ajax.Updater("mynewsliste","articles/index/liste",{encoding:"iso-8859-15",parameters:{recherche:$F("newsrecherche")},
	onComplete: function(){
		lienDetail = $$(".mynewsdetail");
		lienDetail.each(function(elt){
			Event.observe(elt, "click", afficheDetail)
		})
	}
	})
}

/**Module article - Affiche détail d'une news
 * 
 * @param {Object} e
 */
function afficheDetail(e)
{
	Event.stop(e);
	elt=Event.findElement(e,"a");
	new Effect.Opacity("mynewsliste", {
	duration: 0.2,
	from: 1,
	to: 0,
	afterFinish:function(){
		new Ajax.Updater("mynewsliste", elt.href, {
		encoding: "iso-8859-15",
		onComplete: function(){
			new Effect.Opacity("mynewsliste", {duration: 0.2,from: 0,to: 1});
			Event.observe($("mynewsretour"),"click",function(e){
				Event.stop(e);
				rechercheNews()
			})
		}
	})
	}})
}

/** Module connexion controller admin - Affiche détail d'un user dans admin
 * 
 */
function adminDetail(e)
{
	Event.stop(e);
	elt=Event.findElement(e,"a");
	new Effect.Opacity("framework", {
	duration: 0.2,
	from: 1,
	to: 0,
	afterFinish:function(){
		new Ajax.Updater("framework", elt.href, {
		encoding: "iso-8859-15",
		onComplete: function(){
			new Effect.Opacity("framework", {duration: 0.2,from: 0,to: 1});
			Event.observe($("myadminform"),"submit",function(e){
				Event.stop(e);
				new Ajax.Request($("myadminform").action,{encoding:"iso-8859-15",parameters:$("myadminform").serialize(),onSuccess:function(transport){
					alert(transport.responseText)
				}})
			})
		}
	})
	}})
}

/**Module connexion controller admin - Suppression de profile
 * 
 * 
 */
function suppProfil(e)
{
	Event.stop(e);
	var elt=Event.findElement(e,"a");
	if (confirm("Voulez-vous supprimer ce profil")) {
		new Ajax.Request(elt.href,{encoding:"iso-8859-15",onSuccess:function(transport){
			if (transport.responseText=="ok"){
				new Ajax.Updater("framework","liste",{encoding:"iso-8859-15",onComplete:function(){
					init();
				}});
			}else{
				alert(transport.responseText);
			}
		}})
	}
}



function myPassDem(){
	new Ajax.Request("http://my.acfgroup.net/connexion/index/mypassdem",{parameters:{mail:$F("passmaildem")},onSuccess:function(transport){
		var response=transport.responseText.strip();
		var arrResponse=response.split("|");
		if(arrResponse.length==2){
			alert(arrResponse[0]);
			document.location.href=arrResponse[1];
		}else{
			alert(response);
		}
		
	}});
}
