var username_checked = false; // retiens si le username a ete checké ou pas depuis changement (dans le formulaire d'envoi de mail)

$(document).ready(function() {
	// BOOK MARK
	// add a "rel" attrib if Opera 7+
    if(window.opera) {
        if ($("a.jqbookmark").attr("rel") != ""){ // don't overwrite the rel attrib if already set
            $("a.jqbookmark").attr("rel","sidebar");
        }
    }
    $("a.icon.bookmark").click(function(event){
        event.preventDefault(); // prevent the anchor tag from sending the user off to the link
        var url = this.href;
        var title = 'WowTeamFinder.com - Recherche de joueurs d\'arène dans World of Warcraft';

        if (window.sidebar) { // Mozilla Firefox Bookmark
            window.sidebar.addPanel(title, url, "");
        } else if( window.external ) { // IE Favorite
            window.external.AddFavorite( url, title);
        } else if(window.opera) { // Opera 7+
            return false; // do nothing - the rel="sidebar" should do the trick
        } else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
            alert('Unfortunately, this browser does not support the requested action, please bookmark this page manually.');
        }
		return false;
    });

	// repositionnement du popup si affiché	
	$(window).resize(function() { centerPopup(); });
	$(window).scroll(function() { centerPopup(); });
	$("#popup a").live('click', function() {

		var ok_click = $(this).hasClass('ok');
		var ok_url = $(this).attr('href');

		$.ajax({
			type: "POST",
			url: "/"+curLang+"/ajax/popup-ask-certification-shown/",
			success: function(content){
				var result=eval('('+content+')');

				if( ok_click ) {
					document.location.href = ok_url;
				}

				removePopup();
			}
		});

		
		
		return false;
	});
	
	
	// Lance une routine pour l'animation du nombre de nouveau message dans l'espace membre
	animateNbNewMessages();


	// Désactive le bouton "ajouté" de personange de l'équipe
	changeCharacterAddButtonStatus();

	// On check si on doit desactiver ou non le bouton de choix de faction
	updateSwitchFactionButton();
	// On check si on doit desactiver ou non la liste de choix des serveurs
	updateSwitchServersList();

	// Affichage/masquage des ajout de joueurs
	updateShowHideCharactersSetup();
	
	// On cache tous les searchResultDetail
	$("tr[id^=searchResultDetail-]").hide();

	// On change le type de curseur pour les liste d'annonce
	$("tr[id^=searchResult-]").css('cursor', 'pointer');

	/*
	 *	Garni le combo race sur basede la faction choisie
	 */
	$("#post-faction").live('change keyup', function() {
		var faction = $(this).val();

		$("#post-faction-hidden").val(faction);
		
		// On affiche le loader
		showLoader();

		$.ajax({
			type: "POST",
			url: "/"+curLang+"/ajax/get-races-by-faction/",
			data: "faction=" + faction,
			success: function(content){
				var result=eval('('+content+')');


				// Current mate
				var html = '';
				html = html + '<option value="0">-- '+arrLang.editPost_doYourChoice+' --</option>';
				$("#cur-character-add-race").html( html );
				for(i=0; i<result.races.length; i++) {
					var race_id = result.races[i].race.id;
					var race_name = result.races[i].race["name_"+curLang];
					var race_rewrite = result.races[i]._VARS_.race_rewrite;
					html = html + '<option value="'+race_id+'" id="cur-character-add-race_'+race_rewrite+'">'+race_name+'</option>';
				}
				$("#cur-character-add-race").html( html );

				// Wanted mates
				html = '';
				html = html + '<option value="0">-- '+arrLang.editPost_notImportant+' --</option>';
				$("#cur-character-wanted-add-race").html( html );
				for(i=0; i<result.races.length; i++) {
					var race_id = result.races[i].race.id;
					var race_name = result.races[i].race["name_"+curLang];
					var race_rewrite = result.races[i]._VARS_.race_rewrite;
					html = html + '<option value="'+race_id+'" id="cur-character-wanted-add-race_'+race_rewrite+'">'+race_name+'</option>';
				}
				$("#cur-character-wanted-add-race").html( html );

				// On charge les perso du joueur associés a la faction et le serveur sélectionné
				updateUserCharactersList();

				// On arrête le loader
				hideLoader();
			}
		});

		updateShowHideCharactersSetup();


	});

	$("#server-id").live('change keyup', function() {
		$("#post-server-hidden").val( $(this).val() );

		updateUserCharactersList();
		updateShowHideCharactersSetup();
	});



	/*
	 *	Agrandit / Diminute le champ description
	 */

	$("#content .frm fieldset p textarea.field").live('focus', function() {
		$(this).animate({height: 200},500);
	});
	$("#content .frm fieldset p textarea.field").live('blur', function() {
		$(this).animate({height: 80},500);
	});

	/*
	 *	Garni la lsite des classe en fonction de la race choisie
	 */
	$("#cur-character-add-race").live('change', function() {
		var id_race = $("#cur-character-add-race").val();
		var default_html = '<option value="0">-- '+arrLang.editPost_doYourChoice+' --</option>';
		
		$("#cur-character-add-class").html(default_html);

		// On affiche le loader
		showLoader();

		$.ajax({
			type: "POST",
			url: "/"+curLang+"/ajax/get-classes-by-race/",
			data: "id_race=" + id_race,
			success: function(content){						
				var result=eval('('+content+')');				
				
				var html = '';
				html = html + default_html;
				
				$("#cur-character-add-specialisation").html( html );
				
				for(i=0; i<result.classes.length; i++) {
					var class_id = result.classes[i].classtype.id;
					var class_name = result.classes[i].classtype["name_"+curLang];
					var class_rewrite = result.classes[i]._VARS_.class_rewrite;
					
					html = html + '<option value="'+class_id+'" id="cur-character-add-class_'+class_rewrite+'">'+class_name+'</option>';
				}
				$("#cur-character-add-class").html( html );
				changeCharacterAddButtonStatus();

				// On arrête le loader
				hideLoader();
			}
		});			
	});	
	$("#cur-character-wanted-add-race").live('change', function() {
		updateCharactersWantedClassList();
	});


	 
	/*
	 *	Garni la lsite des spécialisation en fonction de la classe choisie
	 */
	
	$("#cur-character-add-class").live('change', function() {
		var id_classtype = $("#cur-character-add-class").val();
		var default_html = '<option value="0">-- '+arrLang.editPost_doYourChoice+' --</option>';
		
		$("#cur-character-add-specialisation").html(default_html);

		// On affiche le loader
		showLoader();

		$.ajax({
			type: "POST",
			url: "/"+curLang+"/ajax/get-specialisations-by-class/",
			data: "id_classtype=" + id_classtype,
			success: function(content){						
				var result=eval('('+content+')');				
				
				var html = '';
				html = html + default_html;
				
				for(i=0; i<result.specialisations.length; i++) {
					var spe_id = result.specialisations[i].specialisation.id;
					var spe_name = result.specialisations[i].specialisation["name_"+curLang];					
					var spe_rewrite = result.specialisations[i]._VARS_.spe_rewrite;

					html = html + '<option value="'+spe_id+'" id="cur-character-add-specialisation_'+spe_rewrite+'">'+spe_name+'</option>';
				}
				$("#cur-character-add-specialisation").html( html );
				changeCharacterAddButtonStatus();

				// On arrête le loader
				hideLoader();
			}
		});			
	});

	$("#cur-character-add-specialisation").live('change', function() {
		changeCharacterAddButtonStatus();
	});

	$("#cur-character-wanted-add-class").live('change', function() {
		var id_classtype = $("#cur-character-wanted-add-class").val();
		var default_html = '<option value="0">-- '+arrLang.editPost_notImportant+' --</option>';
		
		$("#cur-character-wanted-add-specialisation").html(default_html);

		// On affiche le loader
		showLoader();

		$.ajax({
			type: "POST",
			url: "/"+curLang+"/ajax/get-specialisations-by-class/",
			data: "id_classtype=" + id_classtype,
			success: function(content){						
				var result=eval('('+content+')');				
				
				var html = '';
				html = html + default_html;
				
				for(i=0; i<result.specialisations.length; i++) {
					var spe_id = result.specialisations[i].specialisation.id;
					var spe_name = result.specialisations[i].specialisation["name_"+curLang];
					var class_rewrite = result.specialisations[i]._VARS_.class_rewrite;
					var spe_rewrite = result.specialisations[i]._VARS_.spe_rewrite;
					
					html = html + '<option value="'+spe_id+'" id="cur-character-wanted-add-specialisation_'+spe_rewrite+'">'+spe_name+'</option>';
				}
				$("#cur-character-wanted-add-specialisation").html( html );

				// On arrête le loader
				hideLoader();
			}
		});			
	});
	
	/*
	 *	Supprimer un character
	 */
	
	$("#frmAddPost fieldset.team-mates-current table tr td a.del-cur-character").live('click', function() {		
		$(this).parent().parent().fadeOut(200, function() {
			$(this).remove();
			
			// On retire le body si plus d'enregistrement (bug css sinon)
			if ( $("#frmAddPost fieldset.team-mates-current table tbody tr").length < 1){
				$("#frmAddPost fieldset.team-mates-current table tbody").remove();				
			}
			updateSwitchFactionButton();
			updateSwitchServersList();
		});
		
		return false;
	});
	$("#frmAddPost fieldset.team-mates-wanted table tr td a.del-cur-character").live('click', function() {		
		$(this).parent().parent().fadeOut(200, function() {
			$(this).remove();
			
			// On retire le body si plus d'enregistrement (bug css sinon)
			if ( $("#frmAddPost fieldset.team-mates-wanted table tbody tr").length < 1){
				$("#frmAddPost fieldset.team-mates-wanted table tbody").remove();
			}
			updateSwitchFactionButton();
			updateSwitchServersList();
		});
		
		return false;
	});
	
	/*
	 *	Ajout d'un character courant
	 */
	
	$("#copy-user-character-in-team").live('change keyup', function() {

		// 1. On récupère les informations du perso en ajax
		var id_user_character = $(this).val();

		// On affiche le loader
		showLoader();

		$.ajax({
			type: "POST",
			url: "/"+curLang+"/ajax/get-user-character/",
			data: "id_user_character=" + id_user_character,
			success: function(content){
				var result=eval('('+content+')');

				var char_name = result.user_character.name;
				var guild_name = result.user_character.guild_name;
				var race_name = result.race['name_'+curLang];
				var race_rewrite = result.race.rewrite;
				var race_id = result.race.id;
				var classtype_name = result.classtype['name_'+curLang];
				var classtype_rewrite = result.classtype.rewrite;
				var spe_name = result.specialisation['name_'+curLang];
				var spe_rewrite = result.specialisation.rewrite;
				var spe_id = result.specialisation.id;
				var xp = result.user_character.xp;

				var html = '';

				// On check si il ya un tbody
				if ( $("#frmAddPost fieldset.team-mates-current table tbody").length < 1){
					html = html + '<tbody>';
				}

				html = html + '<tr>';
				html = html + '	<td class="name">';
				html = html + '		<span class="name">' + char_name + ' ' + (guild_name.length > 0 ? '&lt;'+guild_name+'&gt;' : '') + '</span>';
				html = html + '		<input type="hidden" name="character[id_user_character][]" value="'+id_user_character+'" />';
				html = html + '	</td>';
				html = html + '	<td class="id-race">';
				html = html + '		<span class="icon-race ' + race_rewrite + '">';
				html = html + '			<span>'+race_name.replace(/ /g, '&nbsp;')+'</span>';
				html = html + '		</span>';
				html = html + '		<input type="hidden" name="character[id_race][]" value="'+race_id+'" />';
				html = html + '	</td>';
				html = html + '	<td class="id-classtype">';
				html = html + '		<span class="icon-classtype ' + classtype_rewrite + '">';
				html = html + '			<span>'+classtype_name.replace(/ /g, '&nbsp;')+'</span>';
				html = html + '		</span>';
				html = html + '	</td>';
				html = html + '	<td class="id-specialisation">';
				html = html + '		<span class="icon-specialisation ' + classtype_rewrite + '-' + spe_rewrite + '">';
				html = html + '			<span>'+spe_name.replace(/ /g, '&nbsp;')+'</span>';
				html = html + '		</span>';
				html = html + '		<input type="hidden" name="character[id_specialisation][]" value="'+spe_id+'" />';
				html = html + '	</td>';
				html = html + '	<td><span>'+(xp > 0 ? xp+'+' : '-')+'</span><input type="hidden" name="character[xp][]" value="'+xp+'" /></td>';
				html = html + '	<td><a class="del-cur-character" href="#"><span>'+arrLang.editPost_playersWanted_remove+'</span></a></td>';
				html = html + '</tr>';

				// On check si il ya un /tbody
				if ( $("#frmAddPost fieldset.team-mates-current table tbody").length < 1){
					html = html + '</tbody>';
				}

				// On remet la valeur par défaut du select
				$("#copy-user-character-in-team").val(0);

				$("#frmAddPost fieldset.team-mates-current table").append( html );

				updateSwitchFactionButton();
				updateSwitchServersList();

				// On arrête le loader
				hideLoader();
			}
		});



		
	});


	/*
	 *	Ajout d'un character
	 */

	$("#cur-character-add-button").live('click', function() {
		var race_name = $("#cur-character-add-race :selected").text();
		var race_rewrite = $("#cur-character-add-race :selected").attr('id').split('_')[1];
		var race_id = $("#cur-character-add-race").val();
		var classtype_name = $("#cur-character-add-class :selected").text();
		var classtype_rewrite = $("#cur-character-add-class :selected").attr('id').split('_')[1];
		var classtype_id = $("#cur-character-add-class").val();
		var spe_name = $("#cur-character-add-specialisation :selected").text();
		var spe_rewrite = $("#cur-character-add-specialisation :selected").attr('id').split('_')[1];
		var spe_id = $("#cur-character-add-specialisation").val();
		var xp = $("#cur-character-add-xp").val();
		var html = '';

		// On check si il ya un tbody
		if ( $("#frmAddPost fieldset.team-mates-current table tbody").length < 1){
			html = html + '<tbody>';
		}

		html = html + '<tr>';
		html = html + '	<td>-</td>';
		html = html + '	<td class="id-race">';
		html = html + '		<span class="icon-race ' + race_rewrite + '">';
		html = html + '			<span>'+race_name.replace(/ /g, '&nbsp;')+'</span>';
		html = html + '		</span>';
		html = html + '		<input type="hidden" name="character[id_race][]" value="'+race_id+'" />';
		html = html + '	</td>';
		html = html + '	<td class="id-classtype">';
		html = html + '		<span class="icon-classtype ' + classtype_rewrite + '">';
		html = html + '			<span>'+classtype_name.replace(/ /g, '&nbsp;')+'</span>';
		html = html + '		</span>';
		html = html + '		<input type="hidden" name="character[id_classtype][]" value="'+classtype_id+'" />';
		html = html + '	</td>';
		html = html + '	<td class="id-specialisation">';
		html = html + '		<span class="icon-specialisation ' + classtype_rewrite + '-' + spe_rewrite + '">';
		html = html + '			<span>'+spe_name.replace(/ /g, '&nbsp;')+'</span>';
		html = html + '		</span>';
		html = html + '		<input type="hidden" name="character[id_specialisation][]" value="'+spe_id+'" />';
		html = html + '	</td>';
		html = html + '	<td><span>'+(xp > 0 ? xp+'+' : '-')+'</span><input type="hidden" name="character[xp][]" value="'+xp+'" /></td>';
		html = html + '	<td><a class="del-cur-character" href="#"><span>'+arrLang.editPost_currentTeamComposition_remove+'</span></a></td>';
		html = html + '</tr>';

		// On check si il ya un tbody
		if ( $("#frmAddPost fieldset.team-mates-current table tbody").length < 1){
			html = html + '</tbody>';
		}

		$("#frmAddPost fieldset.team-mates-current table").append( html );
		updateSwitchFactionButton();
		updateSwitchServersList();

		addCharacterResetFields();

	});


	/*
	 *	Ajout d'un character recherché
	 */
	
	$("#cur-character-wanted-add-button").live('click', function() {		
		var race_name = $("#cur-character-wanted-add-race :selected").text();
		var race_rewrite = $("#cur-character-wanted-add-race :selected").attr('id').split('_')[1];
		var race_id = $("#cur-character-wanted-add-race").val();
		if(race_id < 1) {
			race_name = '('+arrLang.editPost_notImportant+')';
			race_rewrite = 'all';
		}		
		var classtype_name = $("#cur-character-wanted-add-class :selected").text();
		var classtype_rewrite = $("#cur-character-wanted-add-class :selected").attr('id').split('_')[1];
		var classtype_id = $("#cur-character-wanted-add-class").val();		
		if(classtype_id < 1) {
			classtype_name = '('+arrLang.editPost_notImportant+')';
			classtype_rewrite = 'all';
		}
		var spe_name = $("#cur-character-wanted-add-specialisation :selected").text();
		var spe_rewrite = $("#cur-character-wanted-add-specialisation :selected").attr('id').split('_')[1];
		var spe_id = $("#cur-character-wanted-add-specialisation").val();
		if(spe_id < 1) {
			spe_name = '('+arrLang.editPost_notImportant+')';
			spe_rewrite = 'all';
		}
		var html = '';

		// On check si il ya un tbody
		if ( $("#frmAddPost fieldset.team-mates-wanted table tbody").length < 1){
			html = html + '<tbody>';
		}

		html = html + '<tr>';
		html = html + '	<td class="id-race">';
		html = html + '		<span class="icon-race ' + race_rewrite + '">';
		html = html + '			<span>'+race_name.replace(/ /g, '&nbsp;')+'</span>';
		html = html + '		</span>';
		html = html + '		<input type="hidden" name="character_wanted[id_race][]" value="'+race_id+'" />';
		html = html + '	</td>';
		html = html + '	<td class="id-classtype">';
		html = html + '		<span class="icon-classtype ' + classtype_rewrite + '">';
		html = html + '			<span>'+classtype_name.replace(/ /g, '&nbsp;')+'</span>';
		html = html + '		</span>';
		html = html + '		<input type="hidden" name="character_wanted[id_classtype][]" value="'+classtype_id+'" />';
		html = html + '	</td>';
		html = html + '	<td class="id-specialisation">';
		html = html + '		<span class="icon-specialisation ' + (spe_id < 1 ? 'all' : classtype_rewrite) + '-' + spe_rewrite + '">';
		html = html + '			<span>'+spe_name.replace(/ /g, '&nbsp;')+'</span>';
		html = html + '		</span>';
		html = html + '		<input type="hidden" name="character_wanted[id_specialisation][]" value="'+spe_id+'" />';
		html = html + '	</td>';
		html = html + '	<td><a class="del-cur-character" href="#"><span>'+arrLang.editPost_currentTeamComposition_remove+'</span></a></td>';
		html = html + '</tr>';

		// On check si il ya un tbody
		if ( $("#frmAddPost fieldset.team-mates-wanted table tbody").length < 1){
			html = html + '</tbody>';
		}

		$("#frmAddPost fieldset.team-mates-wanted table").append( html );
		updateSwitchFactionButton();
		updateSwitchServersList();

		addCharacterWantedResetFields();

	});

	// click sur des céllules de resultats
	$("#searchResult tbody tr[id^=searchResult-] td").live('click', function() {
		var id_post = $(this).parent().attr('id').split('-')[1];		
		document.location.href = '/' + curLang + '/ad/' + id_post + '/';
	});

	$("#searchResult .search-filter").live('change', function() {
		// Si on change la zone on reinitialize le serveur
		if( $(this).attr('id') == 'search-filter-id-serverzone') {
			$("#search-filter-id-server").val(0);
		}

		var qs = '';

		var faction				= $("#search-filter-faction").val();		
		var id_serverzone		= $("#search-filter-id-serverzone").val();
		var id_battlegroup		= $("#search-filter-id-battlegroup").val();
		var id_server			= $("#search-filter-id-server").val();
		var id_teamtype			= $("#search-filter-id-teamtype").val();
		var xp_ranking			= $("#search-filter-xp-ranking").val();
		var id_spec				= 0;
		var id_spec_wanted		= 0;
		var id_classtype		= 0;
		var id_classtype_wanted = 0;



		// Classtype ou Spec
		if( $("#search-filter-id-specialisation :selected").hasClass('classtype') ) {
			id_classtype = $("#search-filter-id-specialisation").val();			
		}
		else {
			id_spec			= $("#search-filter-id-specialisation").val();
		}

		if( $("#search-filter-id-specialisation-wanted :selected").hasClass('classtype') ) {
			id_classtype_wanted = $("#search-filter-id-specialisation-wanted").val();
		}
		else {
			id_spec_wanted	= $("#search-filter-id-specialisation-wanted").val();
		}

		
		
		// Faction
		qs = qs + (
			faction != 0   ?
			( ( qs.length > 0 ? '&' : '?' ) + 'faction=' + faction )   :
			''
		);



		// serveurzone
		qs = qs + (
			id_serverzone > 0   ?
			( ( qs.length > 0 ? '&' : '?' ) + 'id_serverzone=' + id_serverzone )   :
			''
		);
		
		// battlegroup
		qs = qs + (
			id_battlegroup > 0   ?
			( ( qs.length > 0 ? '&' : '?' ) + 'id_battlegroup=' + id_battlegroup )   :
			''
		);

		// serveur
		qs = qs + (
			id_server > 0   ?
			( ( qs.length > 0 ? '&' : '?' ) + 'id_server=' + id_server )   :
			''
		);

		// TeamType
		qs = qs + (
			id_teamtype > 0   ?
			( ( qs.length > 0 ? '&' : '?' ) + 'id_teamtype=' + id_teamtype )   :
			''
		);

		// Character spe
		qs = qs + (
			id_spec > 0   ?
			( ( qs.length > 0 ? '&' : '?' ) + 'id_specialisation=' + id_spec )   :
			''
		);
		qs = qs + (
			id_classtype > 0   ?
			( ( qs.length > 0 ? '&' : '?' ) + 'id_classtype=' + id_classtype )   :
			''
		);

		// Character spe wanted
		qs = qs + (
			id_spec_wanted > 0   ?
			( ( qs.length > 0 ? '&' : '?' ) + 'id_specialisation_wanted=' + id_spec_wanted )   :
			''
		);
		qs = qs + (
			id_classtype_wanted > 0   ?
			( ( qs.length > 0 ? '&' : '?' ) + 'id_classtype_wanted=' + id_classtype_wanted )   :
			''
		);
		qs = qs + (
			xp_ranking > 0   ?
			( ( qs.length > 0 ? '&' : '?' ) + 'xp_ranking=' + xp_ranking )   :
			''
		);
		
		var url = document.location.href.split('?')[0];
		
		document.location.href = url + qs;
	});

	/*$("#searchResult tbody tr td, #searchResultDetail-"+id_post).live('mouseleave', function() {
		var id_post = $(this).parent().attr('id').split('-')[1];
		$("#searchResultDetail-"+id_post).fadeOut();
	});*/

	// Click sur captcha
	$("#captcha-pic").live('click', function() {
		var rndNum = parseInt(Math.random() *100000000);
		$("#captcha-pic").attr('src', '/captcha.jpg?' + rndNum);
		return false;
	});

	//Click sur liens d'armu
	$(".new-window").live('click', function() {
		window.open($(this).attr('href'));
		return false;
	});


	//
	//	USER :: CHARACTERS
	//

	// Update la liste des classe 
	$("#user-character-id-race").live('change keyup', function() {
		updateUserCharactersClassList();
	});
	
	// Update la liste des spé 
	$("#user-character-id-class").live('change keyup', function() {
		updateUserCharactersSpecialisationList();
	});

	// Suppression d'un perso de la liste du user'
	$("#addUserCharacters tbody tr td a.del-cur-character").live('click', function() {
		var id_user_character = $(this).parent().parent().attr('id').split('-')[2];
		if( confirm('Voulez-vous vraiment effacer ce personnage ?') ) {
			// On affiche le loader
			showLoader();

			$.ajax({
				type: "POST",
				url: "/"+curLang+"/ajax/del-user-character/",
				data: "id_user_character=" + id_user_character,
				success: function(content){
					var result=eval('('+content+')');
					var charDeleted = result.deleted;
					if( charDeleted ) {
						$("#user-character-" + id_user_character).remove();
					}
					else {
						alert('Session expired');
					}

					// On arrête le loader
					hideLoader();
				}
			});
		}
		return false;
	});


	//
	//	USER :: ADS
	//

	// Active ou désactive une annonce
	$("a[id^=enable-disable-ad-]").live('click', function() {
		var id_post = $(this).attr('id').split('-')[3];
		var status = $(this).hasClass('status-enabled');

		// On affiche le loader
		showLoader();


		$.ajax({
			type: "POST",
			url: "/"+curLang+"/ajax/"+(status ? 'disable' : 'enable')+"-post/",
			data: "id_post=" + id_post,
			success: function(content){
				var result=eval('('+content+')');
				$("#enable-disable-ad-"+id_post).attr('class', 'status-' + (status ? 'disabled' : 'enabled') );

				// On arrête le loader
				hideLoader();
			}
		});
	});

	// Suppression d'une annonce'
	$("a[id^=delete-ad-]").live('click', function() {
		var id_post = $(this).attr('id').split('-')[2];

		if( confirm('Confirmez-vous l\'effacement de cette annonce ?') ) {

			// On affiche le loader
			showLoader();

			$.ajax({
				type: "POST",
				url: "/"+curLang+"/ajax/delete-post/",
				data: "id_post=" + id_post,
				success: function(content){
					var result=eval('('+content+')');

					$("#delete-ad-"+id_post).parent().parent().remove(); // On retire la ligne

					// On arrête le loader
					hideLoader();
				}
			});
		}
		return false;
	});

	// Réup d'une annonce
	$("a[id^=reup-ad-]").live('click', function() {
		var id_post = $(this).attr('id').split('-')[2];

		// On affiche le loader
		showLoader();


		$.ajax({
			type: "POST",
			url: "/"+curLang+"/ajax/reup-post/",
			data: "id_post=" + id_post,
			success: function(content){
				var result=eval('('+content+')');
				window.location.reload();

				// On arrête le loader
				hideLoader();
			}
		});
		return false;
	});

	// Suppression du tag VIP
	$("#user-vip-tag").live('dblclick', function() {
		if( prompt('Supprimer ce tag ? (écrivez le mot "DELETE" en toutes lettres)') == 'DELETE' ) {
			// On affiche le loader
			showLoader();

			$.ajax({
				type: "POST",
				url: "/"+curLang+"/ajax/user-delete-vip-tag/",
				success: function(content){
					var result=eval('('+content+')');
					window.location.reload();

					// On arrête le loader
					hideLoader();
				}
			});
		}
	});

	// ALT BG :)
	$("#member-box").after('<div style="left: 623px; top: 83px;" id="member-box-alt-hover"></div>');
	$("#member-box-alt-hover").live('mouseover', function() {
		$("#member-box").css('background', "url(/css/img/menu-member-bg-alt1.png)");
	});
	$("#member-box-alt-hover").live('mouseout', function() {
		$("#member-box").css('background', "url(/css/img/menu-member-bg.png)");
	});

	// Check si le user entré dans le formulaire d'envoi de mail existe ou pas
	$("#frmWriteMail fieldset p input.user_recipient").live('keyup', function() {
		username_checked = false;
	});

	// Déroulement des détails d'un character
	$("#playingCharacters li div.bg.certified").live('click', function() {
		$(this).children('div.details').children('div').hide();
		$(this).children('div.details').children('div.desc').show();
		characterShowHideDetails( $(this) );
	});
	$("#playingCharacters li div.bg.certified .tabs a").live('click', function() {
		if( !$(this).parent().parent().hasClass('big') ) {
			characterShowHideDetails( $(this).parent().parent() );
		}

		var tabClicked = $(this).attr('class');

		//$(this).parent().parent().children('.details div[class~='+tabClicked+']').hide();
		$(this).parent().parent().children('.details').children('div[class!='+tabClicked+']').hide();
		$(this).parent().parent().children('.details').children('div.'+tabClicked).show();

		return false;
	});

	// Effacement d'un message
	$("#lstMessaging tbody tr td a.delete").live('click', function() {
		var id_messaging = $(this).parent().parent().attr('id').split('-')[1];

		// On affiche le loader
		showLoader();

		$.ajax({
			type: "POST",
			url: "/"+curLang+"/ajax/user-delete-mail/",
			data: "id_messaging=" + id_messaging,
			success: function(content){
				var result=eval('('+content+')');
				
				if( result.status ) {					
					$("#messaging-"+id_messaging).remove();
				}

				// On arrête le loader
				hideLoader();
			}
		});
		return false;
	});

	
});

