/*
 * @description Plugin Detection
 * @return {jQuery Object}
 * @param {Object} options
 * @type {jQuery Plugin Method}
 * @author Mike Cravey
 * @version .1
 */

;(function($){
	$.track = {
		defaults: {
			debug: false,
			currentPage: '',
			href: '',
			name: '',
			event: 'click',
			category: '',
			regex: {},
			regPatterns: {
				// Starts with 'http'
				ext: '^http',
				// Ends in one of the listed extensions
				download: '\.(doc|pdf|xls|ppt|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)$',
				// Starts with 'mailto:'
				mailto: '^mailto:'
			}
		},
		// Detect plugins
		pluginDetect: function(options) {
			var o = $.extend({}, $.track.defaults, options);
			
			// Plugin Detection
			var pluginList = {
				'Silverlight': {
					// Detect Silverlight
					detect: function(){
						var SLVersion;
						// Put everything in trys and catch the errors and fail them silently
						try {  
							try {
								// Try to create a Silverlight Active X object and grab version number
								var control = new ActiveXObject('AgControl.AgControl');
								if (control.IsVersionSupported("3.0")) {
									SLVersion = 3;
								} else {
									if (control.IsVersionSupported("2.0")) {
										SLVersion = 2;
									} else {
										SLVersion = 1;           
										control = null;
									}
								}
							}
							catch (e) {
								// Otherwise query the Silverlight plugin
								var plugin = navigator.plugins["Silverlight Plug-In"];
								if (plugin) {         
									// Do some weird logic because MS can't label their releases right
									if (plugin.description === "1.0.30226.2") {
										SLVersion = 2;
									} else {
										SLVersion = parseInt(plugin.description[0]);
									}
								} else {
									SLVersion = false;
								}
							}
						}
						catch (e) { 
							// Otherwise return Silverlight false
							SLVersion = false;
						}
						return SLVersion;
					}
				}
			},
			sPlugins = '';

			// Execute the detect function for the selected plugin
			var isSupported = function (p) {
				try {
					return pluginList[p].detect();
				} catch (e) {
					return false;
				}
			};

			// Iterate through plugin list and overload the sPlugins variable
			$.each(pluginList, function (i) {
				var supported = isSupported(i);
				sPlugins += '/' + i + '=' + supported;
			});

			// Send the pluginStr variable to GA
			_gaq.push(['_setVar', sPlugins]);
			if(o.debug) console.log('plugins: ' + sPlugins);
		}
	}
	
	
	/*
	 * @description Track link clicks back to analytics
	 * @return {jQuery Object}
	 * @param {Object} options
	 * @type {jQuery Plugin Method}
	 * @author Mike Cravey
	 * @version 0.1	
	 */
	$.fn.trackLinkClicks = function(options) {
		var opts = $.extend(true, {}, $.track.defaults, options);
		
		return this.click(function(){
			var $this = $(this),
				// Support Metadata plugin
				o = $.meta ? $.extend(true, {}, opts, $this.data()) : opts,
				// If they exist, use them, otherwise make assumptions on how to get them
				href = o.href ? o.href : $this.attr('href'),
				name = o.name ? o.name : $this.text(),
				// If current page exists, prepend it to the label. Otherwise just trim up the link name
				label = o.currentPage ? o.currentPage + ' : ' + name : $.trim(name);
				// Iterate through the regPatterns object and build out varibles for them
				$.each(o.regPatterns, function(i,v){
					o.regex[i] = new RegExp(v,'i');
				});
			
			if (!o.category) {	
				switch(true) {
					case o.regex.ext.test(href):
						o.category = 'external';
						break;
					case o.regex.download.test(href):
						o.category = 'download';
						break;
					case o.regex.mailto.test(href):
						o.category = 'mailto';
						break;
					// Otherwise assume internal link
					default:
						o.category = 'internal';
						break;
				}
			}
			// Track click to GA as event
			_gaq.push(['_trackEvent', o.category, o.event, label])
			// TODO: SharePoint analytics method call goes here
			if(o.debug) {
				console.log('category: ' + o.category + ', event: ' + o.event + ', label: ' + label);
				return false;
			}
		});
	}

})(jQuery);