/* Chargement de la méthode de déasctivation des champs au chargement de la page */
$(document).ready(function(){
	UDA_ShowCollection();
	UDA_StatusSelector();
	UDA_ProfileListingMoreInfos();
	UDA_ProfileLiveEditor();
	UDA_imageEditor();
	//UDA_CustomURLavailable();
});

function UDA_ProfileListingMoreInfos()
{
	$('a[rel=montreInfosProfileListing]').toggle(
			function()
			{
				$(this).next().slideDown("slow");
			}
			,
			function()
			{
				$(this).next().slideUp("slow");
			}
	);
}

function UDA_ShowCollection()
{
	$('.UDA_CollectionLinkEdit').toggle(
	
		function(){
			$(this).parent().next().slideDown("slow");
			}
			,
		function(){
			$(this).parent().next().slideUp("slow");
			}
	
	);
	
	$('.UDA_CollectionDivEdit input[type=reset]').click(
		function(){
			$(this).parent().parent().prev().find('.UDA_CollectionLinkEdit').trigger('click');
			}
	);
}

// Gestion du changement de statut
function UDA_StatusSelector()
{
	$('#statusSelector input[type=submit]').hide();
	
	$('#statusSelector input').click(
		function()
		{
			$('#statusSelector input[type=submit]').show();
		}
	);
	
	$('#statusUser a').click(
		function()
		{
			$('#statusUser').hide();
			$('#statusSelector').show('slow');
			//$('#statusSelector').next().hide();
		}
	);
	
	$('#statusSelector input[value=autre]').click(
		function()
		{
			if ($('#statutAutre:visible').size() == 1)
			{
				$('#statutAutre').hide('slow');
				$('#statutAutre').val('');
			}
			else
			$('#statutAutre').show('slow');
		}
	);
}

// Edition des champs en live
function UDA_ProfileLiveEditor()
{
	$('.liveEditor .editable .editInfos').attr('href','javascript:;');
	
	$('.liveEditor .editable .editInfos').click(
			function()
			{
				$(this).parent().parent().find('.defaultInfo').hide();
				$(this).parent().parent().find('.inputZone').show('fast');
			}
	);
	
	$('.cancelChanges').click(
		function()
		{
			$(this).parent().parent().find('.inputZone').hide('fast');
			$(this).parent().parent().find('.defaultInfo').show();
		}
	);
	
	$('.applyChanges').click(
		function()
		{
			elem = $(this).parent().attr('rel').split('|');
			user = elem[0];
			metadata = elem[1];
			
			var elementInput = $(this).parent().find('*[name='+metadata+']');
			valeur = elementInput.val();
			
			// On retourne la valeur de l'option pour un select
			dspValue = '';
			
			if (elementInput.attr('class') == 'input-pulldown')
			{
				dspValue = elementInput.find('option:selected').text();						
										
				// modif 9 avril 2010. D Chabaud
				// Cas domaine sur le CV : retourner la liste des choix de l'utilisateur 
			
				if (elementInput.parent().find('.ui-dropdownchecklist-text').size())
				{						
						dspValue = elementInput.parent().find('.ui-dropdownchecklist-text').attr('title');												
						dspValue = dspValue.replace(/\), /g,")<br>");
				}
			}
			
			if ($(this).parent().find('.HTMLeditor').size() == 1)
			{
				valeur = CKEDITOR.instances[metadata].getData();
			}
			
			valeurURI = encodeURIComponent(valeur);
			
			
			// Envoi de la requête ajax
				 $.ajax(
				 {
				   type: "POST",
				   url: $('#ajaxURL').val(),
				   data: "user="+user+"&metadata="+metadata+"&value="+valeurURI,
				   success: function(feedback){
				   	if (feedback == 1)
				   	{	
				   		var obj = $('.inputZone[rel='+user+'|'+metadata+']');
				   		obj.prev().find('.defaultValue').empty();
				   		
				   		if (dspValue != '')
				   		obj.prev().find('.defaultValue').append(dspValue);
				   		else
				   		obj.prev().find('.defaultValue').append(valeur);
				   		
				   		obj.hide('fast');
							obj.prev().show();
							UDA_ApplyChanges(metadata);
				   	}
				   }
				  }
				  );
		}
	);
}

// Editeur de photo
function UDA_imageEditor()
{
	$('#profile_picture_action_list a[href=javascript:;]').click(
		function()
		{
			if ($('#'+$(this).attr('rel')+':visible').size() == 1)
			{
				$('#'+$(this).attr('rel')).hide();
			}
			else
			{
				$("#profile_picture_actions > *").hide();
				$('#'+$(this).attr('rel')).show();
				$("#profile_picture_actions").show('fast');
			}
		}
	);
}

function reloadPage()
{
  $(location).attr('href',$(location).attr('href'));
  return true;
}
/*
function UDA_CustomURLavailable()
{
	$('#customURLform').submit(
		function()
		{
			erreur = false;
			
			$.ajax(
			 {
			   type: "POST",
			   url: $('#customURLform').attr('action'),
			   data: "cmd=isAvailable&customURL="+$('#customURLform .input-text').val(),
			   success: function(feedback){
			   	if (feedback == 'error')
			   	{	
			   		alert('pas dispo');
			   		erreur = true;
			   	}
			   }
			  }
			 );
			 
			 return false;
			 
			 if (erreur == true)
			 return false;
		}
	);
}
*/
