In the above pic xbar is a constant and so can come outside the summation and the second expression in the first line is equal to zero so the second line is just a rearrangement of $\displaystyle \sum u_i x_i \sum u_i$

I can't figure out how we get from $\displaystyle \sum u_i x_i \sum u_i$ to the $\displaystyle \sum ui^2x_i$ plus the double summation of i and i not equal to j of $\displaystyle u_iu_jx_j$