//
//	$version: 			1.0
//	$last_updated:    	26/01/2011
//	$brand:   			Goodyear
//

/*!
 *	Javascript to tag file downloads and external links in Google Analytics
 *	To use, place reference to this file should be placed at the bottom of all pages, 
 *	just above the Google Analytics tracking code.
 *	All outbound links and links to non-html files should now be automatically tracked.
 */

var hrefs = document.getElementsByTagName("a");
var link_path = "";

for (var l = 0; l < hrefs.length; l++) {
	try {
		// Add the hostname and link location into variables 
		if (hrefs[l].pathname) {
			var link_path = hrefs[l].pathname;
			var href = hrefs[l].href;
		} else {
			var link_path = '';
			var href = '';
		} 
		
		var link_location = String(hrefs[l]);

		// Check if it's a mail link
		if (link_location.match(/^mailto:/i)) 
		{                                     
			addmailtotrackerlistener(hrefs[l]);
		}
		// Check to see if the link is an internal link
		else if (location.host == hrefs[l].hostname) 
		{
			if(link_path.match(/\.(doc|pdf|xls|ppt|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)$/)) 
			{                                    
			 	addtrackerlistener(hrefs[l]);
			}
		}
		else if (href.match(/^javascript:/)) {
			// don't do anything because it's a javascript link
		}
		else 
		{   
			addtrackerlistener(hrefs[l]);
		}
	}
	catch(err) { }   
}


// Add a listener to matching file links
function addtrackerlistener(obj) {
	if (obj.addEventListener) {
		obj.addEventListener('click', trackfiles, true);
	} else if (obj.attachEvent) {
		obj.attachEvent("on" + 'click', trackfiles);
	}
}


// Add a special listener to mailto: links
function addmailtotrackerlistener(obj) {
	if (obj.addEventListener) {
		obj.addEventListener('click', trackmailto, true);
	} else if (obj.attachEvent) {
		obj.attachEvent("on" + 'click', trackmailto);
	}
}


// Track file links
function trackfiles(array_element) {
	var file_path = "";
	// Track an external link
	var destination_host = (array_element.srcElement) ? array_element.srcElement.hostname : this.hostname;
	if (location.host != destination_host){
		file_path = GATagLinksGetLocale() + "virtual/exlink/" + cleanURL(window.location, true) + '/' + ((array_element.srcElement) ? array_element.srcElement.hostname : this.hostname);
		file_path = file_path + ((array_element.srcElement) ? "/" + cleanURL(array_element.srcElement.pathname, false) : this.pathname);
	}
	// Track an internal link
	else {
		file_path = ((array_element.srcElement) ? "/" + array_element.srcElement.pathname : this.pathname);	
		var file_details = file_path.split('/');    
		file_path = cleanURL(window.location, true) + '/' + file_details[(file_details.length-1)];
		file_path =  GATagLinksGetLocale() + "virtual/download/" + file_path;
	}
	trackPageview( file_path );
}

// Generate page view for a mailto: link
function trackmailto(array_element) {
	var email = ((array_element.srcElement) ? array_element.srcElement.href : this.href).substring(7);
	var url = cleanURL(window.location, true);
	var mail_path = GATagLinksGetLocale() + 'virtual/mailto/'+url+'/'+email;
	trackPageview( mail_path );
}

// Clean leading & trailing slashes
function cleanURL (url, end) {
	var url = url.toString();
	var urlLen = url.length;
	
	if (end) {
		if (url.charAt((urlLen-1))=='/')
			url = url.substring(0,(urlLen-1));
	}
	else {
		if (url.charAt(0)=='/')
			url = url.substring(1,urlLen);
	}
	return url;
}

// Using queue or not?
function trackPageview( view ) {

  if ( typeof pageTracker == "undefined" ) {
    if ( typeof _gaq == "undefined" ) {
      // unknown page tracking variable
    } else {
      _gaq.push( ['_trackPageview', view] );
    }
  } else {
    pageTracker._trackPageview( view );
  }

}          


// custom for Goodyear

function GATagLinksGetLocale () {
  locale = "unknown_unknown";
  var locale_regex = /\/(\w+_\w+).+/;
  if (locale_regex.exec(window.location.pathname)) {
    var match = locale_regex.exec(window.location.pathname);
    locale = match[1];
  };
  return locale + '/';
}
