/** jQuery intoViewport plugin **/
(function($){jQuery.fn.intoViewport=function(options){options=$.extend({duration:200,easing:"swing"},options||{});return this.each(function(){function scrTo(dest){$("html,body").stop().animate({scrollTop:dest},options);}var scr=$(document).scrollTop()||$(window).scrollTop(),wheight=$(window).height(),top=$(this).offset().top,eheight=$(this).outerHeight();if(scr>top){scrTo(top);}else if(scr!=top&&top+eheight>scr+wheight){scrTo(top+Math.min(eheight-wheight,0));}});};})(jQuery);
/** /jQuery intoViewport plugin **/

/**
 * 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
 *
 */
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;
    }
};


var browser_compatible = !(jQuery.browser.msie && parseInt(jQuery.browser.version) < 7);
var browser_msie7 = jQuery.browser.msie && parseInt(jQuery.browser.version) == 7;

function trierOrdreAlpha(a,b){ 
  var keyA = $(a).text();
  var keyB = $(b).text();

  if (keyA < keyB) return -1;
  if (keyA > keyB) return 1;
  return 0;
}

$(document).ready(function() {
	/*$("#lien_rechercher").click(function () {
		$("#lien_rechercher").hide();
	 	$("#boite_recherche").animate({'opacity':'toggle'});
		$("#boite_recherche input").focus();
		event.preventDefault();
	});
	$("#boite_recherche input").blur(function () {
		$("#boite_recherche").animate({'opacity':'toggle'}, 'fast');
		$("#lien_rechercher").delay('fast').show("blind", {'direction':'vertical'});
	});
	$("#boite_recherche button").click(function() {
		window.alert("caca");
		event.preventDefault();
	});*/
	if($.cookie("adminSelectionne") == 'oui') {
		$("#topUserSection").hide();
		$("#topAdminLink").addClass("selectionned");
		$("#topUserLink").removeClass("selectionned");
		$("#topAdminSection").show();
	} else {
		$("#topAdminSection").hide();
	}
	
	
	$("#topUserLink").hover(function() {
		$("#topAdminSection").hide();
		$("#topUserSection").show();
		$("#topAdminLink").removeClass("selectionned");
		$("#topUserLink").addClass("selectionned");
		$.cookie("adminSelectionne", "non");
		event.preventDefault();
	});
	$("#topAdminLink").hover(function() {
		$("#topUserSection").hide();
		$("#topAdminSection").show();
		$("#topAdminLink").addClass("selectionned");
		$("#topUserLink").removeClass("selectionned");
		$.cookie("adminSelectionne", "oui");
		event.preventDefault();
	});
	// Prépare ze togueule
	if(browser_compatible) {
		$('.element_menu').removeClass('element_menu_declencheur');
		$(".gauche .element_menu").hoverIntent({
			timeout: 100,
			over: amenerMenuGauche,
			out: fairePartirMenuGauche,
			sensivity: 2,
			interval: 200
		});
		$(".droite .element_menu").hoverIntent({
			timeout: 100,
			over: amenerMenuDroite,
			out: fairePartirMenuDroite,
			sensivity: 2,
			interval: 200
		});
	}
	// Trier les éléments de la liste en haut
	trierUnUl('#topUserSection ul');
	trierUnUl('#topAdminSection ul');
	// Offset du fond aléatoire
	$('#barre_titre').animate({
		'background-position-x' : (1920 - Math.floor(Math.random()*1920*2)) + 'px'
	}, 20000, 'swing');
	// Faire apparaître les breadcrumbs
	$('#barre_breadcrumbs ul').show('slide', 'slow');
});

function trierUnUl(unUl) {
	var items = $(unUl + ' li').get();
	items.sort(trierOrdreAlpha);
	var ul = $(unUl);
	$.each(items, function(i, li){
	  ul.append(li);
	});
}

function amenerMenuGauche() {
	$(this).find(".barre_menu").css({
		'position' : 'absolute',
		'top' : '0px',
		'height' : ($("#conteneur_contenu1").height() - 10) + 'px',
		'left':($(".barre.gauche").first().width() - 3)+'px'
		}).stop().show('slide', 200);
	
	var offsetAMoi;
	if(window.pageYOffset) {
		offsetAMoi = window.pageYOffset - $(this).find(".barre_menu").offset().top;
	} else {
		offsetAMoi = document.documentElement.scrollTop - $(this).find(".barre_menu").offset().top;
	}

	$(this).find(".barre_menu .container").css({
		'marginTop': (Math.max(offsetAMoi, 0)) + 'px'
	});
}

function fairePartirMenuGauche() {
	if($(this).find(".barre_menu").is(":visible"))	
		$(this).find(".barre_menu").stop().hide('slide', 200);
}

function amenerMenuDroite() {
	$(this).find(".barre_menu").css({
		'position' : 'absolute',
		'top' : '0px',
		'height' : ($("#conteneur_contenu1").height() - 10) + 'px',
		'right':($(".barre.gauche").width() - 1)+'px'
		}).stop().show('slide', { direction : 'right' }, 200);
	
	var offsetAMoi;
	if(window.pageYOffset) {
		offsetAMoi = window.pageYOffset - $(this).find(".barre_menu").offset().top;
	} else {
		offsetAMoi = document.documentElement.scrollTop - $(this).find(".barre_menu").offset().top;
	}

	$(this).find(".barre_menu .container").css({
		'marginTop': (Math.max(offsetAMoi, 0)) + 'px'
	});
}

function fairePartirMenuDroite() {
	if($(this).find(".barre_menu").is(":visible"))	
		$(this).find(".barre_menu").stop().hide('slide', { direction : 'right' }, 200);
}