// déroulement des détails d'un character
function characterShowHideDetails(obj) {
	if( obj.hasClass('big') ) {

		$("#playingCharacters li div.bg.certified").css('position', 'relative');
		$("#playingCharacters li div.bg.certified").removeClass('big');
		$("#playingCharacters li div.bg.certified").css('left', 'auto');
		$("#playingCharacters li div.bg.certified").css('top', 'auto');
		$("#playingCharacters li div.bg.certified").css('z-index', 1);
		obj.children(".details").hide();
	} else {
		var curWidth = obj.css('width');
		var curHeight = obj.css('height');
		var curPos = obj.position();

		$("#playingCharacters li div.bg.certified").css('position', 'relative');
		$("#playingCharacters li div.bg.certified").removeClass('big');
		$("#playingCharacters li div.bg.certified").css('left', 'auto');
		$("#playingCharacters li div.bg.certified").css('top', 'auto');
		$("#playingCharacters li div.bg.certified").css('z-index', 1);

		obj.css('z-index', 100000);
		obj.addClass('big');
		obj.css('position', 'absolute');
		obj.css('left', curPos.left);
		obj.css('top', curPos.top);

		obj.children(".details").show();

	}
}

// Check si le username existe en ajax 
function checkFormUsernameExists() {
	
	var username = $("#user-recipient-name").val();

	if( $("#user-recipient-name").attr('id') == 'user-recipient-name'  ) {
		if( !username_checked && username.length > 0 ) {
			// On affiche le loader
			showLoader();

			$.ajax({
				type: "POST",
				url: "/"+curLang+"/ajax/check-username-exists/",
				data: "username=" + username,
				success: function(content){
					var result=eval('('+content+')');
					if( result.exists ) {
						$("#error-user-not-found").fadeOut(200);
					}else{
						$("#error-user-not-found").fadeIn(200);
					}
					// On arrête leloader
					hideLoader();

					username_checked = true;
				}
			});
		}

		setTimeout("checkFormUsernameExists()", 2000);
	}
}

