Depending on the exact model of your iMac you can upgrade to Leopard 10.5.8. You may or may not need to first download/install a Firmware Upgrade and which one depends on your exact model. The Early & Late 2005 models have Open Firmware and don't need any Firmware Update. The June 2003, June 2004 & Late 2004 models have individual Firmware Updates and it's crucial you apply the correct one for your iMac before upgrading the OS.
You can buy a Full Retail (Universal) Install version of Mac OS X on eBay with the earlier OS version being somewhat cheaper compared to Leopard.
If you look at the label on the discs
here you need to ensure that the disc you purchase looks the same, has the large X on a black label. Also you need to make sure that you buy a Full Retail Install and not an upgrade which would require you already have the previous Mac OS X version installed first for example, to upgrade to Leopard would require Mac OS X 10.4.x first or the upgrade would fail.
Some eBay sellers sometimes put the wrong discs up for sale and advertise them as Full Retail when they are not. Any disc with a grey label is not suitable and is designed to work solely on the Mac it was originally sold with. The disc and the Mac are one and can't be interchanged with any other disc unless it's a Full Retail Version.
Hope this helps and feel free to return with any questions you have. We're only to happy to point you in the right direction ...