
#= require jquery
#= require jquery_ujs

#= require jquery.easing.1.3



#= require jquery.bxslider.min


@scrollToAnchor = (aid) ->
  aTag = $("#" + aid )
  $("html,body").animate({scrollTop: aTag.offset().top}, 1000)
  return


  
  
bottom = 0
prev_link = ""
$("document").ready ->
  
  $("#logo").hide()
  $("#baseline").hide()
  
  $("#logo").fadeIn()
  $("#baseline").fadeIn()
    
    
  
  position_img_now = ->
    
    
    

    
    imgheight = $("#large .large-img").outerHeight() + $("#large h3").outerHeight() 
    

    
    margintop = (( $(window).height() - imgheight) / 2 )
 
    $("#large .large-img").css
      "margin-top" :(margintop+"px")
      #"width" : "100px"

    
    
  position_img = ->
    $("#large .large-img").one "load", ->
      
      position_img_now()
    
  
  
  $(".rea-gal a").click ->
    maxwidth = 1000
    maxheight = 900
    
    
    
    
    prev_link = $(this)
    $("body").append "<div id='large'></div>"
    title = false
    
        
    $("#large").append "<img src='/arrow-next.png' class='next' />"
    $("#large").append "<img src='/arrow-prev.png' class='prev' />"
    $("#large").append "<div class='img_container first'></div>"
    $(".img_container.first").append "<img src="+$(this).attr("href")+" class='large-img' />"
    
    if $(this).attr "title"
      title = $(this).attr "title"
      $(".img_container.first").append "<h3>"+title+"</h3>"
      
    $("#large .large-img").one "load", ->
      $("#large").fadeIn(500)
    position_img();
    if $(window).height() > (maxheight+100)
      $("#large .large-img").css
        "max-height" : maxheight
    else
      $("#large .large-img").css
        "max-height" : "85%"
        
    if $(window).width() > (maxwidth+100)
      $("#large .large-img").css
        "max-width" : maxwidth
    else
      $("#large .large-img").css
        "max-width" : "85%"
    
    position_img();
    
    
    false
  
  
  
  $("body").on "click", "#large", ->
    $(this).fadeOut 300, ->
      $(this).remove()
    
    
  $("body").on "click" ,"#large .prev",->
    
    if prev_link.prev("a").length > 0
      link = prev_link.closest("a").prev("a")
      
      
      
    else
      link = prev_link.closest("div").children("a:last")
    
   
    #titre = photo.find("h3")
    $(".img_container.first").fadeOut 300, ->
      $("#large h3").remove()
      if link.attr "title"
        title = link.attr "title"
        $(".img_container.first").append "<h3>"+title+"</h3>"
      
      
      $('#large .large-img').attr("src", link.attr("href"))
      $("#large .large-img").one "load", ->
        $(".img_container.first").fadeIn()
      position_img();
    prev_link = link
    
    false
  
  
  $("body").on "click" ,"#large .next",->
    
    if prev_link.next("a").length > 0
      link = prev_link.closest("a").next("a")
      
      
      
    else
      link = prev_link.closest("div").children("a:first")
    
   
    #titre = photo.find("h3")
    
    $(".img_container.first").fadeOut 300, ->
      $("#large h3").remove()
      if link.attr "title"
        title = link.attr "title"
        $(".img_container.first").append "<h3>"+title+"</h3>"
      
      $('#large .large-img').attr("src", link.attr("href"))
      $("#large .large-img").one "load", ->
        $(".img_container.first").fadeIn()
      position_img();
    prev_link = link
    
    false
  
  
  $('.gal').bxSlider
    adaptiveHeight: true,
    auto: true,
    speed:1000,
    pause:5000,

  
  
  
  
 # $(".infos").hide();
  $(".infos-button").click ->
    $(this).next(".infos").slideToggle()
    $("html,body").animate({scrollTop: ($(this).offset().top-60)}, 500)
  
  left =0
  
  top = 0
  offset= 0
  height = 0
  resize = ->
    
    $("#slider_content").each ->
      
    
      height = ($(window).height())
      optimal_height = Math.round($(this).find(".bxslider").width()/ $(this).find(".bxslider").data("ratio"))

      if optimal_height < height
        height = optimal_height

      $(this).find("li").css({"max-height": height+"px","height": height+"px"})
      img_height = Math.round($(this).find(".bxslider").width() / 1.5)
      img_margin = Math.round((height - img_height)/ 2)
      $(this).find("li img").css("margin-top", img_margin+"px")
      
      
      
      $(".slider").css({"height": (height)+"px","width": $(window).width()+"px"  })
      $("#main").css("top",(height))
      $("#menu-container").css("top",(height-50)+"px")
      $("#logo").one("load", ->
    
  
        left = ($(window).width() - $("#logo").width()) / 2
        bottom = (height - $("#logo").height() - $("#baseline").height() - 40)/ 1.10
        $("#logo").css({"top": bottom+"px","left": left+"px"})
    
        $("#baseline").css({"top": (bottom+$("#logo").height())+"px","left": left+"px"})
  
      ).each ->
        $(this).load()  if @complete
        return
      
      
    $(".rea-gal img").css "width", ($(window).width() - 6 * 5 )/ 5
    
#    $("#brainstorming").css({"min-height": ($(window).height())+"px","width": $(window).width()+"px"  })
  
    $("#myCanvas").attr({height: ($(window).height()-160),"width": ($(window).width()-2)+"px"  })
  
    
    


  
    position_img_now();

  
    
  
  

  $("#brainstorming ul li").css("opacity", 0)
  $(".rea-gal img").css("opacity", 0)
  gal_imgs_show= []
  
  $(".rea-gal img").each ->
    gal_imgs_show.push $(this)
  
  
  
  
  test = []
  $("#brainstorming ul li").each ->
    test.push $(this)
  test = test.sort ->
    return .5 - Math.random()
    
  animate_brain = ->
    
    animate_li_i = 0
    if !$("#brainstorming").hasClass "showing"
      animate_li = ->
  
        if test.length > 0
          test[animate_li_i].animate({left:0,opacity : 1}, 800)
          $("#brainstorming").addClass "showing"
          if animate_li_i < (test.length-1)
            animate_li_i = animate_li_i+1
            setTimeout(animate_li, 100)
        
      animate_li()
  
      
  animate_realisation = ->
    
    if !$(".rea-gal").hasClass "showing"
      $(".rea-gal").addClass "showing"
      i_img = 0
      

      
      animate_img = ->
        if gal_imgs_show.length > 1
          gal_imgs_show[i_img].animate({left:0,opacity : 1}, 400)
        
          if i_img  < (gal_imgs_show.length-1)
            i_img  = i_img+1
            setTimeout(animate_img, 50)
        
      animate_img()
  
  
  resize()
  
  $('.bxslider').bxSlider
    mode: 'fade'
    captions: false
    pager :false
    auto: true
  resize()
  
  $(window).on "resize", ->
    resize()
  
  
  animate_realisation()
  animate_brain()
  
  
  $(window).on "scroll", ->
    
    top = $(window).scrollTop()
    
    
    
    
    if $(window).scrollTop() > (height-50)
      #$("#menu").css("position", "fixed")
    else
      #$("#menu").css("position", "relative")
    if $(window).scrollTop() > bottom-200
      $("#logo").fadeOut()
      $("#baseline").fadeOut()
      #$("#logo-min").fadeIn()
      
    else if $(window).scrollTop() < bottom+200
      
      $("#logo").fadeIn()
      $("#baseline").fadeIn()
      #$("#logo-min").fadeOut()
        
  
    
  
  resize()
  



