// Bind two molecules using selected hydrogens

//info.clear();
var model    = project.current_model();
var H1, H2;

//find_selected (model, H1, H2);
//transfer_charged (H1, H2);
//rebuild (H1, H2);

//function find_selected (model, H1, H2)
//{
	var selected = 0;

	for (var i=0;  i<model.atom_count();  ++i)
	{
		var atom = model.atom (i);

		if (atom.is_selected ())
		{
			++selected;
			if (selected == 1)	H1 = atom;
			if (selected == 2)	H2 = atom;
			if (selected >= 3)	info.append ('Too much atoms selected');
		}
	}
//}

//function transfer_charged (H1, H2)
//{
	var scale = 1. / 1.53;
	//var scale = 1.;
	var bound1 = H1.bound_atom(0);
	var bound2 = H2.bound_atom(0);
	bound1.set_charge (bound1.charge() + scale * H2.charge() + (1. - scale) * H1.charge());
	bound2.set_charge (bound2.charge() + scale * H1.charge() + (1. - scale) * H2.charge());
//}

//function rebuild (H1, H2)
//{
	var build = model.build ();
	var bond  = build.new_bond (bound1, bound2, 'Single');
	bond.set_style ('ball_and_stick');

	var erase = model.erase();
	erase.add (H1);
	erase.add (H2);
	erase.execute();
//}

//var geometry = model.geometry ();
//window.menubar.menu_Build.Detect_molecules.trigger();
