
var personnal_box = true;
var ps_userid = "";
var ps_avatar = "";
var ps_username = "";
var ps_authCallback = null;
var ps_salle_uid = 0;
var ps_fiche_id = 0;
var ps_flash_id = "";
var ps_video_id = 0;

Shadowbox.loadSkin('meddle', '/images/skins/cac6/shadowbox');

function ps_checkAuth()
{

	$.post("xmlserver.php",{auth:"1"},ps_checkAuthResult,"xml");
}

function ps_disconnectClicked()
{
	ps_authCallback = null;
	ps_disconnect();
}

function ps_disconnect()
{
	$.post("xmlserver.php",{auth:"1",logout:"1"},ps_checkAuthResult,"xml");
}

function ps_checkAuthRequired(callback)
{
	ps_authCallback= callback;

	$.post("xmlserver.php",{auth:"1"},ps_checkAuthRequiredResult,"xml");
}

function ps_checkAuthRequiredResult(data)
{
	if (personnal_box)
	{
		$("#ps_buttons").empty();
		$("#ps_image").empty();

		if ($("auth",data).text()=="1")
		{
			if ($("userid",data))
			{
				 ps_userid = $("userid",data).text();
				 ps_avatar = $("avatar",data).text();
				 ps_username = $("username",data).text();
			}

			if ($("avatar",data).text()!="")
			{
				$("#ps_image").append('<img src="http://forum.commeaucinema.com/forum/img/avatars/' + $("avatar",data).text() + '">');

			}

			$("#ps_buttons").append('<div id="ps_favoris">Favoris/Alertes</div>');

			if ($("usergroup",data).text()!="32000")
			{
				$("#ps_buttons").append('<div id="ps_account">Mon Compte</div>');
			}
			$("#ps_buttons").append('<div id="ps_disconnect">Se déconnecter</div>');

			$("#ps_favoris").click(ps_favorisClicked);

			$("#ps_account").click(ps_accountClicked);
			$("#ps_disconnect").click(ps_disconnectClicked);

			if (ps_authCallback)
			{
				ps_authCallback();
				ps_authCallback = null;
			}

		}else
		{

			ps_userid="";
			ps_avatar = "";
			ps_username = "";

			$("#ps_buttons").append('<div id="ps_login">S\'identifier</div>');
			$("#ps_buttons").append('<div id="ps_register">S\'inscrire</div>');

			$("#ps_login").click(ps_loginClicked);

			$("#ps_register").click(ps_registerClicked);

			ps_register();


		}

		ps_position();
	}else
	{
		if ($("auth",data).text()=="1")
		{
			if ($("userid",data))
			{
				ps_userid = $("userid",data).text();
				ps_avatar = $("avatar",data).text();
				ps_username = $("username",data).text();
			}
			if (ps_authCallback)
			{
				ps_authCallback();
				ps_authCallback = null;
			}

		}else
		{
			ps_userid="";
			ps_avatar = "";
			ps_username = "";

			ps_register();
		}
	}
	CommentForm();
}

function ps_checkAuthResult(data)
{
	if (personnal_box)
	{
		$("#ps_buttons").empty();
		$("#ps_image").empty();

		if ($("auth",data).text()=="1")
		{
			if ($("userid",data))
			{
				 ps_userid = $("userid",data).text();
				 ps_avatar = $("avatar",data).text();
				 ps_username = $("username",data).text();
			}

			if ($("avatar",data).text()!="")
			{
				$("#ps_image").append('<img src="http://forum.commeaucinema.com/forum/img/avatars/' + $("avatar",data).text() + '">');

			}

			$("#ps_buttons").append('<div id="ps_favoris">Favoris/Alertes</div>');

			if ($("usergroup",data).text()!="32000")
			{
				$("#ps_buttons").append('<div id="ps_account">Mon Compte</div>');
			}
			$("#ps_buttons").append('<div id="ps_disconnect">Se déconnecter</div>');

			$("#ps_favoris").click(ps_favorisClicked);

			$("#ps_account").click(ps_accountClicked);
			$("#ps_disconnect").click(ps_disconnectClicked);

			if (ps_authCallback)
			{
				ps_authCallback();
				ps_authCallback = null;
			}

		}else
		{

			ps_userid="";
			ps_avatar = "";
			ps_username = "";

			$("#ps_buttons").append('<div id="ps_login">S\'identifier</div>');
			$("#ps_buttons").append('<div id="ps_register">S\'inscrire</div>');

			$("#ps_login").click(ps_loginClicked);

			$("#ps_register").click(ps_registerClicked);


		}

		ps_position();
	}else
	{
		if ($("auth",data).text()=="1")
		{
			if ($("userid",data))
			{
				 ps_userid = $("userid",data).text();
				 ps_avatar = $("avatar",data).text();
				 ps_username = $("username",data).text();
			}

			if (ps_authCallback)
			{
				ps_authCallback();
				ps_authCallback = null;
			}

		}else
		{
			ps_userid="";
			ps_avatar = "";
			ps_username = "";
		}
	}

	CommentForm();
}

