/**
 * Functionality specific to Twenty Thirteen.
 *
 * Provides helper functions to enhance the theme experience.
 */

( function( $ ) {
	var body    = $( 'body' ),
	    _window = $( window );

	/**
	 * Adds a top margin to the footer if the sidebar widget area is higher
	 * than the rest of the page, to help the footer always visually clear
	 * the sidebar.
	 */
	$( function() {
		if ( body.is( '.sidebar' ) ) {
			var sidebar   = $( '#secondary .widget-area' ),
			    secondary = ( 0 == sidebar.length ) ? -40 : sidebar.height(),
			    margin    = $( '#tertiary .widget-area' ).height() - $( '#content' ).height() - secondary;

			if ( margin > 0 && _window.innerWidth() > 999 )
				$( '#colophon' ).css( 'margin-top', margin + 'px' );
		}
	} );

	/**
	 * Enables menu toggle for small screens.
	 */
	( function() {
		var nav = $( '#site-navigation' ), button, menu;
		if ( ! nav )
			return;

		button = nav.find( '.menu-toggle' );
		if ( ! button )
			return;

		// Hide button if menu is missing or empty.
		menu = nav.find( '.nav-menu' );
		if ( ! menu || ! menu.children().length ) {
			button.hide();
			return;
		}

		$( '.menu-toggle' ).on( 'click.twentythirteen', function() {
			nav.toggleClass( 'toggled-on' );
		} );
	} )();

	/**
	 * Makes "skip to content" link work correctly in IE9 and Chrome for better
	 * accessibility.
	 *
	 * @link http://www.nczonline.net/blog/2013/01/15/fixing-skip-to-content-links/
	 */
	_window.on( 'hashchange.twentythirteen', function() {
		var element = document.getElementById( location.hash.substring( 1 ) );

		if ( element ) {
			if ( ! /^(?:a|select|input|button|textarea)$/i.test( element.tagName ) )
				element.tabIndex = -1;

			element.focus();
		}
	} );

	/**
	 * Arranges footer widgets vertically.
	 */
	if ( $.isFunction( $.fn.masonry ) ) {
		var columnWidth = body.is( '.sidebar' ) ? 228 : 245;

		$( '#secondary .widget-area' ).masonry( {
			itemSelector: '.widget',
			columnWidth: columnWidth,
			gutterWidth: 20,
			isRTL: body.is( '.rtl' )
		} );
	}
    
    $('#bt-police-plus, #print').removeAttr('disabled');
    
    $('*[target="_blank"]').each(function() {
        var title = $(this).attr('title');
        if (title)
            $(this).attr('title', title + " (Ouverture dans une nouvelle fenêtre)");
        else
            $(this).attr('title', "Ouverture dans une nouvelle fenêtre");
    });
    
    $('div#acces-direct').addClass('cacherContenu');
	$('div#acces-direct').css('display', 'block');
	
	$('div#acces-direct a').focusin(function() {
		$('div#acces-direct').removeClass('cacherContenu');
	});
	$('div#acces-direct a').focusout(function() {
		$('div#acces-direct').addClass('cacherContenu');
	});

	if (jQuery('body').hasClass('home')) {
		resizeVideo();
		resizeBlocHome();
		$(window).resize(function() {
			resizeVideo();
			resizeBlocHome();
		});
	}
} )( jQuery );

function tailleTexte(bt) {
	var currentFontSize = jQuery('#main').css('font-size');
	var currentFontSizeNum = parseFloat(currentFontSize, 10);
	resizeBlocHome();
	if (jQuery(bt).val() == 'moins') {
		jQuery('#bt-police-plus').removeAttr('disabled');
		var newFontSize = currentFontSizeNum * 0.9;
		if (newFontSize < 16) {
			newFontSize = 16;
			jQuery('#bt-police-moins').attr('disabled', 'disabled');
		}
	} else {
		jQuery('#bt-police-moins').removeAttr('disabled');
		var newFontSize = currentFontSizeNum * 1.1;
		if (newFontSize > 23) {
			newFontSize = 23;
			jQuery('#bt-police-plus').attr('disabled', 'disabled');
		}
	}
	jQuery('#main').css('font-size', newFontSize);
}

function AjoutFavori()
{
    NomSite = document.title;
	UrlSite = location.href;
	if (window.sidebar.addPanel)
        window.sidebar.addPanel(NomSite, UrlSite, ""); //Mozilla, FireFox,...
	else if (window.external && typeof(window.external.AddFavorite) != 'undefined')
        window.external.AddFavorite(UrlSite, NomSite); //Internet explorer Windows
	else if (document.all && (navigator.userAgent.indexOf('Win') < 0))
        alert ("Utilisez POMME + D \n pour ajouter " + NomSite + " dans vos favoris !"); //Internet explorer MAC
	else if (window.opera && window.print)
        alert ("Utilisez CTRL + T \n pour ajouter " + NomSite + " dans vos favoris !"); //Opera
	else
        alert ("Cette fonction n'est pas disponible pour votre navigateur.");
}

function resizeVideo()
{
	var iframe = jQuery('.home .content_bloc .bloc2 iframe');
    var w = parseInt(iframe.attr('width'));
    var h = parseInt(iframe.attr('height'));
    var ratio = w / h;
    // console.log(w, h, ratio, iframe.width(), iframe.width() / ratio);
    iframe.height(iframe.width() / ratio);
}

function resizeBlocHome()
{
	jQuery('.home .content_bloc .bloc1, .home .content_bloc .bloc3').removeAttr('style');

	if (jQuery(window).width() <= 768)
		return false;

	var b1 = jQuery('.home .content_bloc .bloc1').innerHeight();
	var b2 = jQuery('.home .content_bloc .bloc2').innerHeight();
	var b3 = jQuery('.home .content_bloc .bloc3').innerHeight();

	b3 += b2 + 15;

	var h = Math.max(b1, b3);
	jQuery('.home .content_bloc .bloc1').height(h);
	jQuery('.home .content_bloc .bloc3').height(h - b2 - 20);
}