

/**
 * Calcule la somme des margins, padding et border des elements présents dans emp_top
 * @return
 */
function showSlideShowPleaseWait(){
	// CSS du pleasewait
	$('.slideshow_pleasewait').each(function(){
		
		nWidthWait = $(this).closest('.plugin').width();
		nHeightWait = $(this).closest('.plugin').height();
//		alert(nWidthWait);
		$(this).css({'display':'block', 'position' : 'absolute', 'z-index' : '15', 'width':nWidthWait+'px',  'height':nHeightWait+'px'});		
	});
	
		

}

/**
 * Calcule la somme des margins, padding et border des elements présents dans emp_top
 * Récursif !!
 * @param $selector
 * @return
 */
function get_SumPadMarBor_Emptop($selector){
	
	var nHeight_PadMarBor = ($selector.outerHeight(true) - $selector.height());
	
	if($selector.attr('class') == 'galerie'){
		return nHeight_PadMarBor;
	}
	else{
		$child = $selector.children(':first-child');
		
		//Si on a un encart, on ignore header et on se base sur repeater
		$child = ($child.hasClass('header')) 	?  $selector.children('.repeater') 	: $child;
		
		//Si on a l'intitulé du plg, on l'ignore
		$child = ($child.hasClass('plgtitle')) 	?  $selector.children('.galerie') 	: $child;	
		
		return nHeight_PadMarBor + get_SumPadMarBor_Emptop($child);
	}
	
}

/**
 * Resize le slider en emp top si height determinée en css et seul plugin présent
 * @return
 */
function resize_slider_emptop(){
		
	var nSum_PadMarBor_Emptop = get_SumPadMarBor_Emptop($('#emp_top'));
	
	var nHeight_Txt 				= $('#emp_top .galerie .item_texte').outerHeight(true);
	var nHeight_MoreInfo 			= $('#emp_top .plugin .moreinfo').outerHeight(true);
	var nHeight_PadMarBor_Slider 	= $('#emp_top .container_slider').outerHeight(true) - $('#emp_top .container_slider').height();
	
	var nHeight_Header 				= ($('#emp_top .plugin .header').length == 1) ? $('#emp_top .plugin .header').outerHeight(true) : 0;
	var nHeight_Footer 				= ($('#emp_top .plugin .footer').length == 1) ? $('#emp_top .plugin .footer').outerHeight(true) : 0;

	var nHeight_Title 				= ($('#emp_top .plugin .plgtitle').length == 1 && $('#emp_top .plugin .header').length == 0) ? $('#emp_top .plugin .plgtitle').outerHeight(true) : 0;
	
	
	var nNewHeight = $('#emp_top').height() - nSum_PadMarBor_Emptop - nHeight_Txt - nHeight_MoreInfo - nHeight_PadMarBor_Slider - nHeight_Header - nHeight_Footer - nHeight_Title;
	
	return nNewHeight;
}


/**
 * Fonction qui permet d'init les slide fade
 * Simple jQuery Slideshow Script
 * Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, 
 * not responsible for anything, etc.  Please link out to me if you like it :)
 * @author LEDSYSTEMS - Estelle
 */