//function ps_favorisSeances(id)
//{
//	$.post("xmlserver.php",{auth:"1",service:"getSallesFavorisForFilmCPUID",id:id},ps_favorisSeancesResult,"xml");
//}

function ps_favorisSeancesResult(data)
{

	if ($("salle",data).length>0)
	{
		$("#favorissalles").append("Dont dans " + $("salle",data).length + " de vos cinémas : <span>");
		for (var i=0;i<$("salle",data).length;i++)
		{
			var item = $("salle",data)[i];

			if (i>0) $("#favorissalles").append(", ");

			$("#favorissalles").append("<a href=\"salle="+ $("salle_uid",item).text() +".html\">"+ $("nom",item).text() +"</a>");
		}
		$("#favorissalles").append("</span>");
	}
}

/*
 * gestion Favoris
 *
 */

function ps_addFavoris()
{
	ps_checkAuthRequired(ps__addFavoris());
}

function ps__addFavoris()
{
	$.post("xmlserver.php",{auth:"1",service:"addFavoris",fiche_id:ps_fiche_id},ps_addFavorisResult,"xml");
}

function ps_addFavorisResult(data)
{
	ps_isFavoris(0);
}

function ps_removeFavoris()
{
	$.post("xmlserver.php",{auth:"1",service:"removeFavoris",fiche_id:ps_fiche_id},ps_removeFavorisResult,"xml");
}

function ps_removeFavorisResult(data)
{
	ps_isFavoris(0);
}

function ps_isFavoris(id)
{
	if (id>0) ps_fiche_id = id;

	$.post("xmlserver.php",{auth:"1",service:"isFavoris",fiche_id:ps_fiche_id},ps_isFavorisResult,"xml");

}


function ps_isFavorisResult(data)
{
	$('#fichefavoris').empty();
	if ($("code",data).text()=="OK")
	{
		$('#fichefavoris').append("<img src=\"/images/skins/cac6/removefavoris.png\" border=\"0\"><span>&nbsp;Retirer des favoris</span>");
		$('#fichefavoris').click(ps_removeFavoris);

	}else
	{

		$('#fichefavoris').append("<img src=\"/images/skins/cac6/addfavoris.png\" border=\"0\"><span>&nbsp;Ajouter aux favoris</span>");
		$('#fichefavoris').click(ps_addFavoris);

	}
}

/*
 * gestion Alertes
 *
 */

function ps_addAlerte()
{
	ps_checkAuthRequired(ps__addAlerte());
}

function ps__addAlerte()
{
	$.post("xmlserver.php",{auth:"1",service:"addAlerte",fiche_id:ps_fiche_id},ps_addAlerteResult,"xml");
}

function ps_addAlerteResult(data)
{
	ps_isAlerte(0);
}

function ps_removeAlerte()
{
	$.post("xmlserver.php",{auth:"1",service:"removeAlerte",fiche_id:ps_fiche_id},ps_removeAlerteResult,"xml");
}

function ps_removeAlerteResult(data)
{
	ps_isAlerte(0);
}

function ps_isAlerte(id)
{
	if (id>0) ps_fiche_id = id;

	$.post("xmlserver.php",{auth:"1",service:"isAlerte",fiche_id:ps_fiche_id},ps_isAlerteResult,"xml");

}


