/* ===== COMMUNIQUES ===== */

function communique_Init(element) {
	/*Effect.toggle(element, 'blind');
	
	Event.observe('picto_archive', 'mouseover', function () { Element.show('window_archive'); checkDiv('erreur_sendfriend'); });
	Event.observe('picto_archive', 'mouseout', function () { Element.hide('window_archive') });
	
	Event.observe('picto_telecharger', 'mouseover', function () { Element.show('window_telecharger'); checkDiv('erreur_sendfriend'); });
	Event.observe('picto_telecharger', 'mouseout', function () { Element.hide('window_telecharger') });
	
	Event.observe('picto_imprimer', 'mouseover', function () { Element.show('window_imprimer'); checkDiv('erreur_sendfriend'); });
	Event.observe('picto_imprimer', 'mouseout', function () { Element.hide('window_imprimer') });
	
	Event.observe('picto_envoi_ami', 'mouseover', function () { Element.show('window_envoi_ami') });
	Event.observe('picto_envoi_ami', 'mouseout', function () { Element.hide('window_envoi_ami') });
	
	Event.observe('bt_sendfriend', 'click', communique_sendFriend);*/
}

function communique_checkArchive () {
	new Ajax.Request(ROOT_PATH_SOCIETE+'remote/archive.php',
			{
				method : 'post',
				parameters : 'action=checkArchive&id_entity='+ $F('id_entity') +'&id_item='+ $F('id_communique') +'&type_item=1',
				onSuccess : function(xhr) {
					$('action-archive').update(xhr.responseText);
					Event.observe('ajout_archive', 'click', communique_addArchive);
				}
			});
}

function communique_addArchive () {
	new Ajax.Request(ROOT_PATH_SOCIETE+'remote/archive.php',
			{
				method : 'post',
				parameters : 'action=INSERT&id_entity='+$F('id_entity')+'&id_item='+$F('id_communique')+'&type_item=1',
				onSuccess : function(xhr) {
				    $$('#action-archive p')[0].update('Ce communiqué vient d\être ajouté à vos archives.');
				}
			});
}

function communique_print(id_communique) {
	window.open(ROOT_PATH + "communique_print.php?id_communique=" + id_communique, "ZoneImpr", "height=500, width=575");
	return true;
}

function communique_sendFriend () {
	checkDiv('erreur_sendfriend');
	
	if($F('ami_nom_exp') != '' && $F('ami_email_exp') != '' && $F('ami_email_dest') != '') {
		if(checkMail($F('ami_email_exp'), false)) {
			new Ajax.Request(ROOT_PATH_SOCIETE+'remote/communique.php',
					{
						method : 'post',
						parameters : 'action=communiqueSendFriend&id_communique='+$F('id_communique')+'&nom='+encodeURIComponent($F('ami_nom_exp'))+'&email='+encodeURIComponent($F('ami_email_exp'))+'&liste_email='+encodeURIComponent($F('ami_email_dest')),
						onSuccess : function(xhr) {
							alert('Le communiqué a bien été envoyé.');
							$('ami_email_dest').clear();
						}
					});
		} else {
			displayErrorForm('check_erreur_sendfriend', 'erreur_sendfriend', 'Le format de votre adresse email est invalide', 'normal');
		}
	} else {
		displayErrorForm('check_erreur_sendfriend', 'erreur_sendfriend', 'Vous devez remplir tous les champs', 'normal');
	}
}

/* --- Liste des commentaires --- */

var statusFleche = 'h';
var statusText = 'Voir';

function communique_displayCommentBlock(element, id_lien)
{
	Effect.toggle(element, 'blind', { duration : 0.5});
	
	if($('fleche').getAttribute('src') == IMG_ROOT_PATH_SOCIETE+'triangle_rose_v.gif') {
		$('fleche').writeAttribute ('src', IMG_ROOT_PATH_SOCIETE+'triangle_rose_h.gif');
		if(id_lien == 'lienVoirMasquer') {
			(totalComComment > 1) ? $(id_lien).update('Voir les '+totalComComment+' commentaires') : $(id_lien).update('Voir le commentaire');
		}
		Element.hide('bt_ajout_com');
		statusFleche = 'h';
		statusText = 'Voir';
	} else {
		$('fleche').writeAttribute ('src', IMG_ROOT_PATH_SOCIETE+'triangle_rose_v.gif');
		if(id_lien == 'lienVoirMasquer') {
			(totalComComment > 1) ? $(id_lien).update('Masquer les '+totalComComment+' commentaires') : $(id_lien).update('Masquer le commentaire');
		}
		Element.show('bt_ajout_com');
		Event.observe('bt_ajout_com', 'click', communique_addComment);
		statusFleche = 'v';
		statusText = 'Masquer';
	}
}

