/**
 * DownloadTracker allows to track downloads with Google Analytics.
 * @author NOSE
 * 
 * @example <script type="text/javascript" src="downloadtracker.js"></script>
 * 
 * @option types The file types to track (space separated).
 * @option prefix The prefix to use.
 * @option mapfile True to map only to the filename, false to map the whole path.
 * @option debug True to output debug info.
 * 
 * @version 1.1.0 event listener
 * @version 1.0.0 initial version
 */
var DownloadTracker = {	
	
	/**
	 * Tracker Parameters.
	 * @types The file types to track.
	 * @prefix Prefix for the mapping.
	 * @mapfile True to map the file only, false to map the path.
	 * @debug Set to true to view debug info.
	 */
	types: ".pdf .doc .zip",
	prefix: "/download/",
	mapfile: false,	
	debug:false,
	
	/**
	 * Initializes the tracker.
	 */
	initialize: function() {		
		// types
		var types = DownloadTracker.types.split(" ");
	
		// links
		var links = document.getElementsByTagName("a");
		for (var i = 0; i < links.length; i++) {
			var l = links[i];
			var h = l.getAttribute("href");
			if (h && DownloadTracker.matchType(types,h)) {
				// track
				DownloadTracker.addListener(l,"click",DownloadTracker.trackDownload);
			}
		}
	},
	
	/*
	 * Tracks the download.
	 */
	trackDownload: function() {
		try {		
			// href
			var h = this.href;
		
			// map
			var dmap = "";
			if (DownloadTracker.mapfile) {
				dmap = DownloadTracker.mapFilename(h);
			}
			else {
				dmap = DownloadTracker.mapPath(h);
			}
			// track
			var t = DownloadTracker.prefix + dmap;
			if (DownloadTracker.debug) {
				alert("track = " + t);
			};
			// google urchin tracker
			urchinTracker(t);
		}
		catch (e) {
			// ignore
		}
		
		// always follow href
	    return true;		
	},
	
	/*
	 * Gets the mapping path.
	 */
	mapPath: function(h) {
		// erase protocol info (e.g. http://www.domain.com/ )
		var p = h.replace(/([a-zA-Z0-9])*:\/\/([a-zA-Z0-9_\.\-])*\//,"");
		return p;
	},
	
	/*
	 * Gets the mapping filename.
	 */
	mapFilename: function(h) {
		// get filename token
		var tokens = h.split("/");
		var filename = tokens[tokens.length-1];
		return filename;
	},
	
	/*
	 * Determines if the type is matched.
	 */
	matchType: function(types,h) {
		var mt = false;
		for (var i = 0; i < types.length; i++) {
			var type = types[i];
			if (h.match(type)) {
				mt = true;
				break; 
			}
		}
		return mt;
	},
	
	/*
	 * Adds an event listener.
	 * @el The element.
	 * @type The event type.
	 * @fn The function to invoke.
	 */
	 addListener: function() {
	 	// firefox, etc.
	 	if ( window.addEventListener ) {
        	return function(el, type, fn) {
        		el.addEventListener(type, fn, false);
        	};
    	} 
    	// ie
    	else if ( window.attachEvent ) {
        	return function(el, type, fn) {
            	var f = function() {
                	fn.call(el, window.event);
            	};
            	el.attachEvent('on'+type, f);
        	};
    	}
    	// other 
    	else {
        	return function(el, type, fn) {
            	element['on'+type] = fn;
        	}
    	}
	 }()
}
/**
 * Initialize on window load.
 */
DownloadTracker.addListener(window, 'load', function(){DownloadTracker.initialize()});