function ps_isAlerteResult(data)
{
	$('#fichealerte').empty();
	if ($("code",data).text()=="OK")
	{
		$('#fichealerte').append("<img src=\"/images/skins/cac6/removealerte.png\" border=\"0\"><span>&nbsp;Retirer des alertes</span>");
		$('#fichealerte').click(ps_removeAlerte);

	}else
	{

		$('#fichealerte').append("<img src=\"/images/skins/cac6/addalerte.png\" border=\"0\"><span>&nbsp;Alertez-moi</span>");
		$('#fichealerte').click(ps_addAlerte);

	}
}

/*
 * gestion Favoris salles
 */
function ps_addFavorisSalle()
{
	ps_checkAuthRequired(ps__addFavorisSalle());
}

function ps__addFavorisSalle()
{
	$.post("xmlserver.php",{auth:"1",service:"addFavorisSalle",salle_uid:ps_salle_uid},ps_addFavorisSalleResult,"xml");
}

function ps_addFavorisSalleResult(data)
{
	ps_isSallesFavoris(0);
}

function ps_removeFavorisSalle()
{
	$.post("xmlserver.php",{auth:"1",service:"removeFavorisSalle",salle_uid:ps_salle_uid},ps_removeFavorisSalleResult,"xml");
}

function ps_removeFavorisSalleResult(data)
{
	ps_isSallesFavoris(0);
}

function ps_isSallesFavoris(id)
{
	if (id>0) ps_salle_uid = id;

	$.post("xmlserver.php",{auth:"1",service:"isSallesFavoris",salle_uid:ps_salle_uid},ps_isSallesFavorisResult,"xml");

}


function ps_isSallesFavorisResult(data)
{
	$('#sallefavoris').empty();
	if ($("code",data).text()=="OK")
	{
		$('#sallefavoris').append("<img src=\"/images/skins/cac6/removefavoris.png\" border=\"0\"><span>&nbsp;Retirer des favoris</span>");
		$('#sallefavoris').click(ps_removeFavorisSalle);

	}else
	{
		$('#sallefavoris').append("<img src=\"/images/skins/cac6/addfavoris.png\" border=\"0\"><span>&nbsp;Ajouter aux favoris</span>");
		$('#sallefavoris').click(ps_addFavorisSalle);
	}
}

function ps_favorisClicked()
{
	ps_authCallback = null;
	ps_favoris();
}

function ps_alertesClicked()
{
	ps_authCallback = null;
	ps_alertes();
}

function ps_accountClicked()
{
	ps_authCallback = null;
	ps_account();
}

function ps_loginClicked()
{
	ps_authCallback = null;
	ps_login();
}

function ps_registerClicked()
{
	ps_authCallback = null;
	ps_register();
}

function ps_favoris()
{
	Shadowbox.open({
	title:"Mes Favoris",
	player:"swf",
	content:"ps_favoris.swf",
	width:840,
	height:460

	});
}

function ps_account()
{
	//document.location.href = "http://forum.commeaucinema.com/forum/account_profile.php?id=" + ps_userid;

	Shadowbox.open({
	title:"Mon Compte",
	player:"swf",
	content:"ps_profil.swf",
	width:750,
	height:460

	});
}

function ps_login()
{

	ps_register();

}

function ps_register()
{

	Shadowbox.open({
	title:"Espace personnel",
	player:"swf",
	content:"ps_register.swf",
	width:750,
	height:460

	});

}

function ps_alertes()
{
	Shadowbox.open({
	title:"Mes Alertes",
	player:"swf",
	content:"ps_alertes.swf",
	width:840,
	height:460

	});
}

function ps_position()
{
	if (personnal_box)
	{
		$("#personnal_space").css("top",($(window).scrollTop() + $(window).height()) - ($("#personnal_space").height()) +"px");
	}
}

function setStars(n)
{
	for (i=1;i<=4;i++)
	{
		$("#star"+i).css('cursor','pointer').css('width','20px').css('height','16px');
		if (i<=n)
		{
			$("#star"+i).css('background','url("/images/skins/cac6/star_on.png") top left no-repeat');
		}else
		{
			$("#star"+i).css('background','url("/images/skins/cac6/star_off.png") top left no-repeat');
		}
	}

	$("#votestars").val(n);

}

function CommentForm()
{
	if ($("#fichecomments_form") && $("#fichecomments_form").length>0)
	{
		ficheCommentForm();
	}else if ($("#actucomments_form") && $("#actucomments_form").length>0)
	{
		actuCommentForm();
	}else if ($("#videocomments_form") && $("#videocomments_form").length>0)
	{
		videoCommentForm();
	}
}

