var typePartie = 'general';
var persoId = 0;

/*
 * jQuery hashchange event - v1.2 - 2/11/2010
 * http://benalman.com/projects/jquery-hashchange-plugin/
 *
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,i,b){var j,k=$.event.special,c="location",d="hashchange",l="href",f=$.browser,g=document.documentMode,h=f.msie&&(g===b||g<8),e="on"+d in i&&!h;function a(m){m=m||i[c][l];return m.replace(/^[^#]*#?(.*)$/,"$1")}$[d+"Delay"]=100;k[d]=$.extend(k[d],{setup:function(){if(e){return false}$(j.start)},teardown:function(){if(e){return false}$(j.stop)}});j=(function(){var m={},r,n,o,q;function p(){o=q=function(s){return s};if(h){n=$('<iframe src="javascript:0"/>').hide().insertAfter("body")[0].contentWindow;q=function(){return a(n.document[c][l])};o=function(u,s){if(u!==s){var t=n.document;t.open().close();t[c].hash="#"+u}};o(a())}}m.start=function(){if(r){return}var t=a();o||p();(function s(){var v=a(),u=q(t);if(v!==t){o(t=v,u);$(i).trigger(d)}else{if(u!==t){i[c][l]=i[c][l].replace(/#.*/,"")+"#"+u}}r=setTimeout(s,$[d+"Delay"])})()};m.stop=function(){if(!n){r&&clearTimeout(r);r=0}};return m})()})(jQuery,this);


$(document).ready(function()	{

	$(window).bind('hashchange', function(){
		var hash = window.location.hash;


		if(hash=='#!guildes') {
			changeType('guildes');
		} else if(hash=='#!general') {
			changeType('general');
		} else if(hash=='#!equipe') {
			changeType('equipe');
		} else if(hash=='#!recherche') {
			recherche();
		}	else {

			// Decoupage du hash
			var paramTmp = hash.split('_');
			var param = new Array();
			var k = 0;

			for(i=0; i<paramTmp.length; i++)	{
				paramTmp2 = paramTmp[i].split('=');
				param[i] = new Array();
				for(j=0; j<paramTmp2.length; j++)	{
					param[i][j] = paramTmp2[j];
				}
			}

			if(param[0][0]=='#!idCat')	{
				var page = param[1][1] ? param[1][1] : 1;
				if(param[0][1])	{
					getCat(param[0][1], page);
				}
			}
			else if(param[0][0]=='#!idSujet')	{
				var page = param[1][1] ? param[1][1] : 1;
				var focusLastPost = false;
				if(param[2])	{
					if(param[2][1])	{
						focusLastPost = param[2][1]==1;
					}
				}
				if(param[0][1]>0)	{
					getSujet(param[0][1], page, focusLastPost);
				}
			}
		}
	});


	if(window.location.hash) {
		$(window).trigger('hashchange');
	}

	var pageToLoad = 1;

	$('.ajax').live('click', function() { return false; });
	$('.cat').live('mousedown', function() { redirection('#!idCat='+$(this).attr('idCat')+'_page='+$(this).attr('page')); });
	$('.sujet').live('mousedown', function() { redirection('#!idSujet='+$(this).attr('idSujet')+'_page='+pageToLoad); pageToLoad = 1; });
	$('.partie').live('mousedown', function() { redirection('#!'+$(this).attr('typePartie')); });
	$('.page').live('mousedown', function() {
		if($(this).parent().parent().attr('mode')=='cat') {
			pageToLoad = $(this).attr('page');
		}
		else if($(this).attr('mode')=='recherche') {
			eval($(this).parent().attr('href'));
		}
		else {
			redirection('#!idSujet='+$(this).attr('idSujet')+'_page='+$(this).attr('page'));
		}
	});
	$('.lienPage').live('mouseenter', function() { $(this).addClass('rouge') }).live('mouseleave', function() { $(this).removeClass('rouge') });

	$('.last').live('mouseenter', function() { startToolTip('Cliquez-ici pour aller directement au dernier message.'); }).live('mouseleave', function() { stopToolTip(); });
	$('.lienDirect').live('mouseenter', function() { startToolTip('Lien direct vers ce message.'); }).live('mouseleave', function() { stopToolTip(); });
	$('.voirMsn').live('mouseenter', function() { startToolTip('Voir son adresse MSN.'); }).live('mouseleave', function() { stopToolTip(); });
	$('.modifierCeMessage').live('mouseenter', function() { startToolTip('Modifier ce message.'); }).live('mouseleave', function() { stopToolTip(); });
	$('.supprimerCeMessage').live('mouseenter', function() { startToolTip('Supprimer ce message.'); }).live('mouseleave', function() { stopToolTip(); });
	$('.citerCeMessage').live('mouseenter', function() { startToolTip('Citer ce message.'); }).live('mouseleave', function() { stopToolTip(); });
	$('.signalerCeMessage').live('mouseenter', function() { startToolTip('<b>Signaler le post ou le topic</b>'); }).live('mouseleave', function() { stopToolTip(); });

	$('#administrerSujet').live('click', function()	{
			$('#formulaire').html('<img src="../images/divers/chargement.gif" />');
			$.post(
				'./amneroth_ajax.php',
				{
					action: 'getFormAdministrerSujet',
					idSujet: $(this).attr('idSujet')
				},
				function(data) {
					$('#formulaire').html(data);
				}
			);
		}
	);

});

