making commonjs and amd compat

- using window instead of  since it's not always the window
- trying to export for commonjs and amd first before attaching function to window