var hostname = "www.helling.fi";
var recordClicks = false;



// Link Tracker, version 1.0
// (c)Glenn Jones - Dec 2005
// For details: http://www.glennjones.net/

// Adapted for asp/mdb use at http://www.envador.com/asp-ajax-LinkTracker/

// Adapted & Bugfixed for DynaWeb by Oskar Helling May2006 


function findLink(e)
{
	if (typeof e == 'undefined')
		var e = window.event;

	var source;
	if (typeof e.target != 'undefined') 
	{
		source = e.target;
	} else if (typeof e.srcElement != 'undefined') {
		source = e.srcElement;
	} else {
		return true;
	}
	if (source.nodeType == 3)
		source = source.parentNode;
	
	return source;
}

function findParentATag(elm)
{
    if( elm.tagName == 'A' )
    {
        return elm;
    }    
    return findParentATag( elm.parentNode );
}


function recordClick(e)
{
    if( !recordClicks )
        return;
        
    var source = findLink(e);
 				
	// Changed to work with hyperlinks 
	// around images. See Peter Bowyer comment
	//---------------------------------
	var id, target, url, label
	
	if( source.tagName == "IMG" )
	{
		if( source.parentNode.tagName == "A" )
		{
			id = source.parentNode.getAttribute('id');
			target = source.parentNode.getAttribute('href');
		}
		label = source.getAttribute("alt");
	}else{
		id = findParentATag(source).getAttribute('id');
		target = findParentATag(source).getAttribute('href');
		label = source.childNodes[0].nodeValue;
		
		//alert( id + '-' + target + '-' + label );
	}
	url = document.location.href;
	//---------------------------------
	
	var pars = '';	
	apiurl = "http://" + hostname + "/HotSpot/addClick.aspx?id=" + id + "&label=" + label + "&target=" + target + "&url=" + url + "&rand="+Math.random();
	ajaxRequest = new Ajax.Request(apiurl, {method: 'get', parameters: pars, onComplete: passThrough});

	//return false;	
}

function passThrough( originalRequest )
{
	// Helps debug api errors
	//alert( originalRequest.responseText );
}

document.getElementsByClassName = function(className) 
{
  var children = document.getElementsByTagName('*') || document.all;
  var elements = new Array();
  for (var i = 0; i < children.length; i++) {
    var child = children[i];
    var classNames = child.className.split(' ');
    for (var j = 0; j < classNames.length; j++) {
      if (classNames[j] == className) {
        elements.push(child);
        break;
      }
    }
  }
  return elements;
}

function addEvent(elm, evType, fn, useCapture) 
{
  // cross-browser event handling for IE5+, NS6 and Mozilla By Scott Andrew 
  if (elm.addEventListener) { 
    elm.addEventListener(evType, fn, useCapture); 
    return true; 
  } else if (elm.attachEvent) { 
    var r = elm.attachEvent('on' + evType, fn); 
    return r; 
  } else {
    elm['on' + evType] = fn;
  }
}

function addLinkTracker()
{
	if (!document.getElementsByTagName) return false;
	
	linksElements = document.getElementsByTagName('a')
	for (var i = 0; i < linksElements.length; i++) 
	{
		addEvent(linksElements[i], 'mousedown', recordClick, false);
		// If a link does not have any id it is given one
		if (! linksElements[i].getAttribute('id') )
		{
			linksElements[i].setAttribute('id',"HotSpot_" + i)
		}
	}
}

addEvent(window, 'load', addLinkTracker, false);




