var cs_prive=window.location.pathname.match(/\/ecrire\/$/)!=null;
jQuery.fn.cs_todo=function(){return this.not('.cs_done').addClass('cs_done');};
var onglet_actif = 0;

// fonction pour montrer un contenu
jQuery.fn.montre_onglet = function( selector ) {
	// click sur un titre
	if(this.is('.onglets_titre')) {
		var contenu = '#' + this[0].id.replace(/titre/,'contenu');
		var bloc = this.parent().parent();
		bloc.children('.selected').removeClass('selected').end()
			.children('.onglets_liste').children('.selected').removeClass('selected');
		jQuery(contenu).addClass('selected');
		this.addClass('selected');
	}
	// click sur un titre
	if(this.is('.onglets_contenu')) {
		var titre = this[0].id.replace(/contenu/,'titre');
		jQuery('#'+titre).montre_onglet();
	}
	return this;
};

// compatibilite Ajax : ajouter "this" a "jQuery" pour mieux localiser les actions 
function onglets_init() {
  var cs_bloc = jQuery('div.onglets_bloc_initial', this);
  if(cs_bloc.length) {
	cs_bloc.prepend('<div class="onglets_liste"></div>')
		.children('.onglets_contenu').each(function(i) {
			this.id = 'onglets_contenu_' + i;
			jQuery(this).parent().children('.onglets_liste').append(
				'<h2 id="'+'onglets_titre_' + i + '" class="onglets_titre">' + this.firstChild.innerHTML + '</h2>'
			);
		})
		.children('h2').remove();
	jQuery('div.onglets_liste', this).each(function() {
		this.firstChild.className += ' selected';
		this.nextSibling.className += ' selected';
	});
	jQuery('h2.onglets_titre', this).hover(
		function(){
			jQuery(this).addClass('hover')
		},function(){
			jQuery(this).removeClass('hover')
		}
	);
	jQuery('div.onglets_bloc_initial', this)
		.attr('class','onglets_bloc').each(function(i) {this.id = 'ongl_'+i;});
	// clic du titre...
	jQuery('h2.onglets_titre', this).click(function(e) {
		jQuery(this).montre_onglet();
		return false;
	});
	// clic des <a>, au cas ou...
	jQuery('h2.onglets_titre a', this).click(function(e){
		jQuery(this).parents('h2').click();
		if (e.stopPropagation) e.stopPropagation();
		e.cancelBubble = true;
		return false;
	});
	// activation d'un onglet grace a l'url
	if(onglet_get && (this==document))
		jQuery('#onglets_titre_'+onglet_get).click();
	// clic vers une note dans un onglet
	jQuery('.spip_note['+cs_sel_jQuery+'name^=nb], .spip_note['+cs_sel_jQuery+'id^=nb]').each(function(i) {
		jQuery(this).click(function(e){
			var href = this.href.substring(this.href.lastIndexOf("#"));
			jQuery(href).parents('.onglets_contenu').eq(0).montre_onglet();
			return true;
		});
	});
  }
}

