$(document).ready(function(){
	
	$('#cats_hover').hide();
	$('#sucre02').hide();
	$('#sucre03').hide();
	$('#sucre04').hide();
	$('#formulaire_return').hide();
	$('#formulaire_loading').hide();
	$('#preloader_polaroid').hide();
	
	/* 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();
	
	/* Scroll animé
	===================== */
		$.localScroll();
		
	/* Infobulles
	===================== */
		$('a[title].blue').qtip({
			style: {
				name: 'blue',
				tip: true,
				border: { radius: 5 }
			},
			position: {
				target: 'mouse',
				adjust: { x: 11, y: 16 }
			}
		});
		$('a[title].cream').qtip({
			style: {
				name: 'cream',
				tip: true,
				border: { radius: 5 }
			},
			position: {
				target: 'mouse',
				corner: { tooltip: 'topRight' }
			}
		});
		$('a[title].light').qtip({
			style: {
				name: 'light',
				tip: true,
				border: { radius: 5 }
			},
			position: {
				target: 'mouse',
				corner: { tooltip: 'topRight' }
			}
		});
		$('a[title].infobulle').qtip({
			style: {
				name: 'light',
				tip: true,
				border: { radius: 5 }
			},
			position: {
				target: 'mouse',
				corner: { tooltip: 'bottomRight' }
			}
		});
		$('a[title].cream, a[title].light, a[title].blue, a[title].infobulle').click(function(){
			$('.qtip').fadeOut("slow");
		});
		
	/* Menu folio (entourage)
	===================== */
		$('span.cats').click(
			function (e) {
				var posX = e.pageX - 101;
				var posY = e.pageY - 24;
				
				var nombreAleat = Math.floor(Math.random() * 3)+1;
				
				$("#cats_hover").hide('blind', {}, 500, callbackCats);
				function callbackCats(){
					setTimeout(function(){
						$('#cats_hover').attr('src', 'images/cats_hover'+ nombreAleat + sh_rot +'.png').css({ top:''+posY+'px', left:''+posX+'px' }).show('blind', {}, 500);
					}, 100);
				};
			}
	    );
		
	/* On place l'entourage par défaut
	===================== */
		$('#cats_hover').css('left', ''+ Math.round((myWidth/2)+200) +'px');
		$('#cats_hover').fadeIn("slow");
		
	/* On place l'animation du sucre
	===================== */
		$(".sucre").each(function (i) {
			if (myWidth < 1017) myWidth = 1017;
			$(this).css( 'left', ''+ Math.round((myWidth/2)+230) +'px');
		});
		$('#sucre01').click(
			function (e) {
				$(this).hide();
				$('#sucre02').fadeIn('slow');
				setTimeout(function(){
					$('#sucre02').hide();
					$('#sucre03').fadeIn('slow');
					setTimeout(function(){
						$('#sucre03').hide();
						$('#sucre04').fadeIn('slow');
					}, 500);
				}, 500);
			}
	    );

	/* Bouton CV
	===================== */
		$("#CV_img").hover(
			function () {
				$(this).attr('src', 'images/profil_CVh.png');
			}, 
			function () {
				$(this).attr('src', 'images/profil_CV.png');
			}
		);

	/* Formulaire de contact
	===================== */
		$('#send_email').click(
			function (e) {
				
					var FNom = $('#nom').val();
					var FEmail = $('#email').val();
					var FMess = $('#message').val();
					
					var FSociete = $('#societe').val();
					var FTel = $('#tel').val();
					var FUrl = $('#url').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, societe:FSociete, tel:FTel, email: FEmail, site:FUrl, message: FMess },
								function(data){
									var html_content = '';
									if (data != 'ok')
									{
										var my_email = 'rf/oamis-niavlys//tcatnoc';
										my_email = my_email.split('').reverse().join('').replace('//', '@').replace(/\//g, '.');
										
										html_content = '<img src="images/error.png" alt="error" /> Une erreure est survenue lors de l\'envoi du mail...<br /><br /><img src="images/email_error.png" alt="email_error" /> Merci de bien vouloir m\'informer du probl&egrave;me en envoyant un mail &agrave; l\'adresse suivante: ' + my_email + '';
									}
									else
									{
										html_content = '<img src="images/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'){
											setTimeout(function(){
													$('#formulaire_return').fadeOut("slow");
													$('input.champ_t1, textarea').each(function (i) {
														$(this).val('');
														$(this).removeAttr("disabled");
													});
											}, 3500);
										}
									});
								}
							);
						}
						else
						{
							$('input.champ_t1, textarea').each(function (i) {
								$(this).attr("disabled","disabled");
							});
							
							$('#formulaire_return p').html('<img src="images/error.png" alt="error" /> L\'adresse email saisie est invalide...');
							$('#formulaire_return').fadeIn("slow");
							
							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="images/error.png" alt="error" /> Les champs marqu&eacute;s d\'une * sont obligatoires...');
						$('#formulaire_return').fadeIn("slow");
						
						setTimeout(function(){
							$('#formulaire_return').fadeOut("slow");
							$('input.champ_t1, textarea').each(function (i) {
								$(this).removeAttr("disabled");
							});
						}, 1500);
					}
				return false;
			}
	    );
	
	/* Antispam emails
	===================== */				
		$('span.email').nospam({
			filterLevel: 'normal'
		});
	    
	/* Redimensionnement fenetre
	===================== */	
		$(window).resize(function(){
			oldWSize = myWidth;
			getSize();
			if (myWidth < 1017) myWidth = 1017;
			var mW=(oldWSize-myWidth)/2;
			
			var valW = str_replace('px', '', $('#cats_hover').css('left'));
			valW = valW - mW;
			$('#cats_hover').css( 'left', ''+ valW +'px' );
			
			
			$(".sucre").each(function (i) {
				var valW = str_replace('px', '', $(this).css('left'));
				valW = valW - mW;
				$(this).css( 'left', ''+ valW +'px' );
			});
		});
		
	/* Préchargement d'images
	===================== */	
		var cache = [];
		
		$.preLoadImages = function() {
			var args_len = arguments.length;
			for (var i = args_len; i--;) {
				var cacheImage = document.createElement('img');
				cacheImage.src = arguments[i];
				cache.push(cacheImage);
			}
		}
});