function ficheCommentForm()
{

	if ($("#fichecomments_form") && $("#fichecomments_form").length>0)
	{
		var comment_note = 0;
		var comment_commentaire_plus = "";
		var comment_commentaire_moins = "";
		var comment_commentaire_reco = "";

		if ($("#votestars").length>0)
		{
			comment_note = $("#votestars").val();

		}

		if ($("#commentaire_plus").length>0)
		{
			comment_commentaire_plus = $("#commentaire_plus").val();
		}
		if ($("#commentaire_moins").length>0)
		{
			comment_commentaire_moins = $("#commentaire_moins").val();
		}
		if ($("#commentaire_reco").length>0)
		{
			comment_commentaire_reco = $("#commentaire_reco").val();
		}

		$("#fichecomments_form").empty();

		var image="/images/skins/cac6/profil_photodefault.png";
		var username="";


		if (ps_userid)
		{
			if (ps_avatar)
			{
				image="http://forum.commeaucinema.com/forum/img/avatars/" + ps_avatar;
			}
			if (ps_username)
			{
				username = ps_username;
			}
		}

		var block = '<div class="bubbleline"><div class="bubblepicto"><div><img src="'+ image +'" /><div class="pictotitle"><span class="username">' + username + '</span></div></div></div><div class="bubblebox"><h2><span style="float:left">Mon avis :&nbsp;&nbsp;</span><div id="star1" style="float:left" onMouseOver="setStars(1)"></div><div id="star2" style="float:left" onMouseOver="setStars(2)"></div><div id="star3" style="float:left" onMouseOver="setStars(3)"></div><div id="star4" style="float:left" onMouseOver="setStars(4)"></div></h2><div style="clear:both"></div><div class="body">Ce que vous avez aimé :<br /><textarea style="width:100%;height:50px" id="commentaire_plus" name="commentaire_plus"></textarea><br />Ce qui ne vous a pas plu :<br /><textarea style="width:100%;height:50px" id="commentaire_moins" name="commentaire_moins"></textarea><br />Vous recommandez à...<br /><textarea style="width:100%;height:50px" id="commentaire_reco" name="commentaire_reco"></textarea><input id="votestars" type="hidden" name="votestars"><div style="text-align:right"><input id="comment_form_submit" type="button" value="valider"></div></div><div class="footer"></div></div></div>';
		$("#fichecomments_form").append(block);
		setStars(comment_note);
		$("#commentaire_plus").val(comment_commentaire_plus);
		$("#commentaire_moins").val(comment_commentaire_moins);
		$("#commentaire_reco").val(comment_commentaire_reco);

		$("#comment_form_submit").click(function(){

				if (ps_userid)
				{
					if ($("#votestars").val()<1)
					{
						alert("veuillez choisir une note");
					}else if ($("#commentaire_plus").val().length<1 || $("#commentaire_plus").val().length>255)
					{
						alert("Votre commentaire doit comporter au moins un et au plus 255 caracteres");
					}else if ($("#commentaire_moins").val().length<1 || $("#commentaire_moins").val().length>255)
					{
						alert("Votre commentaire doit comporter au moins un et au plus 255 caracteres");
					}else if ($("#commentaire_reco").val().length<1 || $("#commentaire_reco").val().length>255)
					{
						alert("Votre commentaire doit comporter au moins un et au plus 255 caracteres");
					}else
					{
						$.post("xmlserver.php",{auth:"1",postfichecomment:"1",fichecomment_ficheid:ps_fiche_id,fichecomment_note:$("#votestars").val(),fichecomment_comment_plus:$("#commentaire_plus").val(),fichecomment_comment_moins:$("#commentaire_moins").val(),fichecomment_comment_reco:$("#commentaire_reco").val()},postFicheCommentResult,"xml");
					}
				}else
				{
					ps_registerClicked();
				}

		});
	}
}

function postFicheCommentResult(data)
{
	if ($("auth_error",data) && $("auth_error",data).text().length>0)
	{
		alert($("auth_error",data).text());
	}else
	{
		setStars(0);
		$("#commentaire_plus").val("");
		$("#commentaire_moins").val("");
		$("#commentaire_reco").val("");

		getFicheComments(ps_fiche_id,0);
	}
}

