
/* Fonctions d'animation des objets
===================== */

	function makeFloatObject(object_id, distance, time){
		$('#'+object_id+'').animate({ top:'+='+distance+'' }, {
			duration: time,
			easing: 'easeInOutSine'
		}).animate({ top:'-='+distance+'' }, {
			duration: time,
			easing: 'easeInOutSine'
		});
		setTimeout("makeFloatObject('"+object_id+"', "+distance+", "+time+")", time);
	}

	function animateShadow(object_id, opacity_min, time){
		//$('#'+object_id+'').animate({opacity:'1', filter:'progid:DXImageTransform.Microsoft.Alpha(Opacity=100)'}, time).animate({opacity:''+opacity_min+'', filter:'progid:DXImageTransform.Microsoft.Alpha(Opacity='+(opacity_min*100)+')'}, time);
		$('#'+object_id+'').fadeTo(time, 1).fadeTo(time, opacity_min);
		setTimeout("animateShadow('"+object_id+"', "+opacity_min+", "+time+")", time);
	}

	function animateReflection(object_id, opacity_min, distance, time){

		$('#'+object_id+'').animate({
				opacity:'1',
				top:'-='+distance+'',
				msfilter:'progid:DXImageTransform.Microsoft.Alpha(Opacity=100)',
				filter:'progid:DXImageTransform.Microsoft.Alpha(Opacity=100)'
			}, time).animate({
				opacity:''+opacity_min+'',
				top:'+='+distance+'',
				msfilter:'progid:DXImageTransform.Microsoft.Alpha(Opacity='+(opacity_min*100)+')',
				filter:'progid:DXImageTransform.Microsoft.Alpha(Opacity='+(opacity_min*100)+')'
			}, time);
			
		setTimeout("animateReflection('"+object_id+"', "+opacity_min+", "+distance+", "+time+")", time);
	}

	function animation() {
		makeFloatObject('logo3D', 30, 4000);
		//setTimeout("makeFloatObject('textelogo3D', 50, 4000)", 450);
		//animateShadow('logo3D_shadow', 0.5, 4000);
		//animateReflection('logo3D_reflection', 0.7, 20, 4000);
		//setTimeout("makeFloatObject('button_services', 35, 4000)", 400);
		//setTimeout("makeFloatObject('button_contact', 75, 4000)", 800);
		//setTimeout("makeFloatObject('button_realisations', 30, 4000)", 400);
		//setTimeout("makeFloatObject('button_showreel', 70, 4000)", 800);
	}
	var my_sound = 'on';
	function setMy_soundOff() 
	{
	my_sound = 'off';
	$("img.soundimg").attr("src", "img/icons/sound_off.png");
	$("#audio_ambiance").jPlayer("stop");
	}