function getFormPost(idSujet, txtareaid, isRp, callback) {
	var form = txtareaid==1 ? '' : 2;
	callback = callback==undefined ? '' : callback;
	$('#formulaire'+form).html('<img src="../images/divers/chargement.gif" />');
	ajax('./amneroth_ajax.php', 'formulaire'+form, 'action=getFormPost&idSujet='+idSujet+'&txtareaid='+txtareaid+'&isRp='+isRp, 'setTxtarea(\'txtarea'+txtareaid+'\');'+callback);
}
function verrouiller(idSujet) {
	ajax('./amneroth_ajax.php', '', 'action=administrerAction&adminAction=verrouiller&idSujet='+idSujet, 'getSujet('+idSujet+', 1);');
}
function postIt(idSujet)	{
	ajax('./amneroth_ajax.php', '', 'action=administrerAction&adminAction=postit&idSujet='+idSujet, 'getSujet('+idSujet+', 1);');
}
function sujetNormal(idSujet)	{
	ajax('./amneroth_ajax.php', '', 'action=administrerAction&adminAction=sujetNormal&idSujet='+idSujet, 'getSujet('+idSujet+', 1);');
}
function rp(idSujet)	{
	ajax('./amneroth_ajax.php', '', 'action=administrerAction&adminAction=rp&idSujet='+idSujet, 'getSujet('+idSujet+', 1);');
}
function switchOnlyAdmin(idSujet)	{
	ajax('./amneroth_ajax.php', '', 'action=administrerAction&adminAction=switchOnlyAdmin&idSujet='+idSujet, 'getSujet('+idSujet+', 1);');
}
function deplacerSujet(idSujet)	{
	ajax('./amneroth_ajax.php', '', 'action=administrerAction&adminAction=deplacerSujet&idSujet='+idSujet+'&newCat='+get('newCat').value, 'getSujet('+idSujet+', 1);');
}

function setIdFiche(newPersoId)	{
	persoId = newPersoId;
}

