var lien = '';


/* Pour les dernières oeuvres */
var currentType  = 'all';
var loadedType   = new Array();
var contentsType = new Array();

loadedType['all']      = true;
loadedType['poesie']   = false;
loadedType['litterature']  = false;
loadedType['chants']  = false;
loadedType['photos']    = false;
loadedType['dessins']   = false;
loadedType['bannieres']   = false;
loadedType['fonds']   = false;

/* Pour les dernières chroniques */
var currentChronType  = 0;
var loadedChronType   = new Array();
var contentsChronType = new Array();

loadedChronType[0]      = true;

/* Pour les articles */
var article = 'chroniques';
var loadedTheory = new Array();
var contentsTheory = new Array();

loadedTheory['chroniques']   = true;
loadedTheory['theorie']      = false;
contentsTheory['chroniques'] = '';
contentsTheory['theorie']    = '';

/* Pour les statuts */
var info = new Array();
info['film2'] = '';
info['musique2'] = '';
info['livre2'] = '';

var bouton = new Array();
bouton['film2'] = '';
bouton['musique2'] = '';
bouton['livre2'] = '';

/* Pour les commentaires du blog */
var comments = new Array();
var liens = new Array();

function addThought(id)
{
	lien = document.getElementById('add-thought').innerHTML;
	document.getElementById('add-thought').innerHTML =
		  '<textarea id="thought-text" style="width: 90%;"></textarea>'
		+ '<input type="button" id="b1" onclick="validerThought(' + id + ');" value="Valider" />'
		+ '<input type="button" id="b2" onclick="annulerThought();" value="Annuler" />';
}

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

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				var texte = document.getElementById('talk-cadre').innerHTML;
				document.getElementById('talk-cadre').innerHTML = xhr.responseText + texte;
				document.getElementById('add-thought').innerHTML = lien;
				document.getElementById('feel-empty').style.display = 'none';
			}
		}
	};

	var str = document.getElementById('thought-text').value;
	document.getElementById('thought-text').disabled = true;
	document.getElementById('b1').disabled = true;
	document.getElementById('b2').disabled = true;
	xhr.open("GET", "ajax_actions.php?action=addthought&id=" + id + "&text=" + escape(encodeURIComponent(str)), true);
	xhr.send(null);
}

function annulerThought()
{
	document.getElementById('add-thought').innerHTML = lien;
}

function toggleComments(divName)
{
	if (divName == 'sent') {
		document.getElementById('comm-rcvd').style.display = 'none';
		document.getElementById('comm-sent').style.display = 'block';
		document.getElementById('sp-comm-rcvd').className = '';
		document.getElementById('sp-comm-sent').className = 'selected';
	}
	else {
		document.getElementById('comm-rcvd').style.display = 'block';
		document.getElementById('comm-sent').style.display = 'none';
		document.getElementById('sp-comm-rcvd').className = 'selected';
		document.getElementById('sp-comm-sent').className = '';
	}
}

function toggleInfos(divName)
{
	if (divName != 'generales' && divName != 'preferences' &&
	    divName != 'artiste' && divName != 'contacts' && divName != 'statut')
		return;


	document.getElementById('profil-infos-generales').style.display = 'none';
	document.getElementById('sp-infos-generales').className = '';

	document.getElementById('profil-infos-preferences').style.display = 'none';
	document.getElementById('sp-infos-preferences').className = '';

	document.getElementById('profil-infos-artiste').style.display = 'none';
	document.getElementById('sp-infos-artiste').className = '';

	document.getElementById('profil-infos-contacts').style.display = 'none';
	document.getElementById('sp-infos-contacts').className = '';

	document.getElementById('profil-infos-statut').style.display = 'none';
	document.getElementById('sp-infos-statut').className = '';


	document.getElementById('profil-infos-' + divName).style.display = 'block';
	document.getElementById('sp-infos-' + divName).className = 'selected';
}


function toggleOeuvres(type,auth,section)
{
	if ( type == currentType || loadedType[type] == null )
		return;

	if (section == 'galerie') {
		table = document.getElementById('profil-galerie-oeuvres');
	}
	else if (section == 'favoris') {
		table = document.getElementById('profil-favoris-oeuvres');
	}
	else {
		section = '';
		table = document.getElementById('profil-table-oeuvres');
	}

	oldspan = document.getElementById('sp-oeuvres-' + currentType);
	newspan = document.getElementById('sp-oeuvres-' + type);
	oldspan.className = '';
	newspan.className = 'selected';

	contentsType[currentType] = table.innerHTML;

	if ( loadedType[type] == true ) {
		table.innerHTML = contentsType[type];
		currentType = type;
		return;
	}
	else {
		table.innerHTML = '<div class="getspace center">Un instant...</div>';

		xhr = createXHR();

		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					table.innerHTML = xhr.responseText;
					currentType = type;
					loadedType[type] = true;
				}
			}
		};

		xhr.open("GET", "ajax_actions.php?action=histoprofil" + section + "&mode=" + type + "&a=" + auth, true);
		xhr.send(null);
	}

	return;
}

function toggleChron(type,auth,limit)
{
	if ( type == currentChronType )
		return;

	table = document.getElementById('profil-galerie-chron');

	oldspan = document.getElementById('sp-chron-' + currentChronType);
	newspan = document.getElementById('sp-chron-' + type);
	oldspan.className = '';
	newspan.className = 'selected';

	contentsChronType[currentChronType] = table.innerHTML;

	if ( loadedChronType[type] ) {
		if ( loadedChronType[type] == true ) {
			table.innerHTML = contentsChronType[type];
			currentChronType = type;
			return;
		}
	}

	table.innerHTML = '<div class="getspace center">Un instant...</div>';

	xhr = createXHR();

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				table.innerHTML = xhr.responseText;
				currentChronType = type;
				loadedChronType[type] = true;
			}
		}
	};

	xhr.open("GET", "ajax_actions.php?action=galeriechron&mode=" + type + "&a=" + auth + "&limit=" + limit, true);
	xhr.send(null);

	return;
}