function fadeInSearchResultDetail(id) {
	$("#"+id).fadeIn(250);
}

/*
 *	Check si on peu changer la faction ou pas (si personnage ajouté on peut pas)
 */
/*function updateSwitchFactionButton() {
	var nb_characters = $("#frmAddPost fieldset.team-mates-current table tbody tr, #frmAddPost fieldset.team-mates-wanted table tbody tr").length;
	if ( nb_characters > 0 ) {
		var faction = $("#post-faction :selected").text();
		$("#post-faction").hide();
		if( $("#post-faction-disabled").length < 1 ) {
			$("#post-faction").after('<strong id="post-faction-disabled">'+faction+'</strong>');
		}
		//alert('hide'+nb_characters);
	} 
	else {
		$("#post-faction").show();
		$("#post-faction-disabled").remove();
		//alert('show'+nb_characters);
	}
}*/
function updateSwitchFactionButton() {
	var nb_characters = $("#frmAddPost fieldset.team-mates-current table tbody tr, #frmAddPost fieldset.team-mates-wanted table tbody tr").length;
	if ( nb_characters > 0 ) {
		$("#post-faction").attr('disabled', 'disabled');
	}
	else {
		$("#post-faction").attr('disabled', '');
	}
}

function updateSwitchServersList() {
	var nb_characters = $("#frmAddPost fieldset.team-mates-current table tbody tr").length;
	if ( nb_characters > 0 ) {
		$("#server-id").attr('disabled', 'disabled');
	}
	else {
		$("#server-id").attr('disabled', '');
	}
}



