/*
	REQUIRES : jQuery

	To enable Outlinks call the following after the page has loaded
	window.onload=Outlinks.enable; // this will use defaults

	this will by default, enable all three types of outlinks with their respective default classnames

	TYPES             DEFAULT CLASSNAME     DESCRIPTION

	outlink           outlink               Simply shows "Open link in new browser window."
	logdomain         outlink_ld            as above, plus GoogleTracks to '/outgoing/'+href.hostname (with leading www's removed)
	logdomainpath     outlink_ldp           as above, with the href.pathname as well
	logstring         *none*                show "Open link in new browser window" and logs the preset string for all of class

logstring in depth :
Must be enabled by doing something like 
   Outlinks.enable({ logstring:{classname:'outlink_log_hello',logstring:'/outgoing/hello_world'}});
this would activate on all a.elements with the class "outlink_log_hello" and have them record a analytics _trackPageview with
a URI of '/outgoing/hello_world'

*/

var Outlinks= function(){
	var me={};
	defaultParams={
		outlink:{classname:'outlink'},
		logdomain:{classname:'outlink_ld'},
		logdomainpath:{classname:'outlink_ldp'}
	};

function carp(e) { }

function _logdomain(href) { try {
	var loghost=href.hostname.replace(/^www\./,"");
	pageTracker._trackPageview('/outgoing/'+loghost);

} catch(e) { carp(e); } }

function _logdomainpath(href) { try {
	var loghost=href.hostname.replace(/^www\./,"");
	var logpath=(href.pathname.charAt(0)=='/' ? '' : '/') + href.pathname; // BROWSER HACK, some show the leading '/', and some don't
	pageTracker._trackPageview('/outgoing/'+loghost+href.pathname);

} catch(e) { carp(e); } }

me.enable = function(params) {
	// Configure to use defaults if no parameters have been provided
	if (
		typeof(params)=='undefined' || // No Parameters
		(typeof(params)=='object' && typeof(params.target)=='object') // Or an event (from onload)
	) { params=defaultParams; } 
	var linktype;
	for (linktype in params) { if (!params.hasOwnProperty(linktype)) { continue; }
		if (typeof(me[linktype])!="function") {
			throw "Outlinks->enable : Unknown link type `" + linktype + "`";
		}
		me[linktype](params[linktype]);
	}
}

function _getTitle(params) {
	if (typeof(params.title)=="string") { return params.title; }
	return 'Open link in new browser window';
}

function _logstring(logme) {
	try {
	pageTracker._trackPageview(logme);
	} catch(e) { carp(e); }
}

me.logstring = function(params) {
	var f=function() { _logstring(params.logstring); }; // cut down on created functions
	try {
		var matches=$("a."+params.classname);
		matches.attr({target:"_blank",title:_getTitle(params)});
		matches.bind("click",f);
//		matches.bind("click",function() { _logstring(params.logstring); });
	} catch(e) { carp(e); }
}


me.outlink = function(params) {
	try {
		var matches=$("a."+params.classname);
		matches.attr({target:"_blank",title:_getTitle(params)});
	} catch(e) { carp(e); }
}

me.logdomainpath = function(params) {
	try {
		var matches=$("a."+params.classname);
		matches.attr({target:"_blank",title:_getTitle(params)});
		matches.bind("click",function() { _logdomainpath(this); });
	} catch(e) { carp(e); }
}


me.logdomain = function(params) {
	try {
		var matches=$("a."+params.classname);
		matches.attr({target:"_blank",title:_getTitle(params)});
		matches.bind("click",function() { _logdomain(this); });
	} catch(e) { carp(e); }
}
	return me;

}();
