/**
 * jquery.linkexternal.js
 * ver 0.9
 * 
 * by default)
 * set target="_blank" to external link, if no taget attributes specified(targetBlank option).
 * ignore href attribute not starting with "http://", "https://" (fixed filter).
 * ignore same scheme, hostname, port(ignoreFilterO option).
 * 
 * with options)
 * not set target="_blank" to external link (targetBlank: true or false)
 * add class to external link (classExternal: class name or function)
 * filter(ignore) some external link(ignoreFilter: selector or function)
 * manipulate external link (manipulation: manipulation function, jQuery object or html string)
 * filter manipulation targets (manipulationFilter: selector or function)
 *
 * @example
 *
 * all anchors
 *   $("a").linkExternal();
 * some anchors
 *   $("a.external").linkExternal();
 * exclude some anchors
 *   $(".include_external a").not(".ignore_external a").linkExternal();
 *
 * do not add target="_blank" attribute
 *   $("a").linkExternal({targetBlank: false});
 * add class to external link anchors
 *   $("a").linkExternal({classExternal: "external_link"});
 * add class to external link anchors by function
 *   $("a").linkExternal({classExternal: function(idx){$(this).addClass($(this).attr("rel") + "_external");}});
 * add icon after external link anchors(excluded link content has img)
 *   $("a").linkExternal({manipulation: '<img src="icon.gif" />', manipulationFilter:":not(:has(img))",});
 * prepend icon to external link anchors
 *   $("a").linkExternal({manipulation: function(idx){ $(this).prepend('<img src="icon.gif" />'); });
 *
 * default
 *   set target _blank(if having target attribute, never override)
 *     targetBlank: true
 *   class name or function for external link anchor
 *     classExternal: null
 *   selector or function of ignore filter
 *     ignoreFilter: ':not([href^="' + location.protocol + '//' + location.hostname + ((location.port)? ':' + location.port: '') + '"])'
 *   selector or function of filter for manipulation
 *     manipulationFilter: null
 *   manipulation function, jQuery object or html string(insert after anchors)
 *     manipulation: null
 *   debug
 *     debug: false
 * 
 */
(function($){
	$.fn.linkExternal = function(options){
		$this = $(this);
		//check existence
		if(!$this.size()){
			return this;
		}

		//extend options
		options = $.extend($.fn.linkExternal.defaults, options);

		//target anchors
		var $ancs = $this;
		//remove not http:// or https://
		$ancs = $ancs.filter("[href^='http://']").add($ancs.filter("[href^='https://']"));
		//remove ignore
		if(options.ignoreFilter){
			$ancs = $ancs.filter(options.ignoreFilter);
		}
		
		//add class
		if(options.classExternal){
			//if function
			if($.isFunction(options.classExternal)){
				$ancs.each(function(idx){
					options.classExternal.apply($(this), [idx]);
				});
			}
			//else
			else{
				$(this).addClass(options.classExternal);
			}
		}

		//manipulation
		if(options.manipulation){
			//manipulation target
			var $manips = (options.manipulationFilter)? $ancs.filter(options.manipulationFilter): $ancs;
			//if function
			if($.isFunction(options.manipulation)){
				//apply func to each object
				$manips.each(function(idx){
					options.manipulation.apply($(this), [idx]);
				});
			}
			//jQuery object or html string, use after
			else{
				$manips.after(options.manipulation);
			}
		}

		//set target blank
		if(options.targetBlank){
			//only no target attribute
			$ancs.not("[target]").attr("target", "_blank");
		}

		return this;
	};

	$.fn.linkExternal.defaults = {
		//set target _blank(if having target attribute, not override)
		targetBlank: true,
		//class name or function for external link anchor
		classExternal: null,
		//selector or function of ignore filter
		ignoreFilter: ':not([href^="' + location.protocol + '//' + location.hostname + ((location.port)? ':' + location.port: '') + '"])',
		//selector or function of filter for manipulation
		manipulationFilter: null,
		//manipulation function, jQuery object or html string(insert after anchors)
		manipulation: null,
		//debug
		debug: false
	};
})(jQuery);