function updateShowHideCharactersSetup() {
	if( $("#post-faction").val() == 0 || $("#server-id").val() == 0 ) {
		$("#addMatesWanted, #addYourTeam").hide();
	} else {
		$("#addMatesWanted, #addYourTeam").fadeIn(500);
	}
}


/*
 * Met à jour la liste des classe de la combo des caracteres recherché 
 */

function updateCharactersWantedClassList() {
	var id_race = $("#cur-character-wanted-add-race").val();
	var default_html = '<option value="0">-- '+arrLang.editPost_doYourChoice+' --</option>';
	
	$("#cur-character-wanted-add-class").html(default_html);

	// On affiche le loader
	showLoader();

	$.ajax({
		type: "POST",
		url: "/"+curLang+"/ajax/get-classes-by-race/",
		data: "id_race=" + id_race + "&all_if_null=1",
		success: function(content){
			var result=eval('('+content+')');

			var html = '';
			html = html + default_html;

			$("#cur-character-wanted-add-specialisation").html( html );

			for(i=0; i<result.classes.length; i++) {
				var class_id = result.classes[i].classtype.id;
				var class_name = result.classes[i].classtype["name_"+curLang];
				var class_rewrite = result.classes[i]._VARS_.class_rewrite;

				html = html + '<option value="'+class_id+'" id="cur-character-wanted-add-class_'+class_rewrite+'">'+class_name+'</option>';
			}
			$("#cur-character-wanted-add-class").html( html );

			// On arrête le loader
			hideLoader();
		}
	});
}


