That sounds pretty complex but intreating all the same! :)

I would advise googling about the subject - you may find helpful information.
Aln alternatie to this however could be to post what code...