/**
 * domready.js
 * 
 * Cross browser mozilla's 'onDOMContentLoaded' implementation.
 * Executes a function when the dom tree is loaded without waiting for images.
 * 
 * Based on +Element.Events.domready+ from Mootools open source project, 
 * this tiny javascript library adds the emulated 'DOMContentLoaded' functionality.
 * 
 * Features:
 *   - No dependency on external libraries
 *   - Compatible with Prototype.js 
 * 
 * Tested browsers (Windows):
 *   - IE 7 (XP standalone)
 *   - IE 6 SP2
 *   - Firefox 2.0.0.4
 *   - Opera 9.21
 * 
 * Tested browsers (Mac OS X):
 *   - Safari 2.0.4
 *   - Firefox 2.0.0.4
 *   - Mac Opera 9.21
 *   - Mac IE 5.2.3
 *
 * Copyright (c) 2007 Takanori Ishikawa.
 * License: MIT-style license.
 * 
 * MooTools Copyright:
 * copyright (c) 2007 Valerio Proietti, <http://mad4milk.net>
 *
 *
 * See Also:
 *
 *   mootools 
 *   http://mootools.net/
 *   
 *   The window.onload Problem - Solved!
 *   http://dean.edwards.name/weblog/2005/09/busted/
 *   
 *   [PATCH] Faster onload for Event.onload
 *   http://dev.rubyonrails.org/ticket/5414
 *   Changeset 6596: Support for "DOMContentLoaded" event handling (prototype.js event branch)
 *   http://dev.rubyonrails.org/changeset/6596
 *
 */

if (typeof Event == 'undefined') Event = new Object();

/*
 * Registers function +fn+ will be executed when the dom 
 * tree is loaded without waiting for images. 
 * 
 * Example:
 *
 *  Event.domReady.add(function() {
 *    ...
 *  });
 *
 */
Event.domReady = {
  add: function(fn) {
    
    //-----------------------------------------------------------
    // Already loaded?
    //-----------------------------------------------------------
    if (Event.domReady.loaded) return fn();
    
    //-----------------------------------------------------------
    // Observers
    //-----------------------------------------------------------
    var observers = Event.domReady.observers;
    if (!observers) observers = Event.domReady.observers = [];
    // Array#push is not supported by Mac IE 5
    observers[observers.length] = fn;
    
    //-----------------------------------------------------------
    // domReady function
    //-----------------------------------------------------------
    if (Event.domReady.callback) return;
    Event.domReady.callback = function() {
      if (Event.domReady.loaded) return;
      
      Event.domReady.loaded = true;
      if (Event.domReady.timer) {
        clearInterval(Event.domReady.timer);
        Event.domReady.timer = null;
      }
      
      var observers = Event.domReady.observers;
      for (var i = 0, length = observers.length; i < length; i++) {
        var fn = observers[i];
        observers[i] = null;
        fn(); // make 'this' as window
      }
      Event.domReady.callback = Event.domReady.observers = null;
    };
    
    //-----------------------------------------------------------
    // Emulates 'onDOMContentLoaded'
    //-----------------------------------------------------------
    var ie = !!(window.attachEvent && !window.opera);
    var webkit = navigator.userAgent.indexOf('AppleWebKit/') > -1;
    
    if (document.readyState && webkit) {
      
      // Apple WebKit (Safari, OmniWeb, ...)
      Event.domReady.timer = setInterval(function() {
        var state = document.readyState;
        if (state == 'loaded' || state == 'complete') {
          Event.domReady.callback();
        }
      }, 50);
      
    } else if (document.readyState && ie) {
      
      // Windows IE 
      var src = (window.location.protocol == 'https:') ? '://0' : 'javascript:void(0)';
      document.write(
        '<script type="text/javascript" defer="defer" src="' + src + '" ' + 
        'onreadystatechange="if (this.readyState == \'complete\') Event.domReady.callback();"' + 
        '><\/script>');
      
    } else {
      
      if (window.addEventListener) {
        // for Mozilla browsers, Opera 9
        document.addEventListener("DOMContentLoaded", Event.domReady.callback, false);
        // Fail safe 
        window.addEventListener("load", Event.domReady.callback, false);
      } else if (window.attachEvent) {
        window.attachEvent('onload', Event.domReady.callback);
      } else {
        // Legacy browsers (e.g. Mac IE 5)
        var fn = window.onload;
        window.onload = function() {
          Event.domReady.callback();
          if (fn) fn();
        }
      }
      
    }
    
  }
}