function communique_addComment() {
	checkDiv('erreur_commentaire');
	
	if($F('auteur') != 'Pseudo' || $F('titre') != 'Titre' || $F('texte') != 'Ecrivez votre commentaire...') {
		new Ajax.Request(ROOT_PATH_SOCIETE+'remote/communique.php',
				{
					method : 'post',
					parameters : 'action=commentaireAjouter&auteur='+encodeURIComponent($F('auteur'))+'&titre='+encodeURIComponent($F('titre'))+'&texte='+encodeURIComponent($F('texte'))+'&id_communique='+$F('id_communique'),
					onSuccess : function(xhr) {
						if(xhr.responseText == 'non_connecte') {
							displayErrorForm('check_erreur_commentaire', 'erreur_commentaire', 'Vous devez vous identifier pour poster un commentaire', 'normal');
						} else {
							communique_createCommentList($F('id_communique'), 0, limitComComment);
						}
					}
				});
	} else {
		displayErrorForm('check_erreur_commentaire', 'erreur_commentaire', 'Vous devez remplir tous les champs', 'normal');
	}
}

function communique_createCommentList(id_communique, pageInit, limitInit)
{
	idCommunique		= id_communique;
	pageComComment  = pageInit;
	limitComComment = limitInit;
	debutComComment = pageInit*limitInit;
	
	urlTotal = 'action=commentaireTotal&id_communique='+id_communique+'&debut='+debutComComment+'&limit='+limitInit;
	url = 'action=commentaire&id_communique='+id_communique+'&debut='+debutComComment+'&limit='+limitInit;

	new Ajax.Request(ROOT_PATH_SOCIETE+'remote/communique.php',
			{
				method : 'post',
				parameters : urlTotal,
				onSuccess : function(xhr) 
				{
					totauxComComment = xhr.responseText.split('|');
					totalComComment = totauxComComment[0];
					totalComCommentXml = totauxComComment[1];
					
					if(!$('loaderComCommentList'))
					{
						var loader = new Element('img', {
													 src : IMG_ROOT_PATH+'loader.gif',
													 id : 'loaderComCommentList'
												 });
						$('contentLoader').insert(loader);
					}
					
					if(totalComComment > 0)
					{
						$('contentLink').update();
						
						txt_nb_com = (totalComComment > 1) ? statusText+' les '+totalComComment+' commentaires' : statusText+' le commentaire';
						
						var a = new Element('a', {
												href : 'javascript:communique_displayCommentBlock(\'liste_commentaire\', \'lienVoirMasquer\')',
												'class' : 'voir_com',
												id : 'lienVoirMasquer'
											}).update(txt_nb_com);
						$('contentLink').insert(a);
						
						var img = new Element('img', {
												 src : IMG_ROOT_PATH_SOCIETE+'triangle_rose_'+statusFleche+'.gif',
												 id : 'fleche',
												 width : 9,
												 height :9
											});
						$('contentLink').insert(img);
						
						new Ajax.Request(ROOT_PATH_SOCIETE+'remote/communique.php',
								{
									method : 'post',
									parameters : url,
									onSuccess : function(xhr) {
										if(totalComCommentXml > 0) {
											communique_displayCommentList(xhr.responseXML, false, false);
										} else {
											communique_createCommentList(idCommunique, pageComComment-1, limitComComment);
										}
									}
								});
					} 
					else
					{
						$('contentLink').update();
						$('liste_commentaire').update();
						Element.remove($('loaderComCommentList'));
						
						var a = new Element('a', {
												href : 'javascript:communique_displayCommentBlock(\'liste_commentaire\', \'aucun\')',
												id : 'aucun',
												'class' : 'voir_com'
											}).update('Aucun commentaire');
						$('contentLink').insert(a);						
						
						var img = new Element('img', {
												 src : IMG_ROOT_PATH_SOCIETE+'triangle_rose_'+statusFleche+'.gif',
												 id : 'fleche',
												 width : 9,
												 height :9
											});
						$('contentLink').insert(img);
						
						commentaire_displayFrom ();
					}
				}
			});
}

function communique_createComment (commentId, commentTitle, commentText, commentAuthor, commentDate, container)
{
	var div = new Element('div', {
							id : 'contenu'
						});
	container.insert(div);
	
	var h3 = new Element('h3').update(commentTitle);
	div.insert(h3);
	
	var span = new Element('span').update(commentAuthor+' | '+commentDate);
	div.insert(span);
	
	var div_texte = new Element('div').update(commentText);
	div.insert(div_texte);
		
	var hr = new Element('hr');
	container.insert(hr);
}

