$(function(){
	$('a#connexion').fancybox({
		href: 'connexion.html?iframe',
		autoDimensions: false,
		width: 440,
		height: 350
	});
	
	$('.help, #footer em').tooltip({
		track: true,
		showURL: false,
		delay: 0	
	});
	
	if ($('table#tableau_notes').length > 0) {
		$('table#tableau_notes').tablesorter({
			headers: {4: {sorter: false}}
		});
	}
	
	if ($('div.options').length > 0 && !$('#n-menu #menu-admin').hasClass('actif')) {
		$('.options dt').addClass('js');
		$('.bleu').accordion({autoHeight: false,collapsible:true,clearStyle: true});
		$('.green').accordion({autoHeight: false,collapsible:true,active:false});
		$('.yellow').accordion({autoHeight: false,collapsible:true,active:false});
		$('.orange').accordion({autoHeight: false,collapsible:true,active:false});
	}
	
	$('.hide-if-no-js').removeClass('hide-if-no-js');
	
	if ($('input.ajax_recherche_membres').length) {
		$('input.ajax_recherche_membres').autocomplete('ajax.php?req=recherche-membres', {
			selectFirst: false,
			minChars: 1,
			autoFill: true,
			scrollHeight: 100
		});
	}
	
	
	/* From Drupal */
    $('textarea.resizable').each( function () {
      // When wrapping the text area, work around an IE margin bug. See:
      // http://jaspan.com/ie-inherited-margin-bug-form-elements-and-haslayout
      var staticOffset = null;
      var textarea = $(this).wrap('<div class="resizable-textarea"><span></span></div>');
      var grippie = $('<div class="grippie"></div>').mousedown(startDrag);

      grippie
        .insertAfter(textarea)
        .css('margin-right', grippie.width() - textarea.width());

      function startDrag(e) {
        staticOffset = textarea.height() - e.pageY;
        textarea.css('opacity', 0.25);
        $(document).mousemove(performDrag).mouseup(endDrag);
        return false;
      }

      function performDrag(e) {
        textarea.height(Math.max(32, staticOffset + e.pageY) + 'px');
        return false;
      }

      function endDrag(e) {
        $(document).unbind('mousemove', performDrag).unbind('mouseup', endDrag);
        textarea.css('opacity', 1);
      }
    });
    
    
    /**
    *	Pour afficher les messages cachés sur le forum
    */
    if ($('a.voir-msg').length) {
    	$('a.voir-msg').each(function(){
    		$(this).click(function(){
	    		var msg = $(this).parents('tr').next();
	    		msg.toggle();
	    		if (msg.is(':hidden')) {
	    			$(this).text('Voir le message');
	    		}
	    		else {
	    			$(this).text('Cacher le message');
	    		}
	    		return false;
    		});
    	});
    }
    
    /**
    *	Affichage de l'éditeur HTML
    */
    if ($('textarea.textarea-editor').length) {
		$('textarea.textarea-editor').tinymce({
			script_url : 'design/js/tinymce/tiny_mce.js',

			width:"100%",
			theme:"advanced",
			language:"fr",
			plugins : "table,advhr,advimage,advlink,emotions,iespell,inlinepopups,preview,media,contextmenu,paste,directionality,noneditable,visualchars,nonbreaking,xhtmlxtras,advlist,wordpress",
			
			theme_advanced_buttons1 : "bold,italic,underline,|,bullist,numlist,blockquote,|,justifyleft,justifycenter,justifyright,|,link,unlink,|,,fontselect,fontsizeselect,wp_adv",
			theme_advanced_buttons2 : "formatselect,strikethrough,forecolor,justifyfull,|,outdent,indent,|,undo,redo,|,anchor,image,cleanup,code,|,table,|,hr,removeformat,|,sub,sup,|,emotions", /*,|,add_image */
			theme_advanced_buttons3 : "",
			theme_advanced_buttons4 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,
			theme_advanced_resize_horizontal: false,

			content_css : "css/content.css",
			
			template_external_list_url : "lists/template_list.js",
			external_link_list_url : "lists/link_list.js",
			external_image_list_url : "lists/image_list.js",
			media_external_list_url : "lists/media_list.js"
		});
    }
});

function rafraichir_captcha() { 
	document.getElementById('captcha').src='captcha.png?'+Math.random();
}

function afficher_secret(div2) {
	if (div2.getElementsByTagName('div').length > 0) {
		var divs = div2.getElementsByTagName('div');
	}
	else { 
		var divs = div2.parentNode.nextSibling.getElementsByTagName('div');
	}
	
	var div3 = divs[0];
	if (div3.style.visibility == 'visible') {
		div3.style.visibility='hidden';
	}
	else {
		div3.style.visibility='visible';
	}
	
	return false;
}

function afficher_secret_reduit(div2) {
	var divs = div2.parentNode.nextSibling.getElementsByTagName('div');
	var div3=divs[0];
	if (div3.style.display == 'block') {
		div3.style.display = 'none';
	}
	else {
		div3.style.display = 'block';
	}
	
	return false;
}