function getCat(idCat, page)	{
	goTo('top');
	if(page<1 || page=='undefined')
		page=1;
	ajax('./amneroth_ajax.php', 'amneroth', 'action=getCat&idCat='+idCat+'&page='+page);
	reinitialiserListeChecked();
	refreshNbConnectes();
}
function getSujet(idSujet, page, focusLastPost)	{
	var callback = (focusLastPost) ? 'get(\'focus\'+get(\'lastPost\').innerHTML).focus();' : '';

	goTo('top');
	ajax('./amneroth_ajax.php', 'amneroth', 'action=getSujet&idSujet='+idSujet+'&page='+page, callback);
	reinitialiserListeChecked();
	refreshNbConnectes();
}
function getIndex(goToTop)	{
	if(goToTop!=false) goTo('top');
	ajax('./amneroth_ajax.php', 'amneroth', 'action=getIndex&typePartie='+typePartie);
	reinitialiserListeChecked();
	refreshNbConnectes();
}
function ajoutSujet(idCat)	{
	goTo('top');
	var onlyAdmin = 0;
	if(get('onlyAdmin'))
		onlyAdmin = get('onlyAdmin').checked ? 1 : 0;

	var rp = 0;
	if(get('rp'))
		rp = get('rp').checked ? 1 : 0;

	var persoId = 0;
	if(get('persoId'))
		persoId = get('persoId').value;

	var position = 0;
	if(get('position'))
		position = get('position').value;

	var sondage = '';
	if(get('sondageQuestion'))	{
		sondage += '&sondageQuestion='+escape(get('sondageQuestion').value);
		for(var i=0; i<10; i++)	{
			if(get('sondageReponse'+i))
				sondage += '&sondageReponse'+i+'='+escape(get('sondageReponse'+i).value);
		}
	}

	ajax('./amneroth_ajax.php', 'formSujet', 'action=ajoutSujet&idCat='+idCat+'&nomSujet='+escape(get('nomSujet').value)+'&contenuPost='+escape(get('textarea').value.replace(/\+/g,'[plus]'))+'&onlyAdmin='+onlyAdmin+'&rp='+rp+'&persoId='+persoId+'&position='+position+sondage, 'setTimeout("getCat('+idCat+', 1)", 1000);');
	reinitialiserListeChecked();
	refreshNbConnectes();
}
function ajoutPost(idSujet)	{
	goTo('top');
	var perso = '';
	var formPost = (txtId=='txtarea1') ? 'formPost1' : 'formPost2';
	if(get('persoId'))
		perso = '&idFiche='+persoId;
	ajax('./amneroth_ajax.php', formPost, 'action=ajoutPost&idSujet='+idSujet+'&contenuPost='+escape(get(txtId).value.replace(/\+/g,'[plus]'))+perso, 'setTimeout("getSujet('+idSujet+', \'last\', true);", 1000);');
	reinitialiserListeChecked();
	refreshNbConnectes();
}
function getFormEditPost(idPost)	{
	ajax('./amneroth_ajax.php', 'contenuPost'+idPost, 'action=getFormEditPost&idPost='+idPost);
	reinitialiserListeChecked();
	refreshNbConnectes();
}
function getCitation(idSujet, txtareaid, isRp, idPost) {
	var callback = "ajax('./amneroth_ajax.php', '', 'action=getCitation&idPost="+idPost+"', 'if(get(\\\'txtarea2\\\').value==\\\'Votre message\\\')get(\\\'txtarea2\\\').value=\\\'\\\'; get(\\\'txtarea2\\\').value=get(\\\'txtarea2\\\').value+xhr.responseText; get(\\\'formPost2\\\').style.display=\\\'block\\\'; get(\\\'txtarea2\\\').focus();');";
	if(!get('txtarea2')) {
		getFormPost(idSujet, txtareaid, isRp, callback);
	}
	else {
		eval(callback);
	}
}
function editerPost(idPost)	{
	var perso = '';
	var callRedirection = '';
	if(get('persoId'+idPost))	{
		perso = '&idFiche='+get('persoId'+idPost).value;
		callRedirection += "redirection('./amneroth.php?idPost="+idPost+"');";
	}
	ajax('./amneroth_ajax.php', 'contenuPost'+idPost, 'action=editerPost&idPost='+idPost+'&contenuPost='+escape(get('textareaContenuPost'+idPost).value.replace(/\+/g,'[plus]'))+perso, callRedirection);
	reinitialiserListeChecked();
	refreshNbConnectes();

}
function supprimerPost(idPost, idSujet)	{
	if(confirm('Vous allez supprimer ce message, confirmez-vous cette action ?'))
		ajax('./amneroth_ajax.php', 'contenuPost'+idPost, 'action=supprimerPost&idPost='+idPost, 'setTimeout("getSujet('+idSujet+', \'last\')", 1000);');
	reinitialiserListeChecked();
	refreshNbConnectes();
}
function supprimerSujet(idSujet)	{
	if(confirm('Vous allez supprimer ce sujet, et tous les messages qu\'il contient. Etes-vous certain ?'))
		ajax('./amneroth_ajax.php', 'titreSujet', 'action=supprimerSujet&idSujet='+idSujet);
	reinitialiserListeChecked();
	refreshNbConnectes();
}
function signaler(idPost)	{
	if(confirm('Voulez-vous vraiment signaler ce message ?'))	{
		ajax('./amneroth_ajax.php', '', 'action=signaler&idPost='+idPost, 'openPopUp(xhr.responseText, true, true, 20);');
	}
}
function switchMode()	{
	ajax('./amneroth_ajax.php', 'amneroth', 'action=switchMode', 'setTimeout("redirection(\'./amneroth.php\')", 1);');
	reinitialiserListeChecked();
	refreshNbConnectes();
}
function voter(idSujet, page)	{
	for(var i=0; true; i++)	{
		if(get('choix'+i).checked)	{
			var choix = get('choix'+i).value;
			break;
		}
	}
	ajax('./amneroth_ajax.php', 'sondage', 'action=voter&idChoix='+escape(choix), 'setTimeout("getSujet('+idSujet+', '+page+')", 1000);', 'true');
	reinitialiserListeChecked();
	refreshNbConnectes();
}
function changeType(type)	{
	if(type=='general' || type=='guildes' || type=='equipe')
		typePartie = type;
	getIndex();
	reinitialiserListeChecked();
}