function get_onglet(url) {
 tab=url.search.match(/[?&]onglet=([0-9]*)/) || url.hash.match(/#onglet([0-9]*)/);
 if (tab==null) return false;
 return tab[1];
}

var onglet_get = get_onglet(window.location);
var gloss_el = null;
var gloss_dt = null;
var gloss_dd = null;
var gloss_dl = null;

// compatibilite Ajax : ajouter "this" a "jQuery" pour mieux localiser les actions
// et tagger avec cs_done pour eviter de binder plrs fois le meme bloc
function glossaire_init() {
  if(jQuery('span.gl_js', this).length) {
	if(!jQuery('#glossOverDiv').length) {
		jQuery('body').append('<div id="glossOverDiv" style="position:absolute; display:none; visibility: hidden;"><span class="gl_dl"><span class="gl_dt">TITRE</span><span class="gl_dd">Definition</span></span></div>');
		gloss_el = document.getElementById('glossOverDiv');
		gloss_dl = gloss_el.firstChild;
		gloss_dt = gloss_dl.firstChild;
		gloss_dd = gloss_dl.lastChild;
	}
	jQuery('span.gl_mot', this).cs_todo().hover(
		function(e) {
			$this = jQuery(this);	  
			// cas du surligneur (SPIP 2)
			if(this.firstChild.className=="spip_surligne") {
				this.className = "gl_mot spip_surligne";
				this.innerHTML = this.firstChild.innerHTML;
			}
			gloss_dt.innerHTML = $this.parent().children('.gl_js')[0].title;  // titre
			gloss_dd.innerHTML = $this.parent().children('.gl_jst')[0].title; // definition
			reg = $this.css('font-size').match(/^\d\d?(?:\.\d+)?px/);
			if(reg) gloss_el.style.fontSize = reg[0];
			jQuery(gloss_el)
				.css('top', e.pageY.toString()+"px")
				.css('left', e.pageX.toString()+"px")
				.css('font-family', jQuery(this).css('font-family'));
			gloss_el.style.display    = 'block';
			gloss_el.style.visibility = 'visible';
			if(typeof jQuery.fn.offset=="function") { // plugin jquery.dimensions disponible a partir de SPIP 2
if(1) {///////////////////////// optimisation du placement, encore en test...
	var $glossOverDiv = jQuery('#glossOverDiv');
	var $gloss_dl = jQuery(gloss_dl);
	positionBy = 'auto'; // Type de positionnement : 'dessus', 'dessous', 'auto', 'mouse' (a tester...)
	ombre = 0;  // Taille d'une ombre, en pixels (non implemente)
	decalX = 2; // decalage entre le glossaire et le lien appelant
	width = 180; // largeur totale de #glossOverDiv : definie dans glossaire.css
	height = 'auto'; // hauteur totale de #glossOverDiv : 'auto' ou nombre de pixels
	// initiation verticale
	var glossHeight, wHeight;
	var linkHeight = this.offsetHeight;
	var defHeight = isNaN(parseInt(height, 10)) ? 'auto' : (/\D/g).test(height) ? height : height + 'px';
	var sTop, linkTop, posY, mouseY, baseLine;
	sTop = jQuery(document).scrollTop();
	// initiation horizontale
	var glossWidth = width + ombre;
	var linkWidth = this.offsetWidth;
	var linkLeft, posX, mouseX, winWidth;
	winWidth = jQuery(window).width();
	// c'est parti !
	linkTop = posY = $this.offset().top;
	linkLeft = $this.offset().left;
	mouseX = e.pageX;
	mouseY = e.pageY;
// securite pour l'instant
$glossOverDiv.css({margin:'0px'}); $gloss_dl.css({margin:'0px'});
	// calcul de la position horizontale : glossaire au centre du lien
	posX = Math.max(linkLeft - (glossWidth-linkWidth)/2,0);
	if (positionBy == 'dessous' || positionBy == 'dessus') { // glossaire fixe
		$glossOverDiv.css({left: posX + 'px'});
	} else {
		// au cas ou, glossaire a droite ou a gauche du lien ?
		posX2 = (linkWidth > linkLeft && linkLeft > glossWidth) || (linkLeft + linkWidth + glossWidth + decalX > winWidth)
		  ? linkLeft - glossWidth - decalX 
		  : linkWidth + linkLeft + decalX;
		// suivre la souris ?
		if (positionBy == 'mouse' || linkWidth + glossWidth > winWidth) {
		  posX = Math.max(mouseX - (glossWidth-linkWidth)/2,0); // glossaire au centre de la souris
		/*if (mouseX + 20 + glossWidth > winWidth)
			posX = (mouseX - glossWidth) >= 0 ? mouseX - glossWidth :  mouseX - (glossWidth/2);
		else posX = mouseX;*/
	}
	var pY = e.pageY;
	$glossOverDiv.css({left: (posX > 0 && positionBy != 'dessus') ? posX : (mouseX + (glossWidth/2) > winWidth) ? winWidth/2 - glossWidth/2 : Math.max(mouseX - (glossWidth/2),0)});
	}
	// calcul de la position verticale
	wHeight = jQuery(window).height();
	$glossOverDiv.css({overflow: defHeight == 'auto' ? 'visible' : 'auto', height: defHeight});
	glossHeight = defHeight == 'auto' ? Math.max($gloss_dl.outerHeight(),$gloss_dl.height()) : parseInt(defHeight,10);   
	glossHeight += ombre;
	tipY = posY;
	baseLine = sTop + wHeight;
	if (positionBy == 'dessous') tipY = posY + linkHeight + 2; // glossaire fixe sous le lien
	else if (positionBy == 'dessus') tipY = posY - glossHeight - 2; // glossaire fixe au-dessus du lien
	else if ( posX < mouseX && Math.max(posX, 0) + glossWidth > mouseX ) { // glossaire cache le lien
		if (posY + glossHeight > baseLine && mouseY - sTop > glossHeight) { 
		  tipY = posY - glossHeight - 2;
		} else { 
		  tipY = posY + linkHeight + 2;
		}
	} else if ( posY + glossHeight > baseLine ) {
		tipY = (glossHeight >= wHeight) ? sTop : baseLine - glossHeight;
	} else if ($this.css('display') == 'block' || positionBy == 'mouse') {
		tipY = pY;
	} else {
		tipY = posY /*- ombre*/;
	}
	$glossOverDiv.css({top: tipY + 'px'});
}////////////////////////////////////////

			} // typeof jQuery.fn.offset=="function"
			gloss_el.style.visibility = 'visible';
		},
		function(e) {
			gloss_el.style.display    = 'none';
			gloss_el.style.visibility = 'hidden';
		}
	);

	// accessibilite au clavier
	if(typeof jQuery.fn.offset=="function") { // plugin jquery.dimensions disponible a partir de SPIP 2
		jQuery('a.cs_glossaire').focus(
			function() {
				legl_mot = this.firstChild;
				gloss_dt.innerHTML = jQuery(this).children('.gl_js')[0].title;  // titre
				gloss_dd.innerHTML = jQuery(this).children('.gl_jst')[0].title; // definition
				reg = jQuery(this.firstChild).css('font-size').match(/^\d\d?(?:\.\d+)?px/);
				if(reg) gloss_el.style.fontSize = reg[0];
				var result = jQuery(this).offset({ scroll: false });
				jQuery(gloss_el)
					.css('top',result.top+"px")
					.css('left', result.left+"px")
					.css('font-family', jQuery(this.firstChild).css('font-family'));
				gloss_el.style.display    = 'block';
				gloss_el.style.visibility = 'visible';
			}
		);
		jQuery('a.cs_glossaire').blur(
			function() {
				gloss_el.style.display    = 'none';
				gloss_el.style.visibility = 'hidden';
			}
		);
	} // typeof jQuery.fn.offset=="function"
  }
}
var cs_init = function() {
	onglets_init.apply(this);
	glossaire_init.apply(this);
}
if(typeof onAjaxLoad=='function') onAjaxLoad(cs_init);
if(window.jQuery) {
var cs_sel_jQuery=typeof jQuery(document).selector=='undefined'?'@':'';
var cs_CookiePlugin="prive/javascript/jquery.cookie.js";
jQuery(document).ready(function(){
	cs_init.apply(document);
});
}
