/**
 * @brief	Fonctions utilisées pour la caserne
 * @author	Renan
 * @since	16/06/2010
 * @version	1
 */
function Caserne(){
	
	this.materielId			= 0; //id du matériel choisi
	this.priceMateriel 		= 0; //prix souhaité à la revente
	this.quantiteRevente	= 0; //nombre de produit que l'on revend
	
	
	this.loadMateriel = function()
	{
		$.post('/fr/game/pages/caserne/ajax.php', { action: 'loadMateriel' }, function(data){
			$('#listeMateriel-container').html( data[0] );

			if( data[1].length > 0 )
			{
				for( var i in data[1])
				{
					// Activation du Carrousel !
					Carrousel( data[1][i] , 130, 130, 5);
				}
			}


			


		}, 'json');
	}
	
	/* Voir la fiche d'un matériel */
	this.materiel = function(id){
		contenuPopupAjax( 'Chargement ...' );
				
		//appel ajax
		$.post('/fr/game/pages/caserne/ajax.php', { action:'popMateriel', materiel:id }, function(data){
			contenuPopupAjax( data );
			
			caserne.previewPrice();
		}, 'json');
		
	}
	
	this.previewPrice = function(occasion)
	{
		if(occasion == true)
		{
			var price = $('input[name=reventePrice]').val();
			this.priceMateriel	= price;
		}
			
		if( is_numeric(this.priceMateriel) )
		{
			this.quantiteRevente= $('select[name=reventeQuantite]').val();
			
			$('#previewPrice').html( number_format(this.quantiteRevente * this.priceMateriel) );
		}
	}
	
	/* Revendre du matériel  */
	this.vendre = function()
	{
		if( is_numeric(this.priceMateriel) && is_numeric(this.quantiteRevente) && this.quantiteRevente>0 )
		{
			$.post('/fr/game/pages/caserne/ajax.php', 
					{ action:'revendreMateriel', materiel:this.materielId, price:this.priceMateriel, quantite:this.quantiteRevente },
				function(data)
				{
					fermerPopupAjax();
					message(data);
					
					//rafraicchissement de la page
					caserne.loadMateriel();
				},
			'json');
		}		
	}
	
	/* Revendre du matériel au S.D.I.S  */
	this.vendre_sdis = function()
	{
		if( is_numeric(this.priceMateriel) && is_numeric(this.quantiteRevente) && this.quantiteRevente>0 )
		{
			$.post('/fr/game/pages/caserne/ajax.php', 
					{ action:'revendreMaterielSdis', materiel:this.materielId, quantite:this.quantiteRevente },
				function(data)
				{
					fermerPopupAjax();
					message(data);
					
					//rafraicchissement de la page
					caserne.loadMateriel();
				},
			'json');
		}		
	}
	
	
	/* Renommer la ville/caserne */ 
	this.formRename = function(){
		
		$.post('/fr/game/pages/caserne/ajax.php', { action:'formRename' }, function( html )
		{
			contenuPopupAjax( html );
		}, 'json');
		
	}
	this.validRename = function(){
		var ville = $('input[name=editCityName]').val();
		var caserne = $('input[name=editFireName]').val();
		
		$.post('/fr/game/pages/caserne/ajax.php', { action:'formRenameValid', city:escape(ville), fire:escape(caserne) }, function( data )
		{
			fermerPopupAjax();
			message(data);
			
			//mise à jour en live de l'affichage
			$('span#connect-menu-caserne').html( caserne );
			$('h1, title').html( 'Caserne '+ caserne );
			$('span#cityname > b').html( ville );
			
		}, 'json');
		
	}
	
}
 