function refreshNbConnectes()	{
	if(get('nbConnectesForum'))
		ajax('./amneroth_ajax.php', 'nbConnectesForum', 'action=refreshNbConnectes', '', 'true');
}

function recherche()	{
	ajax('./amneroth_ajax.php', 'amneroth', 'action=recherche');
	reinitialiserListeChecked();
	refreshNbConnectes();
}
function rechercheValidation(recherchePage)	{
    var advanced = $('#advanced').is(':checked') ? 1 : 0;
	ajax('./amneroth_ajax.php', 'amneroth', 'action=rechercheResult&rechercheType='+escape(get('rechercheType').value)+'&rechercheString='+escape(get('rechercheString').value)+'&rechercheWhere='+escape(get('rechercheWhere').value)+'&rechercheAdvanced='+advanced+'&recherchePage='+recherchePage);
	reinitialiserListeChecked();
	refreshNbConnectes();
}
function hideAdvancedCheckBox(value) {
    if(value == "contenu")
        $("#advanced").removeClass("cache");
    else
        $("#advanced").addClass("cache");
}
function editerTitre(idSujet, nomSujet)	{
	var titre = prompt('Titre du sujet', nomSujet);
	if(titre.length!=nomSujet)
		ajax('./amneroth_ajax.php', 'titreSujet', 'action=editerTitre&idSujet='+idSujet+'&titre='+escape(titre));
	reinitialiserListeChecked();
	refreshNbConnectes();
}
function toutMarquerCommeLu()	{
	ajax('./amneroth_ajax.php', 'toutMarquerCommeLu', 'action=toutMarquerCommeLu&type='+typePartie);
	getIndex();
	reinitialiserListeChecked();
	refreshNbConnectes();
}
function marquerCatCommeLue(idCat)	{
	ajax('./amneroth_ajax.php', 'marquerCatCommeLue', 'action=marquerCatCommeLue&idCat='+idCat);
	getCat(idCat, 1);
	reinitialiserListeChecked();
	refreshNbConnectes();
}
function administrer(n)	{
	display('liensConfigurationAdmin');
	for(var i=0; i<n; i++)	{
		if(get('tdSujet'+i))	{
			display('tdSujet'+i);
		}
	}
}

var listeChecked = Array();
function check(idSujet)	{
	if(contains(listeChecked, idSujet))
		listeChecked.splice(listeChecked.indexOf(idSujet), 1);
	else
		listeChecked.push(idSujet);
}
function reinitialiserListeChecked()	{
	listeChecked = Array();
	persoId = 0;
}
function administrerAction(idCat, action)	{
	var liste = '';
	for(i=0; i<listeChecked.length; i++)	{
		liste += '&listeChecked['+i+']='+listeChecked[i];
	}

	if(action=='deplacer')	{
		if(get('newCat'))
			liste += '&newCat='+escape(get('newCat').value);
	}

	ajax('./amneroth_ajax.php', 'liensConfigurationAdmin', 'action=administrerAction&adminAction='+action+'&idCat='+idCat+liste, 'getCat('+idCat+', 1); reinitialiserListeChecked();');
}
function afficherPartieGuilde(idGuilde)	{
	display('guilde'+idGuilde);

	var choix = '';
	for(i=0, j=0; i<arrayGuildes.length; i++)	{
		if(get('checkbox'+arrayGuildes[i]).checked)	{
			choix += '&choix['+j+']='+arrayGuildes[i];
			j++;
		}
	}
	ajax('./amneroth_ajax.php', '', 'action=guildeAffichage'+choix);
}

function reduction(idPartie)	{
	ajax('./amneroth_ajax.php', '', 'action=reduction&idPartie='+idPartie);
	getIndex(false);
}

/**
 * Votes RP
 */
function addPointAppreciation(idPost) {
	ajax('./amneroth_ajax.php', '', 'action=addPointAppreciation&idPost='+idPost, 'redirection(\'./amneroth.php?idPost='+idPost+'\');');
}

