// JavaScript Document
/*
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.

The Original Code is logger.js.

The Initial Developer of the Original Code is Andy Edmonds
Portions created by Andy Edmonds are Copyright (C) Andy Edmonds, 2007. 
All Rights Reserved.

*/

var ui_useEventing = true;

function ui_getParent(node) {
	//alert(node 	);
	var curNode = node;
	while(curNode.nodeName != "BODY") {
		//alert('looking at ' + curNode.nodeName+ ' of ' + cbGetAttribute('',"ui",curNode));
		if(cbGetAttribute('',"ui",curNode)) {
			return cbGetAttribute('',"ui",curNode);
		} else {
			curNode= curNode.parentNode;
		}
	}
	return false;
}


function ui_tag(evt) {
	var link= evt.target;
	//alert('got evt');
	var tag = ui_getParent(link);
	if(tag)  {
	
		if(ui_useEventing) { 
				if(pageTracker != 'undefined') pageTracker._trackEvent("Click", tag, document.location.pathname);
		
		} else {
			if(link.href.indexOf("?") > -1) {
				link.href = link.href +"&ui=" + tag;
			 } else {
				link.href = link.href +"?ui=" + tag;
			 }
		}	
	}

}

function ui_logger_init() {
	var links = document.links;
	//alert(links.length + " links on page");
	
	for(var i=0;i<links.length;i++) {
		addEvent(links[i], "click", ui_tag) ;
	}
}

/* END MPL LICENSED CODE */


// written by Dean Edwards, 2005
// with input from Tino Zijdel - crisp@xs4all.nl
// http://dean.edwards.name/weblog/2005/10/add-event/
function addEvent(element, type, handler)
{
	if (element.addEventListener)
		element.addEventListener(type, handler, false);
	else
	{
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		if (!element.events) element.events = {};
		var handlers = element.events[type];
		if (!handlers)
		{
			handlers = element.events[type] = {};
			if (element['on' + type]) handlers[0] = element['on' + type];
			element['on' + type] = handleEvent;
		}
	
		handlers[handler.$$guid] = handler;
	}
}
addEvent.guid = 1;

function removeEvent(element, type, handler)
{
	if (element.removeEventListener)
		element.removeEventListener(type, handler, false);
	else if (element.events && element.events[type] && handler.$$guid)
		delete element.events[type][handler.$$guid];
}

function handleEvent(event)
{
	event = event || fixEvent(window.event);
	var returnValue = true;
	var handlers = this.events[event.type];

	for (var i in handlers)
	{
		if (!Object.prototype[i])
		{
			this.$$handler = handlers[i];
			if (this.$$handler(event) === false) returnValue = false;
		}
	}

	if (this.$$handler) this.$$handler = null;

	return returnValue;
}

function fixEvent(event)
{
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	event.target = event.srcElement;
	return event;
}
fixEvent.preventDefault = function()
{
	this.returnValue = false;
}
fixEvent.stopPropagation = function()
{
	this.cancelBubble = true;
}

// This little snippet fixes the problem that the onload attribute on the body-element will overwrite
// previous attached events on the window object for the onload event
if (!window.addEventListener)
{
	document.onreadystatechange = function()
	{
		if (window.onload && window.onload != handleEvent)
		{
			addEvent(window, 'load', window.onload);
			window.onload = handleEvent;
		}
	}
}

/**
    Cross-browser getAttribute function.

    @param String nsPrefix -- The namespace prefix. Supply empty string for default/no namespace.
    @param String attribute -- The local attribute name
    @param Element elem -- The element to work upon.
    @usage -- cbGetAttribute('x','myattribute',document.getElementById('testdiv'));
*/
function cbGetAttribute(nsPrefix, attribute, elem){
    if( elem.getAttributeNS ) { // W3C
        var namespaceURI = document.documentElement.getAttribute('xmlns:'+nsPrefix);
        return elem.getAttributeNS(namespaceURI, attribute);
    } else { // IE
        var prefix = (nsPrefix.length > 0) ? (nsPrefix+':') : '';
        return elem.getAttribute(prefix+attribute);
    }
}

addEvent(window, "load", ui_logger_init);
