﻿; (function($, _win, _doc) {

	$.LinkAlert = function(options) {
		var opties = $.extend(true, {}, $.LinkAlert.defaults, options),
			a = $("a"),
			_obj = Object.prototype.toString;

		if(opties.situation === 0) return;

		if(opties.situation !== 2) {  // inline
			a.each(function() {
				check($(this), 1);
			});
		}

		if(opties.situation !== 1) {  // mouse
			a.mousemove(function() {
				if($("#linkAlertDiv").children().length <= 0) {
					check($(this), 2);
				}
			});
		}

		function check(link, situ) {
			$.each(opties.protocols, match);
			$.each(opties.extensions, match);
			$.each(opties.conditions, match);

			function convert(regexp) {
				return new RegExp('^(' +
					regexp.replace(/,/g, '|')		// ,		=> |
						  .replace(/\//g, '\\\/')	// /		=> \/
						  .replace(/\./g, '\\.')	// .		=> \.
						  .replace(/\*/g, '.*')		// *		=> .*
						  .replace(/\s+/g, '')		// space	=> nothing
					+ ')([\#\?].*)?$', 'i');
			}

			function match(key, value) {
				if(value && value.match && value.image) {
					if(_obj.call(value.match) === "[object String]") {
						value.match = convert(value.match);
					}
					if(_obj.call(value.match) === "[object RegExp]" || value.match instanceof RegExp) {
						if(value.match.test(link.attr("href"))) {
							add(link, key, value.image, situ);
							if(!value.continu) { return false; }
						}
					} else if(_obj.call(value.match) === "[object Function]") {
						if(value.match.call(this, link.context)) {
							add(link, key, value.image, situ);
							if(!value.continu) { return false; }
						}
					}
				}
				key = value = null;
			}
		}

		function add(link, name, src, situ) {
			var img = $("<img/>").addClass("linkAlertImg").attr({
				title: name || "",
				alt: name || "",
				src: opties.folder + src
			});

			if(situ !== 2) {  // inline;
				var span = $(".linkAlertSpan", link);
				if(span.length <= 0) {
					span = $("<span/>").addClass("linkAlertSpan").appendTo(link);
				}
				span.append(img);
				span = null;
			}

			if(situ !== 1) {  // mouse;
				var div = $("#linkAlertDiv");
				if(div.length <= 0) {
					div = $("<div/>").attr("id", "linkAlertDiv").css({
						position: "absolute",
						left: -100,
						top: -100,
						zIndex: 9999
					}).hide().appendTo($("body"));
				}
				div.append(img);

				$(link).bind("mousemove focus scroll", function(e) {
					div.css({
						left: e.pageX + parseInt(opties.mouseOffset[0]),
						top: e.pageY + parseInt(opties.mouseOffset[1])
					}).show();
				}).bind("mouseleave blur", function() {
					div.hide().empty();
				});
			}
			img = null;
		}
	};

	// public defaults;
	$.LinkAlert.defaults = {
		situation: 3,
		folder: "",
		mouseOffset: [15, 10],  // x and y in pixels;
		protocols: {},
		extensions: {},
		conditions: {}
	};

})(jQuery, window, document);   // plugin code end;

jQuery.LinkAlert.defaults.protocols = {
	//'Hypertext Transfer Protocol': { match: 'http:*', image: 'html.png' },
	'About': { match: 'about:*', image: 'about.png' },
	'AIM': { match: 'aim:*', image: 'aim.png' },
	'DCHub': { match: 'dchub:*', image: 'dchub.png' },
	'eDonkey': { match: 'ed2k:*', image: 'ed2k.png' },
	'Email': { match: 'mailto:*', image: 'mail.png' },
	'Fax': { match: 'fax:*', image: 'fax.png' },
	'File': { match: 'file:*', image: 'file.png' },
	'FTP': { match: 'ftp:*', image: 'ftp.png' },
	'Gopher': { match: 'gopher:*,cso:*', image: 'gopher.png' },
	'ICQ': { match: 'icq:*', image: 'icq.png' },
	'IRC': { match: 'irc:*,ircs:*', image: 'irc.png' },
	'JavaScript': { match: 'javascript:*', image: 'javascript.png' },
	'Magnet': { match: 'magnet:*', image: 'magnet.png' },
	'News': { match: 'nntp:*,news:*,snews:*,newspost:*,newsreply:*,snewspost:*,snewsreply:*', image: 'news.png' },
	'Opera': { match: 'opera:*', image: 'opera.png' },
	'RSS': { match: 'feed:*', image: 'rss.png' },
	'SMS': { match: 'sms:*,smsto:*,mms:*,mmsto:*', image: 'sms.png' },
	'Streaming': { match: 'rtsp:*,pnm:*', image: 'video.png' },
	'Telephone': { match: 'tel:*,wap:*,wtai:*', image: 'phone.png' },
	'Telnet': { match: 'telnet:*,tn3270:*,rlogin:*', image: 'unknown.png' }
};
jQuery.LinkAlert.defaults.extensions = {
	//'': { match: 'data:text*,*.htm,*.html,*.php,*.asp,*.aspx,*.jsp,*.esp,*.pl,*.cgi,*.php3,*.xhtml,*.cfm,*.cfml,*.shtml,*.xht,*.xhtm,*.dml,*.stm,*\/', image: 'html.png' },
	'Audio': { match: '*.aac,*.aiff,*.asx,*.au,*.m3u,*.mid,*.midi,*.mp3,*.ogg,*.ra,*.snd,*.wav,*.wma', image: 'audio.png' },
	'C/C++ Source': { match: '*.cpp,*.c,*.cc,*.cxx', image: 'c_source.png' },
	'C/C++ Header': { match: '*.h,*.hpp,*.hh,*.hxx', image: 'c_header.png' },
	'Command': { match: '*.sh,*.bat,*.cmd,*.pif,*.vb,*.vbs,*.vba,*.wsh', image: 'command.png' },
	'Compressed': { match: '*.zip,*.rar,*.tar,*.tgz,*.gz,*.bz2,*.tbz2,*.gzip,*.z,*.sit,*.cab,*.7z,*.lzh,*.pkg', image: 'compressed.png' },
	'Disk Image': { match: '*.iso,*.dmg', image: 'iso.png' },
	'DLL': { match: '*.dll,*.manifest', image: 'dll.png' },
	'Excel': { match: '*.xls,*.xlsx,*.xlt', image: 'excel.png' },
	'Executables': { match: '*.exe,*.scr,*.msi,*.ref,*.app,*.paf', image: 'executable.png' },
	'Firefox Extensie': { match: '*.xpi', image: 'xpi.png' },
	'Flash': { match: '*.swf,*.aam,*.fla', image: 'flash.png' },
	'Image': { match: '*.jpg,*.jpe,*.jpeg,*.gif,*.png,*.bmp,*.ico,*.svg,*.svgz,*.xbm,*.drw,*.psp,*.tif,*.tiff,data:image*', image: 'image.png' },
	'Initialization': { match: '*.ini', image: 'ini.png' },
	'Java': { match: '*.java,*.jar,*.jad', image: 'java.png' },
	'JavaScript': { match: '*.js', image: 'javascript.png', continu: true },
	'OpenOffice': { match: '*.odc,*.odf,*.odm,*.odp,*ods,*.odt,*.otc,*.otf,*.otg,*.oth,*.oti,*.otp,*.ots,*.ott,*.sda,*.sdb,*.sdc,*.sdd,*.sds,*.sdw,*.sor,*.stc,*.std,*.sti,*.stw,*.sxc,*.sxd,*.sxi,*.sxg,*.sxm,*.sxw,*.vor', image: 'openoffice.png' },
	'PDF': { match: '*.pdf', image: 'pdf.png' },
	'PostScript': { match: '*.ps,*.eps', image: 'postscript.png' },
	'Powerpoint': { match: '*.ppt,*.pptx,*.pps', image: 'powerpoint.png' },
	'Python': { match: '*.py', image: 'python.png' },
	/*(XML)*/'RSS': { match: '*.rss,*.rdf,*rss.xml,*feed.xml,*atom.xml,*/rss/feed*', image: 'rss.png', continu: true },
	'RegEdit': { match: '*.reg', image: 'regedit.png' },
	'Shortcut': { match: '*.lnk', image: 'shortcut.png' },
	'Stylesheet': { match: '*.css', image: 'stylesheet.png' },
	'Temporary': { match: '*.temp,*.tmp', image: 'temporary.png' },
	'TeX': { match: '*.tex,*.dvi,*.latex', image: 'tex.png' },
	'Text': { match: '*.txt,*.rtf,*.log,*.readme,*read.me', image: 'text.png' },
	'Torrent': { match: '*.torrent', image: 'torrent.png' },
	'Unknown': { match: '*.dat,*.bak,*.bin', image: 'unknown.png' },
	/*(JavaScript)*/'Userscript': { match: '*.user.js', image: 'userscript.png', continu: true },
	'Video': { match: '*.asf,*.avi,*.mov,*.mp4,*.mpe,*.mpeg,*.mpg,*.qt,*.ram,*.rm,*.wmv', image: 'video.png' },
	'Widget': { match: '*.widget', image: 'widget.png' },
	'Word': { match: '*.doc,*.docx,*.dot,*.psw', image: 'word.png' },
	'XML': { match: '*.xml', image: 'xml.png', continu: true }
};
jQuery.LinkAlert.defaults.conditions = {
	'External': { match: function(link) { return link.hostname && link.hostname != location.hostname; }, image: 'external.png', continu: true },
	'#Internal': { match: function(link) { return link.hash && link.href.replace(/#.*$/, "") == location.href.replace(/#.*$/, ""); }, image: 'internal.png', continu: true },
	'Onclick': { match: function(link) { return !!link.getAttribute("onclick"); }, image: 'javascript.png', continu: true },
	'New window': { match: function(link) { return link.getAttribute("target") && link.getAttribute("target").match(/^(_blank)|(_new)|(_newwindow)$/i); }, image: 'new_window.png', continu: true },
	'Secure': { match: function(link) { return link.protocol == "https:" && location.protocol != "https:"; }, image: 'secure.png', continu: true },
	'Unsecure': { match: function(link) { return link.protocol != "https:" && location.protocol == "https:"; }, image: 'unsecure.png', continu: true }
};