/*
 * Met à jour la liste des classe de la combo des characters
 */

function updateCharactersClassList() {
	var id_race = $("#cur-character-add-race").val();
	var default_html = '<option value="0">-- '+arrLang.editPost_doYourChoice+' --</option>';

	$("#cur-character-add-class").html(default_html);

	// On affiche le loader
	showLoader();

	$.ajax({
		type: "POST",
		url: "/"+curLang+"/ajax/get-classes-by-race/",
		data: "id_race=" + id_race + "&all_if_null=0",
		success: function(content){
			var result=eval('('+content+')');

			var html = '';
			html = html + default_html;

			$("#cur-character-add-specialisation").html( html );

			for(i=0; i<result.classes.length; i++) {
				var class_id = result.classes[i].classtype.id;
				var class_name = result.classes[i].classtype["name_"+curLang];
				var class_rewrite = result.classes[i]._VARS_.class_rewrite;

				html = html + '<option value="'+class_id+'" id="cur-character-add-class_'+class_rewrite+'">'+class_name+'</option>';
			}
			$("#cur-character-add-class").html( html );

			// On arrête le loader
			hideLoader();
		}
	});
}

/*
 * Met à jour la liste des classe de la combo des characters recherché
 */

function updateUserCharactersClassList() {
	var id_race = $("#user-character-id-race").val();
	var default_html = '<option value="0">-- '+arrLang.editPost_doYourChoice+' --</option>';

	$("#user-character-id-class").html(default_html);
	
	// On affiche le loader
	showLoader();

	$.ajax({
		type: "POST",
		url: "/"+curLang+"/ajax/get-classes-by-race/",
		data: "id_race=" + id_race,
		success: function(content){
			
			var result=eval('('+content+')');
			var html = '';
			html = html + default_html;

			$("#user-character-id-specialisation").html( html );
			$("#user-character-id-second-specialisation").html( html );


			for(i=0; i<result.classes.length; i++) {
				var class_id = result.classes[i].classtype.id;
				var class_name = result.classes[i].classtype["name_"+curLang];
				var class_rewrite = result.classes[i]._VARS_.class_rewrite;

				html = html + '<option value="'+class_id+'">'+class_name+'</option>';
			}
			
			$("#user-character-id-class").html( html );

			// On arrête le loader
			hideLoader();
		}
	});
}

