/*
 20.november 2006 by Atle Andersen
 http://www.marcopoloarcade.com - Discover a World of Free Online Games

 Allows the use of PHPArcadeScripts option to use the real URL in place of linkout.php,
 yet still track links out by adding an OnClick event listener

 PHPArcadeScripts function display_links and display_top_links in includes/function.php must be modified for this script to work.
 Add id=\"$linkid\" inside of the first $websitelinks <a-clause.

 Example:
 $websitelink = "<a class=\"LinksPage\" href=\"$websiteurl\" target=\"_blank\">$websitename</a>";
 must be changed to:
 $websitelink = "<a class=\"LinksPage\" id=\"$linkid\" href=\"$websiteurl\" target=\"_blank\">$websitename</a>";

 The script also reports the clicks to Google Analytics (if found) as "/external/<hostname>".
 Script assumes linkout.php is in the root of the domain and must be updated if this is not true.

*/

var LinkoutPath = '<?=$base_url;?>';  // Path to linkout.php
var AnalyticsPath = '/external/';  // Path in Google Analytics

// generic cross-browser add event function
function addEvent(obj, evType, fn){
        if (obj.addEventListener){
                obj.addEventListener(evType, fn, true);
                return true;
        } else if (obj.attachEvent){
                var r = obj.attachEvent('on'+evType, fn);
                return r;
        } else {
                return false;
        }
}

// init the tracking code and add a listener to all links with a numeric ID
function trackInit() {
        var links = document.getElementsByTagName('a');
        for (var i = 0; i < links.length; i++)
                if (links[i].id.match(/^\d/)) links[i].onclick = track;
}

// report clicks
function track() {

        var trackImg = new Image();
        trackImg.src = LinkoutPath+'linkout.php?id='+this.id;

// report to google Analytics if present
        if (typeof urchinTracker == 'function')
                urchinTracker(AnalyticsPath+escape(this.href.toString().replace(/^[^\/]*\/+([^\/]*)(\/.*)?/,'$1')));

        // delay
        var now = new Date();
        var stopTime = now.getTime() + 1500;
        while(now.getTime() < stopTime)
                now = new Date();
}

addEvent(window,'load', trackInit);

