﻿/*  Improved robustness & compatibility checks. Orginal src: http://www.lunametrics.com/blog/2011/12/01/automatic-cross-domain-tracking-revisited/
xdomain.js : Automatically tracks a visitor across domains in Google
Analytics, automatically tracks their downloads, and clicks on
outbound links.
Copyright (C) 2011  LunaMetrics
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
function listenToClicks()
{
	var domains=["dinercheque.nl"];
	var fileTypes=[".doc", ".xls", ".exe", ".zip", ".pdf", ".mov", ".mp3"];
	$('a').each(function(index) {
 		var link = $(this);
		var href = link.attr('href');
		$.each(fileTypes, function(i) {
			if ($(link).attr('href') && ($(link).attr('href').indexOf(this)!=-1)) {
				valid = false;
				$(link).bind('click', function(c) {
					c.preventDefault();
					_gat._getTrackerByName()._trackEvent('Download', 'Click - ' + $(link).attr('href'));
					setTimeout('document.location = "' + $(link).attr('href') + '"', 100);
				});
			}
		});
		var valid = false;
		$.each(domains, function(j) {
			try
			{
				if(($(link).attr('href').indexOf(this)!=-1)&&(window.location.href.indexOf(this)==-1)){
					valid = true;
					if (valid)
					{
						$(link).bind('click', function(l) {
							
							if(typeof(_gat)=="object"){
								l.preventDefault();
								if ($(link).attr('target') != "_blank")
								{
									_gaq.push(['_link',$(link).attr('href')]);
								}
						 		else
						 		{
				 					var tracker = _gat._getTrackerByName();
									var fullUrl = tracker._getLinkerUrl($(link).attr('href'));
									window.open(fullUrl);
						 		}
							}
						});
					}
				}
			}
			catch(e)
			{
				//Bad A tag
			}
		});
		//var rootDomain = document.domain.split(".")[document.domain.split(".").length - 2] + "." + document.domain.split(".")[document.domain.split(".").length - 1];
		if (href && (href.match(/^http/)) && (href.indexOf(document.domain) == -1) && !valid) {
			$(link).bind('click', function(d) {
					d.preventDefault();
				  	_gat._getTrackerByName()._trackEvent('Outbound Link', href);
					setTimeout('window.open("' + href + '", "_blank")', 100);//setTimeout('document.location = "' + href + '"', 100);
				});
		}
	});
}
$(document).ready(function() {
	listenToClicks();
});