/*
 * Met à jour la liste des spé de la combo des caracteres recherché
 */

function updateUserCharactersSpecialisationList() {
	var id_classtype = $("#user-character-id-class").val();
	var default_html = '<option value="0">-- '+arrLang.editPost_doYourChoice+' --</option>';

	$("#user-character-id-specialisation").html(default_html);
	$("#user-character-id-second-specialisation").html( default_html );

	// On affiche le loader
	showLoader();

	$.ajax({
		type: "POST",
		url: "/"+curLang+"/ajax/get-specialisations-by-class/",
		data: "id_classtype=" + id_classtype,
		success: function(content){
			var result=eval('('+content+')');

			var html = '';
			html = html + default_html;

			for(i=0; i<result.specialisations.length; i++) {
				var spe_id = result.specialisations[i].specialisation.id;
				var spe_name = result.specialisations[i].specialisation["name_"+curLang];
				var class_rewrite = result.specialisations[i]._VARS_.class_rewrite;
				var spe_rewrite = result.specialisations[i]._VARS_.spe_rewrite;

				html = html + '<option value="'+spe_id+'">'+spe_name+'</option>';
			}
			$("#user-character-id-specialisation").html( html );
			$("#user-character-id-second-specialisation").html( html );


			// On arrête le loader
			hideLoader();
		}
	});
}

