Retrieving idvisit and data storing

Hello I’m a recent user to Piwik,

I’m interested in using Piwik in conjunction with a bigger project. Piwik seems to handle many of the analytic traits but I’m more interested in being able to tie data of a visitor overtime.

For Example:
idvisit (1) came to the site and logged in using username (sdouglas), the login script read the idvisit (1) and associated that id with username (sdouglas).

How can I get the idvisit of the current visitor in my scripts?

The best solution would be the ability to get the idvisit without using cookies, I understand that because it is an image that is generated that this might not be easily possible. I imagined this could had been done by the piwik.php script returning a javascript variable to the client which then could be picked up and used via ajax calls to other scripts.

An alternative solution would be able to translate the visitor cookie and lookup the idvisit via the database itself. If this is the option, then how is the cookie translated?

Thank you to the Piwik and OpenX teams for two amazing open source projects!

It looks as though the hooks/plug-ins method can be a use for my first option:
How-to Plugins / Hooks - Piwik Documentation

It still would be nice to know how to decode the cookie value that is set by Piwik.