$(document).ready(function(){
	// Global
	$(".ext").attr("target","_blank");
	$("#left ul a").removeAttr("title");
	$("#player").css({opacity:"1"});
	$("#control, .pages").css({display:"block"});
	$(".list").addClass("reduce");
	$(".pages .prev, .pages .next, .role").click(function(){ return false; });
	$(".mail").each(function(i){
	   var email = $(this).html();
	   $(this).html(email).replaceWith('<a href="mailto:' + $(this).text() + '">' + $(this).text() + '</a>');
	});
	
	
	// Slideshow
	$('#slide').cycle({
      fx: "fade",
      speed: 1500,
	   timeout: 5000,
      pager: "#control p"
	});
	nImg = 1;
	$("#control a").each(function(){ $(this).attr("title", "Image " + nImg++); });
	
	
	// Menu
   function mShow(s){ $(s).stop().animate({opacity:1}, 350); }
   function mHide(s){ $(s).stop().animate({opacity:0}, 350); }
   function mPos(p){ $(this).css({backgroundPosition: p}); }
   
	// Fr
   $("#fr .about a").hover(function(){ mShow("#fr .abouth"); mPos.call(this, "-1px 0") }, function(){ mHide("#fr .abouth"); });
   $("#fr .creas a").hover(function(){ mShow("#fr .creash"); mPos.call(this, "-1px -46px"); }, function(){ mHide("#fr .creash"); });
   $("#fr .contact a").hover(function(){ mShow("#fr .contacth"); mPos.call(this, "-1px -88px"); }, function(){ mHide("#fr .contacth"); });
   $("#fr .blog a").hover(function(){ mShow("#fr .blogh"); mPos.call(this, "-1px -131px"); }, function(){ mHide("#fr .blogh"); });
   // En
   $("#en .about a").hover(function(){ mShow("#en .abouth"); mPos.call(this, "-1px -169px"); }, function(){ mHide("#en .abouth"); });
   $("#en .creas a").hover(function(){ mShow("#en .creash"); mPos.call(this, "-1px -215px"); }, function(){ mHide("#en .creash"); });
   $("#en .contact a").hover(function(){ mShow("#en .contacth"); mPos.call(this, "-1px -257px"); }, function(){ mHide("#en .contacth"); });
   $("#en .blog a").hover(function(){ mShow("#en .blogh"); mPos.call(this, "-1px -131px"); }, function(){ mHide("#en .blogh"); });
	
	
	// Works
	$(".work").hover(function(){ $(this).find(".img").stop().css({"opacity": .75}).animate({opacity:1}, 350); }, function(){ $(this).find(".img").stop().animate({opacity:.75}, 700); });
	
   // Sliders
   $("#worksweb").jCarouselLite({ btnNext: ".next.web", btnPrev: ".prev.web", scroll: 2, visible: 2, speed: 1100, circular:false });
   $("#worksother").jCarouselLite({ btnNext: ".next.other", btnPrev: ".prev.other", scroll: 2, visible: 2, speed: 1100, circular:false });
   $("#web li, #other li").css({overflow:"visible"});


   // More
   $("#more .infos").hide();
   $("#more .link").click(function(){ $(this).next("#more .infos").slideToggle(1100, "easeInOutSine"); return false; });
   
   
	// Tooltips
	$("body").append('<div id="tip"></div>');
	$(".available, #legend a[title], .links a[title], #cv[title], .button a[title], #control a[title], abbr[title], #player a[title]").each(function(){
		$(this).hover(function(e){
			$(document).mousemove(function(e){ var tipY = e.pageY + 20; var tipX = e.pageX - 5; $("#tip").css({'top': tipY, 'left': tipX}); });
			$("#tip").html($(this).attr('title')).stop(true, true).fadeIn(300); $(this).removeAttr('title');
		},
		function(){ $("#tip").stop(true, true).fadeOut(300); $(this).attr('title', $("#tip").html()); });
	});
	
	
	// Player
	function r(n){ return (Math.floor(Math.random() * n + 1)); }
	$("#sound").html('<object type="application/x-shockwave-flash" data="/lib/player.swf" width="0" height="0" id="audio"><param name="movie" value="/lib/player.swf" /><param name="FlashVars" value="mp3=/lib/sounds/' + r(4) + '.mp3&amp;autoplay=0&amp;volume=30" /></object>');	
	for (var i=0; i < 4; i++){ $("#player p a").fadeOut(250).fadeIn(250); };
	
	
	// Modalbox
	$("#slide li a, .work a[rel=mod]").fancybox({
	   'padding': 0,
		'zoomSpeedIn': 300,
		'zoomSpeedOut': 300,
		'overlayColor': '#164972',
		'overlayShow': true,
		'overlayOpacity': .25,
		'centerOnScroll': true,
		'hideOnContentClick': true,
		'hideOnOverlayClick': true,
		'imageScale': true,
		'titleShow': false
	}); 
	
	
	// Form errors
	$("#form form").validate({
	   rules:{
         name: "required",
         mail:{ required: true, email: true },
         subject: "required",
         message: "required"
	   },
		messages:{
			name:{ required: '<img src="/img/ic/bad.png" alt="erreur" />' },
			mail:{ required: '<img src="/img/ic/bad.png" alt="erreur" />', email: '<img src="/img/ic/bad.png" alt="erreur" />' },
			subject:{ required: '<img src="/img/ic/bad.png" alt="erreur" />', minlength: '<img src="/img/ic/bad.png" alt="erreur" />' },
			message:{ required: '<img src="/img/ic/bad.png" alt="erreur" class="emsg" />' }
		}
	});
});