$(function(){
   // Global
   $("a[href=#]").live("click", function(){ return false; });
   $(".ext").attr("target", "_blank");
   $("#menu a").removeAttr("title");
   $("#player").css({opacity:"1"});
   $("#control, .pages").css({display:"block"});
   $("#load").hide();
   $("#more .link").show();
   $(".mail").each(function(){
      var mail = $(this).attr("rel");
      $(this).attr("href", "mailto:"+mail);
   });
   
   langsite = $("body").attr("id");
   if (langsite == 'en') nL = 1; else nL = 0;
   $.get("index.php?title=1&lang="+nL, function(data){ sitetitle = data; });
   
   // Links
   $("a[href$=#top]").click(function(){ $("#menu li").removeClass("active"); });
   $("a[href$=#contact]").click(function(){
      $("#menu li").removeClass("active");
      $("#menu .contact").addClass("active");
   });
      
   
   // Scroll
   function scroll(elem){
      $(elem).click(function(){
         if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname){
            var $target = $(this.hash);
            $target = $target.length && $target || $("[name="+ this.hash.slice(1) +"]");
            if ($target.length){
               var targetOffset = $target.offset().top;
               $("html, body").animate({scrollTop:targetOffset-85}, 850, "easeInOutCirc");
            }
         }
         
         return false;
      });
   }


   // More
   $("#more .infos").hide();
   $("#more .link a").click(function(){
      $("#more .infos").animate({height:"toggle", opacity:"toggle"} , 600, "easeInOutSine");
      return false;
   });
   
   
   // Slideshow
   $("#slide").cycle({
      fx: "fade",
      speed: 1500,
      timeout: 5000,
      pager: "#control p"
   });
      nImg = 1;
      $("#control a").each(function(){
         $(this).attr("title", "Image " + nImg++);
      });
   
   // Legend
   $("#links .role").click(function(){ return false; });


   // Menu
   function mShow(){ $(this).find("span").stop().animate({opacity:1}, 300); }
   function mHide(){ $(this).find("span").stop().animate({opacity:0}, 300); }
      
   $("#menu a span").show().css({opacity:"0"});
   $("#menu li a").hover(function(){
      if (!$(this).parent().hasClass("active")){
         mShow.call(this);
      }
   }, function(){
      mHide.call(this);
   });
   
      // Click active on links
      $("#menu li a span").mousedown(function(){
         $(this).animate({opacity:0}, 150);
         $(this).parent().removeClass("active");
      });
      $("#menu li[class!=active] a").click(function(){
         $(this).find("span").animate({opacity:0});
         $("#menu li").removeClass("active");
         $(this).parent().addClass("active");
      });
   

   // Works
   $(".work").hover(function(){
         $(this).find(".img").stop().css({"opacity": .75}).animate({opacity:1}, 300);
      }, function(){
         $(this).find(".img").stop().animate({opacity:.75}, 700);
   });

   // Click to load
   var scrollWeb = $("#web").offset().top-100,
       scrollContact = $("#contact").offset().top-100;
       
   function hideLoader(){ $("#load").fadeOut(); }
   function zoomLoad(){
      $("#load").fadeIn();
      $("html, body").animate({scrollTop: scrollWeb}, 800, function(){
         
         $.get(url, function(data){
            var $data = $(data);
            var content = $data.find("#zoom > *");
         
            $("#zoom").html(content).animate({height:"show", opacity:"show"}, 400, function(){
               hideLoader();
               tip("#picture[title]");
               closeZoom();
               $("title").text($("#infos h4").text()+ " | " +sitetitle.split("|")[0]);
            });
         });
      });
   }
   
      // Click to load
      $("#web .work a").live("click", function(){   
         url = $(this).attr("href");
      
         if ($("#zoom").is(":hidden")) zoomLoad.call(this);
         else $("#zoom").animate({height:"hide", opacity:"hide"}, 600, function(){
            zoomLoad.call(this);
         });
      
         window.location.hash = url.split("/")[url.split("/").length - 2];
         return false;
      });
      
   
   // Load if direct work hash url
   var hashLoad = window.location.hash.substr(1);
   if (hashLoad){
       $(".list li a").each(function(){
          var href = $(this).attr("href");
                            
          if (hashLoad == href.split("/")[href.split("/").length - 2]){
             var toLoad = "work/"+ hashLoad +" #zoom > *";
                 
             $("#load").show();
             $("#zoom").show().load(toLoad, function(){
                $("#load").fadeOut();
                $("title").text($("#infos h4").text()+ " | " +sitetitle.split("|")[0]);
                loadInit();
                hideLoader();
                tip("#picture[title]");
                closeZoom();
             });
             
             return false;
          }
      });
   }
   
      // Load Init
      function loadInit(){
         $("html, body").animate({scrollTop: scrollWeb}, 800);
         $("#menu li").removeClass("active");
         $("#menu li.creas").addClass("active");
      }
   
      // Close zoom
      function closeZoom(){
         $("#zoom #picture").click(function(){
            $("#zoom").animate({height:"toggle", opacity:"hide"});
            $("title").text(sitetitle);

            return false;
         });
      }
   

   // 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"});
   $(".prev").addClass("disabled");
   
      // Click actions on buttons
      $("li.next").click(function(){
         $(this).parent().find(".prev").show();
      });
      $("li.disabled").find("a").live("click", function(){
         return false;
      });


   // Tooltips
   function tip(selectors){
      $(selectors).each(function(){
         var text = $(this).attr("title");
         $(this).attr("title", "");
         if (text != undefined){
            $(this).hover(function(e){
               var tipX = e.pageX - 5, tipY = e.pageY + 20;
               $(this).attr("title", ""); $("body").append("<div id='tip' display:none;'>" + text + "</div>");
               if ($.browser.msie) var tipWidth = $("#tip").outerWidth(true); else var tipWidth = $("#tip").width();
               $("#tip").css("left", tipX).css("top", tipY).show();
            }, function(){
               $("#tip").remove();
               $(this).attr("title", text);
            });
            $(this).mousemove(function(e){
               var tipX = e.pageX - 5, tipY = e.pageY + 20, tipWidth = $("#tip").outerWidth(true), tipHeight = $("#tip").outerHeight(true);
               if (tipX + tipWidth > $(window).scrollLeft() + $(window).width()) tipX = e.pageX - tipWidth; if($(window).height()+$(window).scrollTop() < tipY + tipHeight) tipY = e.pageY - tipHeight;
               $("#tip").css("left", tipX).css("top", tipY);
            });
         }
      });
   }


   // Player
   function rSound(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/' + rSound(4) + '.mp3&amp;autoplay=0&amp;volume=40&amp;javascript=on" /></object>');	
   
   // Controls
   $("#player a").click(function(){ return false; });
      $("#pause").click(function(){
         $(this).fadeTo(300, 0.4);
         $("#play").fadeTo(300, 1);
         var dewp = document.getElementById("audio");
         if (dewp != null) dewp.dewpause();
      });
      $("#play").click(function(){
         $(this).fadeTo(300, 0.2);
         $("#pause").fadeTo(300, 1);
         var dewp = document.getElementById("audio");
         if (dewp != null) dewp.dewplay();
      });


   // Modalbox
   $("#slide li a, .work a[rel=mod]").fancybox({
      padding: 0,
      zoomSpeedIn: 300,
      zoomSpeedOut: 300,
      overlayColor: "#164972",
      overlayShow: true,
      overlayOpacity: .25,
      centerOnScroll: false,
      hideOnContentClick: true,
      hideOnOverlayClick: true,
      imageScale: true,
      cyclic: true,
      titleShow: false,
      transitionOut: "elastic"
   });


   // Form errors
   $("#form form").validate({
      rules:{
         name: "required",
         mail:{
            required: true,
            email: true
         },
         subject: "required",
         message: "required"
      },
      messages:{
         name:{ required: '<img src="img/ic/error.png" alt="" title="Veuillez remplir ce champ" />' },
         mail:{
            required: '<img src="img/ic/error.png" alt="" title="Veuillez remplir ce champ" />',
            email: '<img src="img/ic/error.png" alt="" title="Cette adresse e-mail est invalide !" />'
         },
         subject:{
            required: '<img src="img/ic/error.png" alt="" title="Veuillez remplir ce champ" />',
            minlength: '<img src="img/ic/error.png" alt="" title="Votre sujet ne comporte pas assez de caractères" />'
         },
         message:{ required: '<img src="img/ic/error-alt.png" alt="" title="Veuillez remplir le champ" />' }
      }
   });
   
   function init(){
      closeZoom();
      tip(".available, #legend a[title], .links a[title], #cv[title], #cv a[title], .button a[title], #slideshow a[title], abbr[title], #player a[title], #picture[title]");
      scroll("a[href*=#][href!=#]");
   }
   init();
});