/*
 *  Charge la liste des perso du joueurs suivant le serveur et la faction
 */
function updateUserCharactersList() {
	var faction = $("#post-faction").val();
	var id_server = $("#server-id").val();

	if( faction != 0 && id_server > 0 ) {
		// On affiche le loader
		showLoader();

		$.ajax({
			type: "POST",
			url: "/"+curLang+"/ajax/get-user-characters/",
			data: "id_server=" + id_server + "&faction=" + faction,
			success: function(content){
				var result=eval('('+content+')');

				$("#copy-user-character-in-team option").remove();

				var html = '<option value="0">-- Ajouter un de vos personnage à l\'équipe --</option>';

				for(i=0; i<result.characters.length; i++) {
					var strChar = result.characters[i].serverzone.code + ' :: ' + result.characters[i].user_character.name + '-' + result.characters[i].server.name;

					html = html + '<option value="'+result.characters[i].user_character.id+'">'+strChar+'</option>';
				}

				$("#copy-user-character-in-team").html(html);
				
				// On arrête le loader
				hideLoader();
			}
		});
	}
}

/*
 *  Reset le contenu des champs d'ajout de characters 
 */

function addCharacterResetFields() {
	$("#cur-character-add-race").val(0);
	$("#cur-character-add-class").val(0);
	$("option[id^=cur-character-add-specialisation_]").remove();
	updateCharactersClassList();
}