function Formation(){
	this.id	= 0;	//id du grade
	this.nSelect 	= 0; //nombre de pompier sélectionné
	this.select 	= 0; //nombre de pompier maximum autorisé
	this.price 		= 0; //prix d'une formation
	this.isStarted 	= false;
	
	
	this.totalPrice = function()
	{
		$('#grade-pompier-price > span').html( (this.price * this.nSelect) );
	}
	
	/* Sélectionner/désélectionner un pompier */
	this.add = function(value)
	{
		$container = $('#grade-pompier-container > div#'+value);
		if( $( 'div#'+value+'.selectFormation').length > 0 )
		{
			this.nSelect--;
			this.totalPrice();
			$container.removeClass('selectFormation');
			$('#grade-pompier-autorise').html((this.select  - this.nSelect));
		}
		else
		{
			if( this.nSelect < this.select )
			{
				this.nSelect++;
				this.totalPrice();
				$container.addClass('selectFormation');
				$('#grade-pompier-autorise').html((this.select  - this.nSelect));
			}
		}
	}
	
	/* valider une formation */
	this.start = function()
	{
		var tabPompier = new Array();
		var i = 0;
		$('#grade-pompier-container > div.selectFormation').each(function(data){
			tabPompier[i] = parseInt($(this).attr('id'));
			i++;
		});
		var pompiers = tabPompier.join(',');
		
		if(!this.isStarted)
		{
			this.isStarted = true;
			$.post('/fr/game/pages/administration/ajax.php?action=grade', {
				appel: 'validate',
				id: this.id,
				pompiers: pompiers
			}, function(data){
				fermerPopupAjax();
				formation.reload();
				message(data);
			}, 'json');
		}
		
	}
	
	/* Chargement des Formation en cours */
	this.reload = function()
	{
		$.post('/fr/game/pages/administration/ajax.php?action=grade', { appel:'reload' }, function(data){
			$('#grade-formation-container').html( data );
		}, 'json');
	}
}



//Gestion de la recherche de joueur
$("#searchPseudo").die("focusin").live("focusin",function(){
	if($("#searchPseudo").val() == "Pseudo")
		$("#searchPseudo").val('');
	$("#listPseudo").css("border", "1px solid rgb(255, 0, 0)");
	$("#listPseudo").css("background-color", "white");
});

$("#searchPseudo").die("focusout").live("focusout",function(){
	$("#listPseudo").css("border", "1px solid #AB9B9D");
	$("#listPseudo").css("background-color", "#EFEFEF");
	$("#listPseudo").slideUp();
});

$("#searchPseudo").die("keyup").live("keyup",function(){
	if($("#searchPseudo").val().length > 2)
	{
	$.post("/fr/game/pages/caserne/searchUsers.php",
			{pseudo : $("#searchPseudo").val()},
			function(data){
				textPseudo = "";
				for(id in data) {
					textPseudo += '<a href="/fr/profil-'+ data[id]["id"] +'.htm">'+ data[id]["login"] +'</a><br>';
				}

				if(textPseudo.length > 0)
				{
					$("#listPseudo").html(textPseudo);
					$("#listPseudo").slideDown();
				}
				else
				{
					$("#listPseudo").slideUp();
				}
			}, "json");
	}
	else
	{
		$("#listPseudo").slideUp();
	}
});


/*
 * JS pour la recherche dans les alliances
 */

$("#searchAlliance").die("focusin").live("focusin",function(){
	if($("#searchAlliance").val() == "Alliance")
		$("#searchAlliance").val('');
	$("#listPseudo").css("border", "1px solid rgb(255, 0, 0)");
	$("#listPseudo").css("background-color", "white");
});

$("#searchAlliance").die("focusout").live("focusout",function(){
	$("#listPseudo").css("border", "1px solid #AB9B9D");
	$("#listPseudo").css("background-color", "#EFEFEF");
	
});

$("#searchAlliance").die("keyup").live("keyup",function(){
	if($("#searchAlliance").val().length > 2)
	{
	$.post("/fr/game/pages/alliance/searchAlliance.php",
			{alliance : $("#searchAlliance").val()},
			function(data){
					$("#tab_liste_search").html(data);
					$("#tab_liste_alliance").slideUp(400, function(){
						$("#tab_liste_search").slideDown();
					});
			}, "html");
	}
	else
	{
		$("#tab_liste_search").slideUp(400, function(){
			$("#tab_liste_alliance").slideDown();
			$("#tab_liste_search").html("");
		});
	}
});

