(function ($) {
    // VERTICALLY ALIGN FUNCTION
    $.fn.vAlign = function() {
      return this.each(function(i) {
        var ah = $(this).height();
        var ph = $(this).parent().height();
        var mh = (ph - ah) / 2;
        $(this).css('margin-top', mh);
      });
    };
  })(jQuery);

$(document).ready(function(){

	//etudes accordion
	$("#etudes dd").first().show().toggleClass("active");
	$("#etudes dt").click(function(){
	  $(this).next("dd").slideToggle("slow")
	  .siblings("dd:visible").slideUp("slow").fadeOut("fast");
	  $(this).toggleClass("active");
	  $(this).siblings("dt").removeClass("active");
	});
	
	//actus href
	$(".news h3").click(function(){
		if ($(this).siblings().find("a.postlink").attr("href")) {
		  window.location=$(this).siblings().find("a.postlink").attr("href"); return false;
		}
	});
	
	
	
	$('#frame').vAlign();

	//jQuery.HoverIntent
		function megaHoverOver(){
		    $(this).find("ul").stop().fadeTo('fast', 1).show();
		}
		function megaHoverOut(){
		  $(this).find("ul").stop().fadeTo('fast', 0, function() {
		      $(this).hide();
		  });
		}
		var config = {
		     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
		     interval: 100, // number = milliseconds for onMouseOver polling interval
		     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
		     timeout: 200, // number = milliseconds delay before onMouseOut
		     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
		};
		
	$("ul#navigation li ul").css({'opacity':'0'});
	$("ul#navigation li").hoverIntent(config);
	
	//tab menu
	/*$submenu = $("#tabs").tabs({
		selected: -1 },{
		fx: { height: 'toggle', opacity: 'toggle' }},{ 
 		select: function(event, ui){  document.location.hash =  "_" + $(ui.panel).attr("id");}
	}); 
	$submenu.tabs('select', document.location.hash.slice(2));
	$('#navigation li.active ul li a').click(function() {
		var $link = $(this).attr ( "href" ).substring ($(this).attr ( "href" ).lastIndexOf ( '#' ) + 2 )
	    $submenu.tabs('select', $link);
	    return false;
	});*/	

	//lightbox
	$("a:has(img):not(a#logo)").fancybox({
					'titleShow'		: true,
					'transitionIn'	: 'elastic',
					'transitionOut'	: 'elastic'
	});
	
	
	$("#galerie a").fancybox({
		'showCloseButton'	: false,
		'titlePosition'		: 'inside',
		'titleFormat'		: function (title, currentArray, currentIndex, currentOpts) {
		    return '<div id="galerie-titre"><span><a href="javascript:;" onclick="$.fancybox.close();"><img src="/data/closelabel.gif" /></a></span>' + (title && title.length ? '<b>' + title + '</b>' : '' ) + 'Photo ' + (currentIndex + 1) + ' de ' + currentArray.length + '</div>';
		}
	});

});
