var nom_pag_analytics = "";
var ev_categoria = "";
var ev_accion = "";
var ev_etiqueta = "";
var ev_valor = "";


$(document).ready(function()
{			
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	
	//use jQuery to call the Google Analytics JavaScript
	$.getScript(gaJsHost + "google-analytics.com/ga.js", function(){
	
		//tell Analytics about the current page load using standard _trackPageview method
		try {
			var pageTracker = _gat._getTracker("UA-5469570-5");
			pageTracker._setDomainName("none");
			pageTracker._setAllowLinker(true);
			pageTracker._trackPageview(nom_pag_analytics);
			if(evento==true){ 
				pageTracker._trackEvent(ev_categoria, ev_accion, ev_etiqueta, ev_valor);
			}
			
		} catch(err) {}
		
		//loop though each anchor element
		$('a').each(function()
		{					
			var href = $(this).attr('href');
			if (href==undefined) {href=""}

			var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;	

			if ((top.document.location.protocol.indexOf('http:') >= 0 &&
				(href.indexOf('/on/io/navegacion/form_compra.html') >= 0 || 
				 href.indexOf('https://www.telefonicaonline.com') >= 0)
			   ) ||
			   (top.document.location.protocol.indexOf('https:') >= 0 &&						
				 href.indexOf('http://www.telefonica.es') >= 0))
			{	
				var cadena_onclick=$(this).attr('onclick').toString().replace(/ /gi,'');
				cadena_onclick=cadena_onclick.replace(/\'/gi,"\"");

				if (cadena_onclick.indexOf('location.href')>=0)
					cadena_onclick=cadena_onclick.substring(cadena_onclick.indexOf('="')+1,cadena_onclick.indexOf(';'));		
				else if (cadena_onclick.indexOf('AbreAsistenteEnTop')>=0)
					cadena_onclick=cadena_onclick.substring(cadena_onclick.indexOf('("')+1,cadena_onclick.indexOf(','));
				
				try {v_enlace = eval(cadena_onclick);	} catch (e) {}
				
				//$(this).attr('onclick',"");
				
				if (top.document.location.protocol.indexOf('http:') >= 0 &&
					(href.indexOf('/on/io/navegacion/form_compra.html') >= 0 || 
					href.indexOf('https://www.telefonicaonline.com') >= 0)
					)
				{						
					$(this).click(function() {
						pageTracker._link('https://www.telefonicaonline.com/on/io/pruebas/2009/google-analytics/cambiar_dominio.html?url_des='+encodeURIComponent(v_enlace));
						return false;
					});
				}
				else if (top.document.location.protocol.indexOf('https:') >= 0 &&						
					 href.indexOf('http://www.telefonica.es') >= 0)
				{					
					$(this).click(function() {
						pageTracker._link('http://www.telefonica.es/on/io/pruebas/2009/google-analytics/cambiar_dominio.html?url_des='+encodeURIComponent(v_enlace));
						return false;
					});
				}
			}
			//check for links starting with http or https, making sure that links to our own domain are excluded
			else if ((href.match(/^https?\:/i)) && (!href.match(document.domain))){
				$(this).click(function() {
					var extLink = href.replace(/^https?\:\/\//i, '');
					pageTracker._trackEvent('External', 'Click', extLink);
				});
			}
			
			else if (href.match(/^mailto\:/i)){
				$(this).click(function() {
					var mailLink = href.replace(/^mailto\:/i, '');
					pageTracker._trackEvent('Email', 'Click', mailLink);
				});
			}
			
			else if (href.match(filetypes)){
				$(this).click(function() {
					var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
					var filePath = href.replace(/^https?\:\/\/(www.)mydomain\.com\//i, '');
					pageTracker._trackEvent('Download', 'Click - ' + extension, filePath);
				});
			}

		});
		
	});

});
