/**
 * Google Analytics Extender script
 * adds extra generic tracking info to google analytics
 * @author Rocco Janse, <rocco@efocus.nl>
 * @author Klaas Dieleman
 * @author Ralph Meeuws
 * @version 1.2
 * @since jan 2012
 */
function gaExtender(objConfig) {

	// configuration
	var cfg = objConfig || {};
	var config = {
		// file settings
		fileEvent		: '_trackEvent',		// '_trackPageview' or '_trackEvent'
		fileCategory	: 'Download',			// only applies if event is _trackEvent
		fileAction		: 'Download file - ',	// prefix, file extension will be added, only applies if event is _trackEvent
	
		// mailto settings 
		mailtoEvent		: '_trackEvent',		// '_trackPageview' or '_trackEvent'
		mailtoCategory	: 'Mailto',				// only applies if event is _trackEvent
		mailtoAction	: 'Mailto click',		// only applies if event is _trackEvent
	
		// outgoing settings
		outGoingEvent	: '_trackEvent',		// '_trackPageview' or '_trackEvent'
		outGoingCategory: 'Outgoing link',		// only applies if event is _trackEvent
		outGoingAction	: 'Outgoing link click'	// only applies if event is _trackEvent
	}

	// implement config
	for(i in config) {
		if(cfg[i] != undefined) {
			config[i] = cfg[i];
		}
	}

	// sniff links on the page
	this.hostname = 'http://'+window.location.hostname;
	this.pageLinks = document.getElementsByTagName('a');
	this.linksCount = this.pageLinks.length || 0;
	
	// add events
	for(var i = 0; i < this.linksCount; i++) {
	
		var link = this.pageLinks[i];
		var self = this;
		if(link.href.indexOf('javascript:') == -1 && link.href != '' && link.href.indexOf('#') != link.href.length - 1) {
		
			link.onclick = function(e) {
			
				if (this.href.indexOf(':') > -1) {
				
					var url = this.href.replace(self.hostname, '');
					var ext = url.lastIndexOf('.');

					// check mailto links, if set
					var protocol = this.href.split(':');
					if ('mailto:'.indexOf(protocol[0]+':') > -1) {

						// mailto link
						var mailurl = url.replace('mailto:', '');

						if (config.mailtoEvent == '_trackPageview') {
							
							_gaq.push(['_trackPageview', mailurl]);
							
						} else {
							
							_gaq.push(['_trackEvent', config.mailtoCategory, config.mailtoAction, mailurl]);
							
						}

						// open link
						location.href = this.href;

						// stop click event
						if (!e) {
							var e = window.event;
						}
						e.cancelBubble = true;
						if (e.stopPropagation) {
							e.stopPropagation();
						}
						if (e.preventDefault) {
							e.preventDefault();
						} else {
							return false;
						}
									
					} else if (this.href.indexOf(self.hostname) == -1) {
					
						if (config.outGoingEvent == '_trackPageview') {
						
							_gaq.push(['_trackPageview', this.href]);
					
						} else {
						
							_gaq.push(['_trackEvent', config.outGoingCategory, config.outGoingAction, this.href]);
						}
					
						// open outgoing link
						window.open(this.href, '_blank');
						
						// stop click event
						if (!e) {
							var e = window.event;
						}
						e.cancelBubble = true;
						if (e.stopPropagation) {
							e.stopPropagation();
						}
						if (e.preventDefault) {
							e.preventDefault();
						} else {
							return false;
						}
					
					} else if (ext != -1) {
					
						// local link
						if (url.indexOf('ashx') > -1) {
							var orghref = this.href;
							var xmlhttp = createXmlHttpObject();
							xmlhttp.open('HEAD', this.href, true);
							xmlhttp.onreadystatechange = function() {
								if (xmlhttp.readyState == 4) {
								
									var filetype = xmlhttp.getResponseHeader('Content-Type').split('/')[1];

									if (config.fileEvent == '_trackPageview') {
										
										_gaq.push(['_trackPageview', url]);
										
									} else {
									
										_gaq.push(['_trackEvent', config.fileCategory, config.fileAction + filetype.toLowerCase(), url]);
									
									}
								
									// open link
									window.open(orghref, '_self');
								
								}
							};
							xmlhttp.send(null);

						} else {
					
							// standard link
							var filetype = url.substr(ext, url.length);
							
							if (config.fileEvent == '_trackPageview') {
							
								_gaq.push(['_trackPageview', url]);

							} else {
							
								_gaq.push(['_trackEvent', config.fileCategory, config.fileAction + filetype.toLowerCase(), url]);
							
							}
						
							window.open(this.href, '_self');
						}
						// stop click event
						if (!e) {
							var e = window.event;
						}
						e.cancelBubble = true;
						if (e.stopPropagation) {
							e.stopPropagation();
						}
						if (e.preventDefault) {
							e.preventDefault();
						} else {
							return false;
						}
					}
				}
			}
		}
	}

	/**
	 * creates crossbrowser xmlhttp object to fascilitate our request
	 * @author Rocco Janse, <rocco@efocus.nl>
	 * @date 14/01/2011
	 * @return {object} xmlhttp
	 */ 
	this.createXmlHttpObject = function() {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
		if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
			try {
				xmlhttp = new XMLHttpRequest();
			} catch (e) {
				xmlhttp = false;
			}
		}
		if (!xmlhttp && window.createRequest) {
			try {
				xmlhttp = window.createRequest();
			} catch (e) {
				xmlhttp = false;
			}
		}
		return xmlhttp;
	}
}
