Is there any way to programmatically determine which version of SharePoint (2007 or 2010) is currently running, using the API? I have a web part that needs to emit different blocks of Javacript based on the version of SharePoint.
You can get the Version of the SPFarm object. There is no need to elevate privileges.
Listed below is a code sample from my series on SharePoint 2007 - 2010 migration.
Note that you may want to check if the SP2010 site is running in SP2007 visual mode. You can find a great article on this topic on Microsoft's SharePoint Blog: 'Making Your SharePoint 2010 Customizations Visual Upgrade Aware'.