$.fn.gaExtender = function(options) {
	
	/****************************************************/
	/*				gaextender.jquery.js				*/
	/*			  Author: Martijn Cuijten				*/
	/*				    Version: 0.1					*/
	/*					date: 30-05-2011				*/
	/****************************************************/
	
	/**************************************************************************************************************
	Available options:
	- trackAllExternalLinks			boolean (true/false)	if set to true all outgoing links are being tracked
	- ExternalLinksCatName			textstring				This is the categoryname for outgoing links that will show up in your google analytics account
	- autonumbers					boolean (true/false)	If set to true every link will be autonumbered (usefull if you have multiple links with the same class that you want to track).
	- links_to_track				object					You can configure wich links need to be tracked.
	**************************************************************************************************************/
	
	/*	OP AANVRAAG VAN MARTIJN - DE SAVE VAN ZIJN SCRIPT UITGEZET (db werd gespammed)
		trackAllExternalLinks	:	false,
		ExternalLinksCatName	:	'Externe links',
		autonumbers				: 	false,
		saveCoords				:	false,
		saveUrl					:	'http://www.m-designz.nl/gaextender/saveCoords.php', 
	*/
	
	var _options = $.extend({
		trackAllExternalLinks	:	false,
		ExternalLinksCatName	:	'Externe links',
		autonumbers				: 	false,
		saveCoords				:	false,
		saveUrl					:	'',
		links_to_track			: 	[
										{selector: '.link-download', event: 'click', category: 'Downloads', title: '', optional_label: '', optional_value: '' },
										{selector: '.link-rss', event: 'click', category: 'RSS', title: '', optional_label: '', optional_value: '' },
										{selector: '.ppc', event: 'click', category: 'Pay per click', title: '', optional_label: '', optional_value: 0.10 }
									]
	}, options);
	
	return this.each(function() {	
		var container = $(this);
		var nr_of_selectors		= _options.links_to_track.length;
		
		if ( _options.trackAllExternalLinks) trackExternal(); // Track external links
		
		if (nr_of_selectors > 0)
		{
			for (var i = 0; i < nr_of_selectors; i++)
			{
				container.find(_options.links_to_track[i].selector).each(function(){
					switch (_options.links_to_track[i].event)
					{
						case "click" : handleClick($(this), _options.links_to_track[i]); break;
						case "mouseover" : handleMouseover($(this), _options.links_to_track[i]); break;
					}
				});
			}
		}
		
		function handleClick(el, props){
			var cat 		= props.category;
			var title 		= (props.title != "") ? props.title : el.text();
			var label 		= (props.optional_label != "") ? props.optional_label : false;
			var value 		= (props.optional_value != "") ? props.optional_value : false;
			var indexNr 	= el.index()+1;
			
			title			+= " "+ el.attr("rel");
			
			if (_options.autonumbers)
				title = title + indexNr;
			
			el.click(function(e){
				
				if (_options.saveCoords)
				{				
					/* Future function to save locations of the clicks */
					var coords_x	= e.pageX;
					var coords_y	= e.pageY;
					var pageUrl		= encodeURI(""+window.location);
					
					var url = _options.saveUrl+"?t="+pageUrl+"&x="+coords_x+"&y="+coords_y;
					
					var script=document.createElement('img');
					script.src=url;
					script.style.width="1px";
					script.style.height="1px";
					$("body").append(script);
					
					/*
					$.get(url, function(data) {
						alert("success");
						return true;
					});
					*/
					/* End Future function to save locations of the clicks */
				}
				buildEvent(cat, title, label, value);
			});
			return true;
		}
		
		function handleMouseover(el, props){
			var cat 		= props.category;
			var title 		= (props.title != "") ? props.title : el.text();
			var label 		= (props.optional_label != "") ? props.optional_label : false;
			var value 		= (props.optional_value != "") ? props.optional_value : false;
			var indexNr 	= el.index()+1;
			
			title			+= " "+ el.attr("rel");
			
			if (_options.autonumbers)
				title = title + indexNr;
			
			el.mouseover(function(){
				buildEvent(cat, title, label, value);
			});
			
		}
		
		function trackExternal(){
			container.find("a[href^=http://], a[href^=https://], a[rel*=external]").each(function(){
				var cat = _options.ExternalLinksCatName;
				var title = $(this).text();
				$(this).click(function(){
					buildEvent(cat,title);
				});
			});
		}
		
		function buildEvent(cat, title, label, value){
			if (!label && !value)
				_gaq.push(['_trackEvent',''+cat+'',''+title+'']);
			else if ( !label && value)
				_gaq.push(['_trackEvent',''+cat+'',''+title+'', NULL, ''+value+'']);
			else if ( label && !value)
				_gaq.push(['_trackEvent',''+cat+'',''+title+'', ''+label+'', NULL]);
			else if ( label && value)
				_gaq.push(['_trackEvent',''+cat+'',''+title+'', ''+label+'', ''+value+'']);
		}
		
		function sendCoords(x,y){
			
		}
	});	
};
