Hi,
Did you search the forum a little. Not the first time we have something alike.
Before spending too much time on it trying to work your own from scratch, may be worth checking on the solution from :
may be combined with:
There must be something that can be achieved around these parts