function getFicheComments(id,n)
{
	ps_fiche_id = id;

	$.post("forumxmlserver.php",{service:"getFicheComments",newsid:id,p:n},ficheCommentsResult,"xml");
}

function ficheCommentsResult(data)
{
	$("#comments_list").empty();


	if ($("comment",data) && $("comment",data).length>0)
	{
		$("#comments_list").append("<li><h3>Autres Avis :</h3></li>");
		$("comment",data).each(function(i)
			{
				var item = $("comment",data)[i];
				var id = $($("id",item)[1]).text();
				var text = "<b>Ce que j'ai aimé :</b> " + $("text_plus",item).text() + "<br /><b>Ce qui ne m'a pas plu :</b> " + $("text_moins",item).text() + "<br /><b>Je le recommande à :</b> " + $("text_reco",item).text() ;
				var image="/images/skins/cac6/profil_photodefault.png";

				if ($("image",$("user",item)).text())
				{
					image="http://forum.commeaucinema.com/forum/img/avatars/" + $("image",$("user",item)).text();
				}

				var username = $("name",$("user",item)).text();
				var date = $("date",item).text();;

				var note =$("note",item).text();

				var block = '<li class="bubbleline"><div class="bubblepicto"><div><img src="'+ image +'" /><div class="pictotitle"><span class="username">' + username + '</span><br />'+ date +'</div></div></div><div class="bubblebox"><h2>Mon avis : <img src="/images/skins/cac6/'+ note +'etoiles_presse_trans.png"></h2><div class="body">'+ text +'</div><div class="footer"><div id="signal_'+id+'" style="float:right; width:75px; cursor:pointer;text-align:right;font-size:9px;padding-top:4px">signaler un abus</div></div></div></li>';



				$("#comments_list").append(block);

				$("#signal_"+id).click(function(event){

					$.post("forumxmlserver.php",{service:"signalFicheComment",id:parseInt(event.target.id.substring(7))},signalCommentsResult,"xml");

				})
			}
		)



		$("#comments_list").append("<li><div style=\"clear:both\"></div><div class=\"NavPages\"><ul id=\"comments_pages\"></ul></div></li>");

		if (parseInt($("page",data).text())>0)
		{
			$("#comments_pages").append("<li class=\"NavPages_prev\" style=\"cursor:pointer\" id=\"comments_prev_page\"><img src=\"http://www.commeaucinema.com/images/skins/cac6/flecheg_barrenav.png\" alt=\"Page précédente\" /></li>");
			$("#comments_prev_page").click(function()
				{
					getFicheComments(ps_fiche_id,parseInt($("page",data).text())-1);
				}
			);
		}

		if (Math.floor(parseInt($("count",data).text())/5)>0)
		{
			for (i=0;i<=Math.floor(parseInt($("count",data).text())/5);i++)
			{
				if (i==parseInt($("page",data).text()))
				{
					$("#comments_pages").append("<li class=\"NavPages_current\" id=\"comments_page_"+(i+1)+"\">"+(i+1)+"</li>");
				}else
				{
					$("#comments_pages").append("<li class=\"NavPages_item\" style=\"cursor:pointer\" id=\"comments_page_"+(i+1)+"\">"+(i+1)+"</li>");
				}

				$("#comments_page_"+(i+1)).click(function(event)
					{

						getFicheComments(ps_fiche_id,parseInt(event.target.id.substring(14))-1);
					}
				);
			}
		}

		if (parseInt($("count",data).text()) - ((parseInt($("page",data).text())+1) * 5)>0)
		{
			$("#comments_pages").append("<li class=\"NavPages_next\" style=\"cursor:pointer\" id=\"comments_next_page\"><img src=\"http://www.commeaucinema.com/images/skins/cac6/flechedr_barrenav.png\" alt=\"Page suivante\" /></li>");
			$("#comments_next_page").click(function()
				{
					getFicheComments(ps_fiche_id,parseInt($("page",data).text())+1);
				}
			);
		}
	}
}

