I don't completely understand what you're getting at, but I can try to help...
If you are in situation #2 and it's not working, and you're using frames or iframes, that may be the cause of your problem.
If none of the above is causing your problem, you might use object oriented programming to create a specific namespace for your functions. Then the object gets instantiated when you first load the page in "index" mode, and that object should immediately be available as the gallery bits load asynchronously.
Also, if you use object notation to put your functions in a specific namespace, that might help overcome any issues you might be having around variable scope.
Does that help at all? If not, can you be more specific about what you're trying to do and how it's not working?