$(document).ready(function(){
	//TODO Debug du Topic Wizard
	/*$.fn.extend({
		titleProcess: function(){
		}
	});*/
	$('a#lnkShowSend').toggle(function(){
		/*$('#user_side_more_stats').css('-moz-border-radius-bottomleft', '0px');
		$('#user_side_more_stats').css('-moz-border-radius-bottomright', '0px');
		$('#user_side_more_stats img').attr('src', '/img/ctrl_opened.png');*/
		$('#send_bloc').slideDown('slow', function(){});
	},function(){
		$('#send_bloc').slideUp('slow', function(){
			/*$('#user_side_more_stats').css('-moz-border-radius-bottomleft', '5px');
			$('#user_side_more_stats').css('-moz-border-radius-bottomright', '5px');
			$('#user_side_more_stats img').attr('src', '/img/ctrl_closed.png');*/
		});
	});
	
	//Désactivation de l'assistant Topic
	$('#msgBox_wzd_hide').bind('click', function(){
		$('#message_wizard').hide('blind', {direction: 'vertical'}, 800, function(){
			$('#mess_que_dire').show('blind', {direction: 'vertical'}, 500);
			$('#comment_textarea').removeAttr('readonly');
			$('#topic_wzd_title').val('');
			$('#topic_msg_desc').val('');
			//$('#comment_textarea').val('').focus();
		});
		$('#msgBox_wzd_hide').hide('slide', {direction: 'right'}, 800, function(){
			$('#msgBox_wzd_show').show('slide', {direction: 'right'}, 500);
		});
	});
	
	//Activation de l'assistant Topic 
	$('#msgBox_wzd_show').bind('click', function(){
		/*$('#mess_que_dire').hide('blind',{direction: 'vertical'}, 700, function(){});*/
		$('#mess_que_dire').hide('blind',{direction: 'vertical'}, 500, function(){
			$('#comment_textarea').attr('readonly', 'true');
			//TODO Ajout de style et click = impossible de focus
			$('#message_wizard').show('blind', {direction: 'vertical'}, 800, function(){
				$('#topic_wzd_title').focus();
			});
		});//TODO remplacer par un effet de slide
		$('#msgBox_wzd_show').hide('slide', {direction: 'right'}, 800,function(){
			$('#msgBox_wzd_hide').show('slide', {direction: 'right'}, 500);
		});
	});
	
	$('#topic_wzd_title').bind('keyup', function(){
		// TODO étendre jQuery pour ajouter .val().cmdwithspaces()
		//fonct° si espaces trouvé, mettre des double-guillemet
		var topicTitle = $(this).val().trim();
		if(topicTitle != '') {
			var titleVal = '#"'+topicTitle+'"';
			$('#comment_textarea').val(titleVal);
		}
	});
	$('#topic_wzd_title').blur(function(){
		var topicTitle = $.trim($(this).val());
		if(topicTitle != '') {
			var titleVal = '#"'+topicTitle+'"';
			$('#comment_textarea').val(titleVal);
		}
	});
	$('#topic_msg_desc').bind('keyup', function(){
		var titleVal = $.trim($('#topic_wzd_title').val());
		var contentVal = $.trim($(this).val());
		$('#comment_textarea').val('#"'+titleVal+'" '+contentVal);
	});
	
	//#upload_file //#commentsFile
	$('#upload_file').hover(function(){ $('#upload_file').css('cursor', 'pointer'); }, function(){$('#upload_file').removeAttr('style');});
	
	$('#link_add_files').bind('click', function(){
		//alert('FOUFOU');
		$('#commentsFile').focus();
		return false;
	});
});

var regExpBeginning = /^\s+/;
var regExpEnd = /\s+$/;  

function	update_que_dire() {
	var val_area;
	var reg = new RegExp('[ ]+', 'g');
	var regplus = new RegExp('["]+', 'g');

	val_area = ltrim($('#comment_textarea').val());
	var tableau = val_area.split(reg);

	if (tableau.length > 1) {
		if (val_area.substring(0, 1) == '#') {
			if (val_area.substring(1, 2) == '"') {
				var tableauplus = val_area.split(regplus);
				if (tableauplus[1] && tableauplus[2])
				//alert(tableauplus[0]+"-"+tableauplus[1]+"-"+tableauplus[2]+"-"+tableauplus[3]);
				document.getElementById('span_says').innerHTML = document.getElementById('span_reply_to_topic').innerHTML + ' ' + tableauplus[1];
			}
			else {
				document.getElementById('span_says').innerHTML = document.getElementById('span_reply_to_topic').innerHTML + ' ' + tableau[0].substring(1, tableau[0].length);
			}
		}
		else if (val_area.substring(0, 1) == '@') {
			document.getElementById('span_says').innerHTML = document.getElementById('span_reply_to_user').innerHTML + ' ' + tableau[0].substring(1, tableau[0].length);
		}
	}
	else if (val_area == '') {
		document.getElementById('span_says').innerHTML = document.getElementById('span_que_dire').innerHTML + ' ' + val_area.substring(1, val_area.length);
	}
}

function 	trim(aString) {
    return aString.replace(regExpBeginning, "").replace(regExpEnd, "");
}

function 	ltrim(aString) {
    return aString.replace(regExpBeginning, "");
}

function 	rtrim(aString) {
    return aString.replace(regExpEnd, "");
}

