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

	@file         analytics.js
	@description  Comportements globaux
	@author       remi (ixmedia.com)
	vim: set noet ts=4 fdm=marker fenc=utf-8:

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

(function($) {

	$(function() {

		// Seulement si Google Analytics est présent!
		if (typeof(pageTracker) == "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( /\.(doc|xls|ppt|pdf|zip|vsd|vxd|rar|wma|mov|avi|wmv|mp3|flv)$/ ); },
						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?:\/\//, ''); },
						filter : function() { $(this).attr('target', '_blank').addClass('externe') }
					}
				},
				init : function() {
					$('a').each(function() {
						var lien = this;
						$.each( ixLinkTrack.config, function(k,v) {
							if ( this.check.call( lien ) ) {
								$(lien).bind( 'track', function() {
									var url = '/'+k+v.url.call( $(this).attr('href') )
									if (typeof(pageTracker) == "object") { pageTracker._trackPageview( url ) };
								}).click(function() { $(this).trigger('track'); } );
								if ( v.filter ) { v.filter.call( lien ) }
								return false;
							}
						});
					});
				}
			};
			ixLinkTrack.init();
		}

	});

})(jQuery);
