var events = '';
var theDiv;
var keep = false;
var overlay;

function isDefined(variable)
{
	return (typeof(window[variable]) == "undefined") ? false : true;
}

function showEvents() {

	if (document.getElementById('eventsDivSub2').style.visibility != 'hidden') {

		events = document.getElementById('eventsDivSub2').innerHTML;
		document.getElementById('eventsDivSub2').style.visibility = 'hidden';
		document.getElementById('eventsDivSub2').innerHTML = '';

	} else {

		document.getElementById('eventsDivSub2').style.visibility = 'visible';
		document.getElementById('eventsDivSub2').innerHTML = events;

	}

}

function toggleListFavs()
{
	var obj = document.getElementById('liste-favs');

	if (obj.style.display == 'block') {
		obj.style.display = 'none';
	} else {
		obj.style.display = 'block';
	}
}

/*function alerter(texte, niveau)
{
	document.getElementById('information-inner').innerHTML = texte;
	document.getElementById('information').style.display   = 'block';


	if (niveau == 'bad')
		document.getElementById('information-inner').style.background = '#ff0000';
}*/


function encodeQuotes(texte)
{
	texte = texte.replace(/"/g,'&quot;');
	texte = texte.replace(/\'/g,'&#39;');

	return texte;
}

function showCommentEditForm(id)
{
	var obj = document.getElementById('td-comm-' + id);
	var title = document.getElementById('div-comm-title-' + id).innerHTML;
	var text = document.getElementById('div-comm-text-' + id).innerHTML;
	var form = document.getElementById('form-comm-' + id);

	var oldText = obj.innerHTML;

	title = encodeQuotes(title);
	text = text.replace(/<br \/>/g, '\n');
	text = text.replace(/<br>/g, '\n');

	obj.innerHTML = '<br/><div class="form-content"><form id="form-comm-' + id + '" method="post" action="' + window.location + '" onsubmit="return confirmEditComment(' + id + ');"><div class="form-comm-left">Titre :</div><div class="form-comm-right" ><input type="hidden" name="edit" value="' + id + '" /><input type="text" name="titre" class="medium" value="' + title + '" /></div><div class="clear"><br /></div><div class="form-comm-left">Texte :</div><div class="form-comm-right"><textarea name="texte" cols="20" rows="5">' + text + '</textarea></div><div class="clear"><br /></div><div class="center"><input type="submit" value="Éditer" name="submitcommentaire" /></div><input type="hidden" name="id_comm" value="'+id+'" /></form></div><br/>';

	//'<form><input type="text" /><br /><br/><textarea cols="20" rows="20">texte</textarea><br /><br /><input type="submit" /></form>';
}

function toggleImage(id)
{
	if (id == 'tog-message' || id == 'tog-profil') {
		origin = 'zero';
		dest   = 'over';
	}
	else if (id == 'tog-friend' || id == 'tog-auteurfav') {
		origin = 'zero';
		dest   = 'plus';
	}
	else if (id == 'tog-auteurfavminus' || id == 'tog-friendminus') {
		origin = 'plus';
		dest   = 'minus';
	}
	else if ( id.indexOf('tog-gestion') != -1 ) {
		origin = '-off';
		dest   = '-on';
	}
	else return;

	image = document.getElementById(id);

	if ( image.src.indexOf(origin) != -1 )
	{
		image.src = image.src.replace(origin, dest);
	}
	else
	{
		image.src = image.src.replace(dest, origin);
	}
}

function doClose(id)
{
	obj = document.getElementById(id);
	obj.style.display = 'none';
}

function checkSearch()
{
	obj = document.getElementById('searchField');
	theDiv = document.getElementById('searchResults');

	if (obj.value.length > 1)
	{
		str = obj.value;

		var xhr = createXHR();
		xhr.onreadystatechange = function() {
			//theDiv.innerHTML = xhr.responseText;
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					theDiv.innerHTML = xhr.responseText;
				}
			}
		};

		theDiv.innerHTML = '<div class="center getspace"><img src="images/base/ajax-loader.gif" /><br/>Recherche...</div>';
		search.afficher();
		search.setCloseable(false);

		if ( document.getElementById(profil.id) ) {
			profil.fermer();
		}

		xhr.open("GET", "ajax_actions.php?action=search&query=" + str, true);
		xhr.send(null);
	}
	else {
		search.fermer();
	}
}

