/* ****************************************************

	@file         analytics.js
	@description  Comportements globaux
	@author       remi & jp (ixmedia.com)

***************************************************** */

(function($) {

	$(function() {

		// Seulement si Google Analytics est présent!
		if (typeof(pageTracker) == "object" || typeof(_gaq) == "object") {

			var ixLinkTrack = {
				config : {
					'courriel' : {
						check : function() { return this.protocol == 'mailto:'; },
						url   : function() { return '/'+this.toString().substring(7); }
					},
					'document' : {
						check : function() { return this.hostname == window.location.host && $(this).attr('href').match( /\.(docx?|xlsx?|pptx?|pdf|eps|zip|vsd|vxd|rar|wma|mov|avi|wmv|mp3|mp4|mpg|mpeg|mpeg4|m4a|m4v|f4v|flv|csv|xml|ogg|oga|ogv|webm|jpg|jpeg|png|gif|webp|svg|ico|txt|css|js)$/ ); },
						url   : function() { return this.toString().replace( window.location.host, '' ).replace( window.location.protocol+'//', '' ); }
					},
					'externe' : {
						check : function() { return this.hostname != location.host; },
						url   : function() { return '/'+this.toString().replace(/^https?:\/\//, ''); }
					}
				},
				init : function() {

					$('a').each(function() {
						var lien = this;
						$.each( ixLinkTrack.config, function(k,v) {
							if ( $(lien).hasClass('fancybox') || !$(lien).attr('href') || $(lien).attr('href').match(/javascript:/) ) { return false; }
							if ( this.check.call( lien ) ) {
								$(lien).bind( 'track', function() {
									var url = '/'+k+v.url.call( $(this).attr('href') );
									if (typeof(pageTracker) == "object") { // ancienne version
										pageTracker._trackPageview( url );
									} else if (typeof(_gaq) == "object") { // nouvelle version, on vérifie l’ancienne en premier parce que l’objet "_gaq" existait déjà dans l’ancienne version
										_gaq.push(['_trackEvent', url]);
									}
								}).click(function() { $(this).trigger('track'); } );
								if ( v.filter ) {
									v.filter.call( lien );
								}
								return false;
							}
						});
					});
				}
			};
			ixLinkTrack.init();
		}

	});

})(jQuery);



