/**	gatrack.js */
/**
 *	v 1.1.0b
 *	ref: http://davidwalsh.name/mootools-google-analytics-track-outbound-links
 *	api: http://code.google.com/apis/analytics/docs/tracking/eventTrackerGuide.html
 */


if ( Math.round(window.MooTools.version.substr(0, 3)*10)/10 > 1.1 )
window.addEvent('load', function() {


	if (typeof _gaq === 'object') {
		
		// FETCH _GAaCCS
		// _gaq gets array overiden as by methods soon as GAa is initated
		// var _gaAccs = _gaAccs || [];
		{
			// form _gaAccs array
			if (typeof _gaAccs == "object") {
				for (var i=0; i<_gaAccs.length; i++){
					if (_gaAccs[i] !== "") _gaAccs[i] += ".";
				}
			// from _gaq settings array
			} else if (/native code/.test(_gaq.push)){ // } else if (typeof _gaq[0] !== "undefined") {
				_gaAccs = [];
				for (var i=0; i<_gaq.length; i++){ // _gaq.each(function(v, i){
					// _gaAccs.include(_gaq[i][0].match(/^(.+)?_.*$/i)[1] || "");
					_gaAccs.push(_gaq[i][0].match(/^(.+)?_setAccount$/i)[1] || "");
				}
			// empty
			} else if (typeof _gaAccs === "undefined") {
				_gaAccs = [""];
			}
		}
		
		// TRACK FOR THOSE
		gatrack('a[href^="http"]', "Outgoing");
		gatrack('a[href$=".pdf"]', "Downloads", true);
//		gatrack('#booking input[type="submit"]', "Booking", true);
	}
	
	function gatrack (selector, path, internal, opennew) {
		if (typeof selector === 'undefined') return;
		if (typeof path !== 'string') var path = "/outgoing/";
		if (typeof internal !== 'boolean') var internal = false;
		if (typeof opennew !== 'boolean') var opennew = false;
		
		RXprefix = /^(?:https?:)?\/\/(?:www\.)?/i;
		
		// build elements collenction
		if (internal) {
			elements = document.getElements(selector);
		// only external links
		} else {
			elements = document.getElements(selector).filter(function(a, i) {
				return a.hostname && a.hostname != window.location.hostname;
			});
		}
		
		// loop
		elements.each(function(el) {
			// el.setStyles({ border: '1px solid green', outline: '1px dashed yellow' }); // test
			// el.set('title', "opens in new window");
			
			// assign event
			el.addEvent('click', function(ev) {
				// compute value
				gaValue = "";
				// is href
				if (el.get('href')){
					gaValue = el.get('href').replace(RXprefix, "");
				// is form submit
				} else if (el.get('type') == "submit" && el.form && el.form.get('action')) {
					gaValue = el.form.get('action').replace(RXprefix, "");
				} else {
					// traverse to look for A element
					elmnt = el;
					while (elmnt.tagName != "A" && elmnt.tagName != "BODY") elmnt = elmnt.parentNode;
					if (elmnt.href == "string") {
						gaValue = elmnt.href;
					} else {
						gaValue = "-";
					}
				}
				
				_gaAccs.each(function(acc, i){
					_gaq.push([ acc + '_trackEvent', path, "Open", gaValue]);
					// 				  _trackEvent / category / action / label (optional) / value (optional, integer)
				});
			}.bind(this));
			
			// set properties
			if (opennew){
				el.set({ target: "_blank" });
			}

		});
	};
});
