
#= require jquery
#= require jquery_ujs

#= require shared/forms
#= require event_form

#= require twitter/bootstrap
#= require vendor/jquery-sortable

#= require vendor/jquery.event.drag.js

# require turbolinks

#= require vendor/bootstrap-slider
#= require vendor/bootstrap-datepicker
#= require vendor/bootstrap-timepicker

#= 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 nested_fields





@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 = ""
  $(".block_portlets_sortable").sortable({
    nested:true,
    handle: ".handle",
    group : "block_portlets",
    containerSelector :".block_portlets",
    itemSelector:".portlet",
    placeholder:"<div class='placeholder'></div>",
    onDrop: ($item, container, _super) ->
      
      $item.removeClass("dragged").removeAttr("style")
      $("body").removeClass("dragging")
      
      
      if $item.data("portlet_id")
        
        block_portlet_id = $item.closest(".block_portlets").data('block_id')
        
        update_block_portlet_order block_portlet_id
        
        
      else
        $("#content_types").html item_catched
      
        block_id = container.el.data("block_id")
    
    
        $item.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 : $(this).attr("data_block_id"),
            position : $(this).attr("data_position"),
            content_type : $item.attr("data_type")
            } 
        
        })
      
      
      
    isValidTarget: ($item, container) ->
      
      if container.el.attr("id") != "content_types"
        return true
      
    onDragStart: ($item, container, _super) ->
      item_catched = $("#content_types").html()
      
      
      $item.css({
        height: $item.height(),
        width: $item.width()
      })
      $item.addClass("dragged")
      $("body").addClass("dragging")
      
      
    
    })
    
    
    
  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 "mouseover", ".portlet", ->
  
  
  
  
  
 # $(this).children(".btn-toolbar").show()
  

#$(document).on "mouseout", ".portlet div", ->

#  $(".btn-toolbar").hide()
  
  


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

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

  $("#toolbar-text, #menu_item_informations").css
    top : top
