//jQuery.noConflict();  // Para que jQuery no entre en coflicto con mootools (Joomla): substituimos $() por jQuery() y $. por jQuery.
$(document).ready(function(){

	//Para llamar al script desde HTML:
	//<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script> 
	//<script src="/extendga.js" type="text/javascript"></script>

	//loop though each anchor element
	$('a').each(function(){
		
		var href = $(this).attr('href');
		var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;
		
		//check for links starting with http or https, making sure that links to our own domain are excluded
		if ( href != undefined && href.match(/^https?\:/i) && (!href.match(document.domain))){
			$(this).click(function() {
				var extLink = href.replace(/^https?\:\/\//i, '');
				pageTracker._trackEvent('External', 'Click', extLink);
				if( extLink.indexOf('clicksign-') >= 0 && extLink.indexOf('setup.exe') >= 0 ) {
					// Descaga de Clicksign, alojado en "clicksign.isigma.es", desde www.isigma.es
					extLink = href.replace(/^https?\:\/\/(clicksign.)isigma\.es\//i, '');
					pageTracker._trackEvent('Download', 'Click - exe', extLink);
					pageTracker._setCustomVar( 1, "Download Clicksign", "yes", 1 );
					pageTracker._trackPageview( extLink );
				}
			});
		}
		//check for links starting with mailto:
		else if ( href != undefined && href.match(/^mailto\:/i) ){
			$(this).click(function() {
				var mailLink = href.replace(/^mailto\:/i, '');
				pageTracker._trackEvent('Email', 'Click', mailLink);
			});
		}
		//check for links with file extension that match the filetypes regular expression:
		else if ( href != undefined && href.match(filetypes) ){
			$(this).click(function() {
				var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
				var filePath = href.replace(/^https?\:\/\/(clicksign.)isigma\.es\//i, '');
				pageTracker._trackEvent('Download', 'Click - ' + extension, filePath);
				if( filePath.indexOf('clicksign-') >= 0 && filePath.indexOf('setup.exe') >= 0 ) {
					pageTracker._setCustomVar( 1, "Download Clicksign", "yes", 1 );
					pageTracker._trackPageview( filePath );
				}
			});
		}
	});
});