function init_slidefade(){
	
	//Initialisations des slide fade de plugin galerie	
	$('.plugin_18 .slide_fade').each(function(){
//		var bCrop		= ($(this).attr('data-crop') == 1) 		? true : false;	
//		var bAuto		= ($(this).attr('data-autoplay') == 0) 	?  false : true;
//		var sTheme		= $(this).attr('data-theme');
//		var bBtnPause 	= ($(this).attr('data-button') == 1) ? true : false;
		
		var nDataW = $(this).attr('data-w'); 
		var nDataH = $(this).attr('data-h');
		
		var nContainer_h = nContainer_w = '';		
		var nEmpParentW = $(this).parent().width() - ($(this).outerWidth(true) - $(this).width());
		
		if(nDataW != '' || nDataH != ''){			

			//Vérification si width paramétrée, pas plus grande que le emp content
			if(nDataW != '')
				nContainer_w = (nDataW > nEmpParentW) ? nEmpParentW : nDataW;
			else
				nContainer_w = nEmpParentW;
		
			if(nDataH != '')
				nContainer_h = nDataH;
			else
				nContainer_h = (nContainer_w*3)/4;			
			
		}
		else{			
			nContainer_w = nEmpParentW;
			nContainer_h = (nContainer_w*3)/4;	
		}
		
		$(this).width(nContainer_w);
		$(this).height(nContainer_h);				
		
//		$(this).find('img:first').addClass('active');
		
		$(this).find('.item_img').each(function(){
			
			var nImgHeight 		= $(this).height();														
			var nImgWidth 		= $(this).width();														
			
			
			// Calcule les dimensions de l'image si on la calque sur la hauteur du parent
			var nNewImgHeight_OnH	= nContainer_h;                                                
			var nNewImgWidth_OnH 	= Math.ceil(nImgWidth * (nContainer_h / nImgHeight));    
			
			// Calcule les dimensions de l'image si on la calque sur la largeur du parent
			var nNewImgWidth_OnW	= nContainer_w;													
			var nNewImgHeight_OnW 	= Math.ceil(nImgHeight * (nContainer_w / nImgWidth));		
			
			// Définit les nouvelles dimensions de l'image
			nImgWidth	= (nNewImgWidth_OnH >= nContainer_w) 	? nNewImgWidth_OnH 	: nNewImgWidth_OnW;	 		
			nImgHeight 	= (nNewImgHeight_OnW >= nContainer_h) 	? nNewImgHeight_OnW : nNewImgHeight_OnH;				
			
			$(this).css({'width':'auto', 'height':nImgHeight+'px'});
			
//			no crop
//			$(this).css({'width':nContainer_w+'px', 'height':'auto', 'border':'none', 'padding':'0', 'margin':'0'});
//			nImgHeight  = Math.ceil(nImgHeight * (nContainer_w / nImgWidth));
//			nImgWidth	= nContainer_w;
//			
//			// Redéfinit dimensions de <img> si plus haute que le conteneur
//			if (nImgHeight > nContainer_h){
//				$(this).css({'height':nContainer_h+'px', 'width':'auto'});
//				nImgWidth  = Math.ceil(nImgWidth * (nContainer_h / nImgHeight));
//				nImgHeight = nContainer_h;
//			}				
						
		});	

		//Positionnement de la légende 
//		var nTopLeg = nWindowHeight - $('.container_legende').height();		
//		$(this).find('.container_legende').css({'top' : nTopLeg, 'width' : nWindowWidth});

	});
	
};

/**
 * Fonction qui permet d'animer un slide fade
 * Simple jQuery Slideshow Script
 * Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, 
 * not responsible for anything, etc.  Please link out to me if you like it :)
 * @author LEDSYSTEMS - Estelle
 */
function slideSwitch(){
	
	var $active = $('.slide_fade img.active');	
	
	if ($active.length == 0 ) $active = $('.slide_fade img:last');
	
//	$active_li =  $active.parent();
	
	// use this to pull the images in the order they appear in the markup
//	var $next =  $active_li.next().find('img').length ? $active_li.next().find('img') : $('.slide_fade img:first');
	var $next =  $active.next().length ? $active.next() : $('.slide_fade img:first');
	
	// uncomment the 3 lines below to pull the images in random order
	
	// var $sibs  = $active.siblings();
	// var rndNum = Math.floor(Math.random() * $sibs.length );
	// var $next  = $( $sibs[ rndNum ] );
	
	
	$active.addClass('last-active');
	
	$next.css({opacity: 0.0})
	.addClass('active')
	.animate({opacity: 1.0}, 1000, function() {
		$active.removeClass('active last-active');
	});
};


/**
 * Fonction qui permet d'afficher un slideshow
 * https://github.com/ProLoser/AnythingSlider/wiki/
 * @author LEDSYSTEMS - Estelle
 */
