var starred = false;

// Fonction pour marquer les nouveaux événements comme lus
function closeEvents()
{
	xhr = createXHR();

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				document.getElementById('eventsDiv').style.visibility='hidden';
				document.getElementById('eventsDivSub2').style.visibility='hidden';
			}
		}
	};

	xhr.open("GET", "ajax_actions.php?action=events", true);
	xhr.send(null);
}

// Fonction pour ajouter un favori
function addFav(id, type, id_texte)
{
	starred = true;

	xhr = createXHR();

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				document.getElementById('information-inner').innerHTML=xhr.responseText;
				document.getElementById('information').style.display='block';
			}
		}
	};

	if ( type == 1 ) {
		xhr.open("GET", "ajax_actions.php?action=favoris&id=" + id + "&id_texte=" + id_texte, true);
	} else if ( type == 4 ) {
		xhr.open("GET", "ajax_actions.php?action=chronique&id=" + id + "&id_texte=" + id_texte, true);
	} else if ( type == 5 ) {
		xhr.open("GET", "ajax_actions.php?action=theorie&id=" + id + "&id_texte=" + id_texte, true);
	} else
		return;

	xhr.send(null);
}

// Fonction pour ajouter un artiste comme favori
function addFavArtist(id) {

	xhr = createXHR();

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				document.getElementById('information-inner').innerHTML=xhr.responseText;
				document.getElementById('information').style.display='block';
			}
		}
	};

	xhr.open("GET", "ajax_actions.php?action=artistefavori&id=" + id, true);
	xhr.send(null);
}

// Fonction pour ajouter un artiste comme ami
function addFriend(id) {

	xhr = createXHR();

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				document.getElementById('information-inner').innerHTML=xhr.responseText;
				document.getElementById('information').style.display='block';
			}
		}
	};

	xhr.open("GET", "ajax_actions.php?action=friends&id=" + id, true);
	xhr.send(null);
}

// Fonction pour voter
function voter(id,note,type) {

	xhr = createXHR();

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				document.getElementById('information-inner').innerHTML=xhr.responseText;
				document.getElementById('information').style.display='block';
			}
		}
	};

	xhr.open("GET", "ajax_actions.php?action=vote&oeuvre=" + id + "&vote=" + note + "&type=" + type, true);
	xhr.send(null);
}

// Fonction pour effacer un commentaire
function eraseComments(id) {

	if (confirm("Etes-vous sûr de vouloir effacer ce commentaire ?")) {

		xhr = createXHR();

		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
				document.getElementById('tr-comm-' + id).style.display = 'none';
				}
			}
		};

		xhr.open("GET", "ajax_actions.php?action=eraseComments&id=" + id , true);
		xhr.send(null);
	}
}
function acceptFriend(id, amiid, type) {

		xhr = createXHR();

		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
				document.getElementById('information-inner').innerHTML=xhr.responseText;
				document.getElementById('information').style.display='block';
				document.getElementById('information-inner').style.background = '#90DEC6';
				}
			}
		};
	if (type == 1) {
		if (confirm("Êtes-vous sûr de vouloir accepter cette invitation ?")) {
			xhr.open("GET", "ajax_actions.php?action=acceptFriend&id=" + id + "&amiid=" + amiid + "&type=" + type, true);
			xhr.send(null);
		}
	} else if (type == 2) {
		if (confirm("Êtes-vous sûr de vouloir refuser cette invitation ?")) {
			xhr.open("GET", "ajax_actions.php?action=acceptFriend&id=" + id + "&amiid=" + amiid + "&type=" + type, true);
			xhr.send(null);
		}
	}
}

function makeNew(event_id)
{
	xhr = createXHR();

	xhr.open("GET", "ajax_actions.php?action=makeNew&event_id=" + event_id , true);
	xhr.send(null);

	var obj = document.getElementById('notif' + event_id);
	if (obj.className.indexOf('comment') != -1) {
		obj.className = 'comment-page-solo';
	} else {
		obj.className = 'notif-page-div';
	}
	obj.innerHTML = obj.innerHTML.replace('makeNew', 'makeOld');
	obj.innerHTML = obj.innerHTML.replace('vote-off.png', 'vote.png');
}

function makeOld(event_id)
{
	xhr = createXHR();

	xhr.open("GET", "ajax_actions.php?action=makeOld&event_id=" + event_id , true);
	xhr.send(null);

	var obj = document.getElementById('notif' + event_id);
	if (obj.className.indexOf('comment') != -1) {
		obj.className = 'comment-page-solo-old';
	} else {
		obj.className = 'notif-page-div-old';
	}
	obj.innerHTML = obj.innerHTML.replace('makeOld', 'makeNew');
	obj.innerHTML = obj.innerHTML.replace('vote.png', 'vote-off.png');
}

function eraseNotif(event_id)
{
	xhr = createXHR();

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				document.getElementById('notif' + event_id).innerHTML='';
				document.getElementById('notif' + event_id).style.display='none';
			}
		}
	};
	xhr.open("GET", "ajax_actions.php?action=eraseNotif&event_id=" + event_id , true);
	xhr.send(null);
}

function unactiveFriend(event_id)
{
	xhr = createXHR();

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				document.getElementById('information-inner').innerHTML='Ami supprimé.';
				document.getElementById('information').style.display='block';
			}
		}
	};
	xhr.open("GET", "ajax_actions.php?action=unactiveFriend&ami_id=" + event_id , true);
	xhr.send(null);
}

function unactiveFavArt(artist_id)
{
	xhr = createXHR();

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				document.getElementById('information-inner').innerHTML='Cet artiste est supprimé de vos favoris.'+xhr.responseText;
				document.getElementById('information').style.display='block';
			}
		}
	};
	xhr.open("GET", "ajax_actions.php?action=unactiveFavArt&fav_id=" + artist_id , true);
	xhr.send(null);
}

function unactiveFavArtworks(fav_user, fav_id, fav_type)
{
	starred = true;

	xhr = createXHR();

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				document.getElementById('information-inner').innerHTML='Cet article/oeuvre est supprimé de vos favoris.';
				document.getElementById('information').style.display='block';
			}
		}
	};
	xhr.open("GET", "ajax_actions.php?action=unactiveFavArtworks&fav_user=" + fav_user + "&fav_id=" + fav_id + "&fav_type=" + fav_type, true);
	xhr.send(null);
}

function unactiveArtworks(type, oeuvre)
{
	xhr = createXHR();

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				document.getElementById('information-inner').innerHTML='Cet article/oeuvre est supprimé.';
				document.getElementById('information').style.display='block';
				window.location.replace('/');
			}
		}
	};
	if (confirm("Êtes-vous sûr de vouloir supprimer cette oeuvre ou article ?")) {
		xhr.open("GET", "ajax_actions.php?action=unactiveArtworks&type=" + type + "&oeuvre=" + oeuvre, true);
		xhr.send(null);
	}
}

function changeAll(id)
{
	xhr = createXHR();

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				/*document.getElementById('information-inner').innerHTML=xhr.responseText;
				document.getElementById('information').style.display='block';*/
				window.location.reload();
			}
		}
	};
	xhr.open("GET", "ajax_actions.php?action=changeAll&id=" + id , true);
	xhr.send(null);
}

function toggleStar()
{
	if (starred == true)
		return;

	star = document.getElementById('starfav');

	if ( star.src.indexOf('off') != -1 ) {
		star.src = star.src.replace('off', 'on');
	} else {
		star.src = star.src.replace('on', 'off');
	}
}

