// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function mark_for_destroy(element){
	
	$(element).next('.should_destroy').value = 1;
	$(element).up('.task').hide();
}

function prompt_box(name,id){

	var name=prompt("Please enter your name",name);
	if (name!=null && name!="")  {
	 	return name;
	}
}


/* ------------------------------------------------------------------------
 * categories.js
 * Copyright (c) 2004-2007 37signals, LLC. All rights reserved.
 * ------------------------------------------------------------------------ */

Element.activate = function(element, activate) {
  element = $(element)

  var inactive_text = element['inactive_text']
  if(!inactive_text) {
    element['inactive_text'] = element.innerHTML
    inactive_text = element.innerHTML
  }

  var active_text = element.getAttribute('active_text') || element.innerHTML
  var active_class = element.getAttribute('active_class') || element.className

  if(!activate) {
    element.innerHTML = inactive_text
    if(active_class) Element.removeClassName(element, active_class)
    element['active'] = false
  } else {
    element.innerHTML = active_text
    if(active_class) Element.addClassName(element, active_class)
    element['active'] = true
  }
};


categories = {
  toggleEdit: function() {
    if($('edit_categories_link')['active']) {
      Element.activate('edit_categories_link', false)
      $('CategoryList').removeClassName("editing")
    } else {
      Element.activate('edit_categories_link', true)
      $('CategoryList').addClassName("editing")
    }
    $('add_new_category').toggle();
  },

  toggleOperationSpinner: function(id) {
    Element.toggle('operation_wait_' + id,
      'operation_rename_' + id, 'operation_delete_' + id)
  },

  cancelEdit: function(id) {
    $('edit_category_' + id).remove()
    $('category_menu_item_' + id).show()
  },

  edit: function(id, name, url) {
    name = prompt("Rename this category", name);
    if(name) {
      this.toggleOperationSpinner(id);
      new Ajax.Request(url,
        { asynchronous: true,
          evalScripts:  true,
          method:       'put',
          parameters:   'category[name]=' + encodeURIComponent(name),
          onFailure:    function(req) { categorySelect.addFailed(req) }});
    }
  },


	addSaro: function(url){
	 var name = prompt("Enter the new category name:", "");
	    if(name) name = name.strip()
	    if(!name || name.length < 1) {
			alert("Nessuna categoria creata");
		}
		else { 
	      new Ajax.Request(url,
	        { asynchronous: true,
	          evalScripts:  true,
	          method:       'post',
	          parameters:   'category[name]=' + encodeURIComponent(name),
	          onFailure:    function(req) { categorySelect.addFailed(req) }});			
			
		}
		
	},
	
  addNew: function() {
    toggle = function() { $('add_new_category_link', 'add_new_category_spinner').each(Element.toggle); };
    categories.add({ addFailed: toggle, validName: toggle });
  },

  add: function(options) {
    options = options || {};
    var name = prompt("Enter the new category name:", "");
    if(name) name = name.strip()
    if(!name || name.length < 1) {
      if(options.invalidName) options.invalidName();
    } else {
      if(options.validName) options.validName(name);
      new Ajax.Request(this.url, {asynchronous:true, evalScripts:true,
        parameters:'category[name]=' + encodeURIComponent(name),
        onFailure: function(req) { categories.addFailed(req, options); }
      });
    }
  },

  addFailed: function(request, options) {
    alert("An error prevented the category from being added. Please try again.")
    if(options.addFailed) options.addFailed(req);
  }
}

// categorySelect = {
//   controls: [],
// 
//   register: function(element) {
//     var control = $(element);
//     var optionsIncludeAddCommand = (control.options[control.options.length-1].value == '!');
//     if (!this.controls.include(control) && optionsIncludeAddCommand) {
//       this.controls.push(control);
//       control.observe('change', this.detectChange.bind(this));
//     }
//   },
// 
//   detectChange: function(event) {
//     var control = Event.element(event)
//     if(control.selectedIndex == control.options.length - 1) {
//       categories.add({
//         invalidName: function() {
//           control.selectedIndex = 0;
//         },
// 
//         validName: function(name) {
//           categorySelect.control = control
//           categorySelect.enableControls(false)
//         },
// 
//         addFailed: function(req) {
//           categorySelect.enableControls(true)
//           categorySelect.control.selectedIndex = 0
//        }
//       });
//     }
//   },
// 
//   enableControls: function(setting) {
//     this.controls.each(function(item) {
//       item.disabled = !setting;
//     })
//   },
// 
//   addCategory: function(id, name, position) {
//     position++
//     this.controls.each(function(item){
//       var options = $A(item.options)
//       options.splice(position, 0, new Option(name, id))
//       for(var idx = 0; idx < options.length; idx++) {
//         item.options[idx] = options[idx]
//       }
//     })
//     this.reset(position);
//   },
// 
//   reset: function(selected) {
//     if(this.control) this.control.selectedIndex = (selected || 0);
//     this.enableControls(true)
//   },
// 
//   renameCategory: function(id, name) {
//     this.controls.each(function(item){
//       for(var idx = 0; idx < item.options.length; idx++) {
//         if(parseInt(item.options[idx].value) == id) {
//           item.options[idx].text = name;
//           break;
//         }
//       }
//     })
//   },
// 
//   deleteCategory: function(id) {
//     this.controls.each(function(item){
//       for(var idx = 0; idx < item.options.length; idx++)
//         if(parseInt(item.options[idx].value) == id) {
//           item.options[idx] = null;
//           break;
//         }
//     })
//   }
// }
// 

// function newCategory(element){
// 	var name=prompt("Enter the new category name","");
// 	
// 	
// 	if (name!=null && name!="")
// 	  {
// 	  	alert("ciao" +name);
// 	  }
// 
// 	
// }
