var pageActuelle = 0;
var win;

//function msgSendFriend(o){
//déplacé dans la jsp afin d'avoir le partId

function sendFriend(){
	var obj = document.getElementById("formEnvoyerAmi");
	if (validator.check(obj)) {
		YAHOO.util.Connect.setForm("formEnvoyerAmi");
		YAHOO.util.Connect.asyncRequest('POST', obj.action, 
	 			{success : msgSendFriend,
	 			failure : msgSendFriend});	
	}
}

function setRulesAmi(){
	validator.removeAll();
	validator.addRules("emailAmi","required mails","l'email de votre ami");
	validator.addRules("nom","required","votre nom");
}

function setRulesCommentaire(){
	validator.removeAll();
	validator.addRules("CommentaireNom","required","full:le nom ou pseudo est obligatoire");	
	validator.addRules("CommentaireHidden","required","full:le commentaire est obligatoire");  		
	validator.addRules("CommentaireEmail","mail","Votre email"); 
}

function centerApercu(){
		dlgApercu._dlg.center();
		setTimeout("setRulesAmi()",200);	
}	

function centerApercuCommentaire(){
	setTimeout("setRulesCommentaire()",500);	
}


function envoyerAmi(){
	document.documentElement.scrollTop = 0
	
	dlgApercu = new YAHOO.Sqli.Widgets.Window("envoyerAmi.do",
				"Envoyer à un ami",
				function(){centerApercu();},
				function(){sendFriend();},null,false);
				
	dlgApercu._dlg.cfg.queueProperty("fixedcenter",false);
	dlgApercu._dlg.cfg.queueProperty("width","650px");
	dlgApercu.show();		
}

function showPageSuivante() {
	showPage(pageActuelle+1);
}

function showPagePrecedente() {
	showPage(pageActuelle-1);
}

function showPage(pageId){
	$(".commentairePageSelected").addClass("commentairePageNonSelected");
	$(".commentairePageSelected").removeClass("commentairePageSelected");
	$("#commentaire_" + pageId).addClass("commentairePageSelected");
	
	pageActuelle = parseInt(pageId);
	$(".pageCommentaire").hide();
	
	if(pageId == 0)
		$("#btnCommentairePrev").hide();
	else
		$("#btnCommentairePrev").show();
	
	if(pageId == (nbPage-1))
		$("#btnCommentaireNext").hide();
	else
		$("#btnCommentaireNext").show();
	
	$("#page"+pageId).fadeIn("slow");
}

function envoieCommentaire(){
	var obj = document.getElementById("formAddCommentaire");

	$("#CommentaireNomHidden").val($("#CommentaireNom").val());
	$("#CommentaireHidden").val($("#editCommentaire").val());
	$("#CommentaireEmailHidden").val($("#CommentaireEmail").val());
	
	//alert(validator.check(obj));
	if(validator.check(obj))
	{
		YAHOO.util.Connect.setForm("formAddCommentaire");	
		YAHOO.util.Connect.asyncRequest('POST', "commentaireMagazine.do?method=addCommentaire", 
		 			{success : ajouterCommentaireFin,
		 			failure : function(){
							YAHOO.Sqli.Utils.Tools.error("Une erreur technique s'est produite","Erreur");
							win.close();
							}
		 			});	
	}
}

function ajouterCommentaireFin(o){
	if (o.responseText.indexOf("erreur")>=0){
		YAHOO.Sqli.Utils.Tools.error(o.responseText,"Erreur");
		win.close();
	}else{
		YAHOO.Sqli.Utils.Tools.error(o.responseText,"Succès");	
		win.close();
	}	
}


function annulerCommentaire(){
	YAHOO.Sqli.Utils.Tools.confirm("Êtes-vous sûr de vouloir annuler la création du commentaire ?",function (){win.close();});
}

function encodeURIComponent(truc){
	return escape(truc);
}

function ajouterCommentaire(id){
	var w = new YAHOO.Sqli.Widgets.Window("commentaireMagazine.do?method=initCommentaire&id=" + id,
		"Laissez votre avis sur <span style='color:orange;'>" + $("h1").html() + "</span>",
		function(){centerApercuCommentaire();}, //Open
		function(){envoieCommentaire();},
		function(){annulerCommentaire(); return false},
		false); //Ok
	win = w;
	
	w._dlg.cfg.queueProperty("width","480px");
	w._dlg.cfg.queueProperty("height","300px");
	w.show();
}
