// ===========================================================================
// SETTINGS
// ===========================================================================

var hoverExitDelay = 500
var lastHoverTimer = null
var lastHoverCommand = null

function clearCurrentHover() {
  if(!lastHoverTimer) return
  clearTimeout(lastHoverTimer)
  eval(lastHoverCommand)
  lastHoverTimer = null
  lastHoverCommand = null
}

function hoverEndWith(command) {
  lastHoverCommand = command
  lastHoverTimer = setTimeout(command, hoverExitDelay)
}


// ==================
// AJAX BLOCK MANAGER
// ==================
var ajaxBlock = {
  edit: function(id) {
    if(id) {
      this.editExisting(id)
    } else {
      this.addNew()
    }
  },	
  editExisting: function(id) {
    Element.showIf('edit_item_' + id)
    Element.hideIf('show_item_' + id)
    Field.focus('edit_name_' + id)
  },
  addNew: function() {
    Field.focus('new_name')
  },
  saving: function(id) {
    Element.showIf('saving_indicator_' + id)
  },
  display: function(id) {
    if(id) {
			Element.showIf('show_item_' + id)
			Element.hideIf('edit_item_' + id, 'saving_indicator_' + id)
			Element.showIf('notice_for_item_' + id)
			new Effect.Fade('notice_for_item_' + id) 
			setTimeout("$('notice_for_item_" + id + "').style.display = 'none';$('notice_for_item_" + id + "').innerHTML = '';", 2400)
    }
  }
}




// ===================
// ICCCS BLOCK MANAGER
// ===================

var icccsBlock = {
  hoverBegin: function(icccID) {
    clearCurrentHover()
    toggle_edit_link(true, 'show_iccc' + icccID, 'edit_iccc' + icccID + '_link')
  },
  hoverEnd: function(icccID, delay) {
    if(delay)
      hoverEndWith("icccsBlock.hoverEnd('" + icccID + "')")
    else
      toggle_edit_link(false, 'show_iccc' + icccID, 'edit_iccc' + icccID + '_link')
  },
  edit: function(gpID,icccID) {
    if(icccID) {
      this.editExisting(gpID,icccID)
    } else {
      this.reorderComplete(document.getElementsByClassName('drag_control'),gpID)
      this.addNew(gpID)
    }
  },
  saving: function(icccID) {
    Element.showIf('saving_iccc' + icccID)
  },
  display: function(gpID,icccID) {
   	if(gpID) {
	    Element.showIf('gpMenu_iccc'+gpID, 'gpMenu_iccc'+gpID+'_add')
	    Element.hideIf('gpAddForm_iccc'+gpID)
	    if(document.getElementById('iccc_gp_' + gpID + '_header').numChildren < 2) {
	      Element.hide('gpMenu_iccc'+gpID+'_reorder', 'gpMenu_iccc'+gpID+'_separator')
	    } else {
	      Element.show('gpMenu_iccc'+gpID+'_reorder', 'gpMenu_iccc'+gpID+'_separator')
	    }
	  }
    if(icccID) {
      Element.showIf('show_iccc' + icccID)
      Element.hideIf('edit_iccc' + icccID, 'saving_iccc' + icccID)
      toggle_edit_link(false, 'show_iccc' + icccID, 'edit_iccc' + icccID + '_link')
    }
  },
  show: function() {
    Element.showIf('icccs_block')
  },
  editExisting: function(gpID,icccID) {
    Element.showIf('edit_iccc' + icccID)
    Element.hideIf('gpAddForm_iccc' + gpID, 'gpMenu_iccc' + gpID, 'show_iccc' + icccID)
    Field.focus('edit_code_' + icccID)
  },
  addNew: function(gpID) {
    Element.showIf('gpAddForm_iccc' + gpID)
    Element.hideIf('gpMenu_iccc' + gpID)
    Field.focus('new_iccc_code')
  },
  beforeAdd: function(gpID) {
    Element.show('adding_iccc'+gpID)
  },
  afterAdd: function() {
    Field.clear('new_iccc_code')
    Field.clear('new_iccc_name')
  },
  addCompleted: function(request,gpID) {
  	alert(request.responseText)
    document.getElementById('iccc_gp_' + gpID + '_header').numChildren += 1
    Element.hide('adding_iccc'+gpID)
    item_id = request.getResponseHeader("icccID")
    $('iccc_gp_'+gpID).innerHTML = request.responseText
    new Effect.Highlight('content_for_iccc' + item_id) 
    setTimeout("$('content_for_iccc" + item_id + "').style.background = 'transparent'", 2100)
  },
  checkEmpty: function() {
    if(this.isEmpty()) {
      this.hide()
      return true
    } else {
      this.display()
      return false
    }
  },
	reorder: function(gpID) {
    controls = document.getElementsByClassName('drag_control_gp'+gpID)
    if(!controls) return
    
    //this.makeDraggable(gpID)

    if(!Element.visible(controls[0])) {
      toggle_edit_link_possible = false
      this.reorderBegin(controls,gpID)
    } else {
      toggle_edit_link_possible = true
      this.reorderComplete(controls,gpID)
    }
  },
  reorderBegin: function(controls,gpID) {
    Element.showIf('gpMenu_iccc'+gpID)
    Element.showAll(controls)
  },
  reorderComplete: function(controls,gpID) {
    if(document.getElementById('iccc_gp_' + gpID + '_header').numChildren > 1) Element.show('gpMenu_iccc' + gpID + '_separator')
    Element.hideAll(controls)
  },
  makeDraggable: function(gpID) {
    if ($('iccc_gp_'+gpID+'_list').sortable_tag) {
      return true;
    } else {
      Sortable.create('iccc_gp_'+gpID+'_list', { 
        handle:'dragger',
        onUpdate:function(element) { 
          new Ajax.Request(
            '/admin/icccs/reorder',
            { parameters:Sortable.serialize('iccc_gp_'+gpID+'_list'), asynchronous:true }
          )
        }
      });  
    }
  }
}


