function click_handler(evt){
        // dom node
        var link = evt.element();
        // link url
        var url  = link.getAttribute('href');
        // document name
        var document_name = /.+?([^/]+\.\w{3,5})$/.exec(url)[1];
		
        //
        // Handle the click here
        //
//        alert('Document link clicked: ' + document_name);
        var document_url_for_google = '/documents/' + document_name;
//		console.log('registering link click for google - ' + document_url_for_google);
		pageTracker._trackPageview(document_url_for_google);
}



var google_analytics_click_tracking = {
        init: function(extensions, click_handler, debug){
                var links = document.body.getElementsByTagName('a');
                if(links)
                {
                        //
                        // Make the "or" list of extensions for a regexp
                        //
                        var extensions_imploded = '';
                        var tmp = '';
                        for(var i=0, cnt=extensions.length; i<cnt; i++)
                        {
                                extensions_imploded += tmp+extensions[i];
                                tmp = '|';
                        }

                        // 
                        // Iterate through the list of all links of the page, and if the link is relative, e.g points to a
                        // target on the website - and points to a file ending in one of the specified extensions,
                        // register a click callback on that link
                        //
                        var regexp = new RegExp('^\/.+\.('+extensions_imploded+')$');
                        if(debug)
                                console.group('Click tracking registered for the following links');

                        for(var i=0, cnt=links.length; i<cnt; i++)
                        {
                                if(regexp.test(links[i].getAttribute('href')))
                                {
                                        if(debug)
                                                console.log(links[i]);

                                        links[i].observe('click', click_handler);    
                                }
                        }
                        if(debug)
                                console.groupEnd();
                }
        }
};



document.observe('dom:loaded', function(){
        google_analytics_click_tracking.init(['doc', 'pdf'], click_handler, 'debug');
});
