var popupOpts = {
	autoOpen:'false',
	modal:'true',
	height:'500',
	width:'500',
	title:'',
	close:function() {jQuery(this).dialog('destroy');jQuery('iframe,object').css('visibility','visible');}
}

function initPopup() {

	// preparation des liens
	jQuery('.opener').each (function() {

		if (jQuery(this).hasClass('doLoad')) {
			var myRel 		= jQuery(this).attr('rel');
			var myCleanRel 	= myRel.substring(0,myRel.lastIndexOf('#'));
			var myId		= myRel.substring(myRel.lastIndexOf('#')+1);;
			
			var myHref		= jQuery(this).attr('href');
			var myUrlParam	= myHref.substring( myHref.lastIndexOf('?')+1 , myHref.lastIndexOf('='));
			var cleanHref 	= location.href.substring(0,location.href.lastIndexOf('?'));
			
			jQuery(this).attr('href' , cleanHref + '?' + myUrlParam + '=' + myId);
			jQuery(this).attr('rel' , myCleanRel);
		}
	});

	// ouverture de popup
	jQuery('.opener').click(function(e) {
	
		if (!jQuery(this).hasClass('doLoad'))
		e.stopPropagation();
	
		// on repere la popup a ouvrir (doit etre specifiee par l'attribut rel du lien)		
		var $popup = jQuery('#'+jQuery(this).attr('rel'));
		
		if ($popup.hasClass('dims')) { /* dimensions spécifiques indiquées par la classe css */
			var classes = $popup.attr('class').split(' ');
			for (var i=0; i<classes.length; i++) {
				if (classes[i].substr(0,2)=='h-') popupOpts.height = classes[i].substr(2);
				if (classes[i].substr(0,2)=='w-') popupOpts.width = classes[i].substr(2);				
			}
		} else { /* calcul des dimensions */
			var propWidth = (jQuery('body').innerWidth())*2/3;
			var propHeight = (window.innerHeight)*2.5/3;
			popupOpts.height = propHeight;
			popupOpts.width = propWidth;
		}
		
		// on prepare les options
		var title = (jQuery(this).attr('title')!=null || jQuery(this).attr('title')!=undefined) ? jQuery(this).attr('title') : '';			
		popupOpts.title = title + ' - ' + document.title;
		
		// gestion des galleries
		var $wowslider = $popup.find('.wowslider').first();
				
		if ($wowslider.length) {
		
			var wowId = $wowslider.attr('id');
			var wowImgId = $wowslider.find('>div').first().attr('id');
		
			$wowslider
				.attr('id','wowslider-container')
				.find('>div').attr('id','wowslider-images');
				
			popupOpts.open = function() {
				WowSlider({
					autoPlay:true,
					bullets:0,
					caption:false,
					controls:true,
					delay:4000,
					duration:1000,
					effect:"fade",
					height:450,
					width:800,
					outWidth:820,
					outHeight:470,
					next:"",
					prev:""
				});
			}
			
			popupOpts.close = function() {
				$wowslider.attr('id',wowId); $wowslider.find('>div').first().attr('id',wowImgId);
  			}
  		
  		}
		
		// masque des iframes et objects		
		jQuery('iframe:not(.DoNotHide),object:not(.DoNotHide)').css('visibility','hidden');
		
		// ouverture de la popup		
		$popup.dialog(popupOpts);
		
		if (!jQuery(this).hasClass('doLoad'))
		return false;
	});
	
	// déclenchement de l'ouverture sauf si spécifié (si plusieurs popups trouvés, seul le 1er est retenu)
	var $toOpen = jQuery('.opener:not(.byClick)').first();
	$toOpen.trigger('click');

}

jQuery(document).ready(initPopup);