function toggleArticle(type,auth)
{
	if ( type == article )
		return;

	table = document.getElementById('profil-favoris-chron');

	oldspan = document.getElementById('sp-chron-' + article);
	newspan = document.getElementById('sp-chron-' + type);
	oldspan.className = '';
	newspan.className = 'selected';

	contentsTheory[article] = table.innerHTML;

	if ( loadedTheory[type] == true ) {
		table.innerHTML = contentsTheory[type];
		article = type;
		return;
	}

	table.innerHTML = '<div class="getspace center">Un instant...</div>';

	xhr = createXHR();

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				table.innerHTML = xhr.responseText;
				article = type;
				loadedTheory[type] = true;
			}
		}
	};

	xhr.open("GET", "ajax_actions.php?action=favorischron&a=" + auth, true);
	xhr.send(null);

	return;
}

function allFriends(member,idList)
{
	var row = document.getElementById('rest-of-friends');

	row.cells[0].innerHTML = '<div class="center">Un instant...</div>';

	xhr = createXHR();

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				row.cells[0].innerHTML = '';
				var i = 0;
				var n;
				var matches = xhr.responseText.match(/<td class="fav">(.)*?<\/td>/g);
				n = matches.length;
				for (i = 0; i < n; i++) {
					if (row.cells[i] == undefined) {
						var x = row.insertCell(-1);
						x.innerHTML = matches[i];
						x.colSpan = '1';
					} else {
						row.cells[i].innerHTML = matches[i];
						row.cells[i].colSpan = '1';
					}
				}
			}
		}
	};

	xhr.open("GET", "ajax_actions.php?action=allfriends&a=" + member + "&str=" + idList, true);
	xhr.send(null);

	return false;
}

function togglePlusMinus(id)
{
	picture = document.getElementById('img-' + id);

	if ( picture.src.indexOf('plus') != -1 )
	{
		picture.src = picture.src.replace('plus', 'minus');
	}
	else
	{
		picture.src = picture.src.replace('minus', 'plus');
	}

	lien = document.getElementById('link-' + id);

	if ( lien.href.indexOf('addFriend') != -1 )
	{
		lien.href = lien.href.replace('addFriend', 'unactiveFriend');
	}
	else if ( lien.href.indexOf('unactiveFriend') != -1 )
	{
		lien.href = lien.href.replace('unactiveFriend', 'addFriend');
	}
	else if ( lien.href.indexOf('addFavArtist') != -1 )
	{
		lien.href = lien.href.replace('addFavArtist', 'unactiveFavArt');
	}
	else if ( lien.href.indexOf('unactiveFavArt') != -1 )
	{
		lien.href = lien.href.replace('unactiveFavArt', 'addFavArtist');
	}
}


function editProfil(id)
{
	var text   = document.getElementById('aff-' + id);
	var button = document.getElementById('edit-' + id);

	info[id]   = text.innerHTML;
	bouton[id] = button.innerHTML;

	text.innerHTML   = '<input type="text" id="aff2-' + id + '" value="' + encodeQuotes(text.innerHTML) + '" />';
	button.innerHTML = '<img src="images/base/ok1.png" onclick="confirmEditProfil(\'' + id + '\')" /> <img src="images/base/remove1.png" onclick="cancelEditProfil(\'' + id + '\')" />';
}

function cancelEditProfil(id)
{
	var text   = document.getElementById('aff-' + id);
	var button = document.getElementById('edit-' + id);

	text.innerHTML   = info[id];
	button.innerHTML = bouton[id];
}

function confirmEditProfil(id)
{
	var text   = document.getElementById('aff-' + id);
	var button = document.getElementById('edit-' + id);

	var texte = document.getElementById('aff2-' + id).value;

	xhr = createXHR();

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				text.innerHTML = texte + xhr.responseText;
				button.innerHTML = bouton[id];
			}
		}
	};

	xhr.open("GET", "ajax_actions.php?action=editprofil&field=" + id + "&text=" + texte, true);
	xhr.send(null);

	return;
}

function showBlogComments(id)
{
	var block = document.getElementById('blog-comment-' + id);
	var lien  = document.getElementById('nbcomm' + id).firstChild;

	// means ajax content is already loaded
	if ( comments[id] )
	{
		// means some content was saved in the array,
		// so we want to restore it
		if ( comments[id] == 'on' )
		{
			block.style.display = 'none';
			comments[id] = 'off';
			lien.innerHTML = liens[id];
		}
		else
		{
			block.style.display = 'block';
			comments[id] = 'on';
			lien.innerHTML = 'fermer';
		}
	}
	else
	{
		xhr = createXHR();
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					block.innerHTML = xhr.responseText;
					block.style.display = 'block';
					comments[id] = 'on';
					liens[id] = lien.innerHTML;
					lien.innerHTML = 'fermer';
				}
			}
		};

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

	return;
}

function supprimerNote(id)
{
	if ( !confirm('Êtes-vous sûr(e) de vouloir supprimer cette note ?') ) {
		return false;
	}

	block = document.getElementById('note-'+id);
	block.style.display = 'none';

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