function actuCommentForm()
{

	if ($("#actucomments_form") && $("#actucomments_form").length>0)
	{
		var comment_commentaire = "";


		if ($("#commentaire").length>0)
		{
			comment_commentaire = $("#commentaire").val();
		}

		$("#actucomments_form").empty();

		var image="/images/skins/cac6/profil_photodefault.png";
		var username="";


		if (ps_userid)
		{
			if (ps_avatar)
			{
				image="http://forum.commeaucinema.com/forum/img/avatars/" + ps_avatar;
			}
			if (ps_username)
			{
				username = ps_username;
			}
		}

		var block = '<div class="bubbleline"><div class="bubblepicto"><div><img src="'+ image +'" /><div class="pictotitle"><span class="username">' + username + '</span></div></div></div><div class="bubblebox"><h2><span>&nbsp;&nbsp;</span></h2><div class="body"><textarea style="width:100%;height:50px" id="commentaire" name="commentaire"></textarea><div style="text-align:right"><input id="comment_form_submit" type="button" value="valider"></div></div><div class="footer"></div></div></div>';
		$("#actucomments_form").append(block);

		 $("#commentaire").val(comment_commentaire);

		$("#comment_form_submit").click(function(){

				if (ps_userid)
				{
					if ($("#commentaire").val().length<1 || $("#commentaire").val().length>255)
					{
						alert("Votre commentaire doit comporter au moins un et au plus 255 caracteres");
					}else
					{
						$.post("xmlserver.php",{auth:"1",postactucomment:"1",actucomment_ficheid:ps_fiche_id,actucomment_comment:$("#commentaire").val()},postActuCommentResult,"xml");
					}
				}else
				{
					ps_registerClicked();
				}

		});
	}
}

function postActuCommentResult(data)
{
	if ($("auth_error",data) && $("auth_error",data).text().length>0)
	{
		alert($("auth_error",data).text());
	}else
	{
		$("#commentaire").val("");

		getActuComments(ps_fiche_id,0);
	}
}

function getActuComments(id,n)
{
	ps_fiche_id = id;

	$.post("forumxmlserver.php",{service:"getActuComments",newsid:id,p:n},actuCommentsResult,"xml");
}

function actuCommentsResult(data)
{
	$("#comments_list").empty();


	if ($("comment",data) && $("comment",data).length>0)
	{
		$("#comments_list").append("<li><h3>Autres Avis :</h3></li>");
		$("comment",data).each(function(i)
			{
				var item = $("comment",data)[i];
				var id = $($("id",item)[1]).text();
				var text = $("text",item).text();
				var image="/images/skins/cac6/profil_photodefault.png";

				if ($("image",$("user",item)).text())
				{
					image="http://forum.commeaucinema.com/forum/img/avatars/" + $("image",$("user",item)).text();
				}

				var username = $("name",$("user",item)).text();
				var date = $("date",item).text();;


				var block = '<li class="bubbleline"><div class="bubblepicto"><div><img src="'+ image +'" /><div class="pictotitle"><span class="username">' + username + '</span><br />'+ date +'</div></div></div><div class="bubblebox"><h2>&nbsp;</h2><div class="body">'+ text +'</div><div class="footer"><div id="signal_'+id+'" style="float:right; width:75px; cursor:pointer;text-align:right;font-size:9px;padding-top:4px">signaler un abus</div></div></div></li>';



				$("#comments_list").append(block);

				$("#signal_"+id).click(function(event){

					$.post("forumxmlserver.php",{service:"signalActuComment",id:parseInt(event.target.id.substring(7))},signalCommentsResult,"xml");

				})
			}
		)



		$("#comments_list").append("<li><div style=\"clear:both\"></div><div class=\"NavPages\"><ul id=\"comments_pages\"></ul></div></li>");

		if (parseInt($("page",data).text())>0)
		{
			$("#comments_pages").append("<li class=\"NavPages_prev\" style=\"cursor:pointer\" id=\"comments_prev_page\"><img src=\"http://www.commeaucinema.com/images/skins/cac6/flecheg_barrenav.png\" alt=\"Page précédente\" /></li>");
			$("#comments_prev_page").click(function()
				{
					getActuComments(ps_fiche_id,parseInt($("page",data).text())-1);
				}
			);
		}

		if (Math.floor(parseInt($("count",data).text())/10)>0)
		{
			for (i=0;i<=Math.floor(parseInt($("count",data).text())/10);i++)
			{
				if (i==parseInt($("page",data).text()))
				{
					$("#comments_pages").append("<li class=\"NavPages_current\" id=\"comments_page_"+(i+1)+"\">"+(i+1)+"</li>");
				}else
				{
					$("#comments_pages").append("<li class=\"NavPages_item\" style=\"cursor:pointer\" id=\"comments_page_"+(i+1)+"\">"+(i+1)+"</li>");
				}

				$("#comments_page_"+(i+1)).click(function(event)
					{

						getActuComments(ps_fiche_id,parseInt(event.target.id.substring(14))-1);
					}
				);
			}
		}

		if (parseInt($("count",data).text()) - ((parseInt($("page",data).text())+1) * 10)>0)
		{
			$("#comments_pages").append("<li class=\"NavPages_next\" style=\"cursor:pointer\" id=\"comments_next_page\"><img src=\"http://www.commeaucinema.com/images/skins/cac6/flechedr_barrenav.png\" alt=\"Page suivante\" /></li>");
			$("#comments_next_page").click(function()
				{
					getActuComments(ps_fiche_id,parseInt($("page",data).text())+1);
				}
			);
		}
	}
}


