You might like to try working with the code 'bigimage.js' found here. It may give you some idea about what to do.
However, you should make sure that you are doing this with validated HTML. When people correct their HTML so that it is strictly valid, they often find many problems go away, without touching script at all. For the best results, you should validate as Strict HTML (and also validate CSS, which you should be using). Do not use deprecated markup whatsoever. Once you have validated HTML and CSS, then you can correct scripting problems affecting document structure and style.