$(document).ready(function(){
	
	/* Slide animé des différentes pages du site
	===================== */
		var thiswslide = $("#content_pages").wslide({
			width: $(window).width(),
			height: 800,
			autolink: false,
			pos: 3,
			duration : 800,
			horiz: true
		});
		
	/* Sons boutons au passage de la souris
	===================== */
		/* $('#audio_button').jPlayer({
			ready: function () {
				this.element.jPlayer("setFile", "./sounds/button.mp3", "./sounds/button.ogg");
			},
			volume: 100,
			oggSupport: true,
			swfPath: './',
			nativeSupport: true,
			preload: 'auto'
		}); */
		
		$('#audio_error').jPlayer({
			ready: function () {
				this.element.jPlayer("setFile", "./sounds/error.mp3", "./sounds/error.ogg");
			},
			volume: 80,
			oggSupport: true,
			swfPath: './',
			nativeSupport: true,
			preload: 'auto'
		});
		
		$('#audio_switch_tab').jPlayer({
			ready: function () {
				this.element.jPlayer("setFile", "./sounds/switch_tab1.mp3", "./sounds/switch_tab.ogg");
			},
			volume: 100,
			oggSupport: true,
			swfPath: './',
			nativeSupport: true,
			preload: 'auto'
		});
		
		
		
		
		$('a.nav_button').click(function() {
			
			$("#audio_switch_tab").jPlayer("play");
			$('video').pause();
/*$('#sound_element').html("<embed src='sound/switch_tab1.mp3' hidden=true autostart=true loop=false>");*/


			
		});
	
	/* Amélioration graphique des éléments de formulaire
	===================== */
		$('form').jqTransform({imgPath:'../img/jqtransform/'});
	
	/* Musique d'ambiance du site
	===================== */
		$('#audio_ambiance').jPlayer({
			ready: function () {
				this.element.jPlayer("setFile", "./sounds/ambiance.mp3", "./sounds/ambiance.ogg").jPlayer("play");
			},
			volume: 40,
			swfPath: './',
			nativeSupport: true,
			oggSupport: true
		});
		
	/* Placement du texte 3D
	===================== */
		$('#DynamicView3DText').css({marginLeft:''+ (($(window).width())/2)-(450/2) +'px'});
		
	
	/* Récupération de la dernière news twitter
	===================== 
		$("#twitter").getTwitter({
			userName: "DynamicView",
			numTweets: 1
		});
		
		$("#twitter").fadeTo('0', 0.5);
	
	 Bouton pour stopper la musique d'ambiance
	===================== */
		

	
	
		$('img.soundimg').mouseenter (function() {
			if (my_sound == 'on'){
				$(this).attr('src', 'img/icons/sound_off.png');
			} else {
				$(this).attr('src', 'img/icons/sound_on.png');
			}
		}).mouseleave (function(){
	
			if (my_sound == 'on'){
				$(this).attr('src', 'img/icons/sound_on.png');
			} else {
				$(this).attr('src', 'img/icons/sound_off.png');
			}
		});
		
		$('img.soundimg').click(function() {
			if (my_sound == 'on'){
				$(this).attr('src', 'img/icons/sound_off.png');
				$("#audio_ambiance").jPlayer("stop");
				my_sound = 'off';
			} else {
				$(this).attr('src', 'img/icons/sound_on.png');
				$("#audio_ambiance").jPlayer("play");
				my_sound = 'on';
			}
		});
	
	/* Animation déroulante de l'équipe
	===================== */
		/* var numberpassages = 0;
		var stopanimationvcaroussel = 'off';
			
		function vanimate (selector, numberofchildrens, div_height, vitesse)
		{	
			$(selector).animate({
				marginTop: '-='+ div_height +'px'
			}, vitesse, 'linear', function() {
				numberpassages ++;
				if (numberpassages > 2) {
					$(selector).animate({ marginTop: '0px' }, {
						duration: 3600,
						easing: 'easeOutBounce'
					});
				}
				
				if (stopanimationvcaroussel == 'off')
					vanimate(selector, numberofchildrens, div_height, vitesse);
			});
		}
		
		function vcaroussel (selector, div_height)
		{
			$(selector).parent().css({'height':  div_height+'px', 'overflow': 'hidden'});
			$(selector).css({'marginTop': '0px'});
			
			while ($(selector).height() < div_height){
				$(selector).children().clone().appendTo(selector);
			}
			numberofchildrens = $(selector).children().length;
			$(selector).children().clone().appendTo(selector);
			$(selector).children().clone().appendTo(selector);
			
			vanimate (selector, numberofchildrens, div_height, 18000);
		} */
		
		 $('#button_contact, a.lien_contact').click(function() {
				$('#vertcar_equipe').theatre('play');
			/*numberpassages = 0;
			stopanimationvcaroussel ='off';
			vcaroussel('ul.vcarousel', $('#formulaire_de_contact').height());
			*/
		});
		
		
		$('#button_retour_contact').click(function() {
			/*stopanimationvcaroussel = 'on';*/
				$('#vertcar_equipe').theatre('stop');
		}); 
		
		$("#vertcar_equipe").theatre({effect:"vertical", controls:"none", still:100})
		$("#vertcar_equipe").theatre("stop");
		
	
	/* Taille de la fenêtre courante
	===================== */
		var myWidth = 0, myHeight = 0;
		function getSize() {
			if( typeof( window.innerWidth ) == 'number' ) {
				//Non-IE
				myWidth = window.innerWidth;
				myHeight = window.innerHeight;
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
				//IE 6+ in 'standards compliant mode'
				myWidth = document.documentElement.clientWidth;
				myHeight = document.documentElement.clientHeight;
			} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				//IE 4 compatible
				myWidth = document.body.clientWidth;
				myHeight = document.body.clientHeight;
			}
		}
		getSize();
		
		if(myHeight < 700){
			$('body').css('overflow', 'auto');
		}
	
	/* Appellé en cas de redimensionnement de la fenetre
	===================== */	
		$('div.contenu_slider_page').css( 'margin-left', (myWidth/2)-(($('div.contenu_slider_page').width())/2));
		
		/*$(window).resize(function(){
			oldWSize = myWidth;
			getSize();
			if (myWidth < 900) myWidth = 900;
			var mW=(oldWSize-myWidth)/2;
					
				/* var valW = str_replace('px', '', $('#logo3D_container').css('left'));
				valW = valW - mW;
				$('#logo3D_container').css( 'left', ''+ valW +'px' ); */
				
				/* var valW = str_replace('px', '', $('#logo3D').css('left'));
				valW = valW - mW;
				$('#logo3D').css( 'left', ''+ valW +'px' );
				
				var valW = str_replace('px', '', $('#textelogo3D').css('left'));
				valW = valW - mW;
				$('#textelogo3D').css( 'left', ''+ valW +'px' );
				
				var valW = str_replace('px', '', $('#button_services').css('left'));
				valW = valW - mW;
				$('#button_services').css( 'left', ''+ valW +'px' );
				
				var valW = str_replace('px', '', $('#button_showreel').css('left'));
				valW = valW - mW;
				$('#button_showreel').css( 'left', ''+ valW +'px' );
				
				var valW = str_replace('px', '', $('#button_realisations').css('left'));
				valW = valW - mW;
				$('#button_realisations').css( 'left', ''+ valW +'px' );
				
				var valW = str_replace('px', '', $('#button_contact').css('left'));
				valW = valW - mW;
				$('#button_contact').css( 'left', ''+ valW +'px' );
				
				var valW = str_replace('px', '', $('#button_retour_showreel').css('margin-right'));
				valW = valW - (mW*-2);
				$('#button_retour_showreel').css( 'margin-right', ''+ valW +'px' );
				
				var valW = str_replace('px', '', $('#button_retour_realisations').css('margin-right'));
				valW = valW - (mW*-2);
				$('#button_retour_realisations').css( 'margin-right', ''+ valW +'px' ); */
				
				/*var valW = str_replace('px', '', $('div.contenu_slider_page').css('margin-left'));
				valW = valW - mW;
				$('div.contenu_slider_page').css( 'margin-left', ''+ valW +'px' );
				
				$('#content_pages').css('width', ''+ myWidth*5 +'px' );
				$('ul.wslide').css( 'width', ''+ myWidth*5 +'px' );
				$('ul.wslide').parents().css( 'width', ''+ myWidth*5 +'px' );
				$('div.contenu_slider_page').css('width', ''+ myWidth +'px' );
				$('li.wslide2').css( 'width', ''+ myWidth +'px' );
				
				var valW = str_replace('px', '', $('ul.wslide').css('left'));
				valW = valW - (mW*-1);
				$('ul.wslide').css( 'left', ''+ valW +'px' );
			
			/* $('div.contenu_slider_page').css('width', ''+ myWidth +'px' );
			$('#content_pages').css('width', ''+ myWidth*5 +'px' );
			$('li.wslide2').css( 'width', ''+ myWidth +'px' );
			$('ul.wslide').css( 'width', ''+ myWidth*5 +'px' );
			
			var valW = str_replace('px', '', $('ul.wslide').css('left'));
			valW = valW - ((mW*-2)*2);
			$('ul.wslide').css( 'left', ''+ valW +'px' ); */
		/*});*/
		
	/* Appellé pour charger les realisations
	===================== */
		pagenumber = 1;
		totalpagenumber = 1;
		
		function getRealisationsPage(pagenumber)
		{
		
			/*$('#rea_next_button').hide();
			$('#rea_prev_button').hide();*/
			$('#realisation_loading').fadeIn(0);
			$.post('ajax.php?func=getDictedRealisations', { page: pagenumber },
			function(data){
				$('#getDictedRealisations').html(data);
				
					$('div.rea_title_bandeau').fadeTo(0, 0.6);
					$('#realisation_detail').hide();
					$('#realisation_loading').hide();
				
				$('div.rea_cadre').mouseenter(function() {
					$(this).parents().children('div.rea_img').animate({ marginLeft:'-30' }, {
						duration: 400
					});
					$(this).parents().children('div.rea_title_bandeau').show().animate({ marginTop:'-26' }, {
						duration: 400
					});
				}).mouseleave(function() {
					$(this).parents().children('div.rea_img').animate({ marginLeft:'0' }, {
						duration: 400
					});
					$(this).parents().children('div.rea_title_bandeau').animate({ marginTop:'0' }, {
						duration: 400
					}).hide();
				});
				
				$('div.rea_cadre').click(function() {
				$('#rea_next_button').hide();
				$('#rea_prev_button').hide();
					$('#realisation_loading').fadeIn('slow');
					$.post('ajax.php?func=getRealisation', { id: $(this).attr('name') },
					function(data){
						$('#realisation_detail').html(data);
						$('#button_retour_realisations').css({visibility:'hidden'});
						
						$('#realisation_loading').fadeOut('slow').queue(function() {
							$('#realisations_liste').fadeOut('slow').queue(function() {
								$('#realisation_detail').fadeIn('slow');
								
								$(this).dequeue();
							});
							$(this).dequeue();
						});
					});
				});
				
				$("#button_nos_clients").click(function() {
				$('#rea_next_button').hide();
				$('#rea_prev_button').hide();
					$('#realisation_loading').fadeIn('slow');
					
					$.post('ajax.php?func=MurClients', { },
					function(data){
						$('#realisation_detail').html(data);
						$('#button_retour_realisations').css({visibility:'hidden'});
						
						$('#realisation_loading').fadeOut('slow').queue(function() {
							$('#realisations_liste').fadeOut('slow').queue(function() {
								$('#realisation_detail').fadeIn('slow');
								
								$(this).dequeue();
							});
							$(this).dequeue();
						});
					});
				});
				
				$('#realisation_loading').fadeOut('slow');
			});
		}
		
		getRealisationsPage(pagenumber);
		
		/* $('#rea_next_button').hide();
		$('#rea_prev_button').hide();*/
		
		$('#rea_next_button').click(function() {
		
		if(totalpagenumber==pagenumber){}else
		{	pagenumber++;
			}
			getRealisationsPage(pagenumber);
		});
		
		$('#rea_prev_button').click(function() {
		if(pagenumber<=1){pagenumber = 1;}else
		{
			pagenumber--;}
			getRealisationsPage(pagenumber);
		});
		
	/* Videos HTML5
	===================== */
/* 		$(document).bind('play', function(e){
			$('video').not(e.target).pause();
		}); */
		
		$('#showreel_loading').hide();
							
		$("#button_showreel").click(function() {
			$('#showreel_loading').fadeIn();
			
			$.post('ajax.php?func=showreel', { },
			function(data){
				$('#video_showreel').html(data).queue(function() {
					$('#showreel_loading').fadeOut();
					$(this).dequeue();
				});
			});
		});
		
	/* Popup TV Interne
	===================== */
	
			$("#affichagedynamique1, #affichagedynamique2, #affichagedynamique3, #affichagedynamiqueP, #video1, #siteinternet1, #animation3d1").fancybox({
				'titlePosition'		: 'inside',
				'titleShow'			: false,
				'transitionIn'		: 'none',
				'padding'				: 30,
				'transitionOut'		: 'none',
				'onComplete'		: function() {
					$(".fancybox-ie #fancybox-close").css("background", "url('fancybox/fancy_close.gif') 0px 0px");
				}
			});
			
				$("#b_affichagedynamique").mouseenter(function() {
					$(this).attr('src', 'img/buttons/sb_affichagedynamique_hover.png');
				}).mouseleave(function() {
					$(this).attr('src', 'img/buttons/sb_affichagedynamique.png');
				});
				$("#b_animation3d").mouseenter(function() {
					$(this).attr('src', 'img/buttons/sb_animation3d_hover.png');
				}).mouseleave(function() {
					$(this).attr('src', 'img/buttons/sb_animation3d.png');
				});
				$("#b_video").mouseenter(function() {
					$(this).attr('src', 'img/buttons/sb_video_hover.png');
				}).mouseleave(function() {
					$(this).attr('src', 'img/buttons/sb_video.png');
				});
				$("#b_siteinternet").mouseenter(function() {
					$(this).attr('src', 'img/buttons/sb_siteinternet_hover.png');
				}).mouseleave(function() {
					$(this).attr('src', 'img/buttons/sb_siteinternet.png');
				});
			
	
		var popupStatus = 0; 
		//loading popup with jQuery magic!
		function loadPopup(){
			//loads popup only if it is disabled
			if(popupStatus==0){
				$("#backgroundPopup").css({
					"opacity": "0.7"
				});
				$("#backgroundPopup").fadeIn("slow");
				$("#popupContent").fadeIn("slow");
				popupStatus = 1;
			}
		}
		//disabling popup with jQuery magic!
		function disablePopup(){
			//disables popup only if it is enabled
			if(popupStatus==1){
			$("#backgroundPopup").fadeOut("slow");
			$("#popupContent").fadeOut("slow");
			popupStatus = 0;
			}
		}
		//centering popup
		function centerPopup(){
			//request data for centering
			var windowWidth = document.documentElement.clientWidth;
			var popupWidth = $("#popupContent").width();
			//centering
			$("#popupContent").css({
			"position": "absolute",
			"top": 80,
			"left": windowWidth*3+windowWidth/2-popupWidth/2
			});
			//only need force for IE6
		}
		//LOADING POPUP
		//Click the button event!
		$("a.popup").click(function(){
			//centering with css
			centerPopup();
			//load popup
			loadPopup();
			return false;
		});
		//CLOSING POPUP
		//Click the x event!
		$("#popupContentClose").click(function(){
		disablePopup();
		});
		//Click out event!
		$("#backgroundPopup").click(function(){
		disablePopup();
		});
		//Press Escape event!
		$(document).keypress(function(e){
		if(e.keyCode==27 & popupStatus==1){
			disablePopup();
		}
		});
		
	/* Popup TV Interne 2
	===================== */
		var popupStatus2 = 0; 
		//loading popup with jQuery magic!
		function loadPopup2(){
			//loads popup only if it is disabled
			if(popupStatus2==0){
				$("#backgroundPopup2").css({
					"opacity": "0.7"
				});
				$("#backgroundPopup2").fadeIn("slow");
				$("#popupContent2").fadeIn("slow");
				popupStatus2 = 1;
			}
		}
		//disabling popup with jQuery magic!
		function disablePopup2(){
			//disables popup only if it is enabled
			if(popupStatus2==1){
			$("#backgroundPopup2").fadeOut("slow");
			$("#popupContent2").fadeOut("slow");
			popupStatus2 = 0;
			}
		}
		//centering popup
		function centerPopup2(){
			//request data for centering
			var windowWidth2 = document.documentElement.clientWidth;
			var popupWidth2 = $("#popupContent2").width();
			//centering
			$("#popupContent2").css({
			"position": "absolute",
			"top": 150,
			"left": windowWidth2*3+windowWidth2/2-popupWidth2/2
			});
			//only need force for IE6
		}
		//LOADING POPUP
		//Click the button event!
		$("a.popup2").click(function(){
			//centering with css
			centerPopup2();
			//load popup
			loadPopup2();
			return false;
		});
		//CLOSING POPUP
		//Click the x event!
		$("#popupContentClose2").click(function(){
		disablePopup2();
		});
		//Click out event!
		$("#backgroundPopup2").click(function(){
		disablePopup2();
		});
		//Press Escape event!
		$(document).keypress(function(e){
		if(e.keyCode==27 & popupStatus2==1){
			disablePopup2();
		}
		});

		
	/* Formulaire de contact
	===================== */
		$('#formulaire_return').hide();
		$('#formulaire_loading').hide();
		
		var email_dynamic = 'rf/weivcimanyd//ofni';
		email_dynamic = email_dynamic.split('').reverse().join('').replace('//', '@').replace(/\//g, '.');
		$('#tab_info_contact').append('<span>' + email_dynamic + '</span>');
		
		function test_email (my_email) {
			var new_string = new String(my_email);
			if (!new_string.match('^[-_\.0-9a-zA-Z]{1,}@[-_\.0-9a-zA-Z]{1,}[\.][0-9a-zA-Z]{2,}$')) {
					return false;
			}
			else {
					return true;
			}
		}
		
		$('#send_email').click(
			function (e) {
				
					$('div.form_txt  p').css({ border: '1px solid #ccc'});
					var FNom = $('#nom').val();
					var FEmail = $('#email').val();
					var FMess = $('#message').val();
					var FTel = $('#tel').val();
					
					if (FNom != '' && FEmail != '' && FMess != '')
					{
						if (test_email(FEmail) == true)
						{
							$('input.champ_t1, textarea').each(function (i) {
								$(this).attr("disabled","disabled");
							}); 
						
							$('#formulaire_loading').fadeIn("slow");
							
							$.post('ajax.php?func=sendEmail', { nom: FNom, tel:FTel, email: FEmail, message: FMess },
								function(data){
									var html_content = '';
									if (data != 'ok')
									{
										var my_email = 'rf/weivcimanyd//ofni';
										my_email = my_email.split('').reverse().join('').replace('//', '@').replace(/\//g, '.');
										
										html_content = '<img src="img/icons/error.png" alt="error" /> Une erreure est survenue lors de l\'envoi du mail...<br /><br /><img src="img/icons/email_error.png" alt="email_error" /> Merci de bien vouloir nous contacter directement &agrave; l\'adresse suivante: <strong>' + my_email + '</strong>';
									}
									else
									{
										html_content = '<img src="img/icons/valid.png" alt="ok" /> Votre message a &eacute;t&eacute; envoy&eacute; avec succ&egrave;s !<br />Vous recevrez une r&eacute;ponse sous 24 &agrave; 48h.';
									}
									
									$('#formulaire_loading').fadeOut("slow", function(){
										$('#formulaire_return p').html(html_content);
										$('#formulaire_return').fadeIn("slow");
										
										if (data === 'ok'){
											$('div.form_txt  p').css({ border: '1px solid #b7ee87'});
											setTimeout(function(){
													$('#formulaire_return').fadeOut("slow");
													$('input.champ_t1, textarea').each(function (i) {
														$(this).val('');
														$(this).removeAttr("disabled");
													});
											}, 3500);
										} else {
											$('div.form_txt  p').css({ border: '1px solid #ee9787'});
											$("#audio_error").jPlayer("stop");
											$("#audio_error").jPlayer("play");
										}
									});
								}
							);
						}
						else
						{
							$('input.champ_t1, textarea').each(function (i) {
								$(this).attr("disabled","disabled");
							});
							
							$('#formulaire_return p').html('<img src="img/icons/error.png" alt="error" /> L\'adresse email saisie est invalide...');
							$('div.form_txt  p').css({ border: '1px solid #ee9787'});
							$('#formulaire_return').fadeIn("slow");
							$("#audio_error").jPlayer("stop");
							$("#audio_error").jPlayer("play");
							
							setTimeout(function(){
								$('#formulaire_return').fadeOut("slow");
								$('input.champ_t1, textarea').each(function (i) {
									$(this).removeAttr("disabled");
								});
								$('#email').focus();
							}, 1500);
						}
					}
					else
					{
						$('input.champ_t1, textarea').each(function (i) {
							$(this).attr("disabled","disabled");
						});
						
						$('#formulaire_return p').html('<img src="img/icons/error.png" alt="error" /> Les champs marqu&eacute;s d\'une <sup>*</sup> sont obligatoires...');
						$('div.form_txt  p').css({ border: '1px solid #ee9787'});
						$('#formulaire_return').fadeIn("slow");
						$("#audio_error").jPlayer("stop");
						$("#audio_error").jPlayer("play");
						
						setTimeout(function(){
							$('#formulaire_return').fadeOut("slow");
							$('input.champ_t1, textarea').each(function (i) {
								$(this).removeAttr("disabled");
							});
						}, 1500);
					}
				return false;
			}
	    );
});
