function do_print()
{
    window.print();
    return false;
}

function preload_images()
{
    // Simplest to use absolute paths here.
    var base = "/decor/";
    var type = new Array("plain", "hover");
    for (var s in type)
	for (var i = 1; i <= 5; i++) {
	    var img = new Image();
	    img.src = base + "tab" + i + "-" + type[s] + "-tl.gif";
	}
}

function identify_link()
{
    window.status = this.title;
    return true;
}

function reset_status()
{
    window.status = "";
    return true;
}

function set_hover_actions()
{
    for (var i = 0; i < document.links.length; i++) {
	var l = document.links[i];
	if (l.onclick != null && l.title != "") {
	    l.onmouseover = identify_link;
	    l.onmouseout = reset_status;
	}
    }
}

preload_images();
window.onload = function() {
    set_hover_actions();
    init_exit_logger(); /* see below */
}
    


/**
 * 'Exit' logging based largely on the design and code found at:
 *
 * Website: http://www.jamescaws.co.uk
 * Author: James Caws
 * Original post:
 * http://www.jamescaws.co.uk/2008/07/dynamically-count-exit-link-clicks-throughs-using-javascript-php/
 *
 * My (Ben's) main changes are to remove the CT_LOGGER object (we
 * don't need this sort of modularity yet) and some of the unused flag
 * setting.
 */

/**
 * Array of hostnames you don't want to count the clicking of. You'll probably
 * want to include the hostname of your website unless you really want to count
 * every internal click. If this is the case, other adjustments will be required
 * to this script.
 */
var aIgnoreHosts = new Array ('gmfa', 'www.gmfa.org.uk');

/**
 * The logging URL.  A query string is added to this.
 */
var sLogScript = '/action/log';

/**
 * Initialisation function. Finds 'loggable' links and attaches a
 * handler to them.
 */
function init_exit_logger() {
    var aLinks = document.getElementsByTagName('a');
    var n = 0;
    for (var i=0; i<aLinks.length; i++) {
        if (logable_link(aLinks[i].href)) {
            /* Crudely replace the onclick action.  Saves
               having to set a randon ID for one thing!
            */
            aLinks[i].onclick = exit_link_click;
            n++;
        }
    }
    /*    alert("did " + n + " links"); */
}

/**
 * Decides whether a given link should be loggable. This decision is
 * made purely on the value of the hostname i.e. if it is/isn't in the
 * aIgnoreHosts array.
 */
function logable_link(sLink) {
    var aLink = sLink.parse_url();
    if (typeof aLink['host'] == 'undefined' || aLink['host'] == '') {
        return false;
    }
    if (in_array(aLink['host'], aIgnoreHosts)) {
        return false;
    }
    return true;
}

/**
 * Called when a loggable link is clicked. It determines what the
 * destination URL is and strips off any '#name'.  It then adds the a
 * hidden loggin image.  This may or may not call a PHP logger -- you
 * choose.
 */
function exit_link_click() {
    var elLoggerImage = document.createElement('img');
    var sEncoder = encodeURIComponent||escape, aURL = this.href.split('#');
    var sURL = sEncoder(aURL[0]).replace(/\+/g,'%2B');

    elLoggerImage.setAttribute('src', sLogScript + '?exit=' + sURL);
    elLoggerImage.style.display = 'none';
    document.body.appendChild(elLoggerImage);
    /*    alert("click");*/
    return true;
}

/**
 * JS implementation of PHP's parse_url.
 * Taken from http://www.bytemycode.com/snippets/snippet/798/
 */
String.prototype.parse_url = function(query) {
    var url = this,
    rx = /^((?:ht|f|nn)tps?)\:\/\/(?:([^\:\@]*)(?:\:([^\@]*))?\@)?([^\/]*)([^\?\#]*)(?:\?([^\#]*))?(?:\#(.*))?$/,
    rg = [null,'scheme','user','pass','host','path','query','fragment'],
    r = url.match(rx), i, q, ret = {};

    if (r == null)
        return ret;

    for (i = 1; i < rg.length; i++) {
        if (r[i] != undefined)
            ret[rg[i]] = r[i];
        if (ret.path == '')
            ret.path='/';
        if (query != undefined && r[6] != undefined) {
            var q = r[6];
            ret.query = {};
            q = q.split('&');
            for (var i = 0; i < q.length; i++) {
                q[i] = q[i].split('=', 2);
                ret.query[unescape(q[i][0])] = unescape(q[i][1]);
            }
        }
    }
    return ret;
}

function in_array(needle, haystack) {
    var key;
    for (key in haystack)
        if (haystack[key] == needle)
            return true;
    return false;
}

