
#= require jquery
#= require jquery_ujs
# require turbolinks

#= require event_form
#= require bootstrap

#= require vendor/jquery-sortable
#= require vendor/jquery.event.drag.js
#= require vendor/bootstrap-slider
#= 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 vendor/select2.min


#= require manager
#= require image_files
#= require pane_hover


#= require redactor
#= require redactor_plugins/fontsize
#= require redactor_plugins/fontfamily
#= require redactor_plugins/fontcolor

#= require nested_fields

#= require Chart

portlet_to_move = false
content_type_to_move =false
@open_collapse3 = ->
  $("#collapse3 .panel").show()
  $("#collapse3").show()
  
@reset_edit_pane = ->
  $("#element_form").html()
  $("#collapse3").hide()
  
@unselect_portlet = ->
  $(".portlet.active").removeClass("active")
  reset_edit_pane()
  
@cancel_move_portlet = ->
  $(".move_message").show()
  $(".cancel_message").hide()
  
  $(".move").removeClass("move")
  $(".portlet_placeholder").remove()
  portlet_to_move = false
  
$(document).ready -> 
  
  $(document).on 'click', 'input.datepicker', ->
    $(this).datetimepicker(
      language: 'fr'
      pickTime: false
    ).focus();
    
  $(document).on 'click', 'input.datetimepicker', ->
    $(this).datetimepicker(
      use24hours: true
      format: 'DD/MM/YYYY hh:mm'
      minuteStepping:15
      language: 'fr'
    ).focus();
    
    
  $(document).on 'click', 'input.timepicker', ->
    $(this).datetimepicker(
      use24hours: true
      format: 'DD/MM/YYYY hh:mm'
      minuteStepping:15
      pickDate: false
      language: 'fr'
    ).focus();
  
    
    

  
  
  $(document).on "click", ".portlet", (event) ->
    if !$(this).hasClass("active") and portlet_to_move == false
      
      $(".portlet.active").removeClass("active")
      $(this).addClass("active")
    
    
      $.ajax({url : $(this).data("edit-link"), type: "GET"});
      $("#collapse3 .trash").attr("href", $(this).data("show-link"));
    
    event.stopPropagation();
  
  $(document).on "click", ".block_portlets", (event) ->
    portlet_to_move = false
    content_type_to_move = false
    $("#element_form").html("")
    
    $(".portlet.active").removeClass("active")
    
  
  
  
  
  $(document).on "click", "#content_types .content_type", ->
    cancel_move_portlet()
    unselect_portlet()
    reset_edit_pane()
    
    
    type = $(this).data("type")
    content_type_to_move = type
    init_portlets_place_holder()
    $(this).addClass("move")
    false
  
    
  
  $(document).on "click", ".portlet_handle", ->
    if portlet_to_move == false
      
      $(".move_message").hide()
      $(".cancel_message").show()
      
      
      id = $(this).data("portlet-id")
      portlet_to_move = $("#portlet_"+id)
      init_portlets_place_holder()
      portlet_to_move.addClass("move")
      false
    else
      cancel_move_portlet()
      
      false
    
  $(document).on "click", ".portlet_placeholder", ->
    if portlet_to_move != false
      $(this).after portlet_to_move
      $(".move").removeClass("move")
      $(".portlet_placeholder").remove()
      update_block_portlet_order(portlet_to_move.closest(".block_portlets").data("block_id"))
      cancel_move_portlet()
    else 
      
      block_id = $(this).closest(".block_portlets").data("block_id")
  
  
      $(this).replaceWith('<div id="new_portlet_form_inline" class="portlet"><div id="new_portlet_content_form"></div></div>');
  
      $.ajax({
        url:"/portlet/portlets/new.js", 
        type: "GET",
        data: { 
          block_id : block_id,
          position : $(this).attr("data_position"),
          content_type : content_type_to_move
          } 
      
      })
      content_type_to_move = false
      $(".portlet_placeholder").remove()
      
      
    

@init_portlets_place_holder = ->
  placeholder = '<div class="portlet_placeholder"><i class="fa fa-angle-right place"></i></div>'
  $(".block_portlets").each ->
    $(this).find(".portlet_placeholder").remove()
    $(this).prepend(placeholder)
    $(this).find(".portlet").each ->
      $(this).after(placeholder)


  

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