function communique_displayCommentList (xmlReponse)
{
	Element.remove($('loaderComCommentList'));
	$('liste_commentaire').update();
	
	totalXML = XMLcount(xmlReponse);
	
	var ol = new Element('ol', {
						 id : 'communique_commentaire'
					 });
	$('liste_commentaire').insert(ol);
	
	for(i = 0; i < totalXML; i++)
	{
		var commentId 		= XMLresult (xmlReponse, i, 'comment_id');
		var commentTitle 	= XMLresult (xmlReponse, i, 'comment_title');
		var commentText 	= XMLresult (xmlReponse, i, 'comment_text');
		var commentAuthor	= XMLresult (xmlReponse, i, 'comment_author');
		var commentDate	  = XMLresult (xmlReponse, i, 'comment_date');
		
		var li = new Element('li');
		ol.insert(li);
				
		communique_createComment(commentId, commentTitle, commentText, commentAuthor, commentDate, li);
		
		$('communique_commentaire').insert(li);
	}

	$('liste_commentaire').insert(pagination(idCommunique, totalComComment, limitComComment, pageComComment, 'communique_createCommentList'));
	commentaire_displayFrom ();
}

function commentaire_displayFrom () {
	var hr = new Element('hr');
	$('liste_commentaire').insert(hr);
	
	$('liste_commentaire').insert(new Element('div', { 'id' : 'check_erreur_commentaire' }));
	$('liste_commentaire').insert(new Element('input', { 'id' : 'auteur', 'value' : 'Pseudo' }));
	$('liste_commentaire').insert(new Element('input', { 'id' : 'titre', 'value' : 'Titre' }));
	$('liste_commentaire').insert(new Element('textarea', { 'id' : 'texte' }).update('Ecrivez votre commentaire...'));
	
	Event.observe('auteur', 'click', checkClickAuteur);
	Event.observe('titre', 'click', checkClickTitre);
	Event.observe('texte', 'click', checkClickTexte);
	
	Event.observe('auteur', 'blur', checkBlurAuteur);
	Event.observe('titre', 'blur', checkBlurTitre);
	Event.observe('texte', 'blur', checkBlurTexte);
}

function checkClickAuteur () {
	checkClick ('auteur', 'Pseudo');
}

function checkClickTitre () {
	checkClick ('titre', 'Titre');
}

function checkClickTexte () {
	checkClick ('texte', 'Ecrivez votre commentaire...');
}

function checkClick (value, ref) {
	if($F(value) == ref) {
		$(value).clear();
	}
}

function checkBlurAuteur () {
	checkBlur ('auteur', 'Pseudo');
}

function checkBlurTitre () {
	checkBlur ('titre', 'Titre');
}

function checkBlurTexte () {
	checkBlur ('texte', 'Ecrivez votre commentaire...');
}

function checkBlur (value, ref) {
	if($F(value) == '') {
		$(value).value = ref;
	}
}

/* --- Pagination --- */

function pagination (id, total, perpage, page, func)
{
	var callback = new Function ('a', 'b', 'c', func+'(a, b, c);');
	var page_num = Math.ceil (total / perpage);
	
	if (page_num == 1){
							return;
	}

	var liste = new Element ('ul',{'id':'pagination', 'style' : 'margin: 10px 0'});
	var e;

	// previous
	if (page != 0 && page_num > 1){
							e = new Element ('li',{'class':'previous'}).update ('<< Précédent');
							Event.observe (e, 'click', function (){callback (id, page - 1, perpage);});
							liste.insert (e);
	}

	for (var p = 0; p < page_num; p ++)
	{
		// focus page
		if (p == page){
							 e = new Element ('li',{'class':'on'}).update (p+1);
							 liste.insert (e);
		}else
		// near pages
		if (p > page_num - 4 || p < 3){
							 e = new Element ('li',{'class':'off'}).update (new Element ('a',{'href':'javascript:'+func+'('+id+','+p+','+perpage+');'}).update (p+1));
							 //Event.observe (e, 'click', function (){callback (page);});
							 liste.insert (e);
		}else
		// between pages left
		if (p < page - 4){
							 liste.insert (new Element ('li').update ('...'));
							 p = page - 4;
		}else
		// between pages right
		if (p > page + 3){
							 liste.insert (new Element ('li').update ('...'));
							 p += page_num - p - 4;
		}else{
		// border pages
							 e = new Element ('li',{'class':'off'}).update (new Element ('a',{'href':'javascript:'+func+'('+id+','+p+','+perpage+');'}).update (p+1));
							 //Event.observe (e, 'click', function (){callback (page);});
							 liste.insert (e);
		}
	}
	// next
	if (page != page_num - 1 && page_num > 1){
						  e = new Element ('li',{'class':'next'}).update ('Suivant >>');
							Event.observe (e, 'click', function (){callback (id, page + 1, perpage);});
							liste.insert (e);
	}
	
	liste.insert (new Element ('div', {'class':'clear'}));
	
	return liste;
}
