// Used by permanav list
function derouler (element) {
	jQuery('#' + element + ', #liste' +element).mouseover( function() { jQuery('#liste' + element).show(); });
	jQuery('#zone' + element).mouseout( function() { jQuery('#liste' +element).hide(); });
}

// Nouvelle version de l'infobulle Lexique
this.lexiquePreview = function(){

	/* CONFIG */
		// ces 2 variables définissent la distance de la bulle par rapport au curseur
		xOffset = -20;
		yOffset = 20;
	/* END CONFIG */

	jQuery("a.linkToLexique").hover(function(e){
		this.t = this.title;
		this.r = this.rel;
		this.title = "";
		this.rel = "";
		var c = (this.t != "") ? "" + this.t : "";
		jQuery("body").append("<div id='infobulle'><p>"+ c +"</p><p><a href='"+ this.href +"' />"+ this.r +"</a></p></div>");
		jQuery("#infobulle")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
	},
	function(){
		this.title = this.t;
		this.rel = this.r;
		jQuery("#infobulle").remove();
	});
	jQuery("a.linkToLexique").mousemove(function(e){
		jQuery("#infobulle")
		.css("top",(e.pageY - xOffset) + "px")
		.css("left",(e.pageX + yOffset) + "px");
	});
};

// This first call prevent an IE6 interpreter bug
jQuery(document).ready(function(){});

jQuery(document).ready(function(){

	// On initialise la fonction "menu déroulant" pour les éléments de la permanav
	derouler("TypeVisiteur");
	derouler("Langue");

	// Open close for panel
	if ( jQuery.cookie('Poweo_panneau') == 'off' ) {
		jQuery('#contentpanneau, #closepanneau').hide();
		jQuery('#openpanneau').show();
	}
	jQuery('#closepanneau a, #openpanneau a').click(function(){
		if ( jQuery.cookie('Poweo_panneau') == 'off' ) {
			jQuery.cookie('Poweo_panneau', 'on', { path: '/' });
		} else {
			jQuery.cookie('Poweo_panneau', 'off', { path: '/' });
		}
		jQuery('#contentpanneau, #closepanneau, #openpanneau').slideToggle('fast'); return false;
	});
	
	// On lance le script de l'infobulle lexique
	lexiquePreview();
	
	// Ancienne version de l'infobulle lexique
	/*jQuery('.bubbleInfo').each(function () {
		// options
		var distance = -18;
		var time = 150;
		var hideDelay = 200;
		var hideDelayTimer = null;
		// tracker
		var beingShown = false;
		var shown = false;
		var trigger = jQuery('.trigger', this);
		var popup = jQuery('.popup', this).css('opacity', 0);
		// set the mouseover and mouseout on both element
		jQuery([trigger.get(0), popup.get(0)]).mouseover(function () {
			// stops the hide event if we move from the trigger to the popup element
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			// don't trigger the animation again if we're being shown, or already visible
			if (beingShown || shown) {
				return;
			} else {
				beingShown = true;
				// reset position of popup box
				popup.css({
					top: 0,
					left: 0,
					display: 'block' // brings the popup back in to view
				})
				// (we're using chaining on the popup) now animate it's opacity and position
				.animate({
					top: '-=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					// once the animation is complete, set the tracker variables
					beingShown = false;
					shown = true;
				});
			}
		}).mouseout(function () {
			// reset the timer if we get fired again - avoids double animations
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			// store the timer so that it can be cleared in the mouseover if required
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				popup.animate({
					top: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					// once the animate is complete, set the tracker variables
					shown = false;
					// hide the popup entirely after the effect (opacity alone doesn't do the job)
					popup.css('display', 'none');
				});
			}, hideDelay);
		});
	});*/

/* Scripts spécifique à IE6 */
	if(jQuery.browser.msie== true && jQuery.browser.version.substr(0,1) < '7'){
		// ce script force IE6 à mettre en cache les images utilisées en background CSS
		// document.execCommand("BackgroundImageCache",false,true);
		// activation du menu déroulant de la navigation principale pour IE6
		setHover('nav');
	}
});

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

/*
Deuxieme version pour les menus déroulants
Actif pour la navigation principale sous IE6
*/
function hover(obj){
	if(document.all){
		UL = obj.getElementsByTagName('ul');
		if(UL.length > 0){
			sousMenu = UL[0].style;
			if(sousMenu.display == 'none' || sousMenu.display == ''){
				sousMenu.display = 'block';
			}else{
				sousMenu.display = 'none';
			}
		}
	}
}
//ajout du "forcing" de style pour maintenir le fonctionnement sous IE6
function setHover(id){
	LI = document.getElementById(id).getElementsByTagName('li');
	nLI = LI.length;
	//ajout (test sur la class="current" pour l'élément en cours)
	exp = new RegExp("^(current )","gi");
	//fin ajout
	for(i=0; i < nLI; i++){
		//ajout (affichage du background de la bonne couleur pour l'élément encours)
		theClass = LI[i].className;
		if (theClass.match(exp)) {
			color = theClass.substring(8);
			LI[i].style.background = 'url(fileadmin/templates/images-template/'+color+'/bg-navigation-principale-over.png)';
		} else {
			LI[i].style.background = 'none';
		}
		//fin ajout
		LI[i].onmouseover = function(){
			hover(this);
			//ajout (affichage du background de la bonne couleur pour l'élément survolé)
			testClass = this.className;
			if (testClass.match(exp)) {
				color = testClass.substring(8);
				this.style.background = 'url(fileadmin/templates/images-template/'+color+'/bg-navigation-principale-over.png)';
			} else {
				color = this.className;
				this.style.background = 'url(fileadmin/templates/images-template/'+color+'/bg-navigation-principale-over.png)';
				Links = this.getElementsByTagName('a');
				Links[0].style.color = '#FFF';
			}
			//fin ajout
		}
		LI[i].onmouseout = function(){
			hover(this);
			//ajout (retrait du background sur le mouseOver)
			testClass = this.className;
			if (testClass.match(exp)) {
				color = testClass.substring(8);
				this.style.background = 'url(fileadmin/templates/images-template/'+color+'/bg-navigation-principale-over.png)';
			} else {
				this.style.background = 'none';
				Links = this.getElementsByTagName('a');
				Links[0].style.color = '#333';
			}
			//fin ajout
		}
	}
}