function init_slideshow(){
	
	console.log('*** load_slideshow()');

	//Affiche la légende d'une image de plugin galerie
	var showCaption = function(slider){	
		
		slider.$window.find('.legende').hide().animate({'opacity': 0, 'filter' :'alpha(opacity=0)','-moz-opacity' : 0});	
		
		// Affiche légende
		slider.$currentPage.find('.legende').show().animate({'opacity' : 1, 'filter' : 'alpha(opacity=100)', '-moz-opacity' : 1 }, 1500);
	};
	
	var sTheme = 'perso';
	
	//Initialisations des slider de plugin galerie	
	$('.plugin_18 .container_slider:visible').each(function(){
		var bCrop		= ($(this).attr('data-crop') == 1) 		? true : false;	
		var bAuto		= ($(this).attr('data-autoplay') == 0) 	?  false : true;
		var sTheme		= $(this).attr('data-theme');
		var bBtnPause 	= ($(this).attr('data-button') == 1) ? true : false;
		
		var nDataW = $(this).attr('data-w'); 
		var nDataH = $(this).attr('data-h');
		
		var nContainer_h = nContainer_w = '';		
		var nEmpParentW = $(this).parent().width() - ($(this).outerWidth(true) - $(this).width());
		
		if(nDataW != '' || nDataH != ''){			

			//Vérification si width paramétrée, pas plus grande que le emp content
			if(nDataW != '')
				nContainer_w = (nDataW > nEmpParentW) ? nEmpParentW : nDataW;
			else
				nContainer_w = nEmpParentW;
		
			if(nDataH != '')
				nContainer_h = nDataH;
			else
				nContainer_h = (nContainer_w*3)/4;			
			
		}
		else{			
			nContainer_w = nEmpParentW;
			nContainer_h = (nContainer_w*3)/4;	
		}			
		
		var nHeightWait = parseInt(nContainer_h) + parseInt($(this).outerHeight(true) - $(this).height());

		var nWidthWait 	= $(this).parent().width();
		
		$(this).width(nContainer_w);
		$(this).height(nContainer_h);				
		
				
		$(this).find('ul.slider').anythingSlider({
			pauseOnHover    : true,
			autoPlay 		: bAuto,
//			enablePlay 		: false,
			buildNavigation : true,
//			buildNavigation : false,
			expand 			: true,
			autoPlayLocked 	: bAuto,
			delay           : 5000,
			animationTime 	: 2000,
			theme			: sTheme,  
			startText       : "&nbsp;",   
			stopText        : "&nbsp;",
			easing 			: "easeInOutCubic",
			
			// Callback before slide animates
			onInitialized : function(e, slider) {
				showCaption(slider); 				
			},		    
			// Callback when slide completes - no event variable!
			onSlideComplete: function(slider) { 
				showCaption(slider); 
			}			
		});		
		
		$(this).find('.arrow').hide();

		//Dimensions attribuées
		var nWindowWidth	= $(this).find('.anythingWindow').width();									
		var nWindowHeight	= $(this).find('.anythingWindow').height();											
	
		$(this).find('.item_img').each(function(){
			
			var nImgHeight 		= $(this).height();														
			var nImgWidth 		= $(this).width();														
			
			// Si CROP
			if (bCrop){
								
				// Calcule les dimensions de l'image si on la calque sur la hauteur du parent
				var nNewImgHeight_OnH	= nWindowHeight;                                                
				var nNewImgWidth_OnH 	= Math.ceil(nImgWidth * (nWindowHeight / nImgHeight));    
				
				// Calcule les dimensions de l'image si on la calque sur la largeur du parent
				var nNewImgWidth_OnW	= nWindowWidth;													
				var nNewImgHeight_OnW 	= Math.ceil(nImgHeight * (nWindowWidth / nImgWidth));		
				
				// Définit les nouvelles dimensions de l'image
				nImgWidth	= (nNewImgWidth_OnH >= nWindowWidth) 	? nNewImgWidth_OnH 	: nNewImgWidth_OnW;	 		
				nImgHeight 	= (nNewImgHeight_OnW >= nWindowHeight) 	? nNewImgHeight_OnW : nNewImgHeight_OnH;				
				
				$(this).css({'width':'auto', 'height':nImgHeight+'px'});
				
			}
			// Si NOCROP
			else {			
				
				$(this).css({'width':nWindowWidth+'px', 'height':'auto', 'border':'none', 'padding':'0', 'margin':'0'});
				nImgHeight  = Math.ceil(nImgHeight * (nWindowWidth / nImgWidth));
				nImgWidth	= nWindowWidth;
				
				// Redéfinit dimensions de <img> si plus haute que le conteneur
				if (nImgHeight > nWindowHeight){
					$(this).css({'height':nWindowHeight+'px', 'width':'auto'});
					nImgWidth  = Math.ceil(nImgWidth * (nWindowHeight / nImgHeight));
					nImgHeight = nWindowHeight;
				}				
			}			
		});	

		//Positionnement de la légende 
		var nTopLeg = nWindowHeight - $('.container_legende').height();		
		$(this).find('.container_legende').css({'top' : nTopLeg, 'width' : nWindowWidth});
		
		//Positionnement des fleches dans la legende si theme "europexpo"
		if(sTheme == 'europexpo'){
			nTopLeg = nTopLeg + ($(this).find('.container_legende').height()/2) - ($(this).find('.arrow').height()/2);
			$(this).find('.arrow').css({'top' : nTopLeg});
		}

		//Pas de redim pour europexpo
		if($('body').attr('data-siteid') != "47"){
//			alert('pos slider');
			
			$(this).siblings('.slideshow_pleasewait').css({ 'height':$(this).height()+'px'});

//			alert('pos slider');
		}
		
		if(!bBtnPause){
			$(this).find('.anythingControls').hide();		
		}
	});
		
	$('.plugin_2 .container_slider:visible, .plugin_4 .container_slider:visible').each(function(){
		
		var nContainer_w = (typeof($(this).closest('.repeater').attr('class')) != "undefined") ? $(this).closest('.repeater').width() : $(this).closest('.plugin').width();		
		
		var nContainer_h = 0;
		
		if(typeof($(this).closest('#emp_top').attr('id')) != "undefined"){
			$(this).find('.item').each(function(){
				var nHeight = $(this).find('.gallery').height()+$(this).height();
				
				nContainer_h = (nHeight > nContainer_h) ? nHeight : nContainer_h;
			});
		}
		else{
			$(this).find('.item').each(function(){
				
				var nHeight = $(this).height();
				
				console.log($(this).find('.widget').height());
				console.log($(this).height());
//				var nHeight = $(this).height()+$(this).find('.widget').height();
				
				nContainer_h = (nHeight > nContainer_h) ? nHeight : nContainer_h;
			});
		}
		
		$(this).width(nContainer_w);
		$(this).height(nContainer_h);		
		
		$(this).find('ul.slider').anythingSlider({
			pauseOnHover    : true,
			enablePlay 		: false,
			buildNavigation : false,
			expand 			: true,
			autoPlayLocked 	: true,
			delay           : 6000,
			animationTime 	: 2000,
			theme			: 'perso', 
			easing 			: "easeInOutCubic"	
		});
		
		$(this).find('.anythingControls').hide();		
	});
	
	$('.thumbNav').hide();
};