@init_menu_items_list = ->
  
  if $(".prevv_folder").length > 0
    
    $(".prevv_folder").sortable({
      group:"menu_items",
      containerSelector :".receptable",
      itemSelector:".menu_item_row",
      handle: ".handle",
      nested:false,
      placeholder:"<div class='placeholder'></div>",

  
      })
  oldContainer = ""
  $("#sortable_menu_item").sortable({
    group:"menu_items",
    containerSelector :".receptable",
    itemSelector:".menu_item_row",
    handle: ".handle",
    nested:true,
    placeholder:"<div class='placeholder'></div>",
    afterMove:  (placeholder, container) ->
      if oldContainer != container and container.el.hasClass("prevv_folder")
        if oldContainer
          oldContainer.el.removeClass("active")
        container.el.addClass("active")
      oldContainer = container
    onDrop: (item, Container, _super) ->
      
      
      if Container
        
        parent_id = Container.el.data("parent-id")
        menu_id = Container.el.data("menu-id")
        id = item.data("menu-item-id")
        $.ajax({
          url:"/admin/menu_items/"+id+".js", 
          type: "PUT",
          data: { 
            reorder : true,
            menu_item : {
              parent_id : parent_id,
              menu_id : menu_id
            } 
          },
          success : ->
          
            
            _super(item)
            item.remove()
          
        })
        
        
        
        
        
      else


        
        
        _super(item)
        
        sortable_data = {parent_id :$("#sortable_menu_item").data("parent-id"), menu_id :$("#sortable_menu_item").data("menu-id") , menu_items_ids : []}
        
        $("#sortable_menu_item").find(".menu_item_row").each ->
          sortable_data.menu_items_ids.push $(this).data("menu-item-id")
        
        
        $.ajax({url : "/admin/menu_items/reorder", data : sortable_data, type: "POST"});
  
    })
    

@update_block_portlet_order = (block_portlet_id) ->
  return_order = []
  
  block_portlet = $("#block_portlet_"+block_portlet_id)
  
  block_data = {block_id : block_portlet.data('block_id'), block_portlet_ids : []}

  block_portlet.children('.portlet').each ->
    if $(this).data('portlet_id')
      block_data.block_portlet_ids.push($(this).data('portlet_id'))
      

  
  return_order.push(block_data);



  $.ajax({url : "/portlet/portlets/reorder", data : {blocks:return_order}, type: "POST"})
 
  
jQuery ->
  
  $(".inline-contenteditable").keypress (e) ->
    if e.which == 13
      false
    else
      $(this).html($(this).text())
      return true
      
  
  $(document).on 'click', ".portlet_title_content .inline-contenteditable", () ->
    
    $.ajax
      url : "/portlet/title_contents/"+$(this).data("title-id")+"/edit.js", 
      type: "GET",
  
  $(document).on 'blur keyup paste', ".portlet_title_content .inline-contenteditable", () ->
    $(this).html($(this).text())
    $.ajax
      url : "/portlet/title_contents/"+$(this).data("title-id"), 
      type: "PUT",
      data : 
        title_content : 
          content : $(this).html()
  
    
    
    
  $(".sortable").sortable({

    containerSelector :".block",
    itemSelector:".portlet",
    handle: ".handle",
    nested:true,
    placeholder:"<div class='placeholder'></div>"
    
    })
    
  
  
    
    
  flash_delay();
  

  item_catched = ""
  
    
  adjustment = ""
  
  $("#menu_item_inspector_container").sortable({


    itemSelector:"#menu_item_informations",
    pullPlaceholder :false,
    placeholder : "",
    handle: ".inspector_handle",
    onDragStart: ($item, container, _super) ->
      $item.css({
        height: $item.height(),
        width: $item.width()
        })
      
      $("body").addClass("dragging")
      
      offset = $item.offset()
      pointer = container.rootGroup.pointer
      adjustment = {
        left: pointer.left - offset.left,
        top: pointer.top - offset.top
        }      
      
    onCancel : ($item, container, _super) ->
      $item.css(position)
      $item.removeClass("dragged")
      $("body").removeClass("dragging")
      
    onDrag: ($item, position, _super) ->

      $item.css({
        left: position.left - adjustment.left,
        top: position.top - adjustment.top - $(window).scrollTop()
      })




    
    })
                   


  


$(document).on "scroll", () ->

  if( $(this).scrollTop() <= 52 ) 
    top = 52 - $(this).scrollTop()
  else
    top = 0

  $("#toolbar-text, #menu_item_informations").css
    top : top
    
$(document).on "click", "#menu_item_informations .save", ->
  $(this).closest(".panel").find("form").submit()
  return false
  
$(document).on "click", "#menu_item_informations h4", ->
  $(this).next(".panel").toggle()
  return false



