if (typeof jQuery !== 'undefined') {
	jQuery(function ($) {
		$.fn.extend({
			ttarget: function (options) {
				var settings = $.extend({}, $.fn.ttarget.defaults, options),
					getSelectedText;

				getSelectedText = function () {
					if (window.getSelection) {
						return window.getSelection().toString();
					} else if (document.getSelection) {
						return document.getSelection();
					} else if (document.selection) {
						return document.selection.createRange().text;
					}
				};

				return this.each(function () {
					var $t	= $(this),
						o	= $.metadata ? $.extend({}, settings, $t.metadata()) : settings,
						$a,
						href,
						title;

					if ($t.find(':has(a)')) {
						$a		= $t.find('a:first');
						href	= $a.attr('href');
						title	= $a.attr('title');

						/**
						* Setup the Container
						* Add the 'container' class defined in settings
						* @event hover
						* @event click
						*/
						$t.addClass(o.className.container).hover(
							function (event) {
								var $$ = $(this);

								// Add the 'hover' class defined in settings
								$$.addClass(o.className.hover);

								// Add the Title Attribute if the option is set, and it's not empty
								if (typeof o.title !== 'undefined' && o.title === true && title !== '') {
									$$.attr('title', title);
								}

								// Set the Status bar string if the option is set
								if (typeof o.status !== 'undefined' && o.status === true) {
									if ($.browser.safari) {
										// Safari Formatted Status bar string
										window.status = 'Go to "' + href + '"';
									} else {
										// Default Formatted Status bar string
										window.status = href;
									}
								}
							},
							function (event) {
								var $$ = $(this);

								// Remove the Title Attribute if it was set by the Plugin
								if (typeof o.title !== 'undefined' && o.title === true && title !== '') {
									$$.removeAttr('title');
								}

								// Remove the 'hover' class defined in settings
								$$.removeClass(o.className.hover);

								// Remove the Status bar string
								window.status = '';
							}
						).bind('click.ttarget', function (event) {
							// Don't do anything if selecting text inside ttarget element
							if (getSelectedText().length > 0) {
								return true;
							}

							// Don't override clicks on form elements
							if ($(event.target).is('input, select, option, optgroup textarea, button')) {
								return true;
							}

							if ($(event.target).closest('a').length) {
								$a		= $(event.target).closest('a');
								href	= $a.attr('href');
							}

							// Trigger the Anchor / Follow the Link
							if ($a.is('[rel*=external]')) {
								window.open(href);
								event.preventDefault();
							} else {
								window.location = href;
							}
						});
					}
				});
			}
		});
		
		$.fn.ttarget.defaults = {
			title:	true,
			status:	false,
			className: {
				'container':	'ttarget',
				'hover':		'hover'
			}
		};
		
	}(jQuery));
}