function videoCommentForm()
{

	if ($("#videocomments_form") && $("#videocomments_form").length>0)
	{
		var comment_note = 0;
		var comment_commentaire = "";

		if ($("#votestars").length>0)
		{
			comment_note = $("#votestars").val();

		}

		if ($("#commentaire").length>0)
		{
			comment_commentaire = $("#commentaire").val();
		}

		$("#videocomments_form").empty();

		var image="/images/skins/cac6/profil_photodefault.png";
		var username="";


		if (ps_userid)
		{
			if (ps_avatar)
			{
				image="http://forum.commeaucinema.com/forum/img/avatars/" + ps_avatar;
			}
			if (ps_username)
			{
				username = ps_username;
			}
		}

		var block = '<div class="bubbleline"><div class="bubblepicto"><div><img src="'+ image +'" /><div class="pictotitle"><span class="username">' + username + '</span></div></div></div><div class="bubblebox"><h2><span style="float:left">Mon avis :&nbsp;&nbsp;</span><div id="star1" style="float:left" onMouseOver="setStars(1)"></div><div id="star2" style="float:left" onMouseOver="setStars(2)"></div><div id="star3" style="float:left" onMouseOver="setStars(3)"></div><div id="star4" style="float:left" onMouseOver="setStars(4)"></div></h2><div style="clear:both"></div><div class="body"><textarea style="width:100%;height:50px" id="commentaire" name="commentaire"></textarea><input id="votestars" type="hidden" name="votestars"><div style="text-align:right"><input id="comment_form_submit" type="button" value="valider"></div></div><div class="footer"></div></div></div>';
		$("#videocomments_form").append(block);
		setStars(comment_note);
		 $("#commentaire").val(comment_commentaire);

		$("#comment_form_submit").click(function(){

				if (ps_userid)
				{
					if ($("#votestars").val()<1)
					{
						alert("veuillez choisir une note");
					}else if ($("#commentaire").val().length<1 || $("#commentaire").val().length>255)
					{
						alert("Votre commentaire doit comporter au moins un et au plus 255 caracteres");
					}else
					{
						$.post("xmlserver.php",{auth:"1",postvideocomment:"1",videocomment_ficheid:ps_fiche_id,videocomment_flashid:ps_flash_id,videocomment_videoid:ps_video_id,videocomment_note:$("#votestars").val(),videocomment_comment:$("#commentaire").val()},postVideoCommentResult,"xml");
					}
				}else
				{
					ps_registerClicked();
				}

		});
	}
}

function postVideoCommentResult(data)
{
	if ($("auth_error",data) && $("auth_error",data).text().length>0)
	{
		alert($("auth_error",data).text());
	}else
	{
		setStars(0);
		$("#commentaire").val("");

		getVideoComments(ps_fiche_id,ps_flash_id,ps_video_id,0);
	}
}

function getVideoComments(id,flash,video,n)
{
	ps_fiche_id = id;
	ps_flash_id = flash;
	ps_video_id = video;

	$.post("forumxmlserver.php",{service:"getVideoComments",newsid:id,flashid:flash,videoid:video,p:n},videoCommentsResult,"xml");
}

