You iMac is probably suffering from bad capacitors, this problem plagued iMac G5's, and requires replacing about 25 capacitors on the computer's logic board (Or The Board Itself), this is a degenerative problem that starts out with freezing, and doing what yours is doing, then worsens to the point of not turning on. If you want to verify this, open the back of your iMac, and check the capacitors, for bulging, leaking, and corrosion, capacitors are cylindrical components on the logic board a little less then a centimeter in diameter.