function z_init () {
    // create image object for processing urls
    z_stats_image = new Image;
    z_stats_image.onload = z_returntrue;
    z_stats_image.onerror = z_returntrue;
    // call subclass first
    if (typeof (z_sub_init) == 'function') {
        z_sub_init ();
    }
    if (document.links) {
        var x;
        var link;
        var links = document.links;
        for (x = 0; x < links.length; x++) {
            link = links[x];
            // save old onclick, if any
            if (link.onclick) {
                link.oldonclick = link.onclick;
            }
            link.onclick = z_linkonclick;
        }
    }
    var gafs = document.getElementsByName ("google_ads_frame");
    for (x = 0; x < gafs.length; x++) {
        var gaf = gafs[x];
        gaf.onmouseover = z_gafonmouseover;
        gaf.onmouseout = z_gafonmouseout;
    }
    window.onbeforeunload = z_windowonbeforeunload;
    // give firefox a reason to finish processing windowonbeforeunload
    window.onunload = z_returntrue;
    // process page view
    var url = z_newurl ('insert.php');
    z_process (url);
}

function z_newurl (pg) {
    var url = z_base_url + pg;
    url += "?url=" + escape (document.URL);
    url += "&ref=" + escape (document.referrer);
    url += "&d=" + escape (document.domain);
    url += "&t=" + escape (document.title);
    url += "&an=" + escape (navigator.appName);
    url += "&ua=" + escape (navigator.userAgent);
    url += "&p=" + escape (navigator.platform);
    url += "&ul=" + escape (navigator.userLanguage);
    if (navigator.cookieEnabled) {
        url += "&ce=1";
    } else {
        url += "&ce=0";
    }
    url += "&w=" + escape (screen.width);
    url += "&h=" + escape (screen.height);
    url += "&cd=" + escape (screen.colorDepth);
    url += "&tzo=" + (new Date().getTimezoneOffset()/-60);
    if (typeof(z_vars) != 'undefined') {
        for (var v in z_vars) {
            url += "&z_" + v + "=" + escape (z_vars[v]);
        }
    }
    return url;
}

function z_linkonclick () {
    var link = this;
    // check for old handler
    if (link.oldonclick) {
        // check if function
        if (typeof (link.oldonclick) == 'function') {
            // execute function, if false cancel (undefined is treated as true by the browser)
            if (link.oldonclick () == false) {
                return false;
            }
        }
    }
    z_tracklink (link.href, false);
    return true;
    //don't do this because it breaks open in a new window or tab
    //hopefully firefox will eventually be fixed to load my img.src before it navigates to a new page
    //follow this link in 100ms so that image source is processed
    //window.setTimeout('document.location="' + link.href + '"', 100);
    //return false;
}

function z_tracklink (tu, ads) {
    var url = z_newurl ('insert.php');
    url += "&tu=" + escape (tu);
    if (ads) {
        url += "&ads=1";
    } else {
        url += "&ads=0";
    }
    z_process (url);
}

function z_comment_previewonclick () {
    // save post title before we leave this page
    z_set_comment_post_title_cookie ();
    return true;
}

function z_comment_postonclick () {
    var cp = this;
    // check for old handler
    if (cp.oldonclick) {
        // check if function
        if (typeof (cp.oldonclick) == 'function') {
            // execute function, if false cancel (undefined is treated as true by the browser)
            if (cp.oldonclick () == false) {
                // post won't happen so don't continue
                return false;
            }
        }
    }
    // save comment post information before we leave this page
    z_setcookie ("z_comment_post", "true", "/");
    z_set_comment_post_title_cookie ();
    var cpa = z_get_comment_author ();
    if (cpa) {
	z_setcookie ("z_comment_post_author", cpa, "/");
    }
    var cpe = z_get_comment_extra ();
    if (cpe) {
	z_setcookie ("z_comment_post_extra", cpe, "/");
    }
    return true;
}

function z_set_comment_post_title_cookie () {
    if (z_getcookie ("z_comment_post_title", "/") == undefined) {
	z_setcookie ("z_comment_post_title", z_get_post_title (), "/");
    }
}

function z_get_comment_author () {
    // call subclass first
    if (typeof (z_sub_get_comment_author) == 'function') {
        var x = z_sub_get_comment_author ();
        // if undefined don't return so this function can implement a default
        if (x) {
            return x;
        }
    }
    return undefined;
}

function z_get_comment_extra () {
    // call subclass first
    if (typeof (z_sub_get_comment_extra) == 'function') {
        var x = z_sub_get_comment_extra ();
        // if undefined don't return so this function can implement a default
        if (x) {
            return x;
        }
    }
    return undefined;
}

function z_get_post_title () {
    // call subclass first
    if (typeof (z_sub_get_post_title) == 'function') {
        var x = z_sub_get_post_title ();
        // if undefined don't return so this function can implement a default
        if (x) {
            return x;
        }
    }
    // user knows best
    if (typeof (z_post_title) != 'undefined') {
        return z_post_title;
    }
    var pt = document.getElementById ("post-title");
    if (pt) {
        return pt.innerHTML;
    }
    return document.title;
}

function z_get_post_category () {
    // call subclass first
    if (typeof (z_sub_get_post_category) == 'function') {
        var x = z_sub_get_post_category ();
        // if undefined don't return so this function can implement a default
        if (x) {
            return x;
        }
    }
    // user knows best
    if (typeof (z_post_category) != 'undefined') {
        return z_post_category;
    }
    var pc = document.getElementById ("post-category");
    if (pc) {
        return pc.innerHTML;
    }
    return undefined;
}

function z_process (url) {
    var pt = z_get_post_title ();
    if (typeof (pt) != 'undefined') {
        url += "&pt=" + escape (pt);
    }
    var pc = z_get_post_category ();
    if (typeof (pc) != 'undefined') {
        url += "&pc=" + escape (pc);
    }
    if (z_getcookie ("z_comment_post", "/")) {
	url += "&cp=1";
	// go ahead and add other fields, will be added as "undefined" if not set
	url += "&cpt=" + escape (z_getcookie ("z_comment_post_title", "/"));
	url += "&cpa=" + escape (z_getcookie ("z_comment_post_author", "/"));
	url += "&cpe=" + escape (z_getcookie ("z_comment_post_extra", "/"));
	z_deletecookie ("z_comment_post", "/");
	z_deletecookie ("z_comment_post_title", "/");
	z_deletecookie ("z_comment_post_author", "/");
	z_deletecookie ("z_comment_post_extra", "/");
    }
    if (url) {
        // always add something different to bypass browser cache
        url += "&xyz=" + new Date().getTime();
        z_stats_image.src = url;
        if (typeof(z_stats_debug) != 'undefined') {
            alert(url);
        }
    }
}

function z_returntrue () {
    return true;
}

function z_gafonmouseover () {
    window.z_isgaf = true;
}

function z_gafonmouseout () {
    window.z_isgaf = false;
}

function z_windowonbeforeunload () {
    if (window.z_isgaf) {
        z_tracklink (window.status, true);
    }
    // do not return anything
}

z_init ();
