Charge Li-Ion intelligently from Arduino

Hi all, I'm trying to create a battery powered device that can be recharged by the Arduino's microUSB port. Ideally, I would like to have the battery permanently hooked in to some pins on the Arduino, so when the Arduino was plugged in via microUSB the battery would charge, and when disconnected, the system would be powered from the battery.

I know Li-Ion cells need to be carefully managed to avoid exploding / permanently draining – is this something that can be done in software on the Arduino, or do I need a special board eg Adafruit Micro-Lipo Charger for LiPo/LiIon Batt w/MicroUSB Jack [v1] : ID 1904 : $6.95 : Adafruit Industries, Unique & fun DIY electronics and kits ? If I need a special board, is there a way to make said board take input power from the Arduino and not its own microUSB? Would it be as simple as wiring (for the previous example LiPoly charging board) the Arduino's 5V out / GND pins to the charging board's inputs? In that case, how would I then reverse the direction of current flow when unplugged such that the battery powered the device?

Thanks for any help!

You an buy modules (boards ) with usb on one side and terminals for 3.7V Lithium ion batteries on the other.

However you problem will be that you can't get a 5V lithium battery. The cell voltage is around 3.7V.

For 5V you'd need a 2 cell battery, and also have at least 8V (probably more) to charge it, via a charge controller IC.

5V USB is too low, I guess you could use a buck converter to generate 8V from 5V but you'd also need another voltage regulator (buck converter) to bring the voltage of the battery ie around 7V down to 5V, and that won't be very efficient

The optimal solution is to switch to using a 3.3V Arduino, running on 8Mhz. I know there are Pro Mini boards with 8Mhz clocks that various companies sell (I have 2 such boards I bought from eBay)