function videoCommentsResult(data)
{
	$("#comments_list").empty();


	if ($("comment",data) && $("comment",data).length>0)
	{
		$("#comments_list").append("<li><h3>Autres Avis :</h3></li>");
		$("comment",data).each(function(i)
			{
				var item = $("comment",data)[i];
				var id = $($("id",item)[1]).text();
				var text = $("text",item).text();
				var image="/images/skins/cac6/profil_photodefault.png";

				if ($("image",$("user",item)).text())
				{
					image="http://forum.commeaucinema.com/forum/img/avatars/" + $("image",$("user",item)).text();
				}

				var username = $("name",$("user",item)).text();
				var date = $("date",item).text();;

				var note =$("note",item).text();

				var block = '<li class="bubbleline"><div class="bubblepicto"><div><img src="'+ image +'" /><div class="pictotitle"><span class="username">' + username + '</span><br />'+ date +'</div></div></div><div class="bubblebox"><h2>Mon avis : <img src="/images/skins/cac6/'+ note +'etoiles_presse_trans.png"></h2><div class="body">'+ text +'</div><div class="footer"><div id="signal_'+id+'" style="float:right; width:75px; cursor:pointer;text-align:right;font-size:9px;padding-top:4px">signaler un abus</div></div></div></li>';



				$("#comments_list").append(block);

				$("#signal_"+id).click(function(event){

					$.post("forumxmlserver.php",{service:"signalVideoComment",id:parseInt(event.target.id.substring(7))},signalCommentsResult,"xml");

				})
			}
		)



		$("#comments_list").append("<li><div style=\"clear:both\"></div><div class=\"NavPages\"><ul id=\"comments_pages\"></ul></div></li>");

		if (parseInt($("page",data).text())>0)
		{
			$("#comments_pages").append("<li class=\"NavPages_prev\" style=\"cursor:pointer\" id=\"comments_prev_page\"><img src=\"http://www.commeaucinema.com/images/skins/cac6/flecheg_barrenav.png\" alt=\"Page précédente\" /></li>");
			$("#comments_prev_page").click(function()
				{
					getVideoComments(ps_fiche_id,ps_flash_id,ps_video_id,parseInt($("page",data).text())-1);
				}
			);
		}

		if (Math.floor(parseInt($("count",data).text())/10)>0)
		{
			for (i=0;i<=Math.floor(parseInt($("count",data).text())/10);i++)
			{
				if (i==parseInt($("page",data).text()))
				{
					$("#comments_pages").append("<li class=\"NavPages_current\" id=\"comments_page_"+(i+1)+"\">"+(i+1)+"</li>");
				}else
				{
					$("#comments_pages").append("<li class=\"NavPages_item\" style=\"cursor:pointer\" id=\"comments_page_"+(i+1)+"\">"+(i+1)+"</li>");
				}

				$("#comments_page_"+(i+1)).click(function(event)
					{

						getVideoComments(ps_fiche_id,ps_flash_id,ps_video_id,parseInt(event.target.id.substring(14))-1);
					}
				);
			}
		}

		if (parseInt($("count",data).text()) - ((parseInt($("page",data).text())+1) * 10)>0)
		{
			$("#comments_pages").append("<li class=\"NavPages_next\" style=\"cursor:pointer\" id=\"comments_next_page\"><img src=\"http://www.commeaucinema.com/images/skins/cac6/flechedr_barrenav.png\" alt=\"Page suivante\" /></li>");
			$("#comments_next_page").click(function()
				{
					getVideoComments(ps_fiche_id,ps_flash_id,ps_video_id,parseInt($("page",data).text())+1);
				}
			);
		}
	}
}

function signalCommentsResult(data)
{
	alert("ce commentaire est maintenant signalé, il sera traité par notre équipe dans les plus brefs délais.");
}

$(document).ready(function(){

	var options = {
        overlayColor:     '#eee',
        flashBgColor:     '#fff',
        overlayOpacity:   0.8

    };

	Shadowbox.init(options);

	if (personnal_box)
	{
		$("body").append('<div id="personnal_space"><h3>Espace Personnel</h3><div id="ps_body"><div id="ps_image"></div><div id="ps_buttons"></div></div></div>');
		$("#ps_body").css('display','none');
		$("#personnal_space").hover(function(){$("#ps_body").css('display','block');ps_position()},function(){$("#ps_body").css('display','none');ps_position()});

		ps_position();
		ps_checkAuth();
	}
})

$(window).scroll(function(){
	if (personnal_box)
	{
		ps_position();
	}
})

$(window).resize(function(){

	if (personnal_box)
	{
		ps_position();
	}
})

