$(document).ready(function(){
	/*animation du logo*/
	function animateGlow(){
		$("#logo").css({ backgroundPosition:"0 0"});
		$("#logo").animate({
		backgroundPosition:"-600px 0"},5000,'linear',animateGlow);
	}
	animateGlow();
	
	// $("#mainmenu li:not(.current-menu-item,.current-menu-parent) a").hover(function() {
		// if($(this).parent('li').attr('ID')=='menu-item-10'){
			// $(this).stop().animate({paddingTop: "20px"},250);
		// }else{
			// $(this).stop().animate({paddingTop: "20px",color:"#000000",backgroundColor:"#96c7f0"}, 250);
		// }
	// },
	// function() {
		// if($(this).parent('li').attr('ID')=='menu-item-10'){
			// $(this).stop().animate({paddingTop: "10px"},250);
		// }else{
			// $(this).animate({paddingTop: "10px",color:"#96c7f0",backgroundColor:"#000000"}, 250);
		// }
	// });
	$("#mainmenu").on('mouseenter','li:not(.current-menu-item,.current-menu-parent) a',function(){
		if($(this).parent('li').attr('ID')=='menu-item-10'){
			$(this).stop().animate({paddingTop: "20px"},250);
		}else{
			$(this).stop().animate({paddingTop: "20px",color:"#000000",backgroundColor:"#96c7f0"}, 250);
		}
	});
	$("#mainmenu").on('mouseleave','li:not(.current-menu-item,.current-menu-parent) a',function() {
		if($(this).parent('li').attr('ID')=='menu-item-10'){
			$(this).stop().animate({paddingTop: "10px"},250);
		}else{
			$(this).stop().animate({paddingTop: "10px",color:"#96c7f0",backgroundColor:"#000000"}, 250);
		}
	});
	/*slider*/
	$('#slider').cycle({ 
		 fx:      'custom', 
		cssBefore: {  
			left: 200,  
			top:  150,  
			width: 0,  
			height: 0,  
			opacity: 0, 
			display: 'block' 
		}, 
		animOut: {  
			opacity: 0,
			/*left: 100,  
			top:  100,  
			width: 200,  
			height: 150,  */			
		}, 
		animIn: {  
			left: 0,  
			top: 0,  
			width: 400,  
			height: 300,
			opacity : 1			
		}, 
		cssAfter: {  
			zIndex: 0 
		}, 
		/*sync:   0, */
		timeout:6000
	 });

	
	/*position souris*/
	/*$(document).mousemove(function(e){
		var Ypos = e.pageY;
		var YP = Ypos/70;
		var screen = document.body.clientWidth;
		var Xpos = e.pageX;
		var percent = (Xpos*20)/screen;
		var W = ((1910-screen)/2)+percent;
		var XP = Xpos/10;
		var pos = "-" + W + "px -"+ YP+"px";
		$("#global").css({backgroundPosition : pos});
	}); */
		
	//scroll
	$('#commentaires_barre').on('click',function(){
		$.scrollTo('#comments-bloc',500);
	});
	$(window).scroll(function(){
		var scrollTop = $(window).scrollTop();
		if(scrollTop >400){ 
			$('#scrolltotop').slideDown();
		}else{
			$('#scrolltotop').slideUp();
		}
	});
	$(window).scroll();
	$('#scrolltotop').click(function(){
		$.scrollTo('#logo',500);
	});
	$('#auteur_barre').click(function(){
		$.scrollTo('#auteur',500);
	});
	$('#sharetobottom').click(function(){
		$.scrollTo('#share',500);
	});
	
	//focus formulaire
	$('#author,#email,#url,#contactnom,#contactemail,#contactsociete,#contactsujet').focus(function(){
		if($(this).attr('value') == this.defaultValue) $(this).attr('value', '');
	});
	$('#author').blur(function(){
		if($(this).attr('value') == '') $(this).attr('value','nom (requis)');
	});
	$('#email').blur(function(){
		if($(this).attr('value') == '') $(this).attr('value','adresse email (requise)');
	});
	$('#url').blur(function(){
		if($(this).attr('value') == '') $(this).attr('value','site internet');
	});
	$('#contactnom').blur(function(){
		if($(this).attr('value') == '') $(this).attr('value','nom');
	});
	$('#contactemail').blur(function(){
		if($(this).attr('value') == '') $(this).attr('value','adresse@email.com');
	});
	$('#contactsociete').blur(function(){
		if($(this).attr('value') == '') $(this).attr('value','société');
	});
	$('#contactsujet').blur(function(){
		if($(this).attr('value') == '') $(this).attr('value','sujet');
	});
	$('#contactmessage').focus(function(){
		if($(this).val()=='votre message...'){
			$(this).val('');
		}
	});
	$('#contactmessage').blur(function(){
		if($(this).val()==''){
			$(this).val('votre message...');
		}
	});
	
	//pirobox
	$().piroBox_ext({
	piro_speed : 700,
		bg_alpha : 0.5,
		piro_scroll : true // pirobox always positioned at the center of the page
	});
	
	//envoie de devis
	$('#contactsubmit').live('click',function(e){
		e.preventDefault();
		var value = $('#contactform').serialize();
		console.log(value);
		$.ajax({
			url:"/wp-admin/admin-ajax.php",
			type:'POST',
			data:'action=sendform&'+value,
			success : function(data){
				var obj = $.parseJSON(data);
				if(obj.send == 1){
					$('#contactform').replaceWith('<div id="messageenvoye">Votre message a été transmis à wabeo, il sera traité dans les plus brefs délais</div>');
				}else{
					var errormessage = '';
					if(obj.erreur.type1 == 'retournnom'){
						var errormessage = errormessage+'Veuillez renseigner votre nom\r\n';
					}
					if(obj.erreur.type2 == 'retourmail'){
						var errormessage = errormessage+'Veuillez renseigner une adresse email correcte\r\n';
					}
					if(obj.erreur.type3 == 'retourmessage'){
						var errormessage = errormessage+'Veuillez renseigner votre message\r\n';
					}
					alert(errormessage);	
				}
			}			
		});			
	});
	
	//hash change
	var accueil=true;
	$(window).hashchange( function(){
		if(window.location.pathname=='/'){
			var arg = window.location.hash.substring(3);
			var link = 'http://wabeo.fr/'+arg;
			if((arg=='wordpress')||(arg=='navigation-ajax')||(arg=='galeries-video')){
				$.ajax({
					url:"/wp-admin/admin-ajax.php",
					type:'POST',
					data:'action=get_mypage&mypage=' + arg,
					success:function(data){
						var obj = $.parseJSON(data);
						if(accueil==true){
							$('#slider').fadeOut();		
							$('#contenu.home').animate({opacity:0,paddingBottom:"260px"},500,function(){
								/*$(this).css({width:"648px", marginLeft:"-150px"});*/
								$(this).children('.accueil').html(obj.contenu);
								$(this).animate({opacity:1},500);
							});
							$('#bar.home').animate({height:"50px"},500);
							$('#minibio').fadeOut();
							$('#recentblog').fadeOut();
							accueil = false;
							
							//append menu
							$(obj.menu).prependTo('#global');
						}else{
							$('#contenu.home').animate({opacity:0},500,function(){
								$(this).children('.accueil').html(obj.contenu);
								$(this).animate({opacity:1},500);
							});
							//update menu
							$('#menu-secondary').replaceWith($(obj.menu));
							accueil = false;
						}
					}
				});
			}
			if(arg.length==0){
				if(accueil==false){
					$.ajax({
						url:"/wp-admin/admin-ajax.php",
						type:'POST',
						data:'action=get_homepage',
						success:function(data){
							var obj = $.parseJSON(data);
							$('#contenu.home').animate({opacity:0,paddingBottom:"380px"},500,function(){
								/*$(this).css({width:"548px", marginLeft:"-50px"});*/
								$(this).children('.accueil').html(obj.contenu+'<a href="#!/wordpress" class="more-services"></a>').prepend($('<h1 class="titre_page">' + obj.titre + '</h1>'));
								$(this).animate({opacity:1},500);
							});
							$('#menu-secondary').fadeOut().remove();
							$('#slider').fadeIn();
							$('#bar.home').animate({height:"180px"},500);
							$('#minibio').fadeIn();
							$('#recentblog').fadeIn();
							accueil = true;
						}
					});
				}
			}
		}
	});
	$(window).hashchange();
});
