// JavaScript Document
$(function() {
	//effets pour le bas des bulles dans EN DIRECT
	//----------------------------------------------------------------------------
	$('.bulle .bas .comments A, .bulle .bas .sociable A').fadeTo(0,0.3);
	$('.bulle .bas .comments A, .bulle .bas .sociable A').mouseenter(function(){				
		$(this).animate({ 
			opacity: 1
		}, 200 );
	}).mouseleave(function(){
		$(this).animate({ 
			opacity: 0.3
		}, 500 );
	});	
	
	$('A#publierDirect').click(function() {
		$('div#publierDirectForm').toggle("fast");				
	});
	
	//rendre postHaste resizeable
	//----------------------------------------------------------------------------
	$('textarea.resizable:not(.processed)').TextAreaResizer();
	
	//compteur de mots
	//----------------------------------------------------------------------------
	$('#postText').keyup(function() {
		var charLength = $(this).val().length;
		// Displays count
		$('span#charCount').html(charLength);
		// Alerts when 250 characters is reached
		if($(this).val().length > 0) {
			$('span#charCount').css("color","#063");
			$('span#charMsg').html("");
		}
		if($(this).val().length > 100) {
			$('span#charCount').css("color","#C90");
			$('span#charMsg').html("");
		}
		if($(this).val().length > 140) {
			$('span#charCount').css("color","#900");
			$('span#charMsg').html(": sera tronqu&eacute; sur Twitter.");
		}
	});
	
	//convertisseur bit.ly
	//----------------------------------------------------------------------------
	$('#activeConversion').click(function() {
		//définir la source
		var source = '#leLienBitLy';
		//définir le TEXTAREA cible
		var target = '#posthasteForm #postText';
		//convertir le lien
		var defaults = {
			version:    '2.0.1',
			login:      'branchezvous',
			apiKey:     'R_0aa93c7eee9d5c179e08d68ed66ef9f6',
			history:    '1',
			longUrl:    $(source).val()
		};
		// URL pour connecter l'API
		var daurl = "http://api.bit.ly/shorten?"
			+"version="+defaults.version
			+"&longUrl="+defaults.longUrl
			+"&login="+defaults.login
			+"&apiKey="+defaults.apiKey
			+"&history="+defaults.history
			+"&format=json&callback=?";				
		// Utiliser l'API
		$.getJSON(daurl, function(data){	
			var output = data.results[defaults.longUrl].shortUrl;
			var avant = $(target).val();					
			if(output) {
				$(target).val(avant + output + ' ');	
				//fermer thickbox
				tb_remove();	
			} else 
				alert("Impossible de convertir le lien (" + defaults.longUrl + ").\nEssayez de nouveau!");
		});				
													  
	});
	
	//pièces jointes
	//----------------------------------------------------------------------------
	$('#toggleAttach').click(function() {
		$('ul#attachements').toggle("fast");
		if($('ul#attachements').css('height') == '1px')	
			$('#toggleAttach span').html('cacher');
		else
			$('#toggleAttach span').html('afficher');
	});
	
	//menu de gauche
	//----------------------------------------------------------------------------
	$('A#infolettreLien').click(function() {
		$('div#infolettreForm').toggle("fast");				
	});
	
	//infocourriel
	//----------------------------------------------------------------------------
	$('span.moreFields').click(function() {		
		if($(this).html() == '+') {
			$('div#DIV' + $(this).attr("rel")).removeClass('toggle');			
			$(this).html("-"); 
		} else {
			$('div#DIV' + $(this).attr("rel")).addClass('toggle');	
			$(this).html('+');	
		}
	});
	
	// tab menu
	//-----------------------------------------------------------------------------
	//Get all the LI from the #tabMenu UL
	$('#tabs > li').click(function(){					
		//remove the selected class from all LI    
		$('#tabs > li').removeClass('selected');			
		//Reassign the LI
		$(this).addClass('selected');			
		//Hide all the DIV in .boxBody
		$('.tabsBody div.TAB').hide();			
		//Look for the right DIV in boxBody according to the Navigation UL index, therefore, the arrangement is very important.
		$('.tabsBody div.TAB:eq(' + $('#tabs > li').index(this) + '), .tabsBody div.TAB:eq(' + $('#tabs > li').index(this) + ') DIV').fadeIn('slow');	
		
	}).mouseover(function() {
		//Add and remove class, Personally I dont think this is the right way to do it, anyone please suggest    
		$(this).addClass('mouseover');
		$(this).removeClass('mouseout');   		
	}).mouseout(function() {				
		//Add and remove class
		$(this).addClass('mouseout');
		$(this).removeClass('mouseover');   		
	});
	
});			