function checkAuteur()
{
	obj = document.getElementById('searchAuthor');
	theDiv = document.getElementById('searchAuthorResults');

	if (obj.value.length > 0)
	{
		str = obj.value;

		var xhr = createXHR();
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					auteur.afficher();
					theDiv.innerHTML = xhr.responseText;
				}
			}
		};

		xhr.open("GET", "ajax_actions.php?action=listeMembres&query=" + str, true);
		xhr.send(null);
	}
	else {
		auteur.fermer();
	}
}

function fillAuteur(nom)
{
	document.getElementById('searchAuthor').value = nom;
	auteur.fermer();
	void(null);
}

/* Pour mettre un background noir transparent */
function goBlack()
{
	if ( isDefined(overlay) )
		return;

	overlay = document.createElement('div');
	overlay.id = 'blackground';
	overlay.onclick = function() { goWhite(); }
	var arrayPageSize = getPageSize();
	overlay.style.height = arrayPageSize[1] + "px";

	var theBody = document.getElementsByTagName('body')[0];
	theBody.appendChild(overlay);
}
function goWhite()
{
	var theBody = document.getElementsByTagName('body')[0];
	theBody.removeChild(overlay);

	if (gallery)
		gallery = null;

	overlay = undefined;
}

/*
 * Objet autoFader utilisé pour les <div> qu'on veut fermer automatiquement
 */
function autoFader(id,timeout,timer,closeable,visible)
{
	// attributes
	this.id = id;
	this.timeout = timeout;
	this.timer = null;
	this.closeable = true;
	this.visible = false;

	// methods
	this.afficher = afficher;
	this.fermer = fermer;
	this.setCloseable = setCloseable;
	this.timedClosing = timedClosing;
}

function afficher()
{
    var obj = document.getElementById(this.id);
    obj.style.display  = 'block';

	objet = this;
	clearTimeout(this.timer);
    this.timer = setTimeout('objet.timedClosing();',this.timeout);
    this.visible = true;

    void(null);
}

function fermer()
{
	document.getElementById(this.id).style.display = 'none';
	this.visible = false;

    void(null);
}

function setCloseable(status)
{
	if (!this.visible) {
		void(null);
	}

	this.closeable = status;

	if (status)
	{
		objet = this;
		clearTimeout(this.timer);
		this.timer = setTimeout('objet.timedClosing();',this.timeout);
		this.visible = false;
	}

	void(null);
}

function timedClosing()
{
	if (this.closeable) {
		this.fermer();
	}

	void(null);
}
/*
 * Fin de l'objet autoFader
 */

//
// Thanks to LightBox
// http://www.dynamicdrive.com/dynamicindex4/lightbox2/index.htm
//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize()
{
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}

/* Taken from SMF */
// Surrounds the selected text with text1 and text2.
function surroundText(text1, text2, textarea)
{
	// Can a text range be created?
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
	{
		var caretPos = textarea.caretPos, temp_length = caretPos.text.length;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;

		if (temp_length == 0)
		{
			caretPos.moveStart("character", -text2.length);
			caretPos.moveEnd("character", -text2.length);
			caretPos.select();
		}
		else
			textarea.focus(caretPos);
	}
	// Mozilla text range wrap.
	else if (typeof(textarea.selectionStart) != "undefined")
	{
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text1 + selection + text2 + end;

		if (textarea.setSelectionRange)
		{
			if (selection.length == 0)
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;
	}
	// Just put them on the end, then.
	else
	{
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
	}
}

var profil = new autoFader('nav-profil',3000);
var search = new autoFader('searchResults',3000);
var auteur = new autoFader('searchAuthorResults',60000);