function redim_images_in_slider(){
	
	console.log('*** redim_images_in_slider()');	
	
	$('.plugin_18 .container_slider:visible').each(function(){
		
		var nWindowWidth	= $(this).find('.anythingWindow').width();
		var nWindowHeight	= $(this).find('.anythingWindow').height();
		
		var bCrop = ($(this).attr('data-crop') == 1) ? true : false;
		
		$(this).find('li').each(function(){
			
			$img = $(this).find('img');
			
			var nImgHeight 		= $img.height();
			var nImgWidth 		= $img.width();
			
			var nTop 	=	Math.ceil(((nWindowHeight/2) - (nImgHeight/2)));
			var	nLeft 	= 	Math.ceil((nWindowWidth - nImgWidth)/2);			
			
			if(bCrop){				
				
				// Centre li en Y
				if (nImgHeight > nWindowHeight){
					$img.css({'top':nTop+'px', 'position' : 'relative'});		
				}
				
				// Centre li en X
				if (nImgWidth > nWindowWidth){
					$img.css({'left':nLeft+'px', 'position' : 'relative'});		
				}
			}
			else{
				
				// Centre li en Y
				if (nImgHeight < nWindowHeight){
					$img.css({'top' : nTop+'px', 'position' : 'relative'});				
				}
				
				// Centre li en X
				if (nImgWidth < nWindowWidth){
					$img.css({'left' : nLeft+'px', 'position' : 'relative'});
				}
			}
			
		});
		
		$(this).siblings('.slideshow_pleasewait').hide();
		$(this).find('.arrow').show();
	});
	
	$('.plugin_18 .slide_fade').each(function(){
		
		var nWindowWidth	= $(this).width();
		var nWindowHeight	= $(this).height();
		
		var bCrop = ($(this).attr('data-crop') == 1) ? true : false;
		
		$(this).find('img').each(function(){
			
			$img = $(this);
			
			var nImgHeight 		= $img.height();
			var nImgWidth 		= $img.width();
			
			var nTop 	=	Math.ceil(((nWindowHeight/2) - (nImgHeight/2)));
			var	nLeft 	= 	Math.ceil((nWindowWidth - nImgWidth)/2);			
			
//			if(bCrop){				
//				
//				// Centre li en Y
				if (nImgHeight > nWindowHeight){
					$img.css({'top':nTop+'px'});		
				}
				
				// Centre li en X
				if (nImgWidth > nWindowWidth){
					$img.css({'left':nLeft+'px'});		
				}
//			}
//			else{
				
				// Centre li en Y
//				if (nImgHeight < nWindowHeight){
//					$img.css({'top' : nTop+'px'});				
//				}
//				
//				// Centre li en X
//				if (nImgWidth < nWindowWidth){
//					$img.css({'left' : nLeft+'px'});
//				}
//			}
			
		});
	});
};

