Generalizing the Z-scan theory for nonlocal nonlinear media