This is a very complicated thing to do and the best bit of advice I can give is to study in detail the exact way that XenForo handles it by default.
To answer your question you will need to set up a custom content type which specifies a custom AttachmentHandler.
You shouldn't need any custom javascript.
One thing that might help. If you're buying the Resource Manager, this will show you perfectly how a custom attachment type is added using an add-on. You will be able to see what templates need to be created, what Model functions are required, what an AttachmentHandler should look like, etc.