(function($) {	
	function all() {
		
		
		/* Widget digiclock http://www.radoslavdimov.com/jquery-plugins/jquery-plugin-digiclock/ */
		if ($('#digiclock').length) {
			$('#digiclock').jdigiclock({
	            clockImagesPath : 'images/digiclock/clock/',
	            weatherImagesPath : 'images/digiclock/weather/',
	            weatherLocationCode : 'EUR|FR|FR021|NICE'
	        });
		}
		
		
		/* Sticky menu http://designwoop.com/2011/01/how-to-create-a-jquery-sticky-sidebar/ */
		
		if ($('.sticky').length) {
    		$('.sticky').stickyfloat({ duration: 800, offsetY:20 });
		}
		
		
		/* Carte réseau 
		 * en survol sur une area, affiche le li dont le rel correspond à l'id de l'area
		 */
		 
		/* Au chargement seulement zone centre affichée */
		$('#agences li').not(':nth(1)').fadeOut();
		
		/* En survol affichage des coordonnées de la zone survolée */
		$('#map_reseau area').hover(function() {
			$agenceId = $(this).attr('id');

			if ($('#agences li:visible').attr('rel') != $agenceId && !$('#map_reseau').hasClass('blocked') ) {
				
				/* éteint l'agence affichée */
				$('#agences li:visible').hide();
				$('#agences li[rel=' + $agenceId + ']').fadeIn(200);
				
			}
		
		/* En clic blocage de l'affichage... jusqu'à ce que la carte soit recliquée n'importe où */
		}).click(function() {
			$('#map_reseau').toggleClass('blocked');
			return false;
		});
		
		
		/* En quittant la carte on réaffiche l'adresse principale si la carte n'est pas en état bloqué */
		$('#mapContainer').mouseleave(function() {
			
			if ( !$('#map_reseau').is('.blocked') ) {
				$('#agences li').not(':nth(1)').hide();
				$('#agences li:nth(1)').fadeIn(100);
				
			}
		});
		
		
		/* Explorateur de fichiers */
		$('li.folder ul').slideUp();
		$('li.folder a.folder-name').click(function() { $(this).toggleClass('open').next('ul').slideToggle(); return false; });
		
		
		/* Hauteurs des menus gauches sticky float */
		if ($('.colonne_gauche.sticky').length) {
			var myHeight = $('.colonne_gauche.sticky').height();
			myHeight += 10;
			$('#full_hauteur').css('min-height', myHeight + 'px');
		}
		
				
		//---------------------------------
		// swap images
		// !! IMPORTANT : 
		// - s'applique à un élément avec une classe "imgSwapper", contenant 1 seule image
		// - convention de nommage pour les images : état normal "monImage.ext" / état hover "monImage_2.ext"
		//---------------------------------
		var swapped_src = Array();
		var swapped_ext = Array();
		$('.imgSwapper')
			.each(function(i) {
				var mySrc = $('img', this).attr('src');
				var myExtPos = mySrc.lastIndexOf('.');
				swapped_src.push( mySrc.substring(0,myExtPos) );
				swapped_ext.push( mySrc.substring(myExtPos) );
				$(this).hover(function() { $('img', this).attr( 'src', swapped_src[i] + '_2' + swapped_ext[i] ); });
				$(this).mouseleave( function() { $('img', this).attr( 'src', swapped_src[i] + swapped_ext[i] ); });
			}
		);
		
				
		//---------------------------------
		// check all formulaire contact
		//---------------------------------
		var $allChecker = $('input.all');
		if ($allChecker.length) var myColor = $allChecker.first().closest('td').next('td').find('label').css('color');
		
		$allChecker.each( function(i,elmt) {
			if( $(elmt).is(':checked') && $(elmt).val() == 'oui' ) {
				var $myCheckboxes = $('table[id=' + $(elmt).attr('rel') + ']').find('input[type=checkbox]');
				var $myLabels = $('table[id=' + $(elmt).attr('rel') + ']').find('input[type=checkbox] + label');
				$myCheckboxes.attr('checked','checked');
				$myCheckboxes.attr('disabled','disabled');
				$myLabels.css('color','#ccc');
				$(elmt).attr('checked','checked');
			}
		});
		
		$allChecker.click(
			
			function() {
				var $myCheckboxes = $('table[id=' + $(this).attr('rel') + ']').find('input[type=checkbox]');
				var $myLabels = $('table[id=' + $(this).attr('rel') + ']').find('input[type=checkbox] + label');

				if( $(this).val() == 'non' ) {
					$myCheckboxes.removeAttr('checked');
					$myCheckboxes.removeAttr('disabled');
					$myLabels.css('color',myColor);
				}
				else {
					$myCheckboxes.attr('checked','checked');
					$myCheckboxes.attr('disabled','disabled');
					$myLabels.css('color','#ccc');
				}
				return true;
			}
			
		);
		
	}	
	$(document).ready(
		function() {
			all();
		}
	);
})(jQuery.noConflict());
