I posted something very similar, but yours seems to be a bit more flexible: http://www.hawkee.com/snippet/9368/