
#= require jquery

#= require jquery_ujs

#= require ./jquery.superslides.js
#= require jquery.easing.1.3



bottom = 0
prev_link = ""

$ ->  

  position_img_now = ->
    
    
    
   # alert $("#large .large-img").outerHeight(false)
    
    imgheight = $("#large .large-img").outerHeight(false) + $("#large h3").outerHeight(false) 
    
     
    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 "<img src='/close.png' class='close_link' />"
    
    $("#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






resize = () ->
  

  $("#large").css "min-height", ($(window).height()-30)+"px"
  
  
  
  height = ($(window).height()-50)
  optimal_height = $(window).width()/ 1.66
  if optimal_height < height
    height = optimal_height
  
  $(".slider").css({"height": height+"px","width": $(window).width()+"px"  })
  

$(window).load ->
  resize()
  
  $(document).on "scroll", ->
    scroll_ajust()
  
  $(document).on "touchmove", ->
    scroll_ajust()

  scroll_ajust = () ->
    var_scroll = $(window).scrollTop()
    if var_scroll > 100
      $(".top").addClass("move_top")
    else
      $(".top").removeClass("move_top")
    
  

$ ->
  $(window).bind "resize", ->
    resize()
  resize()
  


  