/*
---
description:   ExitPoll uses Google Analytics to keep track of users leaving your site or downloading files. It works with either the synchronous, or asynchronous Google Analytics tracking code.
 
license: 
   - MIT-style
 
authors:
   - Caleb Crane (http://www.simulacre.org)
 
requires: 
  core/*:
 
provides: 
   - ExitPoll
 
...
*/
var ExitPoll = new Class({
	
	Implements: [Options],
	
	options: {
		parent      : null,
		event       : 'mouseup',
		fileTypes   : ['pdf', 'zip', 'rar', 'tgz', 'gz','gzip', 'jpg', 'png', 'svg', 'gif', 'doc', 'eps', 'xls', 'ppt', 'xls', 'txt', 'vsd', 'js', 'css', 'rar', 'exe', 'wma', 'mov', 'avi', 'wmv', 'mp3'],
		track       : 'pageview' // 'pageview' || 'trackEvent'
	},
	
	initialize: function(options){
		var my = this;
		my.setOptions(options);
		var par = my.options.parent ? my.options.parent : document.body;
		document.id(par).getElements('a').each(function(a){
			if (a.protocol != 'mailto:' && (!$chk(a.href) || a.href == '#')) return;
			if (a.host.test(location.host)){
				if (!my.options.fileTypes.contains(a.pathname.substring(a.pathname.lastIndexOf('.') + 1).toLowerCase() ))
					return
			}
			a.addEvent(my.options.event, my[my.options.track]); 
		
		});
	},

	pageview: function(){
		var l;
		if (this.protocol == 'mailto:') l = this.href.replace(/mailto:/, '');
		else if (!this.host.test(location.host) && /http/.test(this.protocol)) l = '/outgoing/' + this.href;
		else l = '/download' + this.pathname;
		
		if (typeof pageTracker == 'object') pageTracker._trackPageview(l);
		else if (typeof(_gaq) == 'object') _gaq.push(['_trackPageview', l]);
		// else throw('Google Analytics tracking object not found');
		// uncomment to enable throwing errors
				
		return false;
	},
	
	trackEvent: function(){
		var l = [];
		if (this.protocol == 'mailto:') l = ['mailto', this.href.replace(/mailto:/, '')];
		else if (!this.host.test(location.host) && /http/.test(this.protocol)) l = ['outbound', this.href];
		else l = ['download', this.pathname];
		
		if (typeof pageTracker == 'object') pageTracker._trackEvent(l[0], 'click', l[1]);
		 else if (typeof(_gaq) == 'object') _gaq.push(['_trackEvent', l[0], l[1]]);
		// else throw('Google Analytics tracking object not found');
		// uncomment to enable throwing errors
		
		return false;
	}

});