/*
 * @author Tymon
 */

//some conifguration
var new_google_code = false;
var analytics_debug = false;

function initialize_tracker()
{
	//detect which version of google snippet are we using
	if (typeof(pageTracker) == "undefined")
	{
		new_google_code = true;

		if (analytics_debug)
		{
			if (typeof console == "undefined")
			{
				alert('using new analytics code');
			}
			else
			{
				console.log('using new analytics code');
			}
		}
	
	}
}

var extensions_to_track = new Array();
extensions_to_track[0] = 'pdf';
extensions_to_track[1] = 'doc';
extensions_to_track[2] = 'docx';

function addLoadEvent(func)
{
	var oldonload = window.onload;
	 
	if (typeof window.onload != 'function')
	{ 
		window.onload = func; 
	}
	else
	{ 
		window.onload = function() { 
  			if (oldonload) { 
    			oldonload(); 
  			} 
  			func(); 
		} 
	} 
} 

function attach_onclick_listener(listener, link)
{
	if (link.addEventListener)
	{
		link.addEventListener("click", listener, false);
	}
	else if (link.attachEvent)
	{
		link.attachEvent("on" + 'click', listener);
	}
	else
	{
		link.onload = listener;
	}

	if (analytics_debug)
	{
		if (typeof console != "undefined")
		{
			console.log("attaching onclick listener: ", listener, link);
		}
		else
		{
			alert('attaching onclick listener:' + listener + ', link: ' + link);
		}
	}
}

function track_event(category, title, url)
{
	if (analytics_debug)
	{
		//console.log("tracking link:", category, title, url);
		alert('tracking link: category: ' + category + ', title: ' + title + ' url: ' + url);
	}
	

	if (new_google_code)
	{
		_gaq.push(['_trackEvent', category, title, url]);
	}
	else
	{
		pageTracker._trackEvent(category, title, url);
	}
}

function track_links()
{
	var links = document.getElementsByTagName('a');

	for (i = 0 ; i < links.length ; i++)
	{
		var path = String(links[i].href);
		
		//check for email links first
		if (path.match(/^mailto:/i))
		{
			var func = "track_event('E-mail links', 'mailto:', '"+path+"');"
			var function_to_attach = new Function(func);
			attach_onclick_listener(function_to_attach, links[i]);
		}
		else
		{
			//check if the link is internal or external
			if (location.host == links[i].hostname)
			{
				//internal - check for internal files
				var exploded = path.split('.');
				var this_ext = exploded[exploded.length-1];

				for (j = 0 ; j < extensions_to_track.length ; j++)
				{
					if (this_ext.toLowerCase() == extensions_to_track[j])
					{
						var func = "track_event('Internal downloads', '"+extensions_to_track[j]+"', '"+path+"');"
						var function_to_attach = new Function(func);
						attach_onclick_listener(function_to_attach, links[i]);

						break;
					}
				}
			}
			else
			{
				//that's external - check if that's a document
				var exploded = path.split('.');
				var this_ext = exploded[exploded.length-1];
				var document_found = false;

				for (j = 0 ; j < extensions_to_track.length ; j++)
				{
					if (this_ext.toLowerCase() == extensions_to_track[j])
					{
						var func = "track_event('External downloads', '"+extensions_to_track[j]+"', '"+path+"');"
						var function_to_attach = new Function(func);
						attach_onclick_listener(function_to_attach, links[i]);
						document_found = true;

						break;
					}
				}

				//that's not a document!
				if (!document_found)
				{
					var func = "track_event('External link', 'external', '"+path+"');"
					var function_to_attach = new Function(func);
					attach_onclick_listener(function_to_attach, links[i]);
				}
			}
		}
	}
}

//onload!
//addLoadEvent(track_links());
Event.domReady.add(function() {
	initialize_tracker();
	track_links();
});

