//	Javascript to tag file downloads, external links and mailto links in Google Analytics
//	To use, reference to this file should be placed at the bottom of all pages, 
//	just above the Google Analytics tracking code.
//	All outbound links, mailto links and links to non-html files should now be automatically tracked.
//  Script also initiates cross domain tracking with Google Analytics for all sites in the tracked_sites array. You need to make changes to your Google Analytics initiation code also to get this to work.
//
// 	Be aware that this tracks events using the new Google Analytics _trackPageview code. Need to make changes if using old Google Analytics code.
//

var tracked_sites = [];
tracked_sites[0] = 'sunsail.com.au';
tracked_sites[1] = 'sunsailwhitsundays.com.au';
tracked_sites[2] = 'sunsailtahiti.com';
tracked_sites[3] = 'sunsailthailand.com';
tracked_sites[4] = 'sunsailmalaysia.com';
tracked_sites[5] = 'sunsailseychelles.com';
tracked_sites[6] = 'sunsailvietnam.com';

// addEvent manager - http://www.twinhelix.com/javascript/addevent/
if(typeof addEvent!='function'){var addEvent=function(o,t,f,l){var d='addEventListener',n='on'+t,rO=o,rT=t,rF=f,rL=l;if(o[d]&&!l)return o[d](t,f,false);if(!o._evts)o._evts={};if(!o._evts[t]){o._evts[t]=o[n]?{b:o[n]}:{};o[n]=new Function('e','var r=true,o=this,a=o._evts["'+t+'"],i;for(i in a){o._f=a[i];r=o._f(e||window.event)!=false&&r;o._f=null}return r');if(t!='unload')addEvent(window,'unload',function(){removeEvent(rO,rT,rF,rL)})}if(!f._i)f._i=addEvent._i++;o._evts[t][f._i]=f};addEvent._i=1;var removeEvent=function(o,t,f,l){var d='removeEventListener';if(o[d]&&!l)return o[d](t,f,false);if(o._evts&&o._evts[t]&&f._i)delete o._evts[t][f._i]}}

// used to kill form submission via spec- rather than going _return false_ in your onsubmit handler, go _return killEvent(e)_
function killEvent(e){
 if (e && e.preventDefault)
    e.preventDefault(); // DOM style
  return false; // IE style
};

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

for (var l = 0; l < hrefs.length; l++) {
	try {
		var link_path = hrefs[l].pathname;
		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)$/i)) {
                addEvent(hrefs[l], 'click', trackfiles, true);
			}         	
		} else {

		    var is_tracked_site = 'false';
			for ( var i=0, len=tracked_sites.length; i<len; ++i ){
		        if(hrefs[l].hostname == tracked_sites[i] || hrefs[l].hostname == 'www.' + tracked_sites[i]){
		            is_tracked_site = 'true';
		        }
			}

		    if(is_tracked_site == 'false'){
                addEvent(hrefs[l], 'click', trackfiles, true);
		    } else {
                addEvent(hrefs[l], 'click', function(e) {
                    XdomainTracker._link(this.href);
                    return killEvent(e);
                }, true);
		    }
		}
	}
	catch(err) { }
}

function trackfiles(array_element) {
	file_path = "";
	if (this.protocol == "mailto:") {
		file_path = "/" + this.href;
	} else {
		if (location.host != this.hostname) {
			file_path = "/exlinks/" + ((array_element.srcElement) ? "/" + array_element.srcElement.hostname : this.hostname);
		}
		file_path = file_path + ((array_element.srcElement) ? "/" + array_element.srcElement.pathname : this.pathname);
	}
	pageTracker._trackPageview(file_path);
	XdomainTracker._trackPageview(file_path);
}