// ===================
// ITEMS BLOCK MANAGER
// ===================

var itemsBlock = {
  hoverBegin: function(id) {
    clearCurrentHover()
    toggle_edit_link(true, 'show_item_' + id, 'edit_item_' + id + '_link')
  },
  hoverEnd: function(id, delay) {
    if(delay)
      hoverEndWith("itemsBlock.hoverEnd(" + id + ")")
    else
      toggle_edit_link(false, 'show_item_' + id, 'edit_item_' + id + '_link')
  },
  edit: function(id) {
    this.show()
    if(id) {
      this.editExisting(id)
    } else {
      this.reorderComplete(document.getElementsByClassName('movement_control'))
      this.addNew()
    }
  },
  show: function() {
    Element.showIf('items_block')
  },
  isEmpty: function() {
    var incomplete_r = $('incomplete_items_list')
    var completed_r  = $('completed_items_table')
    return ((!incomplete_r || Element.empty(incomplete_r)) &&
             (!completed_r || Element.empty(completed_r)))
  },
  editExisting: function(id) {
    Element.showIf('edit_item_' + id)
    Element.hideIf('additemdialog', 'additem', 'show_item_' + id)
    Field.focus('edit_input_' + id)
  },
  addNew: function() {
    Element.showIf('additemdialog')
    Element.hideIf('additem')
    Field.focus('new_item_content')
  },
  display: function(id) {
    this.show()
    Element.showIf('additem', 'list_edit_toggle')
    Element.hideIf('additemdialog')
    if(this.incompleteItemCount < 2) {
      Element.hide('reorder_items', 'additem_separator')
    } else {
      Element.show('reorder_items', 'additem_separator')
    }
    if(id) {
      Element.showIf('show_item_' + id)
      Element.hideIf('edit_item_' + id, 'saving_item_' + id)
      toggle_edit_link(false, 'show_item_' + id, 'edit_item_' + id + '_link')
    }
  },
  saving: function(id) {
    Element.showIf('saving_item_' + id)
  },
  reorder: function() {
    controls = document.getElementsByClassName('drag_control')
    if(!controls) return
    
    this.makeDraggable()

    if(!Element.visible(controls[0])) {
      toggle_edit_link_possible = false
      this.reorderBegin(controls)
    } else {
      toggle_edit_link_possible = true
      this.reorderComplete(controls)
    }
  },
  reorderBegin: function(controls) {
    this.show()
    Element.showIf('additem')
    Element.showAll(controls)
  },
  reorderComplete: function(controls) {
    this.show()
    if(this.incompleteItemCount > 1) Element.show('additem_separator')
    Element.hideAll(controls)
  },
  hide: function() {
    Element.hideIf('items_block', 'items_link_on')
    Element.showIf('items_link_off')
  },
  checkEmpty: function() {
    if(this.isEmpty()) {
      this.hide()
      pageManager.checkEmpty()
      return true
    } else {
      this.display()
      return false
    }
  },
  beforeAdd: function() {
    Element.show('items_indicator')
  },
  afterAdd: function() {
    Field.clear('new_item_content')
    Field.focus('new_item_content')
  },
  addCompleted: function(request) {
    this.incompleteItemCount += 1
    Element.hide('items_indicator')
    item_id = request.getResponseHeader("BackpackItemID")
    $('incomplete_items').innerHTML = request.responseText
    new Effect.Highlight('content_for_item_' + item_id) 
    setTimeout("$('content_for_item_" + item_id + "').style.background = 'transparent'", 2100)
  },
  itemDeleted: function() {
    this.incompleteItemCount -= 1
    this.checkEmpty()
  },
  makeDraggable: function() {
    if ($('incomplete_items_list').sortable_tag) {
      return true;
    } else {
      Sortable.create('incomplete_items_list', { 
        handle:'dragger',
        onUpdate:function(element) { 
          new Ajax.Request(
            itemReorderUrl,
            { parameters:Sortable.serialize('incomplete_items_list'), asynchronous:true }
          )
        }
      });  
    }
  }
}
