/******************************************************************************
 * University of Ottawa Library
 * Sector-independent javascript code.
 * http://www.biblio.uottawa.ca/
 * Author: Matt DeLuco (mdeluco@uottawa.ca)
 *****************************************************************************/

/***
 * Class:       TargetEventHandler
 * Properties:  handlers - an associative array whose keys are target names and
 *              whose values are event handlers for those targets.
 * Methods:     addHandler(function, string) - adds a handler to a target's
 *                  list.
 *              removeHandler(function, string) - removes a handler from a
 *                  target's list.
 *
 * Description: TargetEventHandler contains a list of event handlers for
 *              specified targets.  A target is a specific element which
 *              received an action - ie: an anchor which was clicked.
 * 
 */
/***
 * This is all garbage, for now.
 *
function TargetEventHandler() {

    // 'handlers' is an associative array whose keys are node names ('a', 'div'
    // etc.) and whose values are event handler functions.
    this.handlers = new Object();

    // Add a handler to a node's list of handlers.
    this.addHandler = function(handler, target) {
   
        if (typeof(this.handlers[target]) == 'undefined')
            this.handlers[target] = new Array();

        return this.handlers[target].push(handler);

    };

    // Remove a handler from a node's list of handlers.
    this.removeHandler = function(handler, target) {

        for (var h in this.handlers[target]) {
            if (this.handlers[target][h] == handler) {
                this.handlers[target].splice(h, 1);
                return this.handlers[target].length;
            }
        }

        throw new Error("Specified handler for target '" + target + "' not found!");

    };

    // Runs the handlers for target of the event.
    this.handleEvent = function(event) {
        
        // Gecko : IE
        var target = (event.target) ? event.target : event.srcElement;
        var node_name = target.nodeName.toLowerCase();
        
        if (typeof(this.handlers[node_name]) == 'undefined')
            return false;
            
        for (var handler in this.handlers[node_name])
            this.handlers[node_name][handler](event, target);

    };

}
*/

/***
 * function:    anchor_handler
 * Parameters:  e - an event object
 *              target - an event target
 *
 * Description: handles click events whose target is an anchor node.
 * Actions:     (1) Track external link usage through WebTrends SDC using
 *              dcsMultiTrack().
 */
//function anchor_handler(e, target) {
function anchor_handler(event) {
    
    // Don't record a statistic if the user is not actually following the link.
    if (event.button != 0) return false;

    // Gecko : IE
    var target = (event.target) ? event.target : event.srcElement;
    var node_name = target.nodeName.toLowerCase();

    // This gets around elements that are embedded in an anchor (like an image.)
    // Typically there isn't too much depth in embedded elements.
    var x = 0;
    while (node_name != "a") {
        target = target.parentNode;
        node_name = target.nodeName.toLowerCase();
        if (++x == 5 || !target.parentNode) return false;
    }

    var url = target.href;
    var pat = "url=(.*?)(&.*)*$";   // Databases search string pattern
    var loc = document.getElementById("DCS.dcsuri").content;  // from metatags

    var title = new String();

    // Databases specific code
    if (target.hostname == "login.proxy.bib.uottawa.ca") {

        url = url.match(pat)[1];
        url = (url) ? url : target.pathname;  // e-CPS & e-Therapeutics
        title = target.attributes["alt"].value;

        dcsMultiTrack('WT.cg_n', 'exitsite;database', 'DCS.dcsuri', url, 'WT.ti', title, 'DCS.dcsref', loc);
        //alert("url: " + url + ", ti: " + title + ", dcsref: " + loc);

    // All other external links
    } else if (target.hostname && target.hostname != window.location.hostname) {

        // Gecko : IE
        title = (target.text) ? target.text : target.innerHTML;

        dcsMultiTrack('WT.cg_n', 'exitsite', 'WT.ti', title, 'DCS.dcsuri', url, 'DCS.dcsref', loc);
        //alert("url: " + url + ", ti: " + title + ", dcsref: " + loc);

    // Something could be done here for non-external links.
    } else {
        return false;
    }

    return true;

}
addEvent(document, "click", anchor_handler);

/*
var teh = new TargetEventHandler();
teh.addHandler(anchor_handler, "a");

function foo(event) {
    teh.handleEvent(event);
}
*/


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

