/**
 * Javascript to tag file downloads and external links in Google Analytics
 * To use, place reference to this file should be placed at the bottom of all pages, 
 * just above the Google Analytics tracking code.
 *
 * All outbound links and links to non-html files should now be automatically tracked.
 *
 * Requires prototype library 
 *
 * based on:
 *
 * @see http://www.iqcontent.com/blog/2007/02/tracking-documents-and-external-links-in-google-analytics
 * @see http://foobr.co.uk/2007/07/track_downloads_with_google_analytics/
 *
 * Copyright (c) 2007 Mishal (mishal at mishal dot cz)
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * @version $Id: my_google_analytics.js 67 2007-08-03 08:10:28Z mishal $
 * @author mishal <mishal at mishal dot cz>
 * @author Colm McBarron, <colm dot mcbarron at iqcontent dot com>
 *
 */

if(typeof Prototype=='undefined')
  throw("myGoogleAnalytics requires the Prototype JavaScript framework"); 
 
var myGoogleAnalytics = function() {

  var version = '0.0.5 beta';
  
  var options = { external_name: 'external', track_external: true, debug: false };
  
  // extensions to track
  // currently not used
  var downloadExtensions = ['rar', 'zip', 'tar', 'gz', // archives
                            'exe', // executables  							 
  							'txt', 'pdf', 'doc', 'ppt', 'xls', 'odt', 'ods', 'odp', 'rtf', // documents  
  							'mp3', 'wma', 'avi', 'wmv', 'mpg', 'flv', 'mov' // multimedia
  							];
                   
  function initialize(customOptions) {
    Object.extend(options, customOptions);
    parseLinks();
  };
  
  function getVersion() {
    return version;
  }
  								
  function parseLinks() { 
    var links = document.getElementsByTagName("a");
  	var path = '';
    
    // regular expression for downloable types of files
    var regexp = new RegExp("\.(" + downloadExtensions.join('|') + ")$");
    
    // var date = new Date();
    // var then = date.getTime();
    
    // Loop through the a elements in reverse order
    // for speed.    
    for(var i = links.length; i != 0; i--) {
      // pull out the element for this iteration.
      var a = links[i-1];      
      // if the element doesn't have an href
      // or if href links to an anchor on this page, skip it
      if(!a.href || (a.href.indexOf("#") != -1 
                      && a.href.indexOf(document.URL) != -1)) continue;      
      try {
        var path = a.href;
        if(location.host == a.hostname) {
          if(path.match(regexp)) {
            Event.observe(a, 'click', trackLink.bindAsEventListener(a), false);
          }
        } else if(options.track_external) { // all external links
          Event.observe(a, 'click', trackLink.bindAsEventListener(a), false);
        }
      } catch(err) { alert(err); }
    }
    // var date = new Date();
    // var now = date.getTime();
    // var elapsed = now - then;
    // alert("time took: " + elapsed/1000 + " s");      
  };
  
  function myUrchinTracker(url) {        
    if(options.debug) {
      alert('urchinTracker(' + url + ')');
      return;
    }
    if(window.urchinTracker) {
      urchinTracker(url);
    }
  };

  function trackLink() {
    path = '';    
    if(location.host != this.hostname) {
      path = '/' + options.external_name + '/' + this.hostname;
    }
    path += this.pathname.charAt(0) == '/' ? this.pathname : '/' + this.pathname;    
    // catch anchor
    if(this.href.indexOf('#') != -1) {
      path += '#' + this.href.substring(this.href.indexOf('#')+1);
    } 
    myUrchinTracker(path);
  };
  
  return {
		// public API
		initialize: initialize,
		getVersion: getVersion
  };
}();



