Sums of two squares in (certain) integral domains While giving the first of eight lectures on introductory model theory and its applications yesterday, I stated Hilbert's 17th problem (or rather, Artin's Theorem): if $f \in \mathbb{R}[t_1,\ldots,t_n]$ is positive semidefinite -- i.e., non-negative when evaluated at every $x = (x_1,\ldots,x_n) \in \mathbb{R}^n$ -- then it is a sum of squares of rational functions.  One naturally asks (i) must $f$ be a sum of squares of polynomials, and (ii) do we know how many rational functions are necessary?  The general answers here are no (Motzkin) and no more than $2^n$ (Pfister).  Then I mentioned that the case of $n=1$ is a very nice exercise, because one can prove in this case that indeed $f(t)$ is positive semidefinite iff it is a sum of two (and not necessarily one, clearly) squares of polynomials.  Finally I muttered that this was a sort of function field analogue of Fermat's Two Squares Theorem (F2ST).
So I thought about how to prove this result, and I was able to come up with a proof that follows the same recipe as the Gaussian integers proof of F2ST.  Then I realized that the key step of the proof was that a monic irreducible quadratic polynomial over $\mathbb{R}$ is a sum of two squares, which can be shown by...completing the square.
But then today I went back to the general setup of a "Gaussian integers" proof, and I came up with the following definition and theorem.
Definition: An integral domain $R$ is imaginary if $-1$ is a square in its fraction field; otherwise it is nonimaginary.  (In fact I will mostly be considering Dedekind domains, hence integrally closed, and in this case if $-1$ is a square in the fraction field it's already a square in $R$, so no need to worry much about that distinction.)  Note that nonimaginary is a much weaker condition than the fraction field being formally real.
(Definition: An element $f$ in a domain $R$ is a sum of two squares up to a unit if there exist $a,b \in R$ and $u \in R^{\times}$ such that $f = u(a^2+b^2)$.)
Theorem: Let $R$ be a nonimaginary domain such that $R[i]$ ($= R[t]/(t^2+1)$) is a PID.
a) Let $p$ be a prime element of $R$ (i.e., $pR$ is a prime ideal).  Then $p$ is a sum of two squares up to a unit iff the residue field $R/(p)$ is imaginary.
b) Suppose moreover that $R$ is a PID.  Then a nonzero element $f$ of $R$ is a sum of two squares up to a unit iff $\operatorname{ord}_p(f)$ is even for each prime element $p$ of $R$ such that $R/(p)$ is nonimaginary.
[Proof: Introduce the "Gaussian" ring $R[i]$ and the norm map $N: R[i] \rightarrow R$.  Follow your nose, referring back to the proof of F2ST as needed.]
Corollaries: 1) F2ST.  2) Artin-Pfister for $n = 1$.  3) A characterization of sums of two squares in a polynomial ring over a nonimaginary finite field (a 1967 theorem of Leahey).
4) Let $p \equiv 3,7 \pmod{20}$ be a prime number.  Then $p$ is a sum of two squares up to a unit in $\mathbb{Z}[\sqrt{-5}]$ but is not (by F2ST) a sum of two squares in $\mathbb{Z}$.
Finally the questions:

Have you seen anything like this result before?

I haven't, explicitly, but somehow I feel subconsciously that I may have.  It's hard to believe that this is something new under the sun.

What do you make of the strange situation in which $R$ is not a PID but $R[i]$ is?

Note that one might think this impossible, but $R = \mathbb{R}[x,y]/(x^2+y^2-1)$ is an example.  [Reference: Theorem 12 of Elliptic Dedekind domains revisited.] Do you have any idea about how one might go about producing more such examples, e.g. with $R$ the ring of integers of a number field (or a localization thereof)?

Addendum: As I commented on below, a good answer to the first question seems to be the paper

MR0578805 (81h:10028)
Choi, M. D.; Lam, T. Y.; Reznick, B.; Rosenberg, A.
Sums of squares in some integral domains.
J. Algebra 65 (1980), no. 1, 234--256.

In this paper, they prove the theorem above with slightly different hypotheses: $R$ is a nonimaginary UFD such that $R[i]$ is also a UFD.  Looking back at my proof, the only reason I assumed PID was not to worry about the distinction between $R/pR$ and its fraction field.  Just now I went back to check that everything works okay with PID replaced by UFD.  So the second question becomes more important: what are some examples to exploit the fact that $R[i]$, but not $R$, needs to be a UFD?
 A: Let $K$ be a complex quadratic number field such that $K(i)$ has class number $1$. If $K$ has class number $\ne 1$, then $K(i)$ must be the Hilbert class field of $K$, which, in this case, coincides with the genus class field of $K$. By genus theory, the discriminant of $K$ must have the form $d = -4p$ for a prime number $p \equiv 1 \bmod 4$.
In these cases, the ring of integers in $K$ is $R = {\mathbb Z}[\sqrt{-p}]$, and the ring of integers in $L$ is ${\mathbb Z}[i, (1 + \sqrt{p})/2] \ne R[i]$. 
Finding number fields of higher degree with this property seems to be an interesting problem; 
I can't think of an obvious approach in general, but if I find anything, I'll let you know.
Edit 1. The argument works for all imaginary number fields: if $R$ is the ring of integers in a number field $K$, and if $S = R[i]$ is the ring of integers in the extension $L = K(i)$, then disc$(L) = \pm 4$ disc$(K)^2$ (this is a simple determinant calculation: take an integral basis
$\{\alpha_1, \ldots, \alpha_n\}$ for $K$; then $\{\alpha_1, \ldots, \alpha_n, i\alpha_1, \ldots, i\alpha_n\}$ is an integral basis for $L$). 
On the other hand, if $L$ has class number $1$ and $K$ is not a PID, then $K$ has class number $2$ and $L$ is the Hilbert class field of $K$. This implies disc$(L) = \pm$  disc$(K)^2$.
The problem in the non-imaginary case is that $K(i)$ might be unramified at all finite primes, but not at infinity; in this case, $K$ has class number $2$ in the strict sense, yet its ring of integers is a UFD. 
Remark 2. By looking at $p = \alpha^2 + \beta^2$ modulo $4$ it follows (unless I did something stupid) that primes $p \equiv 3, 7 \bmod 20$ are not sums of two squares in 
${\mathbb Z}[\sqrt{-5}]$. 
Edit 2. Your suggestion to look at rings $R[\frac12]$, where $R$ is the ring of integers of a quadratic number field, seems to work for $K = {\mathbb Q}(\sqrt{-17})$, which has a cyclic class group of order $4$. The ring $R[\frac12]$ has class number $2$ because $2$ is ramified and so generates a class of order $2$, and $S = R[\frac12,i]$ is the integral closure of $R[\frac12]$ in the extension $L = K(i)$. The ring $R[\frac12]$ has class number $2$, and $S$ is a UFD since $L$ has class number $2$, and its class group is generated by one of the prime ideals above $2$ (the ramified prime above $2$ in $K$ splits in $L$).
Edit 3. The corollary concerning sums of two squares in $R[\frac12]$ shows that primes $p \equiv 3 \bmod 4$ splitting in $K$ are sums of two squares up to units. In fact it follows from genus theory that the prime ideals above such $p$ are not in the principal genus, hence lie in the same class as the prime above $2$ or in its inverse. This implies that either $2p = x^2 + 17y^2$ or $8p = x^2 + 17y^2$, giving a representation of $p$ as a sum of two squares up to a unit (remember $2$ is invertible). Thus everything is working fine.