/*
 *  Reset le contenu des champs d'ajout de characters recherchés
 */


function addCharacterWantedResetFields() {
	$("#cur-character-wanted-add-race").val(0);
	$("#cur-character-wanted-add-class").val(0);
	$("option[id^=cur-character-wanted-add-specialisation_]").remove();
	updateCharactersWantedClassList();
}

/*
 * Change le status du bouton ajouté de personnage de l'équipe
 */

function changeCharacterAddButtonStatus() {
	if( $("#cur-character-add-race").val() > 0 && $("#cur-character-add-class").val() > 0 && $("#cur-character-add-specialisation").val() > 0 ) {
		$("#cur-character-add-button").attr('disabled', '');		
	} else {
		$("#cur-character-add-button").attr('disabled', 'disabled');		
	}
}


/*
 *
 *	POPUP DIVERS
 *
 */

// Centre la fenetre de popup
function centerPopup() {
	var popupWidth = 450;
	var popupHeight = 300;
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var windowScrollTop = $(window).scrollTop();
	var windowScrollLeft = $(window).scrollLeft();

	// Coordonnées du coin supérieur gauche du popup
	var moveLeft = (windowWidth / 2) - (popupWidth / 2) + windowScrollLeft;
	var moveTop = (windowHeight / 2) - (popupHeight / 2) + windowScrollTop;

	$("#popup").css('left', moveLeft);
	$("#popup").css('top', moveTop);
}

// Affiche la fenetre de popup
function showPopup() {
	$("#popup").fadeIn(200); 
}

// Affiche la fenetre de popup
function removePopup() {
	$("#popup").fadeOut(200, function() {
		$("#popup").remove();
	});
}

// Affiche le popup-de demande de certification
function showAskCertificationPopup() {
	var html = '';

	html = html + '<div id="popup" class="ask-certification">';
	html = html + '	<h1 style="background: none; padding: 0;">Veuillez certifier vos personnages...</h1>';
	html = html + '	<p>Nous avons détecté qu\'un ou plusieurs de vos personnages ne sont pas certifiés !</p>';
	html = html + '	<p>La certification a pour objectif de prouver que vos personnages sont bien les vôtres. Les informations d\'un personnage non-certifié ne sont pas affichées dans vos annonces hors-mi la classe, la race et la spécialisation.</p>';
	html = html + '	<p>Pour certifier vos personnages cliquez sur continuer.</p>';
	html = html + '	<a class="button ok" href="/'+curLang+'/user/characters/">Continuer</a>';
	html = html + '	<a class="button cancel" href="#">Pas maintenant...</a>';
	html = html + '</div>';

	$("body").prepend(html);
	centerPopup();
	showPopup();
}

// Loader
function showLoader() {
	var delay = 500; // Délai de latence avant l'affichage du loader (en ms)
	var fadeDuration = 200; // Durée d'execution du fade in et out
	var html = '';

	// On check si le loader n'est pas deja affiché
	if( $("#loader").attr('id') == 'loader' ) {
		return false;
	}
	
	html = html + '<div id="loader">';
	html = html + '	<div class="bg"></div>';
	html = html + '	<div class="anim"><img src="/css/img/loader.gif" /></div>';
	html = html + '</div>';
	$("body").prepend(html);

	
	// Fond noir
	var pageWidth = $("body").width();
	var pageHeight = $("body").height();	
	$("#loader, #loader .bg").css('width', pageWidth);
	$("#loader, #loader .bg").css('height', pageHeight);
	$("#loader").show();
	$("#loader .bg").delay(delay).fadeTo(fadeDuration, .7);

	// Gif animé
	var imgWidth = 220;
	var imgHeight = 19;
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var windowScrollTop = $(window).scrollTop();
	var windowScrollLeft = $(window).scrollLeft();

	// Coordonnées du coin supérieur gauche de l'image
	var moveLeft = (windowWidth / 2) - (imgWidth / 2) + windowScrollLeft;
	var moveTop = (windowHeight / 2) - (imgHeight / 2) + windowScrollTop;

	$("#loader .anim").css('left', moveLeft);
	$("#loader .anim").css('top', moveTop);
	$("#loader .anim").delay(delay).fadeIn(fadeDuration);
	
}

function hideLoader() {
	var fadeDuration = 100; // Durée d'execution du fade in et out
	$("#loader").fadeOut(fadeDuration, function() {
		$("#loader").remove();
	});
}

// Anime le nombre de nouveau message (espace membre)
function animateNbNewMessages() {
	var fadeDelay = 350;
	
	if( $("#member-box ul li.messaging span").html() ) {
		if( $("#member-box ul li.messaging span").html().length > 0) {
			var object = $("#member-box ul li.messaging span");

			if( object.css('display') == 'none' ) {
				object.fadeIn(fadeDelay, function() {
					setTimeout("animateNbNewMessages()", 500);
				});
			} else {
				object.fadeOut(fadeDelay, function() {
					setTimeout("animateNbNewMessages()", 0);
				});
			}
		}

	}
}

 // Return current UNIX timestamp
function time() {
    return Math.floor(new Date().getTime()/1000);
}
