function updateMicroBloging(elem)
{
	$(elem+' q').hide();
	$(elem+' textarea').empty();
	$(elem+' form:first').show();
	$(elem+' a[rel=update]').hide();
	$(elem+' a.showComments').hide();
	$(elem+' a.hidden').show();
	$(elem+' a.UDA_bouton').css('display','block');
	
	if ($('#commentsList:visible').size() == 1)
	$('.showComments').trigger('click');
	
	$(elem+' textarea').focus;
}

function cancelMicroBlogging(elem)
{
	$(elem+' q').show();
	$(elem+' form:first').hide();
	$(elem+' a[rel=update]').show();
	$(elem+' a.showComments').show();
	$(elem+' a.hidden').hide();
}


function showComments()
{
	$('.showComments').toggle(
		function ()
		{
			$(this).siblings(".commentsList").show('fast');
		},
		function ()
		{
			$(this).siblings(".commentsList").hide('fast');
		}
	);
}


function microBlogging()
{
	// Hide the form
	$('#theWireDashboard form[name=noteForm]').hide();
	$('#thewire_submit_button').remove();
	
	
	
	// Gestion d'une taille limite pour la taille des messages du wire
	$(".wireEditMessage").bind("keydown", 
			function(e) 
			{
					// Assurer les compatibilités
					if(e){ // if the event object is present (NN only)
					e = e // var e = event
					}
					else {
					e = window.event // else e = winddow.event for IE
					}
					
					if(e.which){ // if there is syntax support for the property 'which' (NN only)
					var keycode = e.which // e.which is stored in variable "keycode"
					}
					else {
					var keycode = e.keyCode // otherwise for IE, var keycode stores e.keyCode syntax
					}
				
				  if (keycode == 13)
				  {
				  	$('#theWireDashboard a[rel=publish]').trigger('click');
				  	return false;
				  }
				  if (keycode != 8 && $(".wireEditMessage").val().length >= 140)
				  {
				  	return false;
				  }
			});
	
	
	
	
	
	showComments();
	
	$('#theWireDashboard a[rel=update]').click(
		function()
		{
			updateMicroBloging('#theWireDashboard');
		}
	);
	
	$('#theWireDashboard a[rel=cancel]').click(
		function()
		{
			cancelMicroBlogging('#theWireDashboard');
		}
	);

	$('#theWireDashboard a[rel=publish]').click(
		function()
		{
				// Gestion des valeurs
				var note = $('#theWireDashboard form *[name=note]').val();
				var methode = $('#theWireDashboard form *[name=method]').val();
				var loc = 'ajax';
				var access_id = $('#theWireDashboard form *[name=access_id]').val();

			
				// Envoi de la requête ajax
				 $.ajax(
				 {
				   type: "POST",
				   url: $('#theWireDashboard form').attr('action'),
				   data: "note="+encodeURIComponent(note)+"&method="+methode+"&location="+loc+"&access_id="+access_id,
				   success: function(newGuid){
				   	 if (newGuid != 0)
				   	 {
				   	 	$('#theWireDashboard q').text(note);
				   	 	var image = $('#theWireDashboard .showComments img').clone();
				   	 	$('#theWireDashboard .showComments').empty();
				   	 	$('#theWireDashboard .showComments').append(image);
				   	 	$('#theWireDashboard .showComments').append('(0)');
				   	 	
				   	 	cancelMicroBlogging('#theWireDashboard');
				   	 	
				   	 	$('#theWireDashboard .commentsList > .generic_comment').remove();
				   	 	
				   	 	$('#theWireDashboard .commentsList form:last input[name=entity_guid]').val(newGuid);
				   	 	
				   	 	
				   	 	 $.ajax(
							 {
							   type: "POST",
							   url: '../../mod/UDA_dashboard/showActivity.php',
							   data: "",
							   success: function(msg){
							   		$('.river_item_list').empty();
							   	 	$('.river_item_list').append(msg);
							   	 	mergeActivity();
							   	 	showComments();
			   	 					CommentFBstyle();
							   	 }
							 });
				   	 	
				   	 }
				   }
				 });
				
		}
	 );
}
	

function menuDashboard()
{
	$(".dashboardTabs a").click(
		function()
		{
			if ($('#'+$(this).attr('alt')+':visible').size() > 0)
			return true;
			
			$('#dashboardZoneInfos .zoneInfo').hide();
			$('#dashboardZoneInfos .zoneInfo .zoneInfoToolbar').hide();
			$(".dashboardTabs .selected").removeClass('selected');
			$(this).parent().addClass("selected");
			$('#'+$(this).attr('alt')).show();
			$('#'+$(this).attr('alt') +'.zoneInfo .zoneInfoToolbar').slideDown('fast');
			
			return false;
		}
	);

	$('.dashboardTabs a:first').trigger('click');
}


// Fusion des actualités identiques
function mergeActivity()
{
	$('.river_item_list .river_item:gt(0):[rel!=processedOK]').each(
		function()
		{
			if ($(this).hasClass("similarActivity"))
			return true;
			
			//alert(chercherContenuActu($(this))+' :: '+chercherContenuActu($(this).prev()));
			
			if (chercherContenuActu($(this)) == chercherContenuActu($(this).prev()))
			{
				$(this).addClass('similarActivity');
				
				// Nombre actus similaires
				var elementNbre = $(this).parent().find('div[rel=processedOK]:visible:last .similarActivityLink span');
				
				var nbre = elementNbre.text();
				nbre = parseInt(nbre);
				nbre++;
				elementNbre.text(nbre);
				elementNbre.parent().show();
			}
			else
			{
				// On flag l'élément comme étant OK
				$(this).attr('rel','processedOK');
			}	
		}
	);
	
	$('.river_item_list .similarActivityLink').click(
		function()
		{
			$(this).parents('.river_item').nextUntil('div[rel=processedOK]').show('fast');
		}
	);
}

function chercherContenuActu(e)
{
	var contenuActu = e.find('p').not('.UDA_Statut').html();
	
	if (contenuActu)
	{
		var contenuActuTmp = contenuActu.toLowerCase();
		
		var pos = contenuActuTmp.lastIndexOf('<span class="river_item_time"');

		// hack IE
		if (pos == -1)
		var pos = contenuActuTmp.lastIndexOf('<span class=river_item_time');
		
		if (pos != -1)
		contenuActu = contenuActu.substring(0,pos);
		return contenuActu;
	}
	return false;
}

// Affiche les actualités plus anciennes
function olderActivity()
{
	$('#showOlderActivity').click(
		function()
		{
			 $.ajax(
			 {
			   type: "POST",
			   url: '../../mod/UDA_dashboard/showActivity.php',
			   data: "time="+$('.river_item_list .river_item:last').attr('id'),
			   success: function(msg){
			   	 	$('.river_item_list').append(msg);
			   	 	mergeActivity();
			   	 	showComments();
			   	 	CommentFBstyle();
			   	 }
			 });
		}
	);
}

// Cache le message d'accueil lorsque l'on clique et stocke en session la préférence
function hideWelcomeMessage()
{
	$("#hideWelcomeMessage").live('click',
		function()
		{
			$.ajax(
			 {
			   type: "POST",
			   url: '../../mod/UDA_dashboard/hideWelcomeMessage.php',
			   success: function(msg){
			   	 $('.welcomemessage').hide('slow');
				 $('#facebox .close').trigger('click');
			   }
			 });
		}
	);
}


$(document).ready(
		function()
		{
			//menuDashboard();
			microBlogging();
			mergeActivity();
			olderActivity();
			hideWelcomeMessage();
		}
	);
