
#= require jquery
#= require jquery_ujs
#= require public/color-thief.min.js
#= require public/trunk8.js


#= require jquery.bxslider
#= require jquery.easing.1.3
#= require vendor/jquery.ui.widget
#= require vendor/tmpl.min
#= require vendor/jquery.iframe-transport
#= require vendor/jquery.fileupload
#= require vendor/jquery.fileupload-ui
#= require vendor/jquery.fileupload-process

#= require public/prettify.js
#= require public/jquery.jOrgChart.js


#= require bootstrap-p


@truncate_text = ->
  $('.annonce_list h3').trunk8();
  $('.annonce_list .specific').trunk8();
  
  

@flash_delay = ->
  $("#flashs").find(".alert").each ->
    
    if !$(this).hasClass("delay")
      $(this).addClass("delay")
      $(this).delay(3000).fadeOut();

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


  
bottom = 0
prev_link = ""


$("document").ready ->
  
  flash_delay();

  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()
    
  

  $(".expandable_image").click ->
    maxwidth = 1000
    maxheight = 900
    
    
    
    
    prev_link = $(this)
    $("body").append "<div id='large'></div>"
    title = false
    
    
    
    $("#large").append "<div class='img_container first'></div>"
    $("#large").append "<img src='/close.png' class='close_link' />"
    $(".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
  

  $(".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
  
  
  $('.gal').bxSlider
    adaptiveHeight: true,
    auto: true,
    speed:1000,
    pause:5000,

  
  

  left =0
  
  top = 0
  offset= 0
  
  resize = ->
    
    
    inner_height = ($(window).height() - 40)

    $(".annonce_message_list .height_frame").css("height", inner_height+"px")
    $(".annonce_message_show_container .height_frame").css("height", inner_height+"px")
    
    
    $(".annonces_body").css("min-height", $(window).height()+"px")
    truncate_text()
    
    min_height = 0
    min_height = $(window).height() - $(".top").height() - $(".top_home").height() - $(".bottom").height() - 30
    
    $("#main").css("min-height", min_height+"px")
    
    $("iframe").each ->
      $(this).css
        "height" : Math.round($(this).width()/ 1.77)+"px"
    
    if $(window).width() > 1250
      $(".infos .main").css
        "position" : "static"
      $(".bottom_image").css
        "left" : "0px"
        
    else
      $(".infos .main").css
        "position" : "relative"
      $(".bottom_image").css
        "left" : "-120px"
        

    
    
    $("#large").css "min-height", ($(window).height()-30)+"px"
  
    $(".bxslider").each ->
      
    
      height = ($(window).height())
      optimal_height = Math.round($(this).width()/ $(this).data("ratio"))
      if optimal_height < height
        height = optimal_height
  
      $(this).find("li").css("max-height", height+"px")

    
    position_img_now();
  
    
    
    
  
  resize()
  $('.bxslider').bxSlider
    mode: 'fade'
    captions: true
    auto: true
  resize()
  
  place_annonce_sidebar= ->
    past_height = $(".annonces_sidebar").height()
    var_scroll = $(window).scrollTop()
    top_height = $(".top").height()
    #max_scroll = ($(document).height() - $(window).height() - $("#footer").outerHeight())
    #cat_menu_top = 0
    bottom = $("#categories").outerHeight()+$(".bottom").outerHeight()
    initial_height = $(window).height()- top_height
    max_scroll = ($(document).height() - $(window).height() - bottom)
    
    if var_scroll <= top_height
      annonces_sidebar_height = $(window).height() - (top_height - var_scroll)
      $(".annonces_sidebar").css("bottom", "0px")
    else
      annonces_sidebar_height = $(window).height()
      if var_scroll >= max_scroll
        bottom_f = var_scroll - max_scroll
        $(".annonces_sidebar").css("bottom", bottom_f+"px")
        
      else
        $(".annonces_sidebar").css("bottom", "0px")
        
        
    $(".annonces_sidebar").css("height", annonces_sidebar_height+"px")
    
    if past_height != annonces_sidebar_height
      google.maps.event.trigger(map,'resize')
      map.fitBounds(radiusWidget.get('bounds'))
  
  
  resize_annonces = ->
    $(".search_results").each ->
    
      width = $(this).width()
    
      if $(window).width() > 990
        per_line = 3
      else if $(window).width() > 740
        per_line = 2
      else
        per_line = 1
        
          
      small_width = ((width - (per_line*2)*8) / per_line)-1
      small_height = small_width * (200 / 250)
  

  
      $(this).find(".annonce_list").css
        "width" : small_width+"px"
        "max-width" : small_width+"px"
      $(this).find(".annonce_list .default_image").css
        "height" : small_height+"px"
    
    
    truncate_text()
    
    if $("#with_radius_check").length && $("#with_radius_check").is(':checked')
      place_annonce_sidebar()
      
      
      
    
    
  resize_annonces()
  
  $(window).on "resize", ->
    resize()
    resize_annonces()
    
  place_annonce_panel = ->
    if $(window).scrollTop() > 230
      $("#annonce_save").addClass("fixed")
    else
      $("#annonce_save").removeClass("fixed")
      
  
    
    
  $(window).on "scroll", ->
    place_annonce_panel()
    place_annonce_sidebar()
        
  place_annonce_panel()
  
  top_height = $(window).height()
  img_height = 0.0
  initial_y = 0
  


  
  

  $(document).on "click", ".tags label", ->
    if $("#"+$(this).attr("for")).is(':checked') 
      $(this).removeClass("active")
    else
      $(this).addClass("active")
