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


/**
* This function will take in a classname and either a single exception id or an array of exceptionIds.
* The function will then find all the elements with the classname. 
* Note, this will take a class STRING. meaning, the class string should be exactly as it would appear in a css file. 
* e.g. '.special' or 'ul li.special'  
* After which it will display the display area with the id passed in.
* 
*/
function toggleAreasByClass(classname, exceptionIds){
	elements = $$(classname);
	elements.each(function(item) {
		$(item).hide();										 	
	});
	
	if (Object.isString(exceptionIds)){
		exceptionIds = exceptionIds.split(',');
	}	
	exceptionIds.each(function(id){
		$(id).show();
	})
}

/**
* Will blindUp all other areas that are not already hidden and that are not the exception. 
*/
function toggleBlindAreasByClass(classname, exception){
	// Get all 
	areas = $$(classname);
	areas.each(function(item) {
		if (item.visible() && item.id != (exception)){
			Effect.BlindUp(item,{duration:0.2});
		}
	});
	if (!$(exception).visible()){
		Effect.BlindDown($(exception),{duration:0.2});
	}
}

// ************************************** //
// used for accepts_nested_attributes_for
// ************************************** //
function insert_fields(link, method, content) {
  var new_id = new Date().getTime();
  var regexp = new RegExp("new_" + method, "g")
  $(link).previous(".form").insert({
    bottom: content.replace(regexp, new_id)
  }); 
}

// ************************************** //
// used for accepts_nested_attributes_for
// ************************************** //
function remove_fields(link) {
  var hidden_field = $(link).previous("input[type=hidden]");
  if (hidden_field) {
    hidden_field.value = '1';
  }
  $(link).up(".